root/urwid/trunk/fib.py

Revision 97, 3.7 kB (checked in by ian, 1 year ago)

update fib.py and browse.py for ListWalker? changes, use WidgetWrap? in browse.py

  • Property svn:executable set to *
Line 
1 #!/usr/bin/python
2 #
3 # Urwid example fibonacci sequence viewer / unbounded data demo
4 #    Copyright (C) 2004-2007  Ian Ward
5 #
6 #    This library is free software; you can redistribute it and/or
7 #    modify it under the terms of the GNU Lesser General Public
8 #    License as published by the Free Software Foundation; either
9 #    version 2.1 of the License, or (at your option) any later version.
10 #
11 #    This library is distributed in the hope that it will be useful,
12 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
13 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 #    Lesser General Public License for more details.
15 #
16 #    You should have received a copy of the GNU Lesser General Public
17 #    License along with this library; if not, write to the Free Software
18 #    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19 #
20 # Urwid web site: http://excess.org/urwid/
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 # remaining segment not full width wide
117                 if r:
118                         linestarts = range( r, lt, width )
119                         return [
120                                 # right-align the remaining segment on 1st line
121                                 [(width-r,None),(r, 0, r)]
122                                 # fill the rest of the lines
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()
Note: See TracBrowser for help on using the browser.