add curses texpad Esc handler

This commit is contained in:
ivan 2022-06-14 20:36:15 +03:00
parent 37b3926880
commit cd501bdc1e
2 changed files with 67 additions and 31 deletions

View File

@ -13,7 +13,7 @@ def main():
long_description='SSH connection and aliases manager with curses and command line interface',
long_description_content_type='text/x-rst',
license='DWTWL 2.55',
version='1.09.4',
version='1.09.5',
py_modules=['sshch'],
scripts=['sshch/sshch'],
keywords='sshch ssh aliases curses manager',

View File

@ -22,7 +22,7 @@ from curses import textpad, panel
from threading import Thread
# https://gitlab.com/zlax/sshch
version = "1.09.4"
version = "1.09.5"
# expand groups by default
expand_default = True
# path to conf dir and file, default: ~/.config/sshch.conf
@ -355,9 +355,15 @@ def CursesExit(error=False):
exit()
class CursesTextpadEsc(Exception):
"ESC key has been pressed"
def CursesTextpadConfirm(value):
if value == 10:
value = 7
if value == 27:
raise CursesTextpadEsc()
return value
@ -580,22 +586,29 @@ def CursesMain():
new_alias_textpad = CursesTextpad(screen, 1, width - 8,
(height // 2) - 1, 4, "Enter new alias:", "",
normal_text, highlight_text)
add_alias = new_alias_textpad.edit(CursesTextpadConfirm)
try:
add_alias = new_alias_textpad.edit(CursesTextpadConfirm)
except CursesTextpadEsc:
add_alias = ""
if not add_alias == "":
add_alias = add_alias.split()[0].strip()
if not add_alias == "":
add_result = AddNewAlias(add_alias)
if add_result == True:
add_string = ""
while add_string == "":
string_textpad = CursesTextpad(screen, 3,
width - 8, (height // 2) - 1, 4,
"Enter full execution string:",
"ssh ", normal_text, highlight_text)
add_string = string_textpad.edit(
CursesTextpadConfirm)
SetAliasString(add_alias,
add_string.replace("\n", "").rstrip())
try:
while add_string == "":
string_textpad = CursesTextpad(screen, 3,
width - 8, (height // 2) - 1, 4,
"Enter full execution string:",
"ssh ", normal_text, highlight_text)
add_string = string_textpad.edit(
CursesTextpadConfirm)
SetAliasString(add_alias,
add_string.replace("\n", "").rstrip())
except CursesTextpadEsc:
RemoveAliases([add_alias])
add_alias = ""
strings = GetTreeList(False, expanded)
stringsfull = GetTreeList(True, expanded)
row_num = len(strings)
@ -614,23 +627,30 @@ def CursesMain():
new_group_textpad = CursesTextpad(screen, 1, width - 8,
(height // 2) - 1, 4, "Enter group name (without spaces):", "",
normal_text, highlight_text)
add_group = new_group_textpad.edit(CursesTextpadConfirm)
try:
add_group = new_group_textpad.edit(CursesTextpadConfirm)
except CursesTextpadEsc:
add_group = ""
if not add_group == "":
add_group = add_group.split()[0].strip()
if not add_group == "":
add_result = AddNewAlias(add_group)
if add_result == True:
add_string = ""
while add_string.rstrip() == "":
string_textpad = CursesTextpad(screen, 3,
width - 8, (height // 2) - 1, 4,
"Enter aliases for new group (example: alias1 alias2):",
"", normal_text, highlight_text)
add_string = string_textpad.edit(
CursesTextpadConfirm)
SetGroupString(add_group,
add_string.replace("\n", "").rstrip())
expanded.append(add_group)
try:
while add_string.rstrip() == "":
string_textpad = CursesTextpad(screen, 3,
width - 8, (height // 2) - 1, 4,
"Enter aliases for new group (example: alias1 alias2):",
"", normal_text, highlight_text)
add_string = string_textpad.edit(
CursesTextpadConfirm)
SetGroupString(add_group,
add_string.replace("\n", "").rstrip())
expanded.append(add_group)
except CursesTextpadEsc:
RemoveAliases([add_group])
add_group = ""
strings = GetTreeList(False, expanded)
stringsfull = GetTreeList(True, expanded)
row_num = len(strings)
@ -659,7 +679,12 @@ def CursesMain():
"group") if conf.has_option(strings[position - 1],
"group") else ""),
normal_text, highlight_text)
edit_string = string_textpad.edit(CursesTextpadConfirm)
try:
edit_string = string_textpad.edit(CursesTextpadConfirm)
except CursesTextpadEsc:
edit_string = conf.get(strings[position - 1],
"group") if conf.has_option(strings[position - 1],
"group") else ""
SetGroupString(strings[position - 1],
edit_string.replace("\n", "").rstrip())
strings = GetTreeList(False, expanded)
@ -676,7 +701,12 @@ def CursesMain():
"exec_string") if conf.has_option(strings[position - 1].split()[0].strip(),
"exec_string") else ""),
normal_text, highlight_text)
edit_string = string_textpad.edit(CursesTextpadConfirm)
try:
edit_string = string_textpad.edit(CursesTextpadConfirm)
except CursesTextpadEsc:
edit_string = conf.get(strings[position - 1].split()[0].strip(),
"exec_string") if conf.has_option(strings[position - 1].split()[0].strip(),
"exec_string") else ""
SetAliasString(strings[position - 1].split()[0].strip(),
edit_string.replace("\n", "").rstrip())
strings = GetTreeList(False, expanded)
@ -740,9 +770,12 @@ def CursesMain():
"Enter specific command to execute with selected ",
"alias/aliases:"]
), "", normal_text, highlight_text)
command_string = command_textpad.edit(CursesTextpadConfirm)
Connect(selected, command_string.replace("\n", "").rstrip(),
False, screen)
try:
command_string = command_textpad.edit(CursesTextpadConfirm)
Connect(selected, command_string.replace("\n", "").rstrip(),
False, screen)
except CursesTextpadEsc:
command_string = ""
curses.curs_set(0)
if (key_pressed == ord('t') or key_pressed == ord(
'T') or key_pressed == curses.KEY_F11) and row_num != 0:
@ -759,9 +792,12 @@ def CursesMain():
"Enter specific command to execute with selected ",
"alias/aliases:"]
), "", normal_text, highlight_text)
command_string = command_textpad.edit(CursesTextpadConfirm)
Connect(selected, command_string.replace("\n", "").rstrip(),
True, screen)
try:
command_string = command_textpad.edit(CursesTextpadConfirm)
Connect(selected, command_string.replace("\n", "").rstrip(),
True, screen)
except CursesTextpadEsc:
command_string = ""
curses.curs_set(0)
if (key_pressed == ord('k') or key_pressed == ord('K') or
key_pressed == (curses.KEY_F7)) and row_num != 0: