From d96910092b1047d8a9b34cbb52200671fa511ad2 Mon Sep 17 00:00:00 2001 From: Federico Luzzi Date: Thu, 5 Oct 2023 15:21:17 -0300 Subject: [PATCH] Fix completion and save logs during session --- connpy/_version.py | 2 +- connpy/completion.py | 34 +++++++++++++++++------- connpy/connapp.py | 21 +++++++++++---- connpy/core.py | 27 ++++++++++++++++--- docs/connpy/index.html | 60 +++++++++++++++++++++++++++++++++--------- 5 files changed, 111 insertions(+), 33 deletions(-) diff --git a/connpy/_version.py b/connpy/_version.py index 150af39..5519f1f 100644 --- a/connpy/_version.py +++ b/connpy/_version.py @@ -1,2 +1,2 @@ -__version__ = "3.4.3" +__version__ = "3.5.0" diff --git a/connpy/completion.py b/connpy/completion.py index b2617a6..0bad8ff 100755 --- a/connpy/completion.py +++ b/connpy/completion.py @@ -28,6 +28,25 @@ def _getallfolders(config): folders.extend(subfolders) 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(): home = os.path.expanduser("~") 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"]: strings.extend(nodes) 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"] - pathstrings = [s[2:] if s.startswith('./') else s for s in pathstrings] 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) if words[0] == "run": strings.extend(nodes) @@ -102,6 +114,8 @@ def main(): strings=["bash", "zsh"] if words[0] == "config" and words[1] in ["--fzf", "--allow-uppercase"]: strings=["true", "false"] + if words[0] == "config" and words[1] in ["--configfolder"]: + strings=_getcwd(words,words[0],True) if app == "bash": diff --git a/connpy/connapp.py b/connpy/connapp.py index 67c761a..ec4846e 100755 --- a/connpy/connapp.py +++ b/connpy/connapp.py @@ -1184,8 +1184,13 @@ class connapp: _conn() { mapfile -t strings < <(connpy-completion-helper "bash" "${#COMP_WORDS[@]}" "${COMP_WORDS[@]}") - local IFS=$'\\t\\n' - COMPREPLY=($(compgen -W "$(printf '%s' "${strings[@]}")" -- "${COMP_WORDS[-1]}")) + local IFS=$'\t\n' + 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 @@ -1198,14 +1203,20 @@ complete -o nospace -o nosort -F _conn connpy autoload -U compinit && compinit _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 + #Replace the expanded home directory with ~ + if [ "$last_word" != "$words[-1]" ]; then + string=${string/$home_dir/\~} + fi if [[ "${string}" =~ .*/$ ]]; then # If the string ends with a '/', do not append a space - compadd -S '' -- "$string" + compadd -Q -S '' -- "$string" else # If the string does not end with a '/', append a space - compadd -S ' ' -- "$string" + compadd -Q -S ' ' -- "$string" fi done } diff --git a/connpy/core.py b/connpy/core.py index 4e9b473..05cb5e8 100755 --- a/connpy/core.py +++ b/connpy/core.py @@ -161,6 +161,21 @@ class node: else: 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): #Set time for last input when using interact self.lastinput = time() @@ -192,9 +207,15 @@ class node: 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) if 'logfile' in dir(self): + # Initialize self.mylog if not 'mylog' in dir(self): self.mylog = io.BytesIO() 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): print(self.child.after.decode(), end='') if self.idletime > 0: @@ -204,11 +225,9 @@ class node: if debug: print(self.mylog.getvalue().decode()) self.child.interact(input_filter=self._filter) - if "logfile" in dir(self): - output = self._logclean(self.mylog.getvalue().decode(), True) + if 'logfile' in dir(self): with open(self.logfile, "w") as f: - f.write(output) - f.close() + f.write(self._logclean(self.mylog.getvalue().decode(), True)) else: print(connect) diff --git a/docs/connpy/index.html b/docs/connpy/index.html index da7778c..b48023e 100644 --- a/docs/connpy/index.html +++ b/docs/connpy/index.html @@ -2973,8 +2973,13 @@ Categorize the user's request based on the operation they want to perform on _conn() { mapfile -t strings < <(connpy-completion-helper "bash" "${#COMP_WORDS[@]}" "${COMP_WORDS[@]}") - local IFS=$'\\t\\n' - COMPREPLY=($(compgen -W "$(printf '%s' "${strings[@]}")" -- "${COMP_WORDS[-1]}")) + local IFS=$'\t\n' + 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 @@ -2987,14 +2992,20 @@ complete -o nospace -o nosort -F _conn connpy autoload -U compinit && compinit _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 + #Replace the expanded home directory with ~ + if [ "$last_word" != "$words[-1]" ]; then + string=${string/$home_dir/\~} + fi if [[ "${string}" =~ .*/$ ]]; then # If the string ends with a '/', do not append a space - compadd -S '' -- "$string" + compadd -Q -S '' -- "$string" else # If the string does not end with a '/', append a space - compadd -S ' ' -- "$string" + compadd -Q -S ' ' -- "$string" fi done } @@ -3461,6 +3472,21 @@ tasks: else: 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): #Set time for last input when using interact self.lastinput = time() @@ -3492,9 +3518,15 @@ tasks: 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) if 'logfile' in dir(self): + # Initialize self.mylog if not 'mylog' in dir(self): self.mylog = io.BytesIO() 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): print(self.child.after.decode(), end='') if self.idletime > 0: @@ -3504,11 +3536,9 @@ tasks: if debug: print(self.mylog.getvalue().decode()) self.child.interact(input_filter=self._filter) - if "logfile" in dir(self): - output = self._logclean(self.mylog.getvalue().decode(), True) + if 'logfile' in dir(self): with open(self.logfile, "w") as f: - f.write(output) - f.close() + f.write(self._logclean(self.mylog.getvalue().decode(), True)) else: print(connect) @@ -3821,9 +3851,15 @@ tasks: 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) if 'logfile' in dir(self): + # Initialize self.mylog if not 'mylog' in dir(self): self.mylog = io.BytesIO() 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): print(self.child.after.decode(), end='') if self.idletime > 0: @@ -3833,11 +3869,9 @@ tasks: if debug: print(self.mylog.getvalue().decode()) self.child.interact(input_filter=self._filter) - if "logfile" in dir(self): - output = self._logclean(self.mylog.getvalue().decode(), True) + if 'logfile' in dir(self): with open(self.logfile, "w") as f: - f.write(output) - f.close() + f.write(self._logclean(self.mylog.getvalue().decode(), True)) else: print(connect)