44import re
55import shutil
66import sys
7+ import textwrap
78from pathlib import Path
89from subprocess import run
910from tempfile import gettempdir
@@ -72,10 +73,6 @@ def autocompletion(self) -> None:
7273 # Basic POSIX shells does not support autocompletion
7374 return None
7475
75- def init_file (self ) -> None :
76- with open (self .script_file_path , 'w' ) as fd :
77- self .export_file (fd )
78-
7976 def export_file (self , fd : TextIO ) -> None :
8077 fd .write (f'{ self .deactivate_cmd } \n ' )
8178 for var , value in self .new_esp_idf_env .items ():
@@ -87,34 +84,32 @@ def export_file(self, fd: TextIO) -> None:
8784 'Go to the project directory and run:\n \n idf.py build"\n ' ))
8885
8986 def export (self ) -> None :
90- self .init_file ()
87+ with open (self .script_file_path , 'w' ) as fd :
88+ self .export_file (fd )
9189 print (f'. { self .script_file_path } ' )
9290
9391 def click_ver (self ) -> int :
9492 return int (click .__version__ .split ('.' )[0 ])
9593
9694
9795class BashShell (UnixShell ):
98- def get_bash_major_minor (self ) -> float :
99- env = self .expanded_env ()
100- bash_interpreter = conf .DETECTED_SHELL_PATH if conf .DETECTED_SHELL_PATH else 'bash'
101- stdout = run_cmd ([bash_interpreter , '-c' , 'echo ${BASH_VERSINFO[0]}.${BASH_VERSINFO[1]}' ], env = env )
102- bash_maj_min = float (stdout )
103- return bash_maj_min
104-
105- @status_message ('Shell completion' , die_on_err = False )
96+ @status_message ('Shell completion' , msg_result = 'Autocompletion code generated' )
10697 def autocompletion (self ) -> str :
107- bash_maj_min = self .get_bash_major_minor ()
108- # Click supports bash version >= 4.4
109- # https://click.palletsprojects.com/en/8.1.x/changes/#version-8-0-0
110- if bash_maj_min < 4.4 :
111- raise RuntimeError ('Autocompletion not supported' )
112-
113- env = self .expanded_env ()
114- env ['LANG' ] = 'en'
115- env ['_IDF.PY_COMPLETE' ] = 'bash_source' if self .click_ver () >= 8 else 'source_bash'
116- stdout : str = run_cmd ([sys .executable , conf .IDF_PY ], env = env )
117- return stdout
98+ bash_source = 'bash_source' if self .click_ver () >= 8 else 'source_bash'
99+ autocom = textwrap .dedent (f"""
100+ WARNING_MSG="WARNING: Failed to load shell autocompletion for bash version: $BASH_VERSION!"
101+ if test ${{BASH_VERSINFO[0]}} -lt 4
102+ then
103+ echo "$WARNING_MSG"
104+ else
105+ if ! eval "$(env LANG=en _IDF.PY_COMPLETE={ bash_source } idf.py)"
106+ then
107+ echo "$WARNING_MSG"
108+ fi
109+ fi
110+ """ )
111+
112+ return autocom
118113
119114 def init_file (self ) -> None :
120115 with open (self .script_file_path , 'w' ) as fd :
@@ -133,13 +128,19 @@ def spawn(self) -> None:
133128
134129
135130class ZshShell (UnixShell ):
136- @status_message ('Shell completion' , die_on_err = False )
131+ @status_message ('Shell completion' , msg_result = 'Autocompletion code generated' )
137132 def autocompletion (self ) -> str :
138- env = self .expanded_env ()
139- env ['LANG' ] = 'en'
140- env ['_IDF.PY_COMPLETE' ] = 'zsh_source' if self .click_ver () >= 8 else 'source_zsh'
141- stdout = run_cmd ([sys .executable , conf .IDF_PY ], env = env )
142- return f'autoload -Uz compinit && compinit -u\n { stdout } '
133+ zsh_source = 'zsh_source' if self .click_ver () >= 8 else 'source_zsh'
134+ autocom = textwrap .dedent (f"""
135+ WARNING_MSG="WARNING: Failed to load shell autocompletion for zsh version: $ZSH_VERSION!"
136+ autoload -Uz compinit && compinit -u
137+ if ! eval "$(env _IDF.PY_COMPLETE={ zsh_source } idf.py)"
138+ then
139+ echo "$WARNING_MSG"
140+ fi
141+ """ )
142+
143+ return autocom
143144
144145 def init_file (self ) -> None :
145146 # If ZDOTDIR is unset, HOME is used instead.
@@ -188,6 +189,10 @@ def autocompletion(self) -> str:
188189 stdout : str = run_cmd ([sys .executable , conf .IDF_PY ], env = env )
189190 return stdout
190191
192+ def init_file (self ) -> None :
193+ with open (self .script_file_path , 'w' ) as fd :
194+ self .export_file (fd )
195+
191196 def spawn (self ) -> None :
192197 self .init_file ()
193198 new_env = os .environ .copy ()
0 commit comments