Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
IngoMeyer441 committed Sep 21, 2022
2 parents 1c28d04 + 35ca9d5 commit e38ffae
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 27 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -600,7 +600,7 @@ from simple_term_menu import TerminalMenu


def main():
main_menu_title = " Main Menu\n"
main_menu_title = " Main Menu.\n Press Q or Esc to quit. \n"
main_menu_items = ["Edit Menu", "Second Item", "Third Item", "Quit"]
main_menu_cursor = "> "
main_menu_cursor_style = ("fg_red", "bold")
Expand All @@ -617,7 +617,7 @@ def main():
clear_screen=True,
)

edit_menu_title = " Edit Menu\n"
edit_menu_title = " Edit Menu.\n Press Q or Esc to back to main menu. \n"
edit_menu_items = ["Edit Config", "Save Settings", "Back to Main Menu"]
edit_menu_back = False
edit_menu = TerminalMenu(
Expand All @@ -642,7 +642,7 @@ def main():
elif edit_sel == 1:
print("Save Selected")
time.sleep(5)
elif edit_sel == 2:
elif edit_sel == 2 or edit_sel == None:
edit_menu_back = True
print("Back Selected")
edit_menu_back = False
Expand All @@ -652,7 +652,7 @@ def main():
elif main_sel == 2:
print("option 3 selected")
time.sleep(5)
elif main_sel == 3:
elif main_sel == 3 or main_sel == None:
main_menu_exit = True
print("Quit Selected")

Expand Down
67 changes: 44 additions & 23 deletions simple_term_menu.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
__email__ = "i.meyer@fz-juelich.de"
__copyright__ = "Copyright © 2021 Forschungszentrum Jülich GmbH. All rights reserved."
__license__ = "MIT"
__version_info__ = (1, 5, 0)
__version_info__ = (1, 5, 1)
__version__ = ".".join(map(str, __version_info__))


Expand Down Expand Up @@ -109,17 +109,25 @@ def get_locale() -> str:

def wcswidth(text: str) -> int:
if not hasattr(wcswidth, "libc"):
if platform.system() == "Darwin":
wcswidth.libc = ctypes.cdll.LoadLibrary("libSystem.dylib") # type: ignore
else:
wcswidth.libc = ctypes.cdll.LoadLibrary("libc.so.6") # type: ignore
user_locale = get_locale()
# First replace any null characters with the unicode replacement character (U+FFFD) since they cannot be passed
# in a `c_wchar_p`
encoded_text = text.replace("\0", "\uFFFD").encode(encoding=user_locale, errors="replace")
return wcswidth.libc.wcswidth( # type: ignore
ctypes.c_wchar_p(encoded_text.decode(encoding=user_locale)), len(encoded_text)
)
try:
if platform.system() == "Darwin":
wcswidth.libc = ctypes.cdll.LoadLibrary("libSystem.dylib") # type: ignore
else:
wcswidth.libc = ctypes.cdll.LoadLibrary("libc.so.6") # type: ignore
except OSError:
wcswidth.libc = None # type: ignore
if wcswidth.libc is not None: # type: ignore
try:
user_locale = get_locale()
# First replace any null characters with the unicode replacement character (U+FFFD) since they cannot be
# passed in a `c_wchar_p`
encoded_text = text.replace("\0", "\uFFFD").encode(encoding=user_locale, errors="replace")
return wcswidth.libc.wcswidth( # type: ignore
ctypes.c_wchar_p(encoded_text.decode(encoding=user_locale)), len(encoded_text)
)
except AttributeError:
pass
return len(text)


def static_variables(**variables: Any) -> Callable[[Callable[..., Any]], Callable[..., Any]]:
Expand Down Expand Up @@ -309,8 +317,8 @@ def increment_active_index(self) -> None:
self._active_displayed_index = 0
self._viewport.keep_visible(self._active_displayed_index)

if self._active_displayed_index in self._skip_indices:
self.increment_active_index()
if self._displayed_index_to_menu_index[self._active_displayed_index] in self._skip_indices:
self.increment_active_index()

def decrement_active_index(self) -> None:
if self._active_displayed_index is not None:
Expand All @@ -320,8 +328,8 @@ def decrement_active_index(self) -> None:
self._active_displayed_index = len(self._displayed_index_to_menu_index) - 1
self._viewport.keep_visible(self._active_displayed_index)

if self._active_displayed_index in self._skip_indices:
self.decrement_active_index()
if self._displayed_index_to_menu_index[self._active_displayed_index] in self._skip_indices:
self.decrement_active_index()

def is_visible(self, menu_index: int) -> bool:
return menu_index in self._menu_index_to_displayed_index and (
Expand All @@ -348,18 +356,21 @@ def active_menu_index(self) -> Optional[int]:

@active_menu_index.setter
def active_menu_index(self, value: int) -> None:
self._selected_index = value
self._active_displayed_index = [
displayed_index
for displayed_index, menu_index in enumerate(self._displayed_index_to_menu_index)
if menu_index == value
][0]
self._viewport.keep_visible(self._active_displayed_index)
self.active_displayed_index = self._menu_index_to_displayed_index[value]

@property
def active_displayed_index(self) -> Optional[int]:
return self._active_displayed_index

@active_displayed_index.setter
def active_displayed_index(self, value: int) -> None:
self._active_displayed_index = value
self._viewport.keep_visible(self._active_displayed_index)

@property
def max_displayed_index(self) -> int:
return len(self._displayed_index_to_menu_index) - 1

@property
def displayed_selected_indices(self) -> List[int]:
return [
Expand Down Expand Up @@ -507,6 +518,7 @@ def must_scroll(self) -> bool:
"cursor_visible": "cnorm",
"delete_line": "dl1",
"down": "kcud1",
"end": "kend",
"enter_application_mode": "smkx",
"exit_application_mode": "rmkx",
"fg_black": "setaf 0",
Expand All @@ -517,6 +529,7 @@ def must_scroll(self) -> bool:
"fg_purple": "setaf 5",
"fg_red": "setaf 1",
"fg_yellow": "setaf 3",
"home": "khome",
"italics": "sitm",
"reset_attributes": "sgr0",
"standout": "smso",
Expand All @@ -525,6 +538,8 @@ def must_scroll(self) -> bool:
}
_name_to_control_character = {
"backspace": "", # Is assigned later in `self._init_backspace_control_character`
"ctrl-a": "\001",
"ctrl-e": "\005",
"ctrl-j": "\012",
"ctrl-k": "\013",
"enter": "\015",
Expand Down Expand Up @@ -1457,6 +1472,8 @@ def remove_letter_keys(menu_action_to_keys: Dict[str, Set[Optional[str]]]) -> No
menu_action_to_keys = {
"menu_up": set(("up", "ctrl-k", "k")),
"menu_down": set(("down", "ctrl-j", "j")),
"menu_start": set(("home", "ctrl-a")),
"menu_end": set(("end", "ctrl-e")),
"accept": set(self._accept_keys),
"multi_select": set(self._multi_select_keys),
"quit": set(self._quit_keys),
Expand Down Expand Up @@ -1485,6 +1502,10 @@ def remove_letter_keys(menu_action_to_keys: Dict[str, Set[Optional[str]]]) -> No
self._view.decrement_active_index()
elif next_key in current_menu_action_to_keys["menu_down"]:
self._view.increment_active_index()
elif next_key in current_menu_action_to_keys["menu_start"]:
self._view.active_displayed_index = 0
elif next_key in current_menu_action_to_keys["menu_end"]:
self._view.active_displayed_index = self._view.max_displayed_index
elif self._multi_select and next_key in current_menu_action_to_keys["multi_select"]:
if self._view.active_menu_index is not None:
self._selection.toggle(self._view.active_menu_index)
Expand Down

0 comments on commit e38ffae

Please sign in to comment.