Switch to urwid. Implement listbox with j/k navigation, adding and removing items

master
VikingKong 3 years ago
parent c72f35a27a
commit e9483c2053

141
App.py

@ -1,108 +1,53 @@
from textual.containers import Container, Vertical import urwid
from textual.app import ComposeResult, App
from textual.widgets import Static, Header, DataTable
from textual.widget import Widget
class FeedPane(Widget): class TUI(urwid.Frame):
def __init__(self):
super().__init__()
self.id = "feed-pane"
def add(self, content):
table = self.query_one(DataTable)
table.add_rows([[item] for item in content])
def addColumn(self, title):
table = self.query_one(DataTable)
table.add_column(title, width=60)
def clear(self):
table = self.query_one(DataTable)
table.data = {}
table.rows = {}
table.row_count = 0
table._line_no = 0
table._y_offsets = []
table._new_rows = set()
table._clear_caches()
table._require_update_dimensions = True
table.check_idle()
def currentValue(self):
table = self.query_one(DataTable)
row_num = table.cursor_cell.row
column_num = table.cursor_cell.column
return table.data[row_num][column_num]
def focus(self):
table = self.query_one(DataTable)
table.focus()
def compose(self):
yield DataTable(id="feeds_table")
@classmethod
def create(cls):
class FeedItem(Static): tui = cls()
def __init__(self, content, id): palette = [('header', 'white', 'black'), ('reveal focus', 'black', 'dark cyan', 'standout')]
super().__init__() loop = urwid.MainLoop(
self.content = content tui,
self.id = "feed-" + str(id) palette,
if id == 0: event_loop=urwid.AsyncioEventLoop(),
self.set_styles("color: blue;") )
tui.loop = loop
def on_mount(self):
self.update(self.content)
self.on_event
class GUI(App): return tui
CSS_PATH = "app.css"
def __init__(self): def __init__(self):
super().__init__() self.loop = None
self.items = [urwid.Text("Feed# " + str(i)) for i in range(0, 15)]
def on_key(self, event): walker = urwid.SimpleListWalker([urwid.AttrMap(item, None, 'reveal focus') for item in self.items])
if event.key == "j": self.lb = urwid.ListBox(walker)
pane = self.query_one(DataTable) self.body = self.lb
pane.key_down(event)
elif event.key == "k": super().__init__(self.body)
pane = self.query_one(DataTable)
pane.key_up(event) def run(self):
elif event.key == "a": self.loop.run()
pane = self.query_one(FeedPane) self.executor.shutdown(wait=False)
pane.add(["XXX"])
elif event.key == "r": def keypress(self, size, key):
pane = self.query_one(FeedPane) if key in ("j", "down"):
pane.clear() item_size = len(self.lb.body)
elif event.key == "space": focus_widget, idx = self.lb.get_focus()
pane = self.query_one(FeedPane) if idx < item_size - 1:
pane.add([pane.currentValue()]) idx = idx + 1
self.lb.set_focus(idx)
elif key in ("k", "up"):
focus_widget, idx = self.lb.get_focus()
if idx > 0:
idx = idx - 1
self.lb.set_focus(idx)
elif key == "a":
self.lb.body.append(urwid.AttrMap(urwid.Text("XXX"), None, 'reveal focus'))
else: else:
pass self.lb.keypress(size, key)
del self.lb.body[3]
def compose(self) -> ComposeResult:
yield Header()
yield Container(
FeedPane(),
Vertical(
*[Static("Horizontally"),
Static("Positioned"),
Static("Children"),
Static("Here")],
id="right-pane",
),
id="app-grid",
)
def on_mount(self):
fp = self.query_one(FeedPane)
fp.addColumn("Feeds")
fp.add(["Feed#" + str(number) for number in range(15)])
# fp.add_rows([[FeedItem(f"Feed# {number}", number)] for number in range(15)])
fp.focus()
if __name__ == "__main__": tui = TUI.create()
app = GUI() tui.run()
app.run()

Loading…
Cancel
Save