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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
|
#!/usr/bin/env python3
"""The command palette: every action by name, without knowing its key.
The cheatsheet answers "what is the key for this?" and then needs you to press
it. This answers "just do the thing", which is the question you have when you
cannot remember there was a key at all -- and it is the only way to reach an
action that nothing is bound to.
It shares its box, its keys and its mouse handling with the pane finder (they
are one picker with a subject), so the navigation and editing checks live in
test_finder.py and are not repeated here.
"""
import pathlib
import re
import sys
import tempfile
from harness import Session, check, report
SH = ["/bin/sh", "-c", 'printf "\\033]2;shell\\007"; stty raw -echo; cat']
OPEN = r"\x01p" # the default bind: prefix, then p
def cfg(text):
f = tempfile.NamedTemporaryFile("w", suffix=".kdl", delete=False)
f.write(text)
f.close()
return f.name
def is_open(s):
return any(
h["action"].startswith("run:") or h["action"] == "closepalette"
for h in s.snapshot().hits
)
def counter(s):
import re
for line in s.snapshot().screen().split("\n"):
m = re.search(r"(\d+) of (\d+)", line)
if m:
return int(m.group(1)), int(m.group(2))
return None
def rows(s):
snap = s.snapshot()
return [
snap.text[h["y"]]
for h in sorted(
(h for h in snap.hits if h["action"].startswith("run:")),
key=lambda h: h["y"],
)
]
def test_it_opens_and_lists_commands():
with Session(SH, cols=80, rows=22) as s:
s.settle()
s.send(OPEN)
s.settle(100)
check("the palette opens", is_open(s), repr(s.snapshot().screen()[:200]))
screen = s.snapshot().screen()
check(
"it is titled as commands, not as the finder",
"commands" in screen,
repr(screen[:300]),
)
check(
"it lists actions by their phrase",
"split into columns" in screen,
repr(screen),
)
check(
"with the chord that would have run them",
any("split into columns" in r and "\\" in r for r in rows(s)),
str(rows(s)),
)
check(
"and there are a lot of them",
(counter(s) or (0, 0))[1] > 20,
str(counter(s)),
)
def test_it_lists_actions_that_have_no_key():
"""The palette's reason to exist over the cheatsheet, which can only show
what is bound. Nothing binds scroll-up/scroll-down by default."""
with Session(SH, cols=80, rows=22) as s:
s.settle()
s.send(OPEN)
s.send("a line")
s.settle(100)
listed = [r for r in rows(s) if "up a line" in r]
check("an unbound action is listed", listed != [], str(rows(s)))
if listed:
check(
"with no chord against it, rather than a made-up one",
"C-a" not in listed[0],
repr(listed[0]),
)
def test_typing_filters_it():
with Session(SH, cols=80, rows=22) as s:
s.settle()
s.send(OPEN)
s.settle(80)
everything = counter(s)[1]
s.send("columns")
s.settle(80)
check("the phrase finds it", counter(s) == (1, 1), str(counter(s)))
s.send(r"\x15split-cols") # C-u, then the name a config file uses
s.settle(80)
check(
"so does the name you would write in a config",
counter(s) == (1, 1),
str(counter(s)),
)
s.send(r"\x15scroll")
s.settle(80)
c = counter(s)
check("and so does a group", c and 1 < c[1] < everything, str(c))
s.send(r"\x15zzzz")
s.settle(80)
check(
"a query matching nothing says so",
"no command matches" in s.snapshot().screen(),
repr(s.snapshot().screen()[:400]),
)
def test_enter_runs_the_command():
with Session(SH, cols=80, rows=22) as s:
s.settle()
check("one pane to start with", len(s.panes()) == 1)
s.send(OPEN)
s.send("columns")
s.settle(80)
s.send(r"\r")
s.settle(150)
check("the command ran", len(s.panes()) == 2, str(s.panes()))
check("and the palette closed behind it", not is_open(s))
def test_rename_is_reachable_from_the_palette():
"""Renaming was a double-click and nothing else, which made it the one
verb the palette could not reach -- exactly what the palette exists to
prevent. The editor it opens is the same in-place one the gesture uses,
seeded with the current label."""
with Session(SH, cols=80, rows=22) as s:
s.settle()
s.send(OPEN)
s.send("rename this tab")
s.settle(80)
s.send(r"\r")
s.settle(80)
s.send("deploys")
s.settle(40)
s.send(r"\r")
s.settle(40)
check(
"rename-tab renames the tab in place",
[t["name"] for t in s.tabs()] == ["deploys"],
str(s.tabs()),
)
s.send(OPEN)
s.send("rename this pane")
s.settle(80)
s.send(r"\r")
s.settle(80)
# The editor seeds with the current title (a rename is an edit);
# clear it first so the name is what was typed.
s.send(r"\x15") # C-u: clear the field
s.send("agent")
s.settle(40)
s.send(r"\r")
s.settle(40)
check(
"rename-pane renames the pane in place",
[p["title"] for p in s.panes()] == ["agent"],
str([p["title"] for p in s.panes()]),
)
def test_clicking_a_row_runs_it():
with Session(SH, cols=80, rows=22) as s:
s.settle()
s.send(OPEN)
s.send("columns")
s.settle(80)
row = [h for h in s.snapshot().hits if h["action"].startswith("run:")]
check("the row is in the hit list", len(row) == 1, str(s.snapshot().hits))
if not row:
return
s.click(row[0]["x"] + 2, row[0]["y"])
s.settle(150)
check("clicking it runs it", len(s.panes()) == 2, str(s.panes()))
check("and closes the palette", not is_open(s))
def test_it_closes_the_ways_a_modal_does():
with Session(SH, cols=80, rows=22) as s:
s.settle()
s.send(OPEN)
s.settle(80)
s.send(r"\e")
s.settle(80)
check("escape closes it", not is_open(s))
s.send(OPEN)
s.settle(80)
close = [h for h in s.snapshot().hits if h["action"] == "closepalette"]
check("it has a close button", len(close) == 1, str(s.snapshot().hits))
if close:
s.click(close[0]["x"], close[0]["y"])
s.settle(80)
check("which closes it", not is_open(s))
s.send(OPEN)
s.settle(80)
s.click(1, 20)
s.settle(80)
check("and a click outside dismisses it", not is_open(s))
check("without running anything", len(s.panes()) == 1, str(s.panes()))
def test_it_does_not_offer_typing_the_prefix():
"""`literal-prefix` means "send this key to the pane", which is not a
command you can ask for by name -- picked from a list it would type into
a pane hidden behind that list."""
with Session(SH, cols=80, rows=22) as s:
s.settle()
s.send(OPEN)
s.send("prefix")
s.settle(100)
check(
"the prefix is not on the menu",
"no command matches" in s.snapshot().screen(),
repr(s.snapshot().screen()[:400]),
)
def test_the_bind_is_configurable():
conf = cfg("""
keys {
prefix "ctrl+a"
bind "space" "palette"
}
""")
with Session(SH, cols=80, rows=22, config=conf) as s:
s.settle()
s.send(r"\x01 ") # prefix, then space
s.settle(100)
check("the palette opens on the key it was bound to", is_open(s))
s.send("run a command") # its own row is well down a list of thirty
s.settle(80)
check(
"and says so on its own row",
any("run a command" in r and "space" in r for r in rows(s)),
str([r for r in rows(s) if "run a command" in r]),
)
def test_the_finder_is_still_its_own_thing():
"""They share a box; they must not share a list."""
with Session(SH, cols=80, rows=22) as s:
s.settle()
s.send(r"\x01s")
s.settle(100)
check(
"the finder lists panes, not commands",
not is_open(s) and "split into columns" not in s.snapshot().screen(),
repr(s.snapshot().screen()[:300]),
)
s.send(r"\e")
s.settle(80)
s.send(OPEN)
s.settle(100)
check("and the palette lists commands", is_open(s))
def test_every_label_fits_the_column():
"""The palette's label column truncates at 26 and `config.c` says so beside
the table. A label one word too long does not fail anything -- it just reads
as a typo on screen ("undo this pane's own shade"), which is how this check
came to exist."""
src = pathlib.Path(__file__).resolve().parent.parent / "src" / "config.c"
table = src.read_text()
table = table[table.index("ACTION_HELP[]") :]
table = table[: table.index("\n};")]
rows = re.findall(r'\{ACT_[A-Z_0-9]+, "([a-z]+)", "([^"]+)"\}', table)
long = [(g, l) for g, l in rows if len(l) > 26]
check(
"every action label fits the palette's column",
not long,
str([(l, len(l)) for _, l in long]),
)
check("and the table was actually read", len(rows) > 20, str(len(rows)))
if __name__ == "__main__":
test_every_label_fits_the_column()
test_rename_is_reachable_from_the_palette()
test_it_opens_and_lists_commands()
test_it_lists_actions_that_have_no_key()
test_typing_filters_it()
test_enter_runs_the_command()
test_clicking_a_row_runs_it()
test_it_closes_the_ways_a_modal_does()
test_it_does_not_offer_typing_the_prefix()
test_the_bind_is_configurable()
test_the_finder_is_still_its_own_thing()
sys.exit(report())
|