Repository navigation
Expand file tree
/
Copy pathwidgets.py
More file actions
180 lines (149 loc) · 5.65 KB
/
Copy pathwidgets.py
File metadata and controls
180 lines (149 loc) · 5.65 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
# Copyright (c) 2016-2023 Hugo Osvaldo Barrera
# Copyright (c) 2013-2016 Christian Geier et al.
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
from __future__ import annotations
import re
from typing import Callable
import click
import urwid
class ExtendedEdit(urwid.Edit):
"""A text editing widget supporting some more editing commands"""
HELP = (
("Ctrl-W", "Delete word"),
("Ctrl-U", "Delete until beginning of line"),
("Ctrl-K", "Delete until end of line"),
("Ctrl-A", "Go to beginning of line"),
("Ctrl-E", "Go to end of line"),
("Ctrl-D", "Delete forward letter"),
("Ctrl-O", "Edit in $EDITOR"),
)
def __init__(self, parent: urwid.Widget, *a, **kw) -> None:
self._parent = parent
super().__init__(*a, **kw)
def keypress(
self,
size: tuple[int, int] | tuple[int] | tuple[()],
key: str,
) -> None:
if key == "ctrl w":
self._delete_word()
elif key == "ctrl u":
self._delete_till_beginning_of_line()
elif key == "ctrl k":
self._delete_till_end_of_line()
elif key == "ctrl a":
self._goto_beginning_of_line()
elif key == "ctrl e":
self._goto_end_of_line()
elif key == "ctrl d":
self._delete_forward_letter()
elif key == "ctrl o":
# Allow editing in $EDITOR
self._editor()
# TODO: alt b, alt f
else:
return super().keypress(size, key)
return None
def _delete_forward_letter(self) -> None:
text = self.get_edit_text()
pos = self.edit_pos
text = text[:pos] + text[pos + 1 :]
self.set_edit_text(text)
def _delete_word(self) -> None:
"""delete word before cursor"""
text = self.get_edit_text()
t = text[: self.edit_pos].rstrip()
words = re.findall(r"[\w]+|[^\w\s]", t, re.UNICODE)
f_text = t if t == "" else t[: len(t) - len(words[-1])]
self.set_edit_text(f_text + text[self.edit_pos :])
self.set_edit_pos(len(f_text))
def _delete_till_beginning_of_line(self) -> None:
"""delete till start of line before cursor"""
text = self.get_edit_text()
sol = text.rfind("\n", 0, self.edit_pos) + 1
before_line = text[:sol]
self.set_edit_text(before_line + text[self.edit_pos :])
self.set_edit_pos(sol)
def _delete_till_end_of_line(self) -> None:
"""delete till end of line before cursor"""
text = self.get_edit_text()
eol = text.find("\n", self.edit_pos)
after_eol = "" if eol == -1 else text[eol:]
self.set_edit_text(text[: self.edit_pos] + after_eol)
def _goto_beginning_of_line(self) -> None:
text = self.get_edit_text()
sol = text.rfind("\n", 0, self.edit_pos) + 1
self.set_edit_pos(sol)
def _goto_end_of_line(self) -> None:
text = self.get_edit_text()
eol = text.find("\n", self.edit_pos)
if eol == -1:
eol = len(text)
self.set_edit_pos(eol)
def _editor(self) -> None:
self._parent._loop.screen.clear()
new_text = click.edit(self.get_edit_text())
if new_text is not None:
self.set_edit_text(new_text.strip())
class PrioritySelector(urwid.Button):
HELP = (
("left", "Lower Priority"),
("right", "Higher Priority"),
)
RANGES = (
[0],
[9, 8, 7, 6],
[5],
[1, 2, 3, 4],
)
def __init__(
self,
parent: urwid.Widget,
priority: int | None,
formatter_function: Callable[[int | None], str],
) -> None:
self._parent = parent
self._label = urwid.SelectableIcon("", 0)
urwid.WidgetWrap.__init__(self, self._label)
self._priority = priority
self._formatter = formatter_function
self._set_label()
def _set_label(self) -> None:
self.set_label(self._formatter(self._priority))
def _update_label(self, delta: int = 0) -> None:
for i, r in enumerate(PrioritySelector.RANGES):
if self._priority in r:
self._priority = PrioritySelector.RANGES[
(i + delta) % len(PrioritySelector.RANGES)
][0]
self._set_label()
return
def keypress(self, size: tuple[int], key: str) -> None:
if key in ["right", "enter"]:
self._update_label(1)
return None
if key == "left":
self._update_label(-1)
return None
return super().keypress(size, key)
@property
def priority(self) -> int | None:
return self._priority