| 1 |
|
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 22 |
""" |
|---|
| 23 |
Urwid example fibonacci sequence viewer / unbounded data demo |
|---|
| 24 |
|
|---|
| 25 |
Features: |
|---|
| 26 |
- custom list walker class for browsing infinite set |
|---|
| 27 |
- custom wrap mode "numeric" for wrapping numbers to right and bottom |
|---|
| 28 |
""" |
|---|
| 29 |
|
|---|
| 30 |
import urwid |
|---|
| 31 |
import urwid.curses_display |
|---|
| 32 |
|
|---|
| 33 |
class FibonacciWalker(urwid.ListWalker): |
|---|
| 34 |
"""ListWalker-compatible class for browsing fibonacci set. |
|---|
| 35 |
|
|---|
| 36 |
positions returned are (value at position-1, value at poistion) tuples. |
|---|
| 37 |
""" |
|---|
| 38 |
def __init__(self): |
|---|
| 39 |
self.focus = (0L,1L) |
|---|
| 40 |
self.numeric_layout = NumericLayout() |
|---|
| 41 |
|
|---|
| 42 |
def _get_at_pos(self, pos): |
|---|
| 43 |
"""Return a widget and the position passed.""" |
|---|
| 44 |
return urwid.Text("%d"%pos[1], layout=self.numeric_layout), pos |
|---|
| 45 |
|
|---|
| 46 |
def get_focus(self): |
|---|
| 47 |
return self._get_at_pos( self.focus ) |
|---|
| 48 |
|
|---|
| 49 |
def set_focus(self, focus): |
|---|
| 50 |
self.focus = focus |
|---|
| 51 |
self._modified() |
|---|
| 52 |
|
|---|
| 53 |
def get_next(self, start_from): |
|---|
| 54 |
a, b = start_from |
|---|
| 55 |
focus = b, a+b |
|---|
| 56 |
return self._get_at_pos( focus ) |
|---|
| 57 |
|
|---|
| 58 |
def get_prev(self, start_from): |
|---|
| 59 |
a, b = start_from |
|---|
| 60 |
focus = b-a, a |
|---|
| 61 |
return self._get_at_pos( focus ) |
|---|
| 62 |
|
|---|
| 63 |
class FibonacciDisplay(object): |
|---|
| 64 |
palette = [ |
|---|
| 65 |
('body','black','dark cyan', 'standout'), |
|---|
| 66 |
('foot','light gray', 'black'), |
|---|
| 67 |
('key','light cyan', 'black', 'underline'), |
|---|
| 68 |
('title', 'white', 'black',), |
|---|
| 69 |
] |
|---|
| 70 |
|
|---|
| 71 |
footer_text = [ |
|---|
| 72 |
('title', "Fibonacci Set Viewer"), " ", |
|---|
| 73 |
('key', "UP"), ", ", ('key', "DOWN"), ", ", |
|---|
| 74 |
('key', "PAGE UP"), " and ", ('key', "PAGE DOWN"), |
|---|
| 75 |
" move view ", |
|---|
| 76 |
('key', "Q"), " exits", |
|---|
| 77 |
] |
|---|
| 78 |
|
|---|
| 79 |
def __init__(self): |
|---|
| 80 |
self.listbox = urwid.ListBox( FibonacciWalker() ) |
|---|
| 81 |
self.footer = urwid.AttrWrap( urwid.Text( self.footer_text ), |
|---|
| 82 |
'foot') |
|---|
| 83 |
self.view = urwid.Frame( urwid.AttrWrap( self.listbox, 'body'), |
|---|
| 84 |
footer=self.footer ) |
|---|
| 85 |
|
|---|
| 86 |
def main(self): |
|---|
| 87 |
self.ui = urwid.curses_display.Screen() |
|---|
| 88 |
self.ui.register_palette( self.palette ) |
|---|
| 89 |
self.ui.run_wrapper( self.run ) |
|---|
| 90 |
|
|---|
| 91 |
def run(self): |
|---|
| 92 |
size = self.ui.get_cols_rows() |
|---|
| 93 |
while 1: |
|---|
| 94 |
canvas = self.view.render( size, focus=1 ) |
|---|
| 95 |
self.ui.draw_screen( size, canvas ) |
|---|
| 96 |
keys = None |
|---|
| 97 |
while not keys: |
|---|
| 98 |
keys = self.ui.get_input() |
|---|
| 99 |
for k in keys: |
|---|
| 100 |
if k == 'window resize': |
|---|
| 101 |
size = self.ui.get_cols_rows() |
|---|
| 102 |
elif k in ('q','Q'): |
|---|
| 103 |
return |
|---|
| 104 |
self.view.keypress( size, k ) |
|---|
| 105 |
|
|---|
| 106 |
|
|---|
| 107 |
class NumericLayout(urwid.TextLayout): |
|---|
| 108 |
""" |
|---|
| 109 |
TextLayout class for bottom-right aligned numbers |
|---|
| 110 |
""" |
|---|
| 111 |
def layout( self, text, width, align, wrap ): |
|---|
| 112 |
""" |
|---|
| 113 |
Return layout structure for right justified numbers. |
|---|
| 114 |
""" |
|---|
| 115 |
lt = len(text) |
|---|
| 116 |
r = lt % width |
|---|
| 117 |
if r: |
|---|
| 118 |
linestarts = range( r, lt, width ) |
|---|
| 119 |
return [ |
|---|
| 120 |
|
|---|
| 121 |
[(width-r,None),(r, 0, r)] |
|---|
| 122 |
|
|---|
| 123 |
] + [[(width, x, x+width)] for x in linestarts] |
|---|
| 124 |
else: |
|---|
| 125 |
linestarts = range( 0, lt, width ) |
|---|
| 126 |
return [[(width, x, x+width)] for x in linestarts] |
|---|
| 127 |
|
|---|
| 128 |
|
|---|
| 129 |
def main(): |
|---|
| 130 |
FibonacciDisplay().main() |
|---|
| 131 |
|
|---|
| 132 |
|
|---|
| 133 |
|
|---|
| 134 |
if __name__=="__main__": |
|---|
| 135 |
main() |
|---|