root/fib.py

Revision 260:e36b19451b34, 3.6 KB (checked in by Ian Ward, 10 months ago)

use MainLoop in fib example

  • Property exe 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"""
23Urwid example fibonacci sequence viewer / unbounded data demo
24
25Features:
26- custom list walker class for browsing infinite set
27- custom wrap mode "numeric" for wrapping numbers to right and bottom
28"""
29
30import urwid
31       
32class FibonacciWalker(urwid.ListWalker):
33    """ListWalker-compatible class for browsing fibonacci set.
34   
35    positions returned are (value at position-1, value at poistion) tuples.
36    """
37    def __init__(self):
38        self.focus = (0L,1L)
39        self.numeric_layout = NumericLayout()
40   
41    def _get_at_pos(self, pos):
42        """Return a widget and the position passed."""
43        return urwid.Text("%d"%pos[1], layout=self.numeric_layout), pos
44   
45    def get_focus(self): 
46        return self._get_at_pos(self.focus)
47   
48    def set_focus(self, focus):
49        self.focus = focus
50        self._modified()
51   
52    def get_next(self, start_from):
53        a, b = start_from
54        focus = b, a+b
55        return self._get_at_pos(focus)
56   
57    def get_prev(self, start_from):
58        a, b = start_from
59        focus = b-a, a
60        return self._get_at_pos(focus)
61
62def main():
63    palette = [
64        ('body','black','dark cyan', 'standout'),
65        ('foot','light gray', 'black'),
66        ('key','light cyan', 'black', 'underline'),
67        ('title', 'white', 'black',),
68        ]
69       
70    footer_text = [
71        ('title', "Fibonacci Set Viewer"), "    ",
72        ('key', "UP"), ", ", ('key', "DOWN"), ", ",
73        ('key', "PAGE UP"), " and ", ('key', "PAGE DOWN"),
74        " move view  ",
75        ('key', "Q"), " exits",
76        ]
77   
78    def exit_on_q(input):
79        if input in ('q', 'Q'):
80            raise urwid.ExitMainLoop()
81
82    listbox = urwid.ListBox(FibonacciWalker())
83    footer = urwid.AttrMap(urwid.Text(footer_text), 'foot')
84    view = urwid.Frame(urwid.AttrWrap(listbox, 'body'), footer=footer)
85    loop = urwid.MainLoop(view, palette, unhandled_input=exit_on_q)
86    loop.run()
87
88
89class NumericLayout(urwid.TextLayout):
90    """
91    TextLayout class for bottom-right aligned numbers
92    """
93    def layout( self, text, width, align, wrap ):
94        """
95        Return layout structure for right justified numbers.
96        """
97        lt = len(text)
98        r = lt % width # remaining segment not full width wide
99        if r:
100            linestarts = range( r, lt, width )
101            return [
102                # right-align the remaining segment on 1st line
103                [(width-r,None),(r, 0, r)]
104                # fill the rest of the lines
105                ] + [[(width, x, x+width)] for x in linestarts]
106        else:
107            linestarts = range( 0, lt, width )
108            return [[(width, x, x+width)] for x in linestarts]
109
110
111if __name__=="__main__": 
112    main()
Note: See TracBrowser for help on using the browser.