commit 5b2021631e1d8506289221a6e8b8b6d7c6f963ea
Author: Damian Johnson <atagar(a)torproject.org>
Date: Fri Mar 25 09:39:57 2016 -0700
Replace multiple Controller methods with header_panel()
For couple of these methods the only panel we ever need to fetch directly is
the header. Replacing quite a bit with that simple helper.
---
nyx/controller.py | 53 ++++++++++++-----------------------------------------
nyx/menu/actions.py | 6 +++---
nyx/popups.py | 5 ++---
3 files changed, 17 insertions(+), 47 deletions(-)
diff --git a/nyx/controller.py b/nyx/controller.py
index 5bef1dd..3f0d32e 100644
--- a/nyx/controller.py
+++ b/nyx/controller.py
@@ -59,13 +59,11 @@ def get_controller():
def show_message(message = None, *attr, **kwargs):
- header_panel = get_controller().get_panel('header')
- return header_panel.show_message(message, *attr, **kwargs)
+ return get_controller().header_panel().show_message(message, *attr, **kwargs)
def input_prompt(msg, initial_value = ''):
- header_panel = get_controller().get_panel('header')
- return header_panel.input_prompt(msg, initial_value)
+ return get_controller().header_panel().input_prompt(msg, initial_value)
class Controller(object):
@@ -79,7 +77,7 @@ class Controller(object):
top to bottom on the page.
"""
- self._sticky_panels = [nyx.panel.header.HeaderPanel()]
+ self._header_panel = nyx.panel.header.HeaderPanel()
self._page_panels, first_page_panels = [], []
@@ -136,7 +134,7 @@ class Controller(object):
if page_number != self._page:
self._page = page_number
self._force_redraw = True
- self.get_panel('header').redraw(True)
+ self.header_panel().redraw(True)
def next_page(self):
"""
@@ -167,34 +165,15 @@ class Controller(object):
if is_pause != self._is_paused:
self._is_paused = is_pause
self._force_redraw = True
- self.get_panel('header').redraw(True)
+ self.header_panel().redraw(True)
for panel_impl in self.get_all_panels():
panel_impl.set_paused(is_pause)
- def get_panel(self, name):
- """
- Provides the panel with the given identifier. This returns None if no such
- panel exists.
-
- Arguments:
- name - name of the panel to be fetched
- """
-
- for panel_impl in self.get_all_panels():
- if panel_impl.get_name() == name:
- return panel_impl
-
- return None
+ def header_panel(self):
+ return self._header_panel
- def get_sticky_panels(self):
- """
- Provides the panels visibile at the top of every page.
- """
-
- return list(self._sticky_panels)
-
- def get_display_panels(self, page_number = None, include_sticky = True):
+ def get_display_panels(self, page_number = None):
"""
Provides all panels belonging to a page and sticky content above it. This
is ordered they way they are presented (top to bottom) on the page.
@@ -202,18 +181,10 @@ class Controller(object):
Arguments:
page_number - page number of the panels to be returned, the current
page if None
- include_sticky - includes sticky panels in the results if true
"""
return_page = self._page if page_number is None else page_number
-
- if self._page_panels:
- if include_sticky:
- return self._sticky_panels + self._page_panels[return_page]
- else:
- return list(self._page_panels[return_page])
- else:
- return self._sticky_panels if include_sticky else []
+ return list(self._page_panels[return_page]) if self._page_panels else []
def get_daemon_panels(self):
"""
@@ -233,7 +204,7 @@ class Controller(object):
Provides all panels in the interface.
"""
- all_panels = list(self._sticky_panels)
+ all_panels = [self._header_panel]
for page in self._page_panels:
all_panels += list(page)
@@ -258,7 +229,7 @@ class Controller(object):
if self._last_drawn + CONFIG['features.refreshRate'] <= current_time:
force = True
- display_panels = self.get_display_panels()
+ display_panels = [self.header_panel()] + self.get_display_panels()
occupied_content = 0
@@ -333,7 +304,7 @@ def start_nyx():
override_key = None # uses this rather than waiting on user input
while not control.quit_signal:
- display_panels = control.get_display_panels()
+ display_panels = [control.header_panel()] + control.get_display_panels()
# sets panel visability
diff --git a/nyx/menu/actions.py b/nyx/menu/actions.py
index 38e8629..d37a9f6 100644
--- a/nyx/menu/actions.py
+++ b/nyx/menu/actions.py
@@ -33,7 +33,7 @@ def make_menu():
control = nyx.controller.get_controller()
- for page_panel in control.get_display_panels(include_sticky = False):
+ for page_panel in control.get_display_panels():
if page_panel.get_name() == 'graph':
base_menu.add(make_graph_menu(page_panel))
elif page_panel.get_name() == 'log':
@@ -62,7 +62,7 @@ def make_actions_menu():
control = nyx.controller.get_controller()
controller = tor_controller()
- header_panel = control.get_panel('header')
+ header_panel = control.header_panel()
actions_menu = nyx.menu.item.Submenu('Actions')
actions_menu.add(nyx.menu.item.MenuItem('Close Menu', None))
actions_menu.add(nyx.menu.item.MenuItem('New Identity', header_panel.send_newnym))
@@ -95,7 +95,7 @@ def make_view_menu():
page_group = nyx.menu.item.SelectionGroup(control.set_page, control.get_page())
for i in range(control.get_page_count()):
- page_panels = control.get_display_panels(page_number = i, include_sticky = False)
+ page_panels = control.get_display_panels(page_number = i)
label = ' / '.join([str_tools._to_camel_case(panel.get_name()) for panel in page_panels])
view_menu.add(nyx.menu.item.SelectionMenuItem(label, page_group, i))
diff --git a/nyx/popups.py b/nyx/popups.py
index 25ff618..d6a14f1 100644
--- a/nyx/popups.py
+++ b/nyx/popups.py
@@ -50,7 +50,7 @@ def popup_window(height = -1, width = -1, top = 0, left = 0, below_static = True
control = nyx.controller.get_controller()
if below_static:
- sticky_height = sum([sticky_panel.get_height() for sticky_panel in control.get_sticky_panels()])
+ sticky_height = control.header_panel().get_height()
else:
sticky_height = 0
@@ -84,7 +84,6 @@ def show_help_popup():
"""
control = nyx.controller.get_controller()
- sticky_height = sum([sticky_panel.get_height() for sticky_panel in control.get_sticky_panels()])
help_options = []
for panel in reversed(control.get_display_panels()):
@@ -122,7 +121,7 @@ def show_help_popup():
subwindow.addstr(2, 7, 'Press any key...')
with nyx.curses.CURSES_LOCK:
- nyx.curses.draw(_render, top = sticky_height, width = 80, height = 9)
+ nyx.curses.draw(_render, top = control.header_panel().get_height(), width = 80, height = 9)
keypress = nyx.curses.key_input()
if keypress.is_selection() or keypress.is_scroll() or keypress.match('left', 'right'):