Fix completion and save logs during session

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

View File

@ -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)