-
Notifications
You must be signed in to change notification settings - Fork 0
/
issue_removing_list_items.py
107 lines (77 loc) · 2.65 KB
/
issue_removing_list_items.py
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
import hashlib
import rich
from textual import on, work
from textual.app import App, ComposeResult
from textual.binding import Binding
from textual.message import Message
from textual.widgets import Footer, Header, Label, ListItem, ListView
class MyListItem(ListItem):
def __init__(self, my_item_name: str, *args, **kwargs) -> None:
super().__init__(*args, **kwargs)
self.my_item_name = my_item_name
def compose(self) -> ComposeResult:
yield Label(self.my_item_name)
class MyListItemList(ListView):
BINDINGS = [
("r", "reload", "Reload"),
("a", "add", "Add"),
("r", "remove", "Remove"),
]
idx = 0
class MyListItemMessage(Message):
__slots__ = ["my_item_name"]
def __init__(self, name: str) -> None:
super().__init__()
self.my_item_name = name
def __rich_repr__(self) -> rich.repr.Result:
yield "my_item_name", self.my_item_name
class NewMyListItem(MyListItemMessage):
"""New MyListItem found."""
class RemovedMyListItem(MyListItemMessage):
"""MyListItem must be removed."""
def action_add(self) -> None:
self.idx += 1
self._last_name = f"foo_{self.idx}"
self.add_MyListItem(MyListItemList.NewMyListItem(self._last_name))
def action_remove(self) -> None:
self.remove_MyListItem(MyListItemList.RemovedMyListItem(self._last_name))
@on(NewMyListItem)
def add_MyListItem(self, event: NewMyListItem) -> None:
self.append(MyListItem(event.my_item_name, id=event.my_item_name))
@on(RemovedMyListItem)
def remove_MyListItem(self, event: RemovedMyListItem) -> None:
print(f"REMOVING {event}")
widget = self.query_one("#" + event.my_item_name)
print(f"REMOVING WIDGET {widget}")
widget.remove()
@on(ListView.Selected)
@work()
async def fix_MyListItem_name(self, event: ListView.Selected) -> None:
self.post_message(self.RemovedMyListItem(event.item.my_item_name))
async def action_reload(self) -> None:
self.clear()
self.idx = 0
class FixCanonNameApp(App[None]):
BINDINGS = [Binding("q", "quit", "Quit", priority=True)]
CSS = """
MyListItem {
padding: 1 2;
}
PinCodeScreen {
align: center middle;
& Input {
max-width: 40;
}
}
"""
def compose(self) -> ComposeResult:
yield Header()
yield Footer()
yield MyListItemList()
def action_quit(self) -> None:
self.exit()
app = FixCanonNameApp()
def main():
app.run()
if __name__ == "__main__":
main()