diff options
-rwxr-xr-x | logout-manager-ncurses.py | 50 |
1 files changed, 34 insertions, 16 deletions
diff --git a/logout-manager-ncurses.py b/logout-manager-ncurses.py index 7356569..99801d7 100755 --- a/logout-manager-ncurses.py +++ b/logout-manager-ncurses.py @@ -13,6 +13,10 @@ # Improve: # add "disabled" option in menu, with default=enabled # Documentation: +# Improvements for CursesMenu class over origin: +# accepts number key inputs +# accepts enabled attribute +# add "zeroindex" bool import curses, sys, os sys.path.append("/home/bgirton/dev/logout-manager") @@ -28,6 +32,11 @@ class CursesMenu(object): self.selected_option = 0 self._previously_selected_option = None self.running = True + self._zero_offset = 1 + try: + self._zero_offset = 0 if bool(self.menu_options['zeroindex']) else 1 + except: + pass #init curses and curses input curses.noecho() @@ -52,9 +61,7 @@ class CursesMenu(object): input_key = None ENTER_KEY = ord('\n') - #NUM_KEYS = [ord('1')] - #NUM_KEYS = [ord(1),ord(2)...] - NUM_KEYS = [ord(str(i)) for i in range(1,option_count+2)] + NUM_KEYS = [ord(str(i)) for i in range(self._zero_offset,option_count+1+self._zero_offset)] done = False while not done: if self.selected_option != self._previously_selected_option: @@ -69,15 +76,15 @@ class CursesMenu(object): self._draw_option(option, self.normal_color) if self.selected_option == option_count: - self.screen.addstr(5 + option_count, 4, "{:2} - {}".format(option_count+1, + self.screen.addstr(4 + option_count, 4, "{:2} - {}".format(option_count+self._zero_offset, lastoption), self.hilite_color) else: - self.screen.addstr(5 + option_count, 4, "{:2} - {}".format(option_count+1, + self.screen.addstr(4 + option_count, 4, "{:2} - {}".format(option_count+self._zero_offset, lastoption), self.normal_color) max_y, max_x = self.screen.getmaxyx() if input_key is not None: - self.screen.addstr(max_y-3, max_x - 5, "{:3}".format(self.selected_option)) + self.screen.addstr(max_y-3, max_x - 5, "{:3}".format(self.selected_option+self._zero_offset)) self.screen.refresh() @@ -104,19 +111,28 @@ class CursesMenu(object): if input_key == ENTER_KEY or input_key in NUM_KEYS: if input_key in NUM_KEYS: - self.selected_option=int(chr(input_key))-1 + self.selected_option=int(chr(input_key))-self._zero_offset done = True + try: + done = self.menu_options['options'][self.selected_option]['enabled'] + except: + pass return self.selected_option def _draw_option(self, option_number, style): - self.screen.addstr(5 + option_number, + thistext = self.menu_options['options'][option_number]['title'] + try: + if self.menu_options['options'][option_number]['enabled'] == False: thistext += " (disabled)" + except: + pass + self.screen.addstr(4 + option_number, 4, - "{:2} - {}".format(option_number+1, self.menu_options['options'][option_number]['title']), + "{:2} - {}".format(option_number+self._zero_offset, thistext), style) def _draw_title(self): self.screen.addstr(2, 2, self.menu_options['title'], curses.A_STANDOUT) - self.screen.addstr(4, 2, self.menu_options['subtitle'], curses.A_BOLD) + self.screen.addstr(3, 2, self.menu_options['subtitle'], curses.A_BOLD) def display(self): selected_option = self.prompt_selection() @@ -130,10 +146,6 @@ class CursesMenu(object): self.running = False return {'title' : 'Cancel', 'type' : 'exitmenu'} -menu = {'title' : 'Logout Manager', - 'type' : 'menu', - 'subtitle' : 'Use arrows or number keys'} - option_1 = {'title' : 'Hello World', 'type' : 'command', 'command' : 'echo Hello World!'} @@ -142,13 +154,19 @@ option_1 = {'title' : 'Hello World', config = lmlib.Initialize_config() actions = lmlib.Actions -menu['options'] = [ +menu = { + 'title' : 'Logout Manager', + 'type' : 'menu', + 'subtitle' : 'Use arrows or number keys', + 'zeroindex' : True, + 'options' : [ {'title': 'Lock', 'type': 'action', 'action': 'lock'}, {'title': 'Logout', 'type': 'action', 'action': 'logout'}, - {'title': 'Hibernate', 'type': 'action', 'action': 'hibernate'}, + {'title': 'Hibernate', 'type': 'action', 'action': 'hibernate', 'enabled': config.can_hibernate}, {'title': 'Shutdown', 'type': 'action', 'action': 'shutdown'}, {'title': 'Reboot', 'type': 'action', 'action': 'reboot'} ] +} m = CursesMenu(menu) selected_action = m.display() |