
commit 42c19c2664938656edc8319cf4bec809ba2662a2 Author: Damian Johnson <atagar@torproject.org> Date: Tue Feb 9 20:59:19 2016 -0800 Finish rewriting torrc panel's draw method Only functional change is that we no longer overwrite the title while scrolling. This panel is small enough now that there's no need to split up the draw method. --- nyx/menu/actions.py | 6 +-- nyx/torrc_panel.py | 129 ++++++++++++++++++++++------------------------------ 2 files changed, 57 insertions(+), 78 deletions(-) diff --git a/nyx/menu/actions.py b/nyx/menu/actions.py index b6a4ae3..1bbb71a 100644 --- a/nyx/menu/actions.py +++ b/nyx/menu/actions.py @@ -291,10 +291,10 @@ def make_torrc_menu(torrc_panel): torrc_menu = nyx.menu.item.Submenu('Torrc') - if torrc_panel.strip_comments: - label, arg = 'Show', True - else: + if torrc_panel._show_comments: label, arg = 'Hide', False + else: + label, arg = 'Show', True torrc_menu.add(nyx.menu.item.MenuItem('%s Comments' % label, functools.partial(torrc_panel.set_comments_visible, arg))) diff --git a/nyx/torrc_panel.py b/nyx/torrc_panel.py index 1958b4e..260be1f 100644 --- a/nyx/torrc_panel.py +++ b/nyx/torrc_panel.py @@ -22,24 +22,16 @@ class TorrcPanel(panel.Panel): self._scroll = 0 self._show_line_numbers = True # shows left aligned line numbers - self._strip_comments = False # drops comments and extra whitespace - - # height of the content when last rendered (the cached value is invalid if - # _last_content_height_args is None or differs from the current dimensions) - + self._show_comments = True # shows comments and extra whitespace self._last_content_height = 0 self._torrc_location = None self._torrc_content = None self._torrc_load_error = None - # listens for tor reload (sighup) events - controller = tor_controller() controller.add_status_listener(self.reset_listener) - - if controller.is_alive(): - self.reset_listener(controller, State.RESET, None) + self.reset_listener(controller, State.RESET, None) def reset_listener(self, controller, event_type, _): """ @@ -51,7 +43,7 @@ class TorrcPanel(panel.Panel): self._torrc_location = expand_path(controller.get_info('config-file')) with open(self._torrc_location) as torrc_file: - self._torrc_content = [ui_tools.get_printable(line.replace('\t', ' ')) for line in torrc_file.readlines()] + self._torrc_content = [ui_tools.get_printable(line.replace('\t', ' ')).rstrip() for line in torrc_file.readlines()] except ControllerError as exc: self._torrc_load_error = msg('panel.torrc.unable_to_find_torrc', error = exc) self._torrc_location = None @@ -64,19 +56,17 @@ class TorrcPanel(panel.Panel): """ Sets if comments and blank lines are shown or stripped. - Arguments: - is_visible - displayed comments and blank lines if true, strips otherwise + :var bool is_visible: shows comments if true, strips otherwise """ - self._strip_comments = not is_visible + self._show_comments = is_visible self.redraw(True) def set_line_number_visible(self, is_visible): """ Sets if line numbers are shown or hidden. - Arguments: - is_visible - displays line numbers if true, hides otherwise + :var bool is_visible: displays line numbers if true, hides otherwise """ self._show_line_numbers = is_visible @@ -93,7 +83,7 @@ class TorrcPanel(panel.Panel): elif key.match('n'): self.set_line_number_visible(not self._show_line_numbers) elif key.match('s'): - self.set_comments_visible(self._strip_comments) + self.set_comments_visible(not self._show_comments) else: return False @@ -105,87 +95,76 @@ class TorrcPanel(panel.Panel): ('down arrow', 'scroll down a line', None), ('page up', 'scroll up a page', None), ('page down', 'scroll down a page', None), - ('s', 'comment stripping', 'on' if self._strip_comments else 'off'), + ('s', 'comment stripping', 'off' if self._show_comments else 'on'), ('n', 'line numbering', 'on' if self._show_line_numbers else 'off'), ('x', 'reset tor (issue sighup)', None), ] def draw(self, width, height): - if self.is_title_visible(): - location = ' (%s)' % self._torrc_location if self._torrc_location else '' - self.addstr(0, 0, 'Tor Configuration File%s:' % location, curses.A_STANDOUT) - if self._torrc_content is None: self.addstr(1, 0, self._torrc_load_error, 'red', curses.A_BOLD) - return - - self._scroll = max(0, min(self._scroll, self._last_content_height - height + 1)) - - if not self._show_line_numbers: - line_number_offset = 0 - elif len(self._torrc_content) == 0: - line_number_offset = 2 + new_content_height = 1 else: - line_number_offset = int(math.log10(len(self._torrc_content))) + 2 + self._scroll = max(0, min(self._scroll, self._last_content_height - height + 1)) - scroll_offset = 0 - - if self._last_content_height > height - 1: - scroll_offset = 3 - self.add_scroll_bar(self._scroll, self._scroll + height - 1, self._last_content_height, 1) - - y = 1 - self._scroll - is_multiline = False # true if we're in the middle of a multiline torrc entry - - for line_number, line in enumerate(self._torrc_content): - if self._strip_comments: - line = line[:line.find('#')].rstrip() if '#' in line else line.rstrip() + if not self._show_line_numbers: + line_number_offset = 0 + elif len(self._torrc_content) == 0: + line_number_offset = 2 + else: + line_number_offset = int(math.log10(len(self._torrc_content))) + 2 - if not line: - continue # skip blank lines + scroll_offset = 0 - option, argument, comment = '', '', '' + if self._last_content_height > height - 1: + scroll_offset = 3 + self.add_scroll_bar(self._scroll, self._scroll + height - 1, self._last_content_height, 1) - if '#' in line: - line, comment = line.split('#', 1) - comment = '#' + comment + y = 1 - self._scroll + is_multiline = False # true if we're in the middle of a multiline torrc entry - # splits the option and argument, preserving any whitespace around them + for line_number, line in enumerate(self._torrc_content): + if not self._show_comments: + line = line[:line.find('#')].rstrip() if '#' in line else line - stripped_line = line.strip() - option_index = stripped_line.find(' ') + if not line: + continue # skip blank lines - if is_multiline: - argument = line # previous line ended with a '\' - elif option_index == -1: - option = line # no argument provided - else: - option_text = stripped_line[:option_index] - option_end = line.find(option_text) + len(option_text) + if '#' in line: + line, comment = line.split('#', 1) + comment = '#' + comment + else: + comment = '' - option = line[:option_end] - argument = line[option_end:] + if is_multiline: + option, argument = '', line # previous line ended with a '\' + elif ' ' not in line.strip(): + option, argument = line, '' # no argument + else: + whitespace = ' ' * (len(line) - len(line.strip())) + option, argument = line.strip().split(' ', 1) + option = whitespace + option + ' ' - # flags following lines as belonging to this multiline entry if it ends - # with a slash + is_multiline = line.endswith('\\') # next line's part of a multi-line entry - is_multiline = stripped_line.endswith('\\') + if self._show_line_numbers: + self.addstr(y, scroll_offset, str(line_number + 1).rjust(line_number_offset - 1), curses.A_BOLD, 'yellow') - # draws the line number + x = line_number_offset + scroll_offset + min_x = line_number_offset + scroll_offset - if self._show_line_numbers and y < height and y >= 1: - self.addstr(y, scroll_offset, str(line_number + 1).rjust(line_number_offset - 1), curses.A_BOLD, 'yellow') + x, y = self.addstr_wrap(y, x, option, width, min_x, curses.A_BOLD, 'green') + x, y = self.addstr_wrap(y, x, argument, width, min_x, curses.A_BOLD, 'cyan') + x, y = self.addstr_wrap(y, x, comment, width, min_x, 'white') - x = line_number_offset + scroll_offset - min_x = line_number_offset + scroll_offset + y += 1 - x, y = self.addstr_wrap(y, x, option, width, min_x, curses.A_BOLD, 'green') - x, y = self.addstr_wrap(y, x, argument, width, min_x, curses.A_BOLD, 'cyan') - x, y = self.addstr_wrap(y, x, comment, width, min_x, 'white') + new_content_height = y + self._scroll - 1 - y += 1 - - new_content_height = y + self._scroll - 1 + if self.is_title_visible(): + self.addstr(0, 0, ' ' * width) # clear line + location = ' (%s)' % self._torrc_location if self._torrc_location else '' + self.addstr(0, 0, 'Tor Configuration File%s:' % location, curses.A_STANDOUT) if self._last_content_height != new_content_height: self._last_content_height = new_content_height