Skip to content

Commit e40aa05

Browse files
authored
Merge pull request nicolargo#2900 from ariel-anieli/issue-2801-catch-key
Reduced complexity of `_GlancesCurses.__catch_key()`
2 parents c5f06b3 + 1b199ad commit e40aa05

File tree

1 file changed

+23
-18
lines changed

1 file changed

+23
-18
lines changed

glances/outputs/glances_curses.py

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -235,24 +235,16 @@ def get_key(self, window):
235235
# TODO: Check issue #163
236236
return window.getch()
237237

238-
def __catch_key(self, return_to_browser=False):
239-
# Catch the pressed key
240-
self.pressedkey = self.get_key(self.term_window)
241-
if self.pressedkey == -1:
242-
return -1
243-
244-
# Actions (available in the global hotkey dict)...
245-
logger.debug(f"Keypressed (code: {self.pressedkey})")
246-
for hotkey in self._hotkeys:
247-
if self.pressedkey == ord(hotkey) and 'switch' in self._hotkeys[hotkey]:
248-
self._handle_switch(hotkey)
249-
elif self.pressedkey == ord(hotkey) and 'sort_key' in self._hotkeys[hotkey]:
250-
self._handle_sort_key(hotkey)
251-
if self.pressedkey == ord(hotkey) and 'handler' in self._hotkeys[hotkey]:
252-
action = getattr(self, self._hotkeys[hotkey]['handler'])
253-
action()
254-
255-
# Other actions with key > 255 (ord will not work) and/or additional test...
238+
def catch_actions_from_hotkey(self, hotkey):
239+
if self.pressedkey == ord(hotkey) and 'switch' in self._hotkeys[hotkey]:
240+
self._handle_switch(hotkey)
241+
elif self.pressedkey == ord(hotkey) and 'sort_key' in self._hotkeys[hotkey]:
242+
self._handle_sort_key(hotkey)
243+
if self.pressedkey == ord(hotkey) and 'handler' in self._hotkeys[hotkey]:
244+
action = getattr(self, self._hotkeys[hotkey]['handler'])
245+
action()
246+
247+
def catch_other_actions_maybe_return_to_browser(self, return_to_browser):
256248
if self.pressedkey == ord('e') and not self.args.programs:
257249
self._handle_process_extended()
258250
elif self.pressedkey == ord('k') and not self.args.disable_cursor:
@@ -270,6 +262,19 @@ def __catch_key(self, return_to_browser=False):
270262
elif self.pressedkey == curses.KEY_F5 or self.pressedkey == 18:
271263
self._handle_refresh()
272264

265+
def __catch_key(self, return_to_browser=False):
266+
# Catch the pressed key
267+
self.pressedkey = self.get_key(self.term_window)
268+
if self.pressedkey == -1:
269+
return self.pressedkey
270+
271+
# Actions (available in the global hotkey dict)...
272+
logger.debug(f"Keypressed (code: {self.pressedkey})")
273+
[self.catch_actions_from_hotkey(hotkey) for hotkey in self._hotkeys]
274+
275+
# Other actions with key > 255 (ord will not work) and/or additional test...
276+
self.catch_other_actions_maybe_return_to_browser(return_to_browser)
277+
273278
# Return the key code
274279
return self.pressedkey
275280

0 commit comments

Comments
 (0)