Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding the first draft at some ConsoleUI widgets
Signed-off-by: R. Tyler Ballance <tyler@monkeypox.org>
- Loading branch information
Tyler Ballance
committed
Feb 23, 2009
0 parents
commit 71859c1
Showing
2 changed files
with
65 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import curses | ||
import weakref | ||
|
||
import consoleui.errors | ||
|
||
class Keys(object): | ||
ESCAPE = 27 # curses doesn't define a KEY_ESCAPE for some reason | ||
|
||
class Widget(object): | ||
''' | ||
Abstract base class for all ConsoleUI classes | ||
''' | ||
def __init__(self, *args, **kwargs): | ||
super(Widget, self).__init__(*args, **kwargs) | ||
self.children = [] | ||
self.parent = kwargs.get('parent') | ||
|
||
def render(self): | ||
raise consoleui.errors.AbstractError('render() should be defined in the class subclassing Widget') | ||
|
||
class Menu(Widget): | ||
''' | ||
Menu is the base class for rendering a horizontal menu | ||
''' | ||
separator = ' | ' | ||
HOTKEY = curses.A_BOLD | curses.A_UNDERLINE | ||
NORMAL = curses.A_NORMAL | ||
def __init__(self, *args, **kwargs): | ||
super(Menu, self).__init__(*args, **kwargs) | ||
self.elements = kwargs['elements'] | ||
|
||
def render(self): | ||
assert self.parent, ('Parent cannot be None to render a Menu!') | ||
x, y = 1, 3 | ||
for element in self.elements: | ||
under = None | ||
try: | ||
under = element.index('_') | ||
except ValueError: | ||
pass | ||
|
||
if not under == None: | ||
self.parent.addstr(y, x, element[under+1:under+2], self.HOTKEY) | ||
x = x + 1 | ||
self.parent.addstr(y, x, element[under+2:], self.NORMAL) | ||
x = x + len(element[under+2:]) | ||
else: | ||
self.parent.addstr(y, x, element) | ||
x = x + len(element) | ||
|
||
if element != self.elements[-1]: | ||
self.parent.addstr(y, x, self.separator, self.NORMAL) | ||
x = x + len(self.separator) | ||
|
||
class Window(Widget): | ||
pass | ||
|
||
class Submenu(Widget): | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
|
||
class NotImplementedError(Exception): | ||
pass | ||
|
||
class AbstractError(Exception): | ||
pass |