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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
|
#!/usr/bin/env python3
"""Scrollback search: the bar, the matches, and the two ways out.
The machinery is lib-vt's (a GhosttySearch: the scrollback walk, reflow
survival, match tracking); what these tests pin down is ours -- the inline bar
on the pane's status row, the keyboard while it is open, the current match
shown as the selection with the viewport following it, and Enter/Escape
meaning keep-my-place / put-me-back.
"""
import sys
from harness import Session, check, report
# a shell whose output we can generate on demand
SH = ["/bin/sh", "-c", "stty raw -echo; sh"]
# the default theme's match colours (config.c): every match, and the current one
MATCH_BG = "#4d4124"
CUR_BG = "#f2c97a"
def match_cells(snap, bg):
"""Total columns tinted with a given match background."""
return sum(st["w"] for st in snap.styles if st["bg"] == bg)
def fill(s, n=100):
s.raw(f"seq 1 {n}\\n")
s.until_text(str(n))
def bar(s):
"""The search bar's row, or None."""
for row in s.snapshot().text:
if "/ " in row and "\u2588" in row:
return row
return None
def test_bar_opens_and_finds():
with Session(SH, cols=60, rows=12) as s:
s.settle()
fill(s)
p = s.pane()
check("the viewport is at the bottom", "100" in s.snapshot().pane_text(p))
s.send(r"\x01/")
s.settle(50)
row = bar(s)
check(
"the bar opens on the status row",
row is not None,
repr(s.snapshot().screen()[:300]),
)
s.send("42")
s.settle(80)
row = bar(s)
check(
"the query is echoed in the field", row and "/ 42\u2588" in row, repr(row)
)
check("the counter answers k of n", row and "1 of 1" in row, repr(row))
text = s.snapshot().pane_text(p)
check(
"the viewport jumped to the match",
"42" in text and "100" not in text,
repr(text),
)
check(
"and says it is in scrollback",
"scrolled" in s.snapshot().screen(),
repr(s.snapshot().screen()[:300]),
)
# The current match is tinted in the current-match colour, bold.
snap = s.snapshot()
cur = [st for st in snap.styles if st["bg"] == CUR_BG]
check(
"the current match is highlighted apart",
len(cur) == 1 and "bold" in cur[0]["attrs"],
str(cur),
)
pos = snap.find("42")
check(
"and it is on the matched row",
bool(cur) and cur[0]["y"] == pos[1],
f"{cur} vs row {pos[1] if pos else None}",
)
def test_all_matches_are_highlighted():
"""Every occurrence on screen is tinted, not only the one you are on -- and
the current one is a different colour, so 'this one' and 'all of them' read
as two things."""
with Session(SH, cols=60, rows=14) as s:
s.settle()
s.raw("seq 30 39\\n") # a screen full of 3s: many visible matches
s.until_text("39")
s.send(r"\x01/")
s.send("3")
s.settle(80)
snap = s.snapshot()
others = match_cells(snap, MATCH_BG)
cur = match_cells(snap, CUR_BG)
check("more than one match is tinted", others >= 2, f"{others} in {MATCH_BG}")
check("exactly one is the current match", cur == 1, f"{cur} in {CUR_BG}")
# Stepping moves which one is current, not how many are lit.
s.send(r"\e[A")
s.settle(50)
snap = s.snapshot()
check(
"stepping keeps exactly one current",
match_cells(snap, CUR_BG) == 1,
str(match_cells(snap, CUR_BG)),
)
def test_stepping_and_wrap():
with Session(SH, cols=60, rows=12) as s:
s.settle()
fill(s)
s.send(r"\x01/")
s.send("9")
s.settle(80)
row = bar(s)
# 9, 19..89, 90..98 and two in 99: byte matches, newest first.
check("all matches are counted", row and "1 of 20" in row, repr(row))
s.send(r"\e[A") # up: older
s.send(r"\e[A")
s.settle(50)
check("up steps into history", "3 of 20" in bar(s), repr(bar(s)))
s.send(r"\e[B") # down: newer
s.settle(50)
check("down steps back", "2 of 20" in bar(s), repr(bar(s)))
s.send(r"\e[B")
s.send(r"\e[B") # past the newest: wraps to the oldest
s.settle(50)
check("stepping wraps", "20 of 20" in bar(s), repr(bar(s)))
def test_enter_keeps_the_place():
with Session(SH, cols=60, rows=12) as s:
s.settle()
fill(s, 200)
p = s.pane()
s.send(r"\x01/")
s.send("42")
s.settle(80)
check("found it deep in history", "42" in s.snapshot().pane_text(p))
s.send(r"\r")
s.settle(50)
check("enter closes the bar", bar(s) is None, repr(s.snapshot().screen()[:300]))
text = s.snapshot().pane_text(p)
check(
"and the viewport stays on the match",
"42" in text and "200" not in text,
repr(text),
)
check("still marked as scrolled", "scrolled" in s.snapshot().screen())
# Typing snaps back to the live view, like any typing while scrolled.
s.send("x")
s.settle(80)
check(
"typing returns to the present",
"200" in s.snapshot().pane_text(p),
repr(s.snapshot().pane_text(p)),
)
def test_escape_snaps_back():
with Session(SH, cols=60, rows=12) as s:
s.settle()
fill(s, 200)
p = s.pane()
s.send(r"\x01/")
s.send("42")
s.settle(80)
check("found it", "42" in s.snapshot().pane_text(p))
s.send(r"\e")
s.settle(120)
check("escape closes the bar", bar(s) is None)
text = s.snapshot().pane_text(p)
check("and snaps back to the present", "200" in text, repr(text))
check(
"the highlights went with it",
match_cells(s.snapshot(), MATCH_BG) == 0
and match_cells(s.snapshot(), CUR_BG) == 0,
str([st for st in s.snapshot().styles if st["bg"] in (MATCH_BG, CUR_BG)]),
)
check("the session is unharmed", s.api("alive")["alive"])
def test_no_matches_says_so():
with Session(SH, cols=60, rows=12) as s:
s.settle()
fill(s)
s.send(r"\x01/")
s.send("zzz")
s.settle(80)
check("no matches is said in words", "no matches" in bar(s), repr(bar(s)))
# Backspacing to a needle that exists recovers.
s.send(r"\x7f\x7f\x7f")
s.send("42")
s.settle(80)
check("backspace re-searches", "1 of 1" in bar(s), repr(bar(s)))
def test_matching_is_case_insensitive():
with Session(SH, cols=60, rows=12) as s:
s.settle()
s.raw("printf 'Hello World\\n'\\n")
s.until_text("Hello World")
s.send(r"\x01/")
s.send("hello")
s.settle(80)
check("ascii case does not matter", "1 of 1" in bar(s), repr(bar(s)))
def test_new_output_updates_the_count():
with Session(SH, cols=60, rows=12) as s:
s.settle()
fill(s)
s.send(r"\x01/")
s.send("77")
s.settle(80)
check("one match to start", "1 of 1" in bar(s), repr(bar(s)))
# The program keeps printing while the bar is open: the search is fed
# per composed frame, so the count follows without being asked.
s.raw("echo 77\\n")
s.until(lambda snap: "of 2" in (bar_row(snap) or ""))
# The selected match is the one we were on -- now the *older* of the
# two, since matches count from the newest.
check("the count follows new output", "2 of 2" in bar(s), repr(bar(s)))
def bar_row(snap):
for row in snap.text:
if "/ " in row and "\u2588" in row:
return row
return None
def test_the_keyboard_belongs_to_the_bar():
with Session(SH, cols=60, rows=12) as s:
s.settle()
fill(s)
s.send(r"\x01/")
s.send("q") # would quit the session if it fell through as C-a q's kin
s.send("x") # would close the pane
s.settle(50)
check(
"letters land in the query, not the session",
"/ qx\u2588" in bar(s),
repr(bar(s)),
)
check(
"the pane did not see them either",
"qx" not in s.snapshot().pane_text(s.pane()),
)
s.send(r"\e")
s.settle(120)
check("the session is unharmed", s.api("alive")["alive"])
def test_click_away_commits():
with Session(SH, cols=60, rows=12) as s:
s.settle()
fill(s, 200)
p = s.pane()
s.send(r"\x01/")
s.send("42")
s.settle(80)
check("found it", "42" in s.snapshot().pane_text(p))
# Clicking into the pane is leaving the field: it commits (keeps the
# viewport) rather than cancelling -- the click said done, not undo.
s.click(p["content_x"] + 2, p["content_y"] + 2)
s.settle(50)
check("a click away closes the bar", bar(s) is None)
check(
"and keeps the viewport",
"42" in s.snapshot().pane_text(p),
repr(s.snapshot().pane_text(p)),
)
def test_the_wheel_is_not_a_click():
with Session(SH, cols=60, rows=12) as s:
s.settle()
fill(s)
p = s.pane()
s.send(r"\x01/")
s.send("42")
s.settle(80)
# Scrolling the pane you are searching is part of searching it.
s.send(rf"\e[<64;{p['content_x'] + 3};{p['content_y'] + 3}M")
s.settle(50)
check(
"the wheel leaves the bar open",
bar(s) is not None,
repr(s.snapshot().screen()[:300]),
)
def test_help_and_search_share_the_slash():
"""`/` is search and `?` is help -- which needs the decoder to read the
shift out of a legacy `?` byte, since the byte carries no modifier."""
with Session(SH, cols=70, rows=20) as s:
s.settle()
s.send(r"\x01?")
s.settle(80)
check(
"? opens the cheatsheet",
"then:" in s.snapshot().screen(),
repr(s.snapshot().screen()[:200]),
)
s.send(r"\e")
s.settle(120)
s.send(r"\x01/")
s.settle(50)
check(
"/ opens the search bar",
bar(s) is not None,
repr(s.snapshot().screen()[:300]),
)
if __name__ == "__main__":
test_bar_opens_and_finds()
test_all_matches_are_highlighted()
test_stepping_and_wrap()
test_enter_keeps_the_place()
test_escape_snaps_back()
test_no_matches_says_so()
test_matching_is_case_insensitive()
test_new_output_updates_the_count()
test_the_keyboard_belongs_to_the_bar()
test_click_away_commits()
test_the_wheel_is_not_a_click()
test_help_and_search_share_the_slash()
sys.exit(report())
|