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='SSH connection and aliases manager with curses and command line interface',
long_description_content_type='text/x-rst', long_description_content_type='text/x-rst',
license='DWTWL 2.55', license='DWTWL 2.55',
version='1.09.4', version='1.09.5',
py_modules=['sshch'], py_modules=['sshch'],
scripts=['sshch/sshch'], scripts=['sshch/sshch'],
keywords='sshch ssh aliases curses manager', keywords='sshch ssh aliases curses manager',

View File

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