Fix completion and save logs during session

This commit is contained in:
Federico Luzzi 2023-10-05 15:21:17 -03:00
parent 0813b927b0
commit d96910092b
5 changed files with 111 additions and 33 deletions

View File

@ -1,2 +1,2 @@
__version__ = "3.4.3" __version__ = "3.5.0"

View File

@ -28,6 +28,25 @@ def _getallfolders(config):
folders.extend(subfolders) folders.extend(subfolders)
return folders return folders
def _getcwd(words, option, folderonly=False):
# Expand tilde to home directory if present
if words[-1].startswith("~"):
words[-1] = os.path.expanduser(words[-1])
if words[-1] == option:
path = './*'
else:
path = words[-1] + "*"
pathstrings = glob.glob(path)
for i in range(len(pathstrings)):
if os.path.isdir(pathstrings[i]):
pathstrings[i] += '/'
pathstrings = [s[2:] if s.startswith('./') else s for s in pathstrings]
if folderonly:
pathstrings = [s for s in pathstrings if os.path.isdir(s)]
return pathstrings
def main(): def main():
home = os.path.expanduser("~") home = os.path.expanduser("~")
defaultdir = home + '/.config/conn' defaultdir = home + '/.config/conn'
@ -73,18 +92,11 @@ def main():
if words[0] in ["--rm", "--del", "-r", "--mod", "--edit", "-e", "--show", "-s", "mv", "move", "cp", "copy"]: if words[0] in ["--rm", "--del", "-r", "--mod", "--edit", "-e", "--show", "-s", "mv", "move", "cp", "copy"]:
strings.extend(nodes) strings.extend(nodes)
if words[0] in ["run", "import", "export"]: if words[0] in ["run", "import", "export"]:
if words[-1] in ["run", "import", "export"]:
path = './*'
else:
path = words[-1] + "*"
pathstrings = glob.glob(path)
for i in range(len(pathstrings)):
if os.path.isdir(pathstrings[i]):
pathstrings[i] += '/'
strings = ["--help"] strings = ["--help"]
pathstrings = [s[2:] if s.startswith('./') else s for s in pathstrings]
if words[0] == "export": if words[0] == "export":
pathstrings = [s for s in pathstrings if os.path.isdir(s)] pathstrings = _getcwd(words, words[0], True)
else:
pathstrings = _getcwd(words, words[0])
strings.extend(pathstrings) strings.extend(pathstrings)
if words[0] == "run": if words[0] == "run":
strings.extend(nodes) strings.extend(nodes)
@ -102,6 +114,8 @@ def main():
strings=["bash", "zsh"] strings=["bash", "zsh"]
if words[0] == "config" and words[1] in ["--fzf", "--allow-uppercase"]: if words[0] == "config" and words[1] in ["--fzf", "--allow-uppercase"]:
strings=["true", "false"] strings=["true", "false"]
if words[0] == "config" and words[1] in ["--configfolder"]:
strings=_getcwd(words,words[0],True)
if app == "bash": if app == "bash":

View File

@ -1184,8 +1184,13 @@ class connapp:
_conn() _conn()
{ {
mapfile -t strings < <(connpy-completion-helper "bash" "${#COMP_WORDS[@]}" "${COMP_WORDS[@]}") mapfile -t strings < <(connpy-completion-helper "bash" "${#COMP_WORDS[@]}" "${COMP_WORDS[@]}")
local IFS=$'\\t\\n' local IFS=$'\t\n'
COMPREPLY=($(compgen -W "$(printf '%s' "${strings[@]}")" -- "${COMP_WORDS[-1]}")) local home_dir=$(eval echo ~)
local last_word=${COMP_WORDS[-1]/\~/$home_dir}
COMPREPLY=($(compgen -W "$(printf '%s' "${strings[@]}")" -- "$last_word"))
if [ "$last_word" != "${COMP_WORDS[-1]}" ]; then
COMPREPLY=(${COMPREPLY[@]/$home_dir/\~})
fi
} }
complete -o nospace -o nosort -F _conn conn complete -o nospace -o nosort -F _conn conn
@ -1198,14 +1203,20 @@ complete -o nospace -o nosort -F _conn connpy
autoload -U compinit && compinit autoload -U compinit && compinit
_conn() _conn()
{ {
strings=($(connpy-completion-helper "zsh" ${#words} $words)) local home_dir=$(eval echo ~)
last_word=${words[-1]/\~/$home_dir}
strings=($(connpy-completion-helper "zsh" ${#words} $words[1,-2] $last_word))
for string in "${strings[@]}"; do for string in "${strings[@]}"; do
#Replace the expanded home directory with ~
if [ "$last_word" != "$words[-1]" ]; then
string=${string/$home_dir/\~}
fi
if [[ "${string}" =~ .*/$ ]]; then if [[ "${string}" =~ .*/$ ]]; then
# If the string ends with a '/', do not append a space # If the string ends with a '/', do not append a space
compadd -S '' -- "$string" compadd -Q -S '' -- "$string"
else else
# If the string does not end with a '/', append a space # If the string does not end with a '/', append a space
compadd -S ' ' -- "$string" compadd -Q -S ' ' -- "$string"
fi fi
done done
} }

View File

@ -161,6 +161,21 @@ class node:
else: else:
return t return t
def _savelog(self):
'''Save the log buffer to the file at regular intervals if there are changes.'''
t = threading.current_thread()
prev_size = 0 # Store the previous size of the buffer
while getattr(t, "do_run", True): # Check if thread is signaled to stop
current_size = self.mylog.tell() # Current size of the buffer
# Only save if the buffer size has changed
if current_size != prev_size:
with open(self.logfile, "w") as f: # Use "w" to overwrite the file
f.write(self._logclean(self.mylog.getvalue().decode(), True))
prev_size = current_size # Update the previous size
sleep(5)
def _filter(self, a): def _filter(self, a):
#Set time for last input when using interact #Set time for last input when using interact
self.lastinput = time() self.lastinput = time()
@ -192,9 +207,15 @@ class node:
self.child.setwinsize(int(size.group(2)),int(size.group(1))) self.child.setwinsize(int(size.group(2)),int(size.group(1)))
print("Connected to " + self.unique + " at " + self.host + (":" if self.port != '' else '') + self.port + " via: " + self.protocol) print("Connected to " + self.unique + " at " + self.host + (":" if self.port != '' else '') + self.port + " via: " + self.protocol)
if 'logfile' in dir(self): if 'logfile' in dir(self):
# Initialize self.mylog
if not 'mylog' in dir(self): if not 'mylog' in dir(self):
self.mylog = io.BytesIO() self.mylog = io.BytesIO()
self.child.logfile_read = self.mylog self.child.logfile_read = self.mylog
# Start the _savelog thread
log_thread = threading.Thread(target=self._savelog)
log_thread.daemon = True
log_thread.start()
if 'missingtext' in dir(self): if 'missingtext' in dir(self):
print(self.child.after.decode(), end='') print(self.child.after.decode(), end='')
if self.idletime > 0: if self.idletime > 0:
@ -204,11 +225,9 @@ class node:
if debug: if debug:
print(self.mylog.getvalue().decode()) print(self.mylog.getvalue().decode())
self.child.interact(input_filter=self._filter) self.child.interact(input_filter=self._filter)
if "logfile" in dir(self): if 'logfile' in dir(self):
output = self._logclean(self.mylog.getvalue().decode(), True)
with open(self.logfile, "w") as f: with open(self.logfile, "w") as f:
f.write(output) f.write(self._logclean(self.mylog.getvalue().decode(), True))
f.close()
else: else:
print(connect) print(connect)

View File

@ -2973,8 +2973,13 @@ Categorize the user&#39;s request based on the operation they want to perform on
_conn() _conn()
{ {
mapfile -t strings &lt; &lt;(connpy-completion-helper &#34;bash&#34; &#34;${#COMP_WORDS[@]}&#34; &#34;${COMP_WORDS[@]}&#34;) mapfile -t strings &lt; &lt;(connpy-completion-helper &#34;bash&#34; &#34;${#COMP_WORDS[@]}&#34; &#34;${COMP_WORDS[@]}&#34;)
local IFS=$&#39;\\t\\n&#39; local IFS=$&#39;\t\n&#39;
COMPREPLY=($(compgen -W &#34;$(printf &#39;%s&#39; &#34;${strings[@]}&#34;)&#34; -- &#34;${COMP_WORDS[-1]}&#34;)) local home_dir=$(eval echo ~)
local last_word=${COMP_WORDS[-1]/\~/$home_dir}
COMPREPLY=($(compgen -W &#34;$(printf &#39;%s&#39; &#34;${strings[@]}&#34;)&#34; -- &#34;$last_word&#34;))
if [ &#34;$last_word&#34; != &#34;${COMP_WORDS[-1]}&#34; ]; then
COMPREPLY=(${COMPREPLY[@]/$home_dir/\~})
fi
} }
complete -o nospace -o nosort -F _conn conn complete -o nospace -o nosort -F _conn conn
@ -2987,14 +2992,20 @@ complete -o nospace -o nosort -F _conn connpy
autoload -U compinit &amp;&amp; compinit autoload -U compinit &amp;&amp; compinit
_conn() _conn()
{ {
strings=($(connpy-completion-helper &#34;zsh&#34; ${#words} $words)) local home_dir=$(eval echo ~)
last_word=${words[-1]/\~/$home_dir}
strings=($(connpy-completion-helper &#34;zsh&#34; ${#words} $words[1,-2] $last_word))
for string in &#34;${strings[@]}&#34;; do for string in &#34;${strings[@]}&#34;; do
#Replace the expanded home directory with ~
if [ &#34;$last_word&#34; != &#34;$words[-1]&#34; ]; then
string=${string/$home_dir/\~}
fi
if [[ &#34;${string}&#34; =~ .*/$ ]]; then if [[ &#34;${string}&#34; =~ .*/$ ]]; then
# If the string ends with a &#39;/&#39;, do not append a space # If the string ends with a &#39;/&#39;, do not append a space
compadd -S &#39;&#39; -- &#34;$string&#34; compadd -Q -S &#39;&#39; -- &#34;$string&#34;
else else
# If the string does not end with a &#39;/&#39;, append a space # If the string does not end with a &#39;/&#39;, append a space
compadd -S &#39; &#39; -- &#34;$string&#34; compadd -Q -S &#39; &#39; -- &#34;$string&#34;
fi fi
done done
} }
@ -3461,6 +3472,21 @@ tasks:
else: else:
return t return t
def _savelog(self):
&#39;&#39;&#39;Save the log buffer to the file at regular intervals if there are changes.&#39;&#39;&#39;
t = threading.current_thread()
prev_size = 0 # Store the previous size of the buffer
while getattr(t, &#34;do_run&#34;, True): # Check if thread is signaled to stop
current_size = self.mylog.tell() # Current size of the buffer
# Only save if the buffer size has changed
if current_size != prev_size:
with open(self.logfile, &#34;w&#34;) as f: # Use &#34;w&#34; to overwrite the file
f.write(self._logclean(self.mylog.getvalue().decode(), True))
prev_size = current_size # Update the previous size
sleep(5)
def _filter(self, a): def _filter(self, a):
#Set time for last input when using interact #Set time for last input when using interact
self.lastinput = time() self.lastinput = time()
@ -3492,9 +3518,15 @@ tasks:
self.child.setwinsize(int(size.group(2)),int(size.group(1))) self.child.setwinsize(int(size.group(2)),int(size.group(1)))
print(&#34;Connected to &#34; + self.unique + &#34; at &#34; + self.host + (&#34;:&#34; if self.port != &#39;&#39; else &#39;&#39;) + self.port + &#34; via: &#34; + self.protocol) print(&#34;Connected to &#34; + self.unique + &#34; at &#34; + self.host + (&#34;:&#34; if self.port != &#39;&#39; else &#39;&#39;) + self.port + &#34; via: &#34; + self.protocol)
if &#39;logfile&#39; in dir(self): if &#39;logfile&#39; in dir(self):
# Initialize self.mylog
if not &#39;mylog&#39; in dir(self): if not &#39;mylog&#39; in dir(self):
self.mylog = io.BytesIO() self.mylog = io.BytesIO()
self.child.logfile_read = self.mylog self.child.logfile_read = self.mylog
# Start the _savelog thread
log_thread = threading.Thread(target=self._savelog)
log_thread.daemon = True
log_thread.start()
if &#39;missingtext&#39; in dir(self): if &#39;missingtext&#39; in dir(self):
print(self.child.after.decode(), end=&#39;&#39;) print(self.child.after.decode(), end=&#39;&#39;)
if self.idletime &gt; 0: if self.idletime &gt; 0:
@ -3504,11 +3536,9 @@ tasks:
if debug: if debug:
print(self.mylog.getvalue().decode()) print(self.mylog.getvalue().decode())
self.child.interact(input_filter=self._filter) self.child.interact(input_filter=self._filter)
if &#34;logfile&#34; in dir(self): if &#39;logfile&#39; in dir(self):
output = self._logclean(self.mylog.getvalue().decode(), True)
with open(self.logfile, &#34;w&#34;) as f: with open(self.logfile, &#34;w&#34;) as f:
f.write(output) f.write(self._logclean(self.mylog.getvalue().decode(), True))
f.close()
else: else:
print(connect) print(connect)
@ -3821,9 +3851,15 @@ tasks:
self.child.setwinsize(int(size.group(2)),int(size.group(1))) self.child.setwinsize(int(size.group(2)),int(size.group(1)))
print(&#34;Connected to &#34; + self.unique + &#34; at &#34; + self.host + (&#34;:&#34; if self.port != &#39;&#39; else &#39;&#39;) + self.port + &#34; via: &#34; + self.protocol) print(&#34;Connected to &#34; + self.unique + &#34; at &#34; + self.host + (&#34;:&#34; if self.port != &#39;&#39; else &#39;&#39;) + self.port + &#34; via: &#34; + self.protocol)
if &#39;logfile&#39; in dir(self): if &#39;logfile&#39; in dir(self):
# Initialize self.mylog
if not &#39;mylog&#39; in dir(self): if not &#39;mylog&#39; in dir(self):
self.mylog = io.BytesIO() self.mylog = io.BytesIO()
self.child.logfile_read = self.mylog self.child.logfile_read = self.mylog
# Start the _savelog thread
log_thread = threading.Thread(target=self._savelog)
log_thread.daemon = True
log_thread.start()
if &#39;missingtext&#39; in dir(self): if &#39;missingtext&#39; in dir(self):
print(self.child.after.decode(), end=&#39;&#39;) print(self.child.after.decode(), end=&#39;&#39;)
if self.idletime &gt; 0: if self.idletime &gt; 0:
@ -3833,11 +3869,9 @@ tasks:
if debug: if debug:
print(self.mylog.getvalue().decode()) print(self.mylog.getvalue().decode())
self.child.interact(input_filter=self._filter) self.child.interact(input_filter=self._filter)
if &#34;logfile&#34; in dir(self): if &#39;logfile&#39; in dir(self):
output = self._logclean(self.mylog.getvalue().decode(), True)
with open(self.logfile, &#34;w&#34;) as f: with open(self.logfile, &#34;w&#34;) as f:
f.write(output) f.write(self._logclean(self.mylog.getvalue().decode(), True))
f.close()
else: else:
print(connect) print(connect)