From cd501bdc1ec115771d2a884c40807a899299ccdb Mon Sep 17 00:00:00 2001 From: zlax Date: Tue, 14 Jun 2022 20:36:15 +0300 Subject: [PATCH] add curses texpad Esc handler --- setup.py | 2 +- sshch/sshch | 96 ++++++++++++++++++++++++++++++++++++----------------- 2 files changed, 67 insertions(+), 31 deletions(-) diff --git a/setup.py b/setup.py index cd7df86..44ba4c7 100644 --- a/setup.py +++ b/setup.py @@ -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', diff --git a/sshch/sshch b/sshch/sshch index 19fbb52..5cb54bb 100755 --- a/sshch/sshch +++ b/sshch/sshch @@ -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: