Repository navigation
Expand file tree
/
Copy pathinteractive.py
More file actions
231 lines (196 loc) · 7.05 KB
/
Copy pathinteractive.py
File metadata and controls
231 lines (196 loc) · 7.05 KB
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
from __future__ import annotations
import typing
import urwid
from todoman import widgets
if typing.TYPE_CHECKING:
from collections.abc import Iterable
from todoman.formatters import Formatter
from todoman.model import Todo
from todoman.model import TodoList
_palette = [("error", "light red", "")]
class TodoEditor:
"""
The UI for a single todo entry.
"""
def __init__(
self,
todo: Todo,
lists: Iterable[TodoList],
formatter: Formatter,
) -> None:
"""
:param model.Todo todo: The todo object which will be edited.
"""
self.current_list = todo.list
self.lists = list(lists)
self.formatter = formatter
self._loop = None
self._status = urwid.Text("")
self._init_basic_fields()
self._init_list_selector()
self._init_help_text()
save_btn = urwid.Button("Save", on_press=self._save)
cancel_text = urwid.Text("Hit Ctrl-C to cancel, F1 for help.")
buttons = urwid.Columns([(8, save_btn), cancel_text], dividechars=2)
pile_items = []
for label, field in [
("Summary", self._summary),
("Description", self._description),
("Location", self._location),
("Categories", self._categories),
("Start", self._dtstart),
("Due", self._due),
("Completed", self._completed),
("Priority", self._priority),
]:
label = urwid.Text(label + ":", align="right")
column = urwid.Columns([(13, label), field], dividechars=1)
pile_items.append(("pack", column))
grid = urwid.Pile(pile_items)
spacer = urwid.Divider()
self.left_column = urwid.ListBox(
urwid.SimpleListWalker([grid, spacer, self._status, buttons])
)
right_column = urwid.ListBox(
urwid.SimpleListWalker([urwid.Text("List:\n"), *self.list_selector])
)
self._ui = urwid.Columns([self.left_column, right_column])
def _init_basic_fields(self) -> None:
self._summary = widgets.ExtendedEdit(
parent=self,
edit_text=self.todo.summary,
)
self._description = widgets.ExtendedEdit(
parent=self,
edit_text=self.todo.description,
multiline=True,
)
self._categories = widgets.ExtendedEdit(
parent=self,
edit_text=self.formatter.format_categories(self.todo.categories),
)
self._location = widgets.ExtendedEdit(
parent=self,
edit_text=self.todo.location,
)
self._due = widgets.ExtendedEdit(
parent=self,
edit_text=self.formatter.format_datetime(self.todo.due),
)
self._dtstart = widgets.ExtendedEdit(
parent=self,
edit_text=self.formatter.format_datetime(self.todo.start),
)
self._completed = urwid.CheckBox("", state=self.todo.is_completed)
self._priority = widgets.PrioritySelector(
parent=self,
priority=self.todo.priority,
formatter_function=self.formatter.format_priority,
)
def _init_list_selector(self) -> None:
self.list_selector: list[urwid.RadioButton] = []
for _list in self.lists:
urwid.RadioButton(
self.list_selector,
_list.name,
state=_list == self.current_list,
on_state_change=self._change_current_list,
user_data=_list,
)
def _init_help_text(self) -> None:
self._help_text = urwid.Text(
"\n\n"
"Global:\n"
" F1: Toggle help\n"
" Ctrl-C: Cancel\n"
" Ctrl-S: Save (only works if not a shell shortcut already)\n"
"\n"
"In Textfields:\n"
+ "\n".join(f" {k}: {v}" for k, v in widgets.ExtendedEdit.HELP)
+ "\n\nIn Priority Selector:\n"
+ "\n".join(f" {k}: {v}" for k, v in widgets.PrioritySelector.HELP)
)
def _change_current_list(
self,
radio_button: urwid.RadioButton,
new_state: bool,
new_list: TodoList,
) -> None:
if new_state:
self.current_list = new_list
def _toggle_help(self) -> None:
if self.left_column.body.contents[-1] is self._help_text:
self.left_column.body.contents.pop()
else:
self.left_column.body.contents.append(self._help_text)
assert self._loop, "Loop must be defined while toggling help"
self._loop.draw_screen()
def set_status(self, text: tuple[str, str]) -> None:
self._status.set_text(text)
def edit(self) -> None:
"""Shows the UI for editing a given todo."""
self._loop = urwid.MainLoop(
self._ui,
palette=_palette,
unhandled_input=self._keypress,
handle_mouse=False,
)
assert self._loop, "Loop must remain defined"
try:
self._loop.run()
except KeyboardInterrupt:
self._loop.stop() # Try to leave terminal in usable state
self._loop = None
def _save(self, btn: urwid.Button | None = None) -> None:
try:
self._save_inner()
except Exception as e:
self.set_status(("error", str(e)))
else:
raise urwid.ExitMainLoop
def _save_inner(self) -> None:
self.todo.list = self.current_list
self.todo.summary = self.summary
self.todo.description = self.description
self.todo.location = self.location
self.todo.due = self.formatter.parse_datetime(self.due)
self.todo.start = self.formatter.parse_datetime(self.dtstart)
if not self.todo.is_completed and self._completed.get_state():
self.todo.complete()
elif self.todo.is_completed and not self._completed.get_state():
self.todo.status = "NEEDS-ACTION"
self.todo.completed_at = None
self.todo.categories = [c.strip() for c in self.categories.split(",")]
self.todo.priority = self.priority or 0
# TODO: categories
# TODO: comment
# https://tools.ietf.org/html/rfc5545#section-3.8
# geo (lat, lon)
# RESOURCE: the main room
def _keypress(self, key: str) -> None:
if key.lower() == "f1":
self._toggle_help()
elif key == "ctrl s":
self._save()
@property
def summary(self) -> str:
return self._summary.edit_text
@property
def description(self) -> str:
return self._description.edit_text
@property
def location(self) -> str:
return self._location.edit_text
@property
def due(self) -> str:
return self._due.edit_text
@property
def dtstart(self) -> str:
return self._dtstart.edit_text
@property
def categories(self) -> str:
return self._categories.edit_text
@property
def priority(self) -> int | None:
return self._priority.priority