1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
|
#!/usr/bin/env python
#
# Urwid example fibonacci sequence viewer / unbounded data demo
# Copyright (C) 2004-2007 Ian Ward
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
# Urwid web site: https://urwid.org/
"""
Urwid example fibonacci sequence viewer / unbounded data demo
Features:
- custom list walker class for browsing infinite set
- custom wrap mode "numeric" for wrapping numbers to right and bottom
"""
from __future__ import annotations
import typing
import urwid
if typing.TYPE_CHECKING:
from typing_extensions import Literal
class FibonacciWalker(urwid.ListWalker):
"""ListWalker-compatible class for browsing fibonacci set.
positions returned are (value at position-1, value at position) tuples.
"""
def __init__(self) -> None:
self.focus = (0, 1)
self.numeric_layout = NumericLayout()
def _get_at_pos(self, pos: tuple[int, int]) -> tuple[urwid.Text, tuple[int, int]]:
"""Return a widget and the position passed."""
return urwid.Text(f"{pos[1]:d}", layout=self.numeric_layout), pos
def get_focus(self) -> tuple[urwid.Text, tuple[int, int]]:
return self._get_at_pos(self.focus)
def set_focus(self, focus) -> None:
self.focus = focus
self._modified()
def get_next(self, position) -> tuple[urwid.Text, tuple[int, int]]:
a, b = position
focus = b, a + b
return self._get_at_pos(focus)
def get_prev(self, position) -> tuple[urwid.Text, tuple[int, int]]:
a, b = position
focus = b - a, a
return self._get_at_pos(focus)
def main() -> None:
palette = [
("body", "black", "dark cyan", "standout"),
("foot", "light gray", "black"),
("key", "light cyan", "black", "underline"),
(
"title",
"white",
"black",
),
]
footer_text = [
("title", "Fibonacci Set Viewer"),
" ",
("key", "UP"),
", ",
("key", "DOWN"),
", ",
("key", "PAGE UP"),
" and ",
("key", "PAGE DOWN"),
" move view ",
("key", "Q"),
" exits",
]
def exit_on_q(key: str | tuple[str, int, int, int]) -> None:
if key in {"q", "Q"}:
raise urwid.ExitMainLoop()
listbox = urwid.ListBox(FibonacciWalker())
footer = urwid.AttrMap(urwid.Text(footer_text), "foot")
view = urwid.Frame(urwid.AttrMap(listbox, "body"), footer=footer)
loop = urwid.MainLoop(view, palette, unhandled_input=exit_on_q)
loop.run()
class NumericLayout(urwid.TextLayout):
"""
TextLayout class for bottom-right aligned numbers
"""
def layout(
self,
text: str | bytes,
width: int,
align: Literal["left", "center", "right"] | urwid.Align,
wrap: Literal["any", "space", "clip", "ellipsis"] | urwid.WrapMode,
) -> list[list[tuple[int, int, int | bytes] | tuple[int, int | None]]]:
"""
Return layout structure for right justified numbers.
"""
lt = len(text)
r = lt % width # remaining segment not full width wide
if r:
return [
[(width - r, None), (r, 0, r)], # right-align the remaining segment on 1st line
*([(width, x, x + width)] for x in range(r, lt, width)), # fill the rest of the lines
]
return [[(width, x, x + width)] for x in range(0, lt, width)]
if __name__ == "__main__":
main()
|