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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
|
//! The frame: how the core builds the canonical Surface every shell draws.
//! `render` is the whole order, top to bottom: the page, each pane (its
//! grip, tag and body, and the body layer of its compact rows), the notice
//! chips, the workspace and column tags, the tag layers, drag overlays, the
//! debug box, then the cursor.
const std = @import("std");
const vaxis = @import("vaxis");
const pardes = @import("root.zig");
const layout = @import("layout.zig");
const config = @import("config.zig");
const panes = @import("panes.zig");
const modal = @import("modal.zig");
const tracy = @import("tracy.zig");
const colors = @import("colors.zig");
const edit = @import("edit.zig");
const tagline = @import("tagline.zig");
const body_layer = @import("body_layer.zig");
const Messages = @import("Messages.zig");
const TagState = @import("TagState.zig");
const builtins = @import("builtins.zig");
const Color = @import("surface.zig").Color;
const FontRole = @import("surface.zig").FontRole;
const CellStyle = @import("surface.zig").CellStyle;
const Cell = @import("surface.zig").Cell;
const Surface = @import("surface.zig").Surface;
const Pardes = pardes.Pardes;
const Pane = panes.Pane;
const Text = panes.Text;
const Rect = layout.Rect;
const Region = @import("surface.zig").Region;
const TagKind = @import("Layer.zig").Kind;
const wordBoundsAtCol = tagline.wordBoundsAtCol;
const BOX_H = pardes.BOX_H;
const TAG_TEXT_INSET = pardes.TAG_TEXT_INSET;
const MAX_PANES = pardes.MAX_PANES;
const MAX_COLS = pardes.MAX_COLS;
const MAX_TAG_ROWS = pardes.MAX_TAG_ROWS;
const NOTICE_LAYER_BASE = pardes.NOTICE_LAYER_BASE;
/// What a frontend reads beside the cells (surface.Facts), of the live
/// view as it is now: with each frame (`place`), and by a runner after an
/// input it applied, outside any step, writing nothing of the core's.
pub fn facts(p: *const Pardes, f: *pardes.Facts) void {
viewFacts(p, f);
for (p.panes, 0..) |slot, id| if (slot) |pane| paneFacts(p, f, id, pane);
}
/// The view's facts; its panes' table emptied (`paneFacts` fills it).
fn viewFacts(p: *const Pardes, f: *pardes.Facts) void {
const v = p.view();
// Field by field: the panes' table is written only where a pane is.
f.screen_w = v.screen_w;
f.screen_h = v.screen_h;
f.cell_pixels = v.cell_pixels;
f.row_metrics = v.row_metrics;
f.open = 0;
f.active = @intCast(v.active);
f.back = v.jumps.n > 0 and v.jumps.cur > 0;
f.forward = v.jumps.n > 0 and v.jumps.cur + 1 < v.jumps.n;
f.cursor_blink = p.settings.cursor_blink;
f.jump_trail = p.settings.jump_trail;
f.pet = p.settings.pet;
f.inactive_dim = p.settings.inactive_dim;
f.tagline_percent = p.settings.font.tagline_percent;
}
/// Open pane `id`'s facts.
fn paneFacts(p: *const Pardes, f: *pardes.Facts, id: usize, pane: *const Pane) void {
f.open |= @as(u64, 1) << @intCast(id);
f.panes[id] = .{
.rect = p.view().rects[id],
.tag_rows = pane.tag_rows,
.context_rows = pane.context_rows,
.serial = pane.serial,
.kind = if (pane.addon != null) .addon else if (pane.isTerminal()) .terminal else .text,
.program_wheel = pane.isTerminal() and panes.Term.modes(pane.terminal).wheel,
.normal = pane.body.mode == .normal,
};
}
/// Where every part of the frame goes, decided once before anything is
/// painted: the page; each pane's grip, tag, body, rail (with its thumb)
/// and notice chips; each column's grip and tag; the workspace tag. The
/// painters below and every shell read these rectangles.
pub fn place(p: *Pardes, s: *Surface) void {
const v = p.view();
s.nregions = 0;
s.nrail_ticks = 0;
s.lanes.clearRetainingCapacity();
s.heat.clearRetainingCapacity();
// The facts filled in the walk the frame's places take anyway.
viewFacts(p, &p.facts);
s.facts = &p.facts;
s.addRegion(.{ .kind = .page, .rect = .{ .x = 0, .y = 0, .w = s.cols, .h = s.rows } });
for (&p.panes, 0..) |*slot, id| {
const pane = slot.* orelse continue;
paneFacts(p, &p.facts, id, pane);
Messages.collectNotices(p, pane, v.rects[id]);
const r = v.rects[id];
if (r.w <= config.GUTTER or r.h == 0) continue;
const owner: u8 = @intCast(id);
const active = id == v.active;
const tag_y = p.tagTop(pane, r);
const body_y = p.bodyTop(pane, r);
const reading = pardes.Trail.reading(p, pane);
s.addRegion(.{ .kind = .grip, .owner = owner, .serial = pane.serial, .active = active, .dirty = tagline.dirty(pane), .reading = reading, .rect = .{ .x = r.x, .y = tag_y, .w = config.GUTTER, .h = pane.tag_rows } });
s.addRegion(.{ .kind = .tag, .owner = owner, .serial = pane.serial, .active = active, .reading = reading, .rect = .{ .x = r.x + config.GUTTER, .y = tag_y, .w = r.w - config.GUTTER, .h = pane.tag_rows } });
// A folded pane is its tag rows and nothing else.
if (pane.collapsed) continue;
const body_h = r.h -| pane.tag_rows;
const line: i32 = if (pane.file) |f| @intCast(@min(f.scroll, std.math.maxInt(i32))) else if (pane.isTerminal()) @intCast(pane.termTop()) else 0;
// The text it shows, for a frontend's glide to tell apart: a file's
// path, a generated text's revision with it (rows rewritten as its
// view moved are another text's); none for a terminal or a picture.
const doc: u64 = if (pane.file) |f| std.hash.Wyhash.hash(if (f.output != null) f.revision else 0, f.path) else 0;
const row: u32 = if (pane.file != null) panes.File.top(p.settings.tab, pane).row else 0;
s.addRegion(.{ .kind = .body, .owner = owner, .serial = pane.serial, .active = active, .line = line, .row = row, .doc = doc, .rect = .{ .x = r.x + config.GUTTER, .y = body_y, .w = r.w - config.GUTTER, .h = body_h } });
var rail: Region = .{ .kind = .rail, .owner = owner, .serial = pane.serial, .active = active, .rect = .{ .x = r.x, .y = body_y, .w = config.GUTTER, .h = body_h } };
// A kind's rail is its paint's (drawPane); every other pane's is
// here.
if (pane.addon == null and body_h > 0) {
const sb: struct { total: usize, offset: usize, len: usize } = if (pane.file) |*f| .{
.total = panes.File.nlines(p.gpa, f),
.offset = f.scroll,
.len = pane.rows,
} else blk: {
const grid = panes.Term.grid(pane.terminal);
break :blk .{ .total = grid.total, .offset = pane.termTop(), .len = grid.len };
};
const track_h: usize = body_h;
const total = if (sb.total == 0) 1 else sb.total;
const len = @max(1, (track_h * @max(1, sb.len)) / total);
const pos = (track_h * sb.offset) / total;
rail.thumb_y = @intCast(@min(pos, track_h));
rail.thumb_h = @intCast(@min(len, track_h - rail.thumb_y));
rail.thumb_at = @as(f32, @floatFromInt(sb.offset)) / @as(f32, @floatFromInt(total));
rail.thumb_span = @as(f32, @floatFromInt(@max(1, sb.len))) / @as(f32, @floatFromInt(total));
}
s.addRegion(rail);
if (body_h > 0 and pane.addon == null) pane.places.addTicks(p, pane, s, owner);
// Each notice a chip at the right edge, one to a row in the order
// collectNotices chose, as wide as its own message and no wider, so
// the text beneath the rest of the row keeps reading as the file.
if (r.h <= pane.tag_rows) continue;
const tx = r.x + config.GUTTER;
const tw = r.w - config.GUTTER;
for (0..pane.notices.len) |i| {
const left = pane.notices.left[i];
if (left == std.math.maxInt(u16)) continue;
const chip = tx + tw - left;
s.addRegion(.{ .kind = .notice, .owner = owner, .serial = pane.serial, .active = active, .notice = @intCast(i), .rect = .{ .x = tx + tw - chip, .y = body_y + @as(u16, pane.notices.row[i]), .w = chip, .h = BOX_H } });
}
}
const column_rows = layout.columnBarHeight(p);
// A lone column's tag at the window row's left (UX K'2): no grip, a
// column's edge has nothing to be dragged against.
if (layout.columnInWindowRow(p)) {
const inset = @min(TAG_TEXT_INSET, v.screen_w);
s.addRegion(.{ .kind = .column_tag, .owner = 0, .serial = v.cols[0].serial, .active = layout.findPane(p, v.active) != null, .rect = .{ .x = inset, .y = 0, .w = tagline.windowRow(p).column, .h = p.topBarHeight() } });
}
if (column_rows > 0) for (0..p.view().ncol) |c| {
if (v.col_w[c] == 0) continue;
const active = if (layout.findPane(p, v.active)) |found| found.col == c else false;
const inset = @min(TAG_TEXT_INSET, v.col_w[c]);
s.addRegion(.{ .kind = .column_grip, .owner = @intCast(c), .serial = v.cols[c].serial, .active = active, .rect = .{ .x = v.col_x[c], .y = p.topBarHeight(), .w = @min(config.GUTTER, v.col_w[c]), .h = column_rows } });
s.addRegion(.{ .kind = .column_tag, .owner = @intCast(c), .serial = v.cols[c].serial, .active = active, .rect = .{ .x = v.col_x[c] + inset, .y = p.topBarHeight(), .w = v.col_w[c] - inset, .h = column_rows } });
};
const active_col = if (layout.findPane(p, v.active)) |found| found.col else null;
for (0..p.view().ncol) |c| {
if (v.col_w[c] == 0) continue;
s.addRegion(.{ .kind = .column, .owner = @intCast(c), .serial = v.cols[c].serial, .active = active_col == c, .rect = .{ .x = v.col_x[c], .y = p.topBarHeight(), .w = v.col_w[c], .h = s.rows -| (p.topBarHeight() + layout.noticeRowHeight(p)) } });
}
if (p.settings.workspace_tag)
s.addRegion(.{ .kind = .workspace_tag, .rect = .{ .x = 0, .y = 0, .w = s.cols, .h = p.topBarHeight() } });
const foot = layout.noticeRowHeight(p);
if (foot > 0) s.addRegion(.{ .kind = .notice_row, .rect = .{ .x = 0, .y = s.rows - foot, .w = s.cols, .h = foot } });
}
/// A pane's grip in the grid: `box` focused, else halfway between its ring
/// and `box_dim`, so it shows on its tag without the ring a pixel shell
/// draws (gui.zig draws its button from the region and Chrome instead).
/// A grip cell's colour: the focused pane's button, else its ring mixed
/// with the box's dim fill; where that fill is the tag's own ground
/// (acme's), a reading pane's is its tint, as a window rings a reading
/// pane's tag ground (gui.zig gripFill).
fn gripBg(p: *const Pardes, active: bool, reading: bool) [3]u8 {
const chrome = p.chromeTheme();
if (active) return chrome.box;
const fill = if (reading and std.mem.eql(u8, &chrome.box_dim, &chrome.tag_bg)) chrome.tag_reading_bg else chrome.box_dim;
return colors.mix(boxBorder(p), fill);
}
/// An unfocused grip's ring, and a dirty one's fill (colors.Theme).
fn boxBorder(p: *const Pardes) [3]u8 {
return p.theme().box_border orelse p.chromeTheme().box_dim;
}
pub fn boxDirty(p: *const Pardes) [3]u8 {
const th = p.theme();
return th.box_dirty orelse th.diagnostic_warning orelse th.num;
}
fn inkOn(bg: [3]u8) [3]u8 {
const lum = (@as(u16, bg[0]) * 3 + @as(u16, bg[1]) * 6 + @as(u16, bg[2])) / 10;
return if (lum > 140) .{ 0x00, 0x00, 0x00 } else .{ 0xff, 0xff, 0xff };
}
const Printed = struct { left: u16, dropped: usize };
/// Prints `text` flush with the right edge of the band, and answers the
/// column it started at so a cursor can follow it. Text wider than the
/// band loses its HEAD: the end is the part that says something -- a
/// path's file name, an error's reason -- and `dropped` is how many
/// columns of it went.
fn printRight(tab: u16, s: *Surface, x: u16, row: u16, w: u16, text: []const u8, style: CellStyle) Printed {
const width = panes.File.displayWidth(tab, text);
if (width <= w) {
const left: u16 = @intCast(x + w - width);
_ = s.print(left, row, @intCast(width), text, style);
return .{ .left = left, .dropped = 0 };
}
// Whole glyphs only: a cut inside a wide one would leave the rest a
// column too wide and clip its LAST glyph instead of the first.
var dropped = width - w;
var start = panes.File.rawAtDisplay(tab, text, dropped);
if (panes.File.rawDisplayCol(tab, text, start) < dropped) {
dropped += 1;
start = panes.File.rawAtDisplay(tab, text, dropped);
}
const left: u16 = @intCast(x + w - (width - dropped));
_ = s.print(left, row, @intCast(width - dropped), text[start..], style);
return .{ .left = left, .dropped = dropped };
}
/// A notice chip, painted once as a tag layer at the tagline's pitch, and the
/// grid's chip a copy of its right end, where its text is.
fn joinNotice(p: *Pardes, s: *Surface, arena: std.mem.Allocator, region: Region) !void {
const v = p.view();
const chrome = p.chromeTheme();
const id: usize = region.owner;
const pane = p.panes[id].?;
const i: usize = region.notice;
const kind = pane.notices.kinds[i];
const r = v.rects[id];
const chip = region.rect.w;
const text = Messages.noticeText(pane, i);
const bg = chrome.tag_bg;
const fg = chrome.tag_fg;
// Nothing to say, or too narrow for a layer: a blank chip on the grid.
if (text.len == 0 or r.w <= TAG_TEXT_INSET) {
s.fill(region.rect.x, region.rect.y, chip, 1, .{ .bg = .{ .rgb = bg }, .font_role = .tagline });
if (text.len == 0) return;
const printed = printRight(p.settings.tab, s, region.rect.x, region.rect.y, chip -| 1, text, .{ .fg = .{ .rgb = fg }, .bg = .{ .rgb = bg }, .font_role = .tagline });
if (kind != .prompt or id != v.active or pane.prompt == .del_side or pane.prompt == .repl_choice) return;
const prompt_col = panes.File.displayWidth(p.settings.tab, text[0..@min(@as(usize, @intCast(@max(0, pane.input.cur_col))), text.len)]);
if (prompt_col < printed.dropped) return;
s.cursor = .{ .x = @intCast(printed.left + (prompt_col - printed.dropped)), .y = region.rect.y, .bar = pane.input.mode == .insert };
return;
}
// Right aligned inside the chip, a blank cell short of its edge: a
// prompt's caret sits AFTER its last character the moment it opens, and
// flush right would put it past the pane and off the grid. Wider than
// the chip, the text loses its head: the end is the part that says
// something.
const capacity = layout.tagCapacity(p, chip);
const room = capacity -| 1;
const shown = panes.File.displayWidth(p.settings.tab, text);
var kept = text;
if (shown > room) {
var start = panes.File.rawAtDisplay(p.settings.tab, text, shown - room);
if (panes.File.rawDisplayCol(p.settings.tab, text, start) < shown - room) start = panes.File.rawAtDisplay(p.settings.tab, text, shown - room + 1);
kept = text[start..];
}
const pad = room -| panes.File.displayWidth(p.settings.tab, kept);
const line = try arena.alloc(u8, pad + kept.len);
@memset(line[0..pad], ' ');
@memcpy(line[pad..], kept);
const layer = &s.tag_layers[NOTICE_LAYER_BASE + id * Pane.Notices.max + i];
try renderHeaderLayer(p, layer, .notice, @intCast(id), region.rect, line, 0, bg, fg);
// The caret rides the band, where the prompt's glyphs are.
if (kind == .prompt and id == v.active and pane.prompt != .del_side and pane.prompt != .repl_choice) caret: {
const typed = panes.File.displayWidth(p.settings.tab, text[0..@min(@as(usize, @intCast(@max(0, pane.input.cur_col))), text.len)]);
const dropped = shown - panes.File.displayWidth(p.settings.tab, kept);
if (typed < dropped) break :caret;
layer.cursor = .{ .x = @intCast(pad + typed - dropped), .y = 0, .bar = pane.input.mode == .insert };
}
// The grid shows the chip's last cells of the band, where the text ends
// (all of it where the tagline's pitch is the grid's).
const skip = capacity -| chip;
joinCells(s, region.rect.x, region.rect.y, layer.cells[0..layer.cols], skip, chip, .{ .rgb = fg });
// The band's padding before the text is the chip's fill, which carries
// no ink of its own (the layer prints it as spaces in the band's ink).
// A notice wider than its band has less padding than the band hides.
for (skip..@max(skip, @min(pad, skip + chip))) |col| s.at(region.rect.x + @as(u16, @intCast(col - skip)), region.rect.y).style.fg = .default;
if (layer.cursor) |caret| {
// Counted wide: a band of 65535 glyphs (Layer.capacity's bound, which
// a frontend's row metrics reach) puts its caret past u16, and past
// the grid, where it is not drawn.
const x = @as(u32, region.rect.x) + caret.x;
if (caret.x >= skip and x - skip < s.cols) s.cursor = .{ .x = @intCast(x - skip), .y = region.rect.y, .bar = caret.bar };
}
}
pub fn render(p: *Pardes, arena: std.mem.Allocator) !*Surface {
// By hand, not `taped`: the arena is no input (a replay resets its own, or skips the frame).
const v = p.view();
const taped = pardes.tape.enter(p, .render);
defer pardes.tape.leave(p, taped);
edit.drawing = true;
defer edit.drawing = false;
panes.File.refreshHighlights(p);
const s = &p.surface;
const ncells = @as(usize, v.screen_w) * v.screen_h;
if (s.cells.len != ncells) {
const cells = try p.gpa.alloc(Cell, ncells);
p.gpa.free(s.cells);
s.cells = cells;
}
s.cols = v.screen_w;
s.rows = v.screen_h;
s.tab = p.settings.tab;
s.cursor = null;
s.pointer_shape = .arrow;
// A look-hover preview means the pointed word is a real Look or Exec
// operand: the same plumbing a click would act on. Hosts that model
// the pointer per-text (the macOS I-beam) read this as "target" and
// afford it the arrow instead.
if (p.look_hover_preview != null) s.pointer_shape = .target;
if (v.drag == .move or v.drag == .column_move) s.pointer_shape = .box;
// Over a kind's pane, the pointer its last frame asked for (a PDF's
// hand over a link; 2.2).
if (v.kind_hover) |id| if (p.panes[id]) |pane| if (pane.addon) |k| {
if (k.pointer == pardes.Kind.c.PA_POINTER_LINK) s.pointer_shape = .link;
};
for (&s.body_layers) |*layer| layer.rows = 0;
for (&s.tag_layers) |*layer| layer.rows = 0;
s.placements.clearRetainingCapacity();
@memset(&s.gpu_frames, null);
const chrome = p.chromeTheme();
// The window's rules and grips from the chrome (the themes programme's
// L2e; DESIGN.md R6, R11, G1').
const resolved = p.chrome();
s.chrome = .{
.page = p.theme().bg,
.fg = p.theme().fg,
.border = resolved.rule,
.topbar_rule = config.gui_topbar_pane_border_rgb orelse resolved.rule,
.tag_bg = chrome.tag_bg,
.tagline_bg = chrome.tagline_bg,
.tag_focus_bg = chrome.tag_active_bg,
.tag_reading_bg = chrome.tag_reading_bg,
.tag_fg = chrome.tag_fg,
.scroll_track = chrome.scroll_track,
.scroll_thumb = chrome.scroll_thumb,
.match_tick = chrome.search_bg,
.match_current = p.theme().fg orelse chrome.tag_fg,
.rule_px = resolved.rule_px,
.grip_border = resolved.grip.ring,
.grip_focus_ring = resolved.grip.focus,
.grip_dirty = resolved.grip.dirty,
.grip_mode = resolved.grip.mode,
.grip_ink = resolved.grip.ink,
.tag_rule = resolved.tag_rule,
.rail_px = @intCast(@min(255, @as(u32, resolved.rail_px) * p.settings.grip_width / 100)),
};
place(p, s);
// Separators have their own role; a scrollbar need not frame the page.
const whole = s.regions[0].rect;
s.fill(whole.x, whole.y, whole.w, whole.h, .{ .bg = .{ .rgb = chrome.border } });
for (0..p.view().ncol) |c| if (v.cols[c].n == 0) s.fill(v.col_x[c], p.topBarHeight() + layout.columnBarHeight(p), v.col_w[c], s.rows -| (p.topBarHeight() + layout.columnBarHeight(p) + layout.noticeRowHeight(p)), .{ .bg = .{ .rgb = chrome.empty_col } });
for (&p.panes, 0..) |*slot, id| {
const pane = slot.* orelse continue;
s.body_layers[id].rows = 0;
try renderPane(p, s, arena, pane, v.rects[id], id, id == v.active);
}
// A tag out of rest after every body (UX DESIGN I1, M2): drawn as at
// rest, its head and band hanging under it over the top rows of its
// body, which stay where they were; beside them the grip's column on
// the tag's ground, and a caret under them hidden until rest.
if (TagState.hostOf(p)) |id| if (p.panes[id]) |pane| if (v.rects[id].w > config.GUTTER and v.rects[id].h > 0) {
const r = v.rects[id];
const at = pardes.Band.covered(p, pane, id);
const y = p.tagTop(pane, r) + at.tag;
const rows = @min(at.over, r.h -| at.tag);
if (rows > 0) {
s.fill(r.x, y, config.GUTTER, rows, .{ .bg = .{ .rgb = tagGround(p, pane, id == v.active) }, .font_role = .tagline });
if (s.cursor) |c| if (c.x >= r.x and c.x < r.x + r.w and c.y >= y and c.y < y + rows) {
s.cursor = null;
};
if (s.body_layers[id].cursor) |c| if (pane.file != null and c.y < panes.File.coveredRows(pane)) {
s.body_layers[id].cursor = null;
};
}
try joinPaneTag(p, s, arena, pane, r, id, id == v.active, at.tag);
if (at.over > 0) try joinBand(p, s, arena, pane, r, id, id == v.active, at);
};
// Each notice once, as a tag layer, and the grid's chip a copy of it.
for (s.regionList()) |region| {
if (region.kind == .notice) try joinNotice(p, s, arena, region);
}
try paintHeaders(p, s);
try paintNoticeRow(p, s);
// debug overlay: a small stats box drawn last, top-right ("Debug")
if (p.settings.debug) if (p.panes[v.active]) |at| {
var nlive: usize = 0;
for (p.panes) |slot| {
if (slot != null) nlive += 1;
}
var sb_off: usize = undefined;
var sb_total: usize = undefined;
if (at.file) |*f| {
sb_off = f.scroll;
sb_total = panes.File.nlines(p.gpa, f);
} else if (at.addon != null) {
sb_off = 0;
sb_total = 0;
} else {
sb_off = at.termTop();
sb_total = panes.Term.grid(at.terminal).total;
}
const effective_font = p.settings.font.effective_name.get();
const font_name = if (effective_font.len == 0) "default" else effective_font;
var ov_buf: [384]u8 = undefined;
// The probe's last second (FrameStats); under a virtual clock its
// real-time numbers would make every golden of this box differ.
var frames_buf: [pardes.FrameStats.summary_max]u8 = undefined;
const virtual = std.c.getenv("PARDES_TEST_CLOCK") != null;
const timings = if (virtual) "frame virtual clock\n" else p.frames.summary(&frames_buf);
const text = std.fmt.bufPrint(
&ov_buf,
"pardes resizes={d}\ntheme {s}\nfont {s}\nterms {d} cols {d}\nactive #{d} {d}x{d}\nscroll {d}/{d}\n{s}",
.{ p.resize_count, p.theme().name, font_name[0..@min(font_name.len, 64)], nlive, v.ncol, v.active, at.cols, at.rows, sb_off, sb_total, timings },
) catch "";
// wide enough for the longest theme name compiled in, which is the
// only row here whose width is not ours to choose
const ow: u16 = 38;
const oh: u16 = 13;
const ox: u16 = if (s.cols > ow) s.cols - ow else 0;
if (ox + ow <= s.cols and p.topBarHeight() + oh <= s.rows) {
s.addRegion(.{ .kind = .debug, .rect = .{ .x = ox, .y = p.topBarHeight(), .w = ow, .h = oh } });
// vaxis single_rounded border ring, default style
s.set(ox, p.topBarHeight(), "╭", .{});
s.set(ox + ow - 1, p.topBarHeight(), "╮", .{});
s.set(ox, p.topBarHeight() + oh - 1, "╰", .{});
s.set(ox + ow - 1, p.topBarHeight() + oh - 1, "╯", .{});
var bx = ox + 1;
while (bx < ox + ow - 1) : (bx += 1) {
s.set(bx, p.topBarHeight(), "─", .{});
s.set(bx, p.topBarHeight() + oh - 1, "─", .{});
}
var by = p.topBarHeight() + 1;
while (by < p.topBarHeight() + oh - 1) : (by += 1) {
s.set(ox, by, "│", .{});
s.set(ox + ow - 1, by, "│", .{});
}
s.fill(ox + 1, p.topBarHeight() + 1, ow - 2, oh - 2, .{ .bg = .{ .rgb = chrome.scroll_track } });
var lines = std.mem.splitScalar(u8, text, '\n');
var ly: u16 = p.topBarHeight() + 1;
while (lines.next()) |line| : (ly += 1) {
if (ly >= p.topBarHeight() + oh - 1) break;
_ = s.print(ox + 1, ly, ow - 2, line, .{ .fg = .{ .rgb = chrome.tag_fg } });
}
}
};
return s;
}
fn renderPane(p: *Pardes, s: *Surface, arena: std.mem.Allocator, pane: *Pane, r: Rect, id: usize, active: bool) !void {
const tz = tracy.zone(@src(), "renderPane");
defer tz.end();
if (r.w <= config.GUTTER or r.h == 0) return;
const th = p.theme();
const chrome = p.chromeTheme();
const tx = r.x + config.GUTTER; // text area (tag + body), right of the gutter
const tw = r.w - config.GUTTER;
const tag_y = p.tagTop(pane, r);
const body_y = p.bodyTop(pane, r);
const pane_bg: Color = if (th.bg) |c| .{ .rgb = c } else .default;
// text area resets to terminal-default cells (vaxis clear semantics);
// light themes paint the page over it.
{
// Two full passes over every cell in the pane, every frame.
const tz_clear = tracy.zone(@src(), "paneClear");
defer tz_clear.end();
s.clearRect(tx, r.y, tw, r.h);
if (th.bg) |bg| s.fill(tx, r.y, tw, r.h, .{ .bg = .{ .rgb = bg } });
}
// The grip is acme's button: the focused pane's filled, the others'
// a ring round the tag's ground, a dirty one's the dirty colour inside
// its ring. A pixel shell draws the ring (gui.zig gripDecor); a grid,
// which cannot, marks dirty in the grip's second cell with a bold `*`
// on the dirty colour, a cue that survives any palette.
const band = tagGround(p, pane, active);
const dirty = tagline.dirty(pane);
const box_bg = gripBg(p, active, pardes.Trail.reading(p, pane));
const box_style: CellStyle = .{
.fg = .{ .rgb = inkOn(box_bg) },
.bg = .{ .rgb = box_bg },
// Grips keep physical grid dimensions, independently of tag text.
.font_role = .tagline,
};
// On the first row alone, as acme's button is; the rows under it are
// the tag band's, and still grab the pane (the region spans them all,
// as acme's button column does).
s.fill(r.x, tag_y, config.GUTTER, @min(1, pane.tag_rows), box_style);
if (dirty and pane.tag_rows > 0 and config.GUTTER > 1)
s.set(r.x + config.GUTTER - 1, tag_y, "*", .{ .fg = .{ .rgb = inkOn(boxDirty(p)) }, .bg = .{ .rgb = boxDirty(p) }, .bold = true, .font_role = .tagline });
// The mark (pick-ui SPEC §2): the status of the most important live
// message about the pane, in the box's second cell, on the dirty ground
// when there is unsaved text: a cell of the box's, never the tag's
// text, so it moves nothing.
if (pane.tag_rows > 0 and config.GUTTER > 1) if (p.messages.markOf(pane)) |kind|
s.set(r.x + config.GUTTER - 1, tag_y, kind.glyph(), .{ .fg = .{ .rgb = Messages.ink(chrome, kind) }, .bg = .{ .rgb = if (dirty) boxDirty(p) else box_bg }, .bold = true, .font_role = .tagline });
s.fill(r.x, tag_y + 1, config.GUTTER, pane.tag_rows -| 1, .{
.bg = .{ .rgb = band },
.font_role = .tagline,
});
// In window mode the box holds the pane's letter instead of its mode.
const label: ?u8 = if (p.view().window_pick != null) layout.windowLabel(p, id) else null;
// A tag in a typing state is typed in: its box says so (walk UI SPEC
// §3.2), whatever the body's mode.
const typing = p.view().tag_state.kind != .rest and TagState.hostOf(p) == id;
s.set(r.x, tag_y, if (label) |*letter| @as(*const [1]u8, letter) else if (typing) config.tag_insert else switch (pane.body.mode) {
.normal => config.tag_normal,
.insert => config.tag_insert,
.tty => config.tag_tty,
}, box_style);
// Out of rest its tag is drawn after the bodies, over its own (render).
if (!pardes.Trail.outOfRest(p, pane)) try joinPaneTag(p, s, arena, pane, r, id, active, @min(pane.tag_rows, MAX_TAG_ROWS));
// Folded panes retain their body state, but neither body pixels nor
// its cursor belong outside the one-row tag rectangle.
if (pane.collapsed) return;
const rail = s.region(.rail, id).?;
if (pane.addon != null) {
// Below the notice chips: a picture is drawn after the cells (the
// GUI's image pass, kitty's z-order), so a chip over it would be
// painted out. Text gets the overlay; a kind's pane gives up the rows.
const shown = pane.notices.span();
const body_h = (r.h -| pane.tag_rows) -| shown;
// A picker's preview through a kind (Pick.showKind): its file named
// until its kind has drawn it.
if (!pardes.Kind.draw(p, s, arena, pane, id, tx, body_y + shown, tw, body_h) and body_h > 0 and pardes.Pick.previewing(p, id)) {
const k = pane.addon.?;
if (pardes.Pick.nameLine(arena, k.path, k.nameOf(), k.size, "")) |line| {
_ = s.print(tx, body_y + shown, tw, line, .{ .fg = if (th.fg) |c| .{ .rgb = c } else .default, .bg = pane_bg });
} else |_| {}
}
// The same one column as the real scrollbar below, running past
// the notice bands so the gutter has no notch in it; its thumb
// where this frame's paint said its window is in its whole (a
// PDF's strip), none for a kind that said nothing (an image).
s.fill(rail.rect.x, rail.rect.y, 1, rail.rect.h, .{ .bg = .{ .rgb = chrome.scroll_track } });
s.fill(rail.rect.x + 1, rail.rect.y, 1, rail.rect.h, .{ .bg = pane_bg });
const k = pane.addon.?;
// (a body of no rows has no thumb: a pane squeezed to its tag)
if (k.thumb_span > 0 and rail.rect.h > 0) {
const track: f32 = @floatFromInt(rail.rect.h);
rail.thumb_y = @intFromFloat(@min(track - 1, @floor(k.thumb_at * track)));
rail.thumb_h = @intFromFloat(@min(track - @as(f32, @floatFromInt(rail.thumb_y)), @max(1, k.thumb_span * track)));
rail.thumb_at = k.thumb_at;
rail.thumb_span = k.thumb_span;
s.fill(rail.rect.x, rail.rect.y + rail.thumb_y, 1, rail.thumb_h, .{ .bg = .{ .rgb = chrome.scroll_thumb } });
}
return;
}
try body_layer.joinBody(p, s, arena, pane, r, id, active, rail.*);
// The keyboard is in the head's field: its caret (render), not the
// body's.
if (typing) {
s.body_layers[id].cursor = null;
s.cursor = null;
}
if (pane.layers.heat > 0) @import("jj.zig").railHeat(p, s, pane, id, rail.rect);
// A terminal panned to its cursor in a view shorter than it: an arrow
// in the gutter's first body row says rows are above (views.zig).
if (pane.pan > 0) s.set(r.x, p.bodyTop(pane, r), "↑", .{ .fg = .{ .rgb = chrome.box } });
// A query's places' ticks, each in the track's cell of its row: chrome,
// painted with the rail (a pixel shell draws its own from the same list).
for (s.rail_ticks[0..s.nrail_ticks]) |tick| {
if (tick.owner != id or rail.rect.h == 0) continue;
const row: u16 = @intCast(@min(rail.rect.h - 1, @as(usize, tick.at) * rail.rect.h / panes.Places.tick_scale));
s.fill(rail.rect.x, rail.rect.y + row, 1, 1, .{ .bg = .{ .rgb = if (tick.current) s.chrome.match_current else s.chrome.match_tick } });
}
}
/// The workspace and column tags: each line a tag layer, and the grid's
/// rows copies of those; the column grips, which are the grid's alone.
fn paintHeaders(p: *Pardes, s: *Surface) !void {
const v = p.view();
const chrome = p.chromeTheme();
// global tagbar: full width, top rows — unless a native shell has
// taken its commands over, in which case the rows are not ours to draw
if (s.region(.workspace_tag, Region.none)) |region| {
const top = if (v.header != null and p.headerColumn() == null) v.header_top else 0;
const layer = &s.tag_layers[MAX_PANES + MAX_COLS];
s.chrome.tagline_rows = @intCast(region.rect.y + region.rect.h);
// The window row (UX K'2), on the tagline's ground end to end: a
// lone column's tag at its left (its region), in the row's inks;
// the window's at its right end (tagline.windowRow).
s.fill(region.rect.x, region.rect.y, region.rect.w, region.rect.h, .{ .bg = .{ .rgb = chrome.tagline_bg }, .fg = .{ .rgb = chrome.tagline_fg }, .font_role = .tagline });
if (s.region(.column_tag, 0)) |column| if (column.rect.y < region.rect.y + region.rect.h) {
const column_top = if (v.header != null and p.headerColumn() == 0) v.header_top else 0;
const column_layer = &s.tag_layers[MAX_PANES];
try renderHeaderLayer(p, column_layer, .column, 0, column.rect, tagline.columnTag(p, 0), column_top, chrome.tagline_bg, chrome.tagline_fg);
joinHeaderRows(s, column_layer, chrome.tagline_fg);
};
const words = tagline.windowRow(p).words;
const viewport: Rect = .{ .x = words, .y = region.rect.y, .w = region.rect.w -| words, .h = region.rect.h };
const text = tagline.topbar(p);
try renderHeaderLayer(p, layer, .workspace, 0, viewport, text, top, chrome.tagline_bg, chrome.tagline_fg);
// A server's progress (a message of no pane) at its first row's
// right end, where the row has room for it.
if (layer.rows > 0 and top == 0) if (tagline.sessionProgress(p)) |said| {
var buf: [Messages.LoggedMessage.cap + 8]u8 = undefined;
const shown = std.fmt.bufPrint(&buf, "{s} {s}", .{ Messages.Kind.progress.glyph(), said.words() }) catch "";
const width = panes.File.displayWidth(p.settings.tab, shown);
const used = panes.File.displayWidth(p.settings.tab, modal.lineSlice(text, 0));
const room = layout.tagCapacity(p, viewport.w);
var row: Surface = .{ .cols = layer.cols, .rows = 1, .cells = layer.cells[0..layer.cols], .tab = p.settings.tab };
if (used + 2 + width < room) _ = row.print(@intCast(room - 1 - width), 0, @intCast(width), shown, .{ .fg = .{ .rgb = Messages.ink(chrome, .progress) }, .bg = .{ .rgb = chrome.tagline_bg }, .font_role = .tagline });
};
joinHeaderRows(s, layer, chrome.tagline_fg);
}
const column_rows = layout.columnBarHeight(p);
if (column_rows > 0) for (0..p.view().ncol) |c| {
const region = s.region(.column_tag, c) orelse continue;
const active = if (layout.findPane(p, v.active)) |found| found.col == c else false;
const bg = if (active) chrome.tag_active_bg else chrome.tag_bg;
const fg = if (active) chrome.tag_active_fg else chrome.tag_fg;
if (v.col_x[c] < s.cols) {
// A grip is muted until it is grabbed; then it lights, as a
// pane's box does for the active pane, so the drag shows what it
// holds.
const grip = @min(config.GUTTER, v.col_w[c]);
const inset = @min(TAG_TEXT_INSET, v.col_w[c]);
const grabbed = v.drag == .column_move and layout.columnBySerial(p, v.drag.column_move.serial) == c;
const grip_bg = if (grabbed) chrome.column_box else chrome.column_box_dim;
const ink = inkOn(grip_bg);
s.fill(v.col_x[c], region.rect.y, grip, @min(1, column_rows), .{ .bg = .{ .rgb = grip_bg }, .fg = .{ .rgb = ink }, .font_role = .tagline });
s.fill(v.col_x[c], region.rect.y + 1, grip, column_rows -| 1, .{ .bg = .{ .rgb = bg }, .font_role = .tagline });
s.fill(v.col_x[c] + grip, region.rect.y, inset - grip, column_rows, .{ .bg = .{ .rgb = bg }, .font_role = .tagline });
}
const top = if (v.header != null and p.headerColumn() == c) v.header_top else 0;
const layer = &s.tag_layers[MAX_PANES + c];
try renderHeaderLayer(p, layer, .column, @intCast(c), region.rect, tagline.columnTag(p, c), top, bg, fg);
if (v.col_x[c] < s.cols) joinHeaderRows(s, layer, fg);
};
}
/// The notice row (Messages.noticeRow), the live messages' index at the
/// window's foot: a header layer at the tagline's pitch in the workspace
/// tag's colours, the glyph in its kind's, a message gone dim and the
/// counts at the right end dimmed.
fn paintNoticeRow(p: *Pardes, s: *Surface) !void {
const region = s.region(.notice_row, Region.none) orelse return;
const chrome = p.chromeTheme();
var viewport = region.rect;
const inset = @min(TAG_TEXT_INSET, viewport.w);
s.fill(viewport.x, viewport.y, inset, viewport.h, .{ .bg = .{ .rgb = chrome.tagline_bg }, .font_role = .tagline });
viewport.x += inset;
viewport.w -= inset;
var buf: [Messages.notice_row_cap]u8 = undefined;
const row = Messages.noticeRow(p, layout.tagCapacity(p, viewport.w) -| 1, &buf);
const layer = &s.tag_layers[pardes.NOTICE_ROW_LAYER];
try renderHeaderLayer(p, layer, .notice_row, 0, viewport, row.text, 0, chrome.tagline_bg, chrome.tagline_fg);
if (layer.rows == 0) return;
// The dim ink of the notice row's ground (colors.Roles: 3:1 at least).
const dim: Color = .{ .rgb = chrome.roles.notice.dim };
const ink: Color = .{ .rgb = chrome.tagline_fg };
const cells = layer.cells[0..layer.cols];
const tab = p.settings.tab;
const left = @min(cells.len, panes.File.displayWidth(tab, row.text[0..row.left]));
const right = @min(cells.len, panes.File.displayWidth(tab, row.text[0..row.right]));
// A picker's sentence: dim, its address in the text's ink.
if (row.quiet_left) {
const lo = panes.File.displayWidth(tab, row.text[0..@min(row.word[0], row.left)]);
const hi = panes.File.displayWidth(tab, row.text[0..@min(row.word[1], row.left)]);
for (cells[0..left], 0..) |*cell, x| if ((x < lo or x >= hi) and std.meta.eql(cell.style.fg, ink)) {
cell.style.fg = dim;
};
}
if (row.lead) |lead| {
if (lead.kind == .@"error" or lead.kind == .warn) if (cells.len > 0 and std.meta.eql(cells[0].style.fg, ink)) {
cells[0].style.fg = .{ .rgb = if (lead.kind == .@"error") chrome.diagnostic_error else chrome.diagnostic_warning };
};
if (lead.faded(p.clock.now_ns, p.settings.message_linger_ms)) for (cells[0..left]) |*cell| {
if (std.meta.eql(cell.style.fg, ink)) cell.style.fg = dim;
};
}
for (cells[right..]) |*cell| if (std.meta.eql(cell.style.fg, ink)) {
cell.style.fg = dim;
};
joinHeaderRows(s, layer, chrome.tagline_fg);
}
/// Copy a header's layer into the grid under it, row by row, and its caret.
fn joinHeaderRows(s: *Surface, layer: *const pardes.Layer, fg: [3]u8) void {
const v = layer.viewport;
const w = @min(v.w, s.cols -| v.x);
for (0..layer.rows) |line| joinCells(s, v.x, v.y + @as(u16, @intCast(line)), layer.cells[line * layer.cols ..][0..layer.cols], 0, w, .{ .rgb = fg });
if (layer.cursor) |caret| {
if (caret.x < v.w) s.cursor = .{ .x = v.x + caret.x, .y = v.y + caret.y, .bar = caret.bar };
}
}
/// Copy `w` cells of a layer row, from `from`, into the grid at (x, y) --
/// the one place the grid gets a tag's, a header's or a notice's cells. A
/// wide glyph cut by either end is left as the band's blank, the way the
/// grid's own print never drew half of one: no head without its spacer,
/// no spacer without its head. `ink` is the band's text colour, which a
/// blank cell does not carry.
fn joinCells(s: *Surface, x: u16, y: u16, cells: []const Cell, from: usize, w: u16, ink: Color) void {
// A tagline wider than the body's cells can lay out fewer than `w`.
const n: u16 = @intCast(@min(w, cells.len -| from));
if (n == 0 or y >= s.rows) return;
const row = s.cells[@as(usize, y) * s.cols ..][0..s.cols];
@memcpy(row[x..][0..n], cells[from..][0..n]);
const blank = struct {
fn of(cell: *Cell, band_ink: Color) void {
cell.text[0] = ' ';
cell.len = 1;
if (std.meta.eql(cell.style.fg, band_ink)) cell.style.fg = .default;
}
}.of;
const last = from + n - 1;
if (last + 1 < cells.len and !cells[last + 1].default and cells[last + 1].len == 0) blank(&row[x + n - 1], ink);
if (!cells[from].default and cells[from].len == 0) blank(&row[x], ink);
}
fn tagTintRange(p: *const Pardes, pane: *const Pane, tag: []const u8, prefix_len: usize) ?struct { lo: usize, hi: usize } {
if (pane.isTerminal()) {
// `tag` is the first row; a prefix longer than it ran to a second.
var words = std.mem.tokenizeAny(u8, tag[@min(prefix_len, tag.len)..], " \t\r\n");
while (words.next()) |word| {
if (!std.mem.eql(u8, word, "Term") and !std.mem.startsWith(u8, word, "Term+")) continue;
const lo = @intFromPtr(word.ptr) - @intFromPtr(tag.ptr);
return .{ .lo = lo, .hi = lo + word.len };
}
return null;
}
var start: usize = tagline.nameAt(p, pane);
const name = if (tagline.named(p, pane)) |file|
if (pane.prompt == .name) pane.input.own orelse file.path else file.path
else if (pane.addon) |k| blk: {
// A kind's tag may put its words before the path (an image's
// renderer controls).
if (k.path.len == 0) return null;
start = std.mem.lastIndexOf(u8, tag[0..@min(prefix_len, tag.len)], k.path) orelse return null;
break :blk k.path;
} else return null;
const basename = if (std.mem.lastIndexOfScalar(u8, name, '/')) |slash| slash + 1 else 0;
if (basename == name.len) return null;
// A walked file's path cut from its left: a `…` for the bytes it drops
// (tagline.folded), never its basename's.
const cut = if (pane.file != null) tagline.folded(p, pane).cut else 0;
const dots: usize = if (cut > 0) "…".len else 0;
return .{ .lo = start + dots + basename - cut, .hi = start + dots + name.len - cut };
}
/// A header or a notice band as a tag layer: `viewport.h` rows, line `top`
/// of `text` and on, laid out at the tagline's pitch -- as wide as its
/// widest line and never narrower than its viewport.
fn renderHeaderLayer(p: *Pardes, layer: *pardes.Layer, kind: TagKind, id: u16, viewport: Rect, text: []const u8, top: usize, bg: [3]u8, fg: [3]u8) !void {
const v = p.view();
if (viewport.w == 0 or viewport.h == 0) {
// No layer, and nothing of an earlier frame's left in it.
layer.rows = 0;
layer.viewport = viewport;
layer.cursor = null;
return;
}
var widest: usize = 0;
for (0..viewport.h) |line| widest = @max(widest, panes.File.displayWidth(p.settings.tab, modal.lineSlice(text, line + top)));
const columns: u16 = @intCast(@min(std.math.maxInt(u16), @max(layout.tagCapacity(p, viewport.w), widest + 1)));
const count = @as(usize, columns) * viewport.h;
if (layer.cells.len != count) layer.cells = try p.gpa.realloc(layer.cells, count);
layer.kind = kind;
layer.id = id;
layer.serial = if (kind == .column) v.cols[id].serial else 0;
layer.viewport = viewport;
layer.cols = columns;
layer.rows = viewport.h;
layer.context_rows = 0;
layer.bg = bg;
layer.cursor = null;
// A notice band carries a PANE id, not a column index, so it must not
// answer to the column being edited -- that would scroll and select
// its text with the column tag's offsets and give it a second caret.
const editing: ?*Text = if (v.header != null and switch (kind) {
.workspace => p.headerColumn() == null,
.column => p.headerColumn() == id,
.pane, .notice, .body, .band, .notice_row => false,
}) tagline.focusedHeader(p) else null;
const scroll = if (editing != null) v.header_scroll else 0;
for (0..viewport.h) |index| {
const line: u16 = @intCast(index);
const shown = modal.lineSlice(text, index + top);
var surface: Surface = .{ .cols = columns, .rows = 1, .cells = layer.cells[index * columns ..][0..columns], .tab = p.settings.tab };
surface.fill(0, 0, columns, 1, .{ .bg = .{ .rgb = bg }, .font_role = .tagline });
// The scroll is the caret's line's: a shorter line shows nothing.
_ = surface.print(0, 0, columns, shown[@min(shown.len, panes.File.rawAtDisplay(p.settings.tab, shown, scroll))..], .{ .fg = .{ .rgb = fg }, .bg = .{ .rgb = bg }, .font_role = .tagline });
// The word under the pointer, a Look or Exec target: on a header even
// while it is edited, on a notice, and on the grid's copy of either
// (joined from this); not on a column a carried column's pointer is
// aiming at a place over; nor under a held middle or right sweep,
// which paints what it sweeps instead.
const hovered = v.pointer_inside and !(kind == .column and v.drag == .column_move) and v.header_sweep == null;
if (hovered) {
const tag_hit = v.pointer_tag_hit;
const col: ?usize = if (tag_hit) |pointer|
(if (pointer.kind == kind and pointer.id == id and pointer.line == line) pointer.col else null)
else if (v.hover_row == viewport.y + line and v.hover_col >= viewport.x and v.hover_col - viewport.x < viewport.w)
v.hover_col - viewport.x
else
null;
if (col) |hover| if (wordBoundsAtCol(shown, panes.File.rawAtDisplay(p.settings.tab, shown, hover + scroll))) |bounds| {
var cell = panes.File.rawDisplayCol(p.settings.tab, shown, bounds.lo) -| scroll;
const end = panes.File.rawDisplayCol(p.settings.tab, shown, bounds.hi) -| scroll;
while (cell < end and cell < columns) : (cell += 1) {
surface.at(@intCast(cell), 0).style.bg = .{ .rgb = p.theme().tag_sel_bg orelse p.theme().sel_bg };
surface.at(@intCast(cell), 0).style.fg = .{ .rgb = p.theme().sel_fg };
}
};
}
if (editing) |t| {
if (tagline.headerSelection(t, try edit.flatSurface(p.editor(), t), line + v.header_top)) |span| {
var col = panes.File.rawDisplayCol(p.settings.tab, shown, span.lo) -| scroll;
const end = panes.File.rawDisplayCol(p.settings.tab, shown, span.hi) -| scroll;
while (col < end and col < columns) : (col += 1) {
surface.at(@intCast(col), 0).style.bg = .{ .rgb = p.theme().tag_sel_bg orelse p.theme().sel_bg };
surface.at(@intCast(col), 0).style.fg = .{ .rgb = p.theme().sel_fg };
}
}
const caret = panes.File.rawDisplayCol(p.settings.tab, shown, @min(@as(usize, @intCast(@max(0, t.cur_col))), shown.len)) -| scroll;
const asking = if (p.panes[v.active]) |active| active.prompt != .none else false;
if (!asking and t.cur_row == line + v.header_top and caret < columns) layer.cursor = .{ .x = @intCast(caret), .y = line, .bar = t.mode == .insert };
}
// A middle or right sweep held over this header, tinted by its
// button as a pane tag's is.
if (v.header_sweep) |sweep| if (sweep.column(p)) |column| {
const mine = switch (kind) {
.workspace => column == null,
.column => column == id,
.pane, .notice, .body, .band, .notice_row => false,
};
const swept = sweep.span(text.len);
const start = modal.lineStartOffset(text, index + top);
const end = start + shown.len;
if (mine and swept.lo < swept.hi and swept.hi > start and swept.lo < end) {
const button = @backingInt(sweep.button);
var col = panes.File.rawDisplayCol(p.settings.tab, shown, @max(swept.lo, start) - start) -| scroll;
const stop = panes.File.rawDisplayCol(p.settings.tab, shown, @min(swept.hi, end) - start) -| scroll;
while (col < stop and col < columns) : (col += 1) {
surface.at(@intCast(col), 0).style.bg = .{ .rgb = p.chrome().sweeps[button].ground };
surface.at(@intCast(col), 0).style.fg = .{ .rgb = p.chrome().sweeps[button].text };
}
}
} else |_| {};
}
}
/// A pane's tag's `rows` text rows (MAX_TAG_ROWS at most; out of rest its
/// rows at rest, Band.covered), painted once into their layer, a row to a
/// line, and copied into the grid; the grid's caret where the layer has it.
fn joinPaneTag(p: *Pardes, s: *Surface, arena: std.mem.Allocator, pane: *Pane, r: Rect, id: usize, active: bool, rows: u16) !void {
const v = p.view();
const capacity = layout.tagCapacity(p, r.w -| TAG_TEXT_INSET);
// Too narrow for a layer: only the grid's gap cell.
if (r.w <= TAG_TEXT_INSET) return paintPaneTag(p, s, arena, pane, r, id, active, rows);
const chrome = p.chromeTheme();
const region = s.region(.tag, id).?;
const tag = (try tagline.shownTag(p, arena, pane)).text;
// As wide as the widest row it shows, and never narrower than its
// viewport.
var widest: usize = 0;
var split = std.mem.splitScalar(u8, tag, '\n');
for (0..pane.tag_top) |_| _ = split.next();
for (0..rows) |_| widest = @max(widest, panes.File.displayWidth(p.settings.tab, split.next() orelse ""));
const columns: u16 = @intCast(@min(std.math.maxInt(u16) - TAG_TEXT_INSET, @max(capacity, widest + 1)));
const pitch = @as(usize, columns) + TAG_TEXT_INSET;
const storage = try arena.alloc(Cell, pitch * rows);
@memset(storage, .{});
var temporary: Surface = .{ .cols = columns + TAG_TEXT_INSET, .rows = rows, .cells = storage, .tab = p.settings.tab };
try paintPaneTag(p, &temporary, arena, pane, .{ .x = 0, .y = 0, .w = columns + TAG_TEXT_INSET, .h = rows }, id, active, rows);
// Window mode (layout.windowKey): the outlined pane's tag in the
// selection's colours.
if (v.window_pick == id) {
for (0..rows) |row| temporary.tint(0, @intCast(row), temporary.cols, p.theme().sel_bg, p.theme().sel_fg);
}
const tag_fg: Color = .{ .rgb = if (active) chrome.tag_active_fg else chrome.tag_fg };
// One layer, a row to each line the tag shows.
const layer = &s.tag_layers[id];
const count = @as(usize, columns) * rows;
if (layer.cells.len != count) layer.cells = try p.gpa.realloc(layer.cells, count);
layer.kind = .pane;
layer.id = @intCast(id);
layer.serial = pane.serial;
layer.viewport = .{ .x = region.rect.x, .y = region.rect.y, .w = region.rect.w, .h = rows };
layer.cols = columns;
layer.rows = rows;
layer.context_rows = 0;
layer.bg = if (v.window_pick == id) p.theme().sel_bg else tagGround(p, pane, active);
layer.cursor = if (v.header != null) null else if (temporary.cursor) |cursor|
.{ .x = cursor.x - TAG_TEXT_INSET, .y = cursor.y, .bar = cursor.bar }
else
null;
for (0..rows) |line| {
@memcpy(layer.cells[line * columns ..][0..columns], storage[line * pitch + TAG_TEXT_INSET ..][0..columns]);
// The grid's row: the gap cell and the text, as far as the pane.
joinCells(s, region.rect.x, region.rect.y + @as(u16, @intCast(line)), storage[line * pitch ..][0..pitch], config.GUTTER, region.rect.w, tag_fg);
}
// Past a full row's end the layer's caret is at the tag's edge, as
// acme's tick is; the grid has no cell there, so it takes the last one.
if (temporary.cursor) |cursor| {
s.cursor = .{ .x = r.x + @min(cursor.x, r.w - 1), .y = region.rect.y + cursor.y, .bar = cursor.bar };
}
}
/// The head and the band under pane `id`'s tag (UX DESIGN M2): the head's
/// `drawn.head` rows (TagState.head: its keys, the level or the field and
/// its caret, the count, the hints), then the band's `drawn.band`
/// (Band.zig), painted once into their layer at the tagline's pitch, a row
/// to a line, and copied into the grid: the spines and steps in the tag's
/// tint, the keys in the key ink, the cursor's row tinted as a selection.
fn joinBand(p: *Pardes, s: *Surface, arena: std.mem.Allocator, pane: *Pane, r: Rect, id: usize, active: bool, drawn: pardes.Band.Covered) !void {
const layer = &s.tag_layers[pardes.BAND_LAYER];
const rows = drawn.over;
if (r.w <= TAG_TEXT_INSET) return;
const head = (try TagState.head(p, arena, pane)) orelse return;
const band = try pardes.Band.of(p, arena, pane, id);
const chrome = p.chromeTheme();
const tab = p.settings.tab;
const columns = @max(layout.tagCapacity(p, r.w -| TAG_TEXT_INSET), 1);
// The walk UI's inks on this tag's ground (colors.Roles, UX DESIGN.md
// D4), and on its cursor row's selection.
const ground = groundOf(p, pane, active);
const inks = chrome.roles.on(ground);
const selected = chrome.roles.selectedOn(ground);
// A symbol's kind mark (colors.Chrome.Inks.kind): held on this tag's
// ground and on its cursor row's selection.
const kind = p.chrome().on(if (active) .focus else .plain).kind;
const bg = inks.ground;
const fg = inks.text;
const storage = try arena.alloc(Cell, @as(usize, columns) * rows);
@memset(storage, .{});
var t: Surface = .{ .cols = columns, .rows = rows, .cells = storage, .tab = tab };
t.fill(0, 0, columns, rows, .{ .bg = .{ .rgb = bg }, .font_role = .tagline });
// The head's rows, as it laid itself out at the tag's width (UX D2, D3).
const shown = try tagline.wrapTag(tab, arena, head.text, columns);
var lines: [MAX_TAG_ROWS][]const u8 = @splat("");
var split = std.mem.splitScalar(u8, shown.text, '\n');
for (lines[0..drawn.head], 0..) |*line, y| {
line.* = split.next() orelse break;
_ = t.print(0, @intCast(y), columns, line.*, .{ .fg = .{ .rgb = fg }, .bg = .{ .rgb = bg }, .font_role = .tagline });
}
// Its keys in the key ink, its dimmed, underlined and struck words
// (TagState.Span), over whichever rows they wrapped onto.
for (head.spans) |span| {
const a = modal.positionAt(shown.text, shown.fromReal(span.lo));
const b = modal.positionAt(shown.text, shown.fromReal(span.hi));
var row = a.row;
while (row <= b.row and row < drawn.head) : (row += 1) {
const line = lines[row];
var col = panes.File.rawDisplayCol(tab, line, @min(if (row == a.row) a.col else 0, line.len));
const end = panes.File.rawDisplayCol(tab, line, @min(if (row == b.row) b.col else line.len, line.len));
while (col < end and col < columns) : (col += 1) {
const style = &t.at(@intCast(col), @intCast(row)).style;
switch (span.style) {
.key => {
style.fg = .{ .rgb = chrome.roles.key };
style.bold = true;
},
.dim => style.fg = .{ .rgb = inks.dim },
.under => style.ul = .single,
.strike => style.strikethrough = true,
.err => style.fg = .{ .rgb = chrome.roles.err },
.rule => style.fg = .{ .rgb = inks.rule },
}
}
}
}
// The band's rows under the head's.
if (band) |b| for (0..drawn.band) |y| {
const row: u16 = @intCast(drawn.head + y);
const ln = try b.line(arena, tab, @intCast(y));
_ = t.print(0, row, columns, ln.text, .{ .fg = .{ .rgb = fg }, .bg = .{ .rgb = bg }, .font_role = .tagline });
// The spines and the rules between steps in the rule ink; a row's
// `▶` (the cursor) and `•` (marked) in the key ink.
var col: u16 = 0;
var at: usize = 0;
while (at < ln.text.len and col < columns) : (col += 1) {
const next = modal.nextGrapheme(ln.text, at);
const glyph = ln.text[at..next];
if (std.mem.eql(u8, glyph, "│")) t.at(col, row).style.fg = .{ .rgb = inks.rule };
if (std.mem.eql(u8, glyph, "▶") or std.mem.eql(u8, glyph, "•")) t.at(col, row).style.fg = .{ .rgb = chrome.roles.key };
at = next;
}
for (ln.parts) |part| {
const end = @min(columns, part.col + part.width);
const on = if (part.row.on) selected else inks;
// The cursor's row across its step's width, its lead too (UX
// D5), the lead's `▶` keeping the key ink (D4).
if (part.row.on and part.col < end) {
const lead = part.col -| 2;
t.tint(lead, row, part.col - lead, selected.ground, null);
t.tint(part.col, row, end - part.col, selected.ground, selected.text);
}
var c = part.col;
var i: usize = 0;
while (i < part.row.text.len and c < end) : (c += 1) {
const next = modal.nextGrapheme(part.row.text, i);
const style = &t.at(c, row).style;
if (i < part.row.key) {
style.fg = .{ .rgb = chrome.roles.key };
style.bold = true;
}
// A chord (Help's `SPC h t`): keys, at the text's weight.
if (i < part.row.chord) style.fg = .{ .rgb = chrome.roles.key };
if (i < part.row.pre) style.fg = .{ .rgb = if (active) chrome.tag_active_name_fg else chrome.tag_name_fg };
if (part.row.dim or (i >= part.row.pre and i < part.row.pre + part.row.faint) or (i >= part.row.quiet[0] and i < part.row.quiet[1])) style.fg = .{ .rgb = on.dim };
if (i < part.row.kind) style.fg = .{ .rgb = kind };
for (part.row.keys) |k| if (k == i) {
style.fg = .{ .rgb = chrome.roles.key };
style.bold = true;
};
if (part.row.under) style.ul = .single;
// A matched letter: the match ink, bold, on the row's own
// ground (never a ground of its own: on the cursor row's
// selection one vanished).
for (part.row.marks) |m| if (m == i) {
style.fg = .{ .rgb = chrome.roles.match };
style.bold = true;
};
i = next;
}
// Its meta, right-aligned in the part (Band.line), dim.
if (pardes.Band.metaShown(tab, part.row, part.width) and part.col + part.width <= columns) {
const meta: u16 = @intCast(panes.File.displayWidth(tab, part.row.meta));
for (part.col + part.width - meta..part.col + part.width) |x| t.at(@intCast(x), row).style.fg = .{ .rgb = on.dim };
}
}
};
const count = @as(usize, columns) * rows;
if (layer.cells.len != count) layer.cells = try p.gpa.realloc(layer.cells, count);
@memcpy(layer.cells, storage);
// Under the tag's rows, over the body's top ones (Band.covered).
const y0 = p.tagTop(pane, r) + drawn.tag;
layer.kind = .band;
layer.id = @intCast(id);
layer.serial = pane.serial;
layer.viewport = .{ .x = r.x + TAG_TEXT_INSET, .y = y0, .w = r.w - TAG_TEXT_INSET, .h = rows };
layer.cols = columns;
layer.rows = rows;
layer.context_rows = 0;
layer.bg = bg;
layer.cursor = null;
for (0..rows) |y| joinCells(s, r.x + TAG_TEXT_INSET, y0 + @as(u16, @intCast(y)), storage[y * columns ..][0..columns], 0, r.w - TAG_TEXT_INSET, .{ .rgb = fg });
// A head typed in: its caret a bar at the field's, the keyboard's
// where the pane has it.
if (head.caret) |c| if (active) {
const at = modal.positionAt(shown.text, shown.fromReal(c));
if (at.row < drawn.head) {
const col = panes.File.rawDisplayCol(tab, lines[at.row], @min(at.col, lines[at.row].len));
if (col < columns) {
layer.cursor = .{ .x = @intCast(col), .y = @intCast(at.row), .bar = true };
if (col < r.w - TAG_TEXT_INSET) s.cursor = .{ .x = r.x + TAG_TEXT_INSET + @as(u16, @intCast(col)), .y = y0 + @as(u16, @intCast(at.row)), .bar = true };
}
}
};
}
/// Bytes `lo` to `hi` of a tag's text in `ink`, over whichever of its
/// shown rows (`lines`, from shown row `top`, drawn at `x`, `y`, `w`
/// wide) they wrapped onto.
fn inkTag(p: *Pardes, s: *Surface, shown: tagline.Shown, lines: []const []const u8, top: usize, x: u16, y: u16, w: u16, lo: usize, hi: usize, ink: [3]u8, under: bool) void {
const a = modal.positionAt(shown.text, shown.fromReal(lo));
const b = modal.positionAt(shown.text, shown.fromReal(hi));
var row = @max(a.row, top);
while (row <= b.row and row < top + lines.len) : (row += 1) {
const line = lines[row - top];
const from = if (row == a.row) a.col else 0;
const to = if (row == b.row) b.col else line.len;
var col = panes.File.rawDisplayCol(p.settings.tab, line, @min(from, line.len));
const end = panes.File.rawDisplayCol(p.settings.tab, line, @min(to, line.len));
const row_y = y + @as(u16, @intCast(row - top));
while (col < end and col < w) : (col += 1) {
const style = &s.at(x + @as(u16, @intCast(col)), row_y).style;
style.fg = .{ .rgb = ink };
if (under) style.ul = .single;
}
}
}
/// A pane's tag's ground: a reading pane's tint (its focus is its grip's),
/// else the focused tag's or the tag's.
fn tagGround(p: *Pardes, pane: *const Pane, active: bool) [3]u8 {
return p.chromeTheme().roles.on(groundOf(p, pane, active)).ground;
}
/// Which of a tag's grounds pane `pane`'s is: its walk UI inks
/// (colors.Roles) are that ground's.
fn groundOf(p: *Pardes, pane: *const Pane, active: bool) colors.TagGround {
if (pardes.Trail.reading(p, pane)) return if (active) .reading_focused else .reading;
return if (active) .focused else .plain;
}
fn paintPaneTag(p: *Pardes, s: *Surface, arena: std.mem.Allocator, pane: *Pane, r: Rect, id: usize, active: bool, rows: u16) !void {
const th = p.theme();
const chrome = p.chromeTheme();
const tx = r.x + @min(TAG_TEXT_INSET, r.w);
const tw = r.w -| TAG_TEXT_INSET;
const tag_y = p.tagTop(pane, r);
const page_rgb = th.bg orelse th.tag_bg;
// Focus is visible along the tag without changing its live text grid.
const inks = chrome.roles.on(groundOf(p, pane, active));
const tag_bg = inks.ground;
const tag_fg = if (active) chrome.tag_active_fg else chrome.tag_fg;
const tag_style: CellStyle = .{ .fg = .{ .rgb = tag_fg }, .bg = .{ .rgb = tag_bg }, .font_role = .tagline };
// Padding belongs to the same font row but keeps its default ink: a
// role must not manufacture a foreground color for blank cells.
s.fill(tx, tag_y, tw, rows, .{
.bg = .{ .rgb = tag_bg },
.font_role = .tagline,
});
const whole = try tagline.tagText(p, arena, pane);
// A question in place of the tag's own text (TagState.asking).
const question = try TagState.asking(p, arena, pane);
const tail = if (question) |h| h.text else tagline.curTail(pane);
const prefix_len = whole.len - tail.len;
// A question in the walk UI's text ink (the tag's own text its own).
const text_style: CellStyle = if (question == null) tag_style else .{ .fg = .{ .rgb = inks.text }, .bg = .{ .rgb = tag_bg }, .font_role = .tagline };
// A row per line shown, from `tag_top`: the tag's first line carries
// the prefix, the rest are the tag's own text alone.
const top = pane.tag_top;
// Its lines wrapped at its width (tagline.shownTag): a row per row shown.
const shown = try tagline.shownTag(p, arena, pane);
var lines: [MAX_TAG_ROWS][]const u8 = @splat("");
var split = std.mem.splitScalar(u8, shown.text, '\n');
for (0..top) |_| _ = split.next();
for (lines[0..rows]) |*line| line.* = split.next() orelse continue;
// The keyboard's caret, as a row and a byte offset in that row's
// line: in the tag's own text when the tag has the keyboard, in the
// path while a name is drafted.
const Caret = struct { row: usize, at: usize };
const caret: ?Caret = if (!active)
null
else if (pane.prompt == .name) caret: {
// A drafted name is the prefix: its caret is on row 0 of the tag,
// the rows it wrapped onto included.
const at = modal.positionAt(shown.text, shown.fromReal(tagline.nameAt(p, pane) + @as(usize, @intCast(@max(0, pane.input.cur_col)))));
if (at.row < top or at.row >= top + rows) break :caret null;
break :caret .{ .row = at.row - top, .at = @min(lines[at.row - top].len, at.col) };
} else if (pane.focus == .tag and pane.prompt == .none and question == null) caret: {
const real = modal.offsetAt(whole, .{ .row = @intCast(@max(0, pane.tag.cur_row)), .col = @intCast(@max(0, pane.tag.cur_col)) });
const at = modal.positionAt(shown.text, shown.fromReal(real));
if (at.row < top or at.row >= top + rows) break :caret null;
const row: usize = at.row - top;
break :caret .{ .row = row, .at = @min(lines[row].len, at.col) };
} else null;
// Its rows are wrapped to fit: a pane tag never scrolls sideways.
pane.tag_scroll = 0;
for (lines[0..rows], 0..) |line, row| {
// The scroll is the caret's line's: a shorter row shows nothing.
const start = @min(line.len, panes.File.rawAtDisplay(p.settings.tab, line, pane.tag_scroll));
_ = s.print(tx, tag_y + @as(u16, @intCast(row)), tw, line[start..], text_style);
}
// The pane's progress at its first row's right end, where the row has
// room for it (SPEC §10.2).
if (top == 0 and rows > 0) for (p.messages.live[0..p.messages.live_len]) |*said| {
if (said.kind != .progress or said.pane != pane.serial) continue;
var buf: [Messages.LoggedMessage.cap + 8]u8 = undefined;
const text = std.fmt.bufPrint(&buf, "{s} {s}", .{ Messages.Kind.progress.glyph(), said.words() }) catch break;
const width = panes.File.displayWidth(p.settings.tab, text);
const used = panes.File.displayWidth(p.settings.tab, lines[0]);
if (used + 2 + width <= tw) _ = s.print(tx + @as(u16, @intCast(tw - width)), tag_y, @intCast(width), text, .{ .fg = .{ .rgb = Messages.ink(chrome, .progress) }, .bg = .{ .rgb = tag_bg }, .font_role = .tagline });
break;
};
// The name's tint, in the tag's first line, over whichever rows it
// wrapped onto.
const first_line = whole[0 .. std.mem.indexOfScalar(u8, whole, '\n') orelse whole.len];
if (tagTintRange(p, pane, first_line, prefix_len)) |name| if (name.lo < name.hi)
inkTag(p, s, shown, lines[0..rows], top, tx, tag_y, tw, name.lo, @min(name.hi, first_line.len), if (active) chrome.tag_active_name_fg else chrome.tag_name_fg, false);
// A walk's forward steps, after the name, dimmed (Trail.zig); its fold
// `+N` too; the word n walks underlined in the key ink (UX D8).
const ahead = tagline.forwardSpan(p, pane);
if (ahead[0] < ahead[1]) inkTag(p, s, shown, lines[0..rows], top, tx, tag_y, tw, ahead[0], @min(ahead[1], first_line.len), inks.dim, false);
if (pane.file != null and pane.trail.n > 0) {
const kept = tagline.folded(p, pane).whole;
const t = &pane.trail;
// Each run of folded places' `+N` (UX L'1: words never fold).
for (0..t.backs()) |i| if (t.isFolded(i, kept) and !(i > 0 and t.isFolded(i - 1, kept))) {
const fold = t.spanOf(i, kept);
inkTag(p, s, shown, lines[0..rows], top, tx, tag_y, tw, fold[0], @min(fold[1], first_line.len), inks.dim, false);
};
if (t.walked()) |i| if (!t.isFolded(i, kept)) {
const word = t.spanOf(i, kept);
inkTag(p, s, shown, lines[0..rows], top, tx, tag_y, tw, word[0], @min(word[1], first_line.len), chrome.roles.key, true);
};
}
// A search word its places no longer light, dimmed (Prompt.zig).
if (pardes.Prompt.unlit(p, pane)) |span| inkTag(p, s, shown, lines[0..rows], top, tx, tag_y, tw, span[0], @min(span[1], first_line.len), inks.dim, false);
// A question's keys in the key ink, its dimmed words (TagState.Span),
// over whichever rows they wrapped onto.
if (question) |h| for (h.spans) |span| {
const a = modal.positionAt(shown.text, shown.fromReal(prefix_len + span.lo));
const b = modal.positionAt(shown.text, shown.fromReal(prefix_len + span.hi));
var row = @max(a.row, top);
while (row <= b.row and row < top + rows) : (row += 1) {
const line = lines[row - top];
const from = if (row == a.row) a.col else 0;
const to = if (row == b.row) b.col else line.len;
var col = panes.File.rawDisplayCol(p.settings.tab, line, @min(from, line.len));
const end = panes.File.rawDisplayCol(p.settings.tab, line, @min(to, line.len));
const y = tag_y + @as(u16, @intCast(row - top));
while (col < end and col < tw) : (col += 1) {
const style = &s.at(tx + @as(u16, @intCast(col)), y).style;
switch (span.style) {
.key => {
style.fg = .{ .rgb = chrome.roles.key };
style.bold = true;
},
.dim => style.fg = .{ .rgb = inks.dim },
.under => style.ul = .single,
.strike => style.strikethrough = true,
.err => style.fg = .{ .rgb = chrome.roles.err },
.rule => style.fg = .{ .rgb = inks.rule },
}
}
}
};
// The tag's own selections, while it has the keyboard, a line at a
// time. A range of one grapheme is the block cursor, not a selection.
if (pane.focus == .tag and question == null) {
var ranges: [Text.max_selections]modal.Selection = undefined;
const got = pane.tag.ranges(whole, 0, &ranges);
for (ranges[0..got.n]) |range| {
const lo = @min(range.anchor, range.head);
const hi = @max(range.anchor, range.head);
if (modal.nextGrapheme(whole, lo) >= hi) continue;
const first = modal.positionAt(shown.text, shown.fromReal(lo));
const last = modal.positionAt(shown.text, shown.fromReal(hi));
var row = @max(first.row, top);
while (row <= last.row and row < top + rows) : (row += 1) {
const line = lines[row - top];
const from = if (row == first.row) first.col else 0;
const to = @min(line.len, if (row == last.row) last.col else line.len);
const col = panes.File.rawDisplayCol(p.settings.tab, line, from) -| pane.tag_scroll;
const end = @min(panes.File.rawDisplayCol(p.settings.tab, line, to) -| pane.tag_scroll, tw);
if (col < end) s.tint(tx + @as(u16, @intCast(col)), tag_y + @as(u16, @intCast(row - top)), @intCast(end - col), th.tag_sel_bg orelse th.sel_bg, th.sel_fg);
}
}
}
// the caret: bar while typing, block in normal mode (same rule as a body)
if (caret) |at| {
const col = panes.File.rawDisplayCol(p.settings.tab, lines[at.row], at.at) -| pane.tag_scroll;
const mode = if (pane.prompt == .name) pane.input.mode else pane.tag.mode;
if (col < tw) s.cursor = .{ .x = tx + @as(u16, @intCast(col)), .y = tag_y + @as(u16, @intCast(at.row)), .bar = mode == .insert };
}
// Pointer sweeps over the tag rows. A select sweep is the tag's own, in
// its selection colours; the look-hover affordance goes over it, and a
// middle or right sweep over both, tinted by its button the way the
// body tints it.
for (pane.sel, 0..) |selection, button| {
if (selection.state == .none or (selection.state == .done and button != 0)) continue;
if (button != pardes.sel_slot or @min(selection.r0, selection.r1) >= rows) continue;
body_layer.paintPointerSelection(p, s, pane, r, selection, 0, th.tag_sel_bg orelse th.sel_bg, th.sel_fg);
}
if (p.look_hover_preview) |preview| {
if (preview.pane == id and preview.serial == pane.serial) if (preview.sel) |sel| if (@min(sel.r0, sel.r1) < rows) {
const preview_bg = colors.mix(page_rgb, colors.mix(page_rgb, th.sel_bg));
body_layer.paintPointerSelection(p, s, pane, r, sel, 0, preview_bg, null);
};
}
const sweeps = &p.chrome().sweeps;
for (pane.sel, 0..) |selection, button| {
if (selection.state == .none or (selection.state == .done and button != 0)) continue;
if (button == pardes.sel_slot or @min(selection.r0, selection.r1) >= rows) continue;
body_layer.paintPointerSelection(p, s, pane, r, selection, 0, sweeps[button].ground, sweeps[button].text);
}
}
test "an unfocused grip shows on its tag in the grid, in every native theme" {
const p = try Pardes.init(std.testing.allocator, .{ .cols = 100, .rows = 30 });
defer p.deinit();
_ = try p.setTestFile("text\n");
p.newScratchBelow(0);
var arena: std.heap.ArenaAllocator = .init(std.testing.allocator);
defer arena.deinit();
var ok = true;
for (pardes.themes[0..colors.native_count], 0..) |theme, index| {
colors.setThemeIndex(p, index);
p.view().active = 1;
_ = arena.reset(.retain_capacity);
const s = try p.render(arena.allocator());
const r = p.view().rects[0];
const grip = s.at(r.x, p.tagTop(p.panes[0].?, r)).style.bg.rgb;
// Its tag's ground: a reading pane's the reading tint.
const ratio = colors.contrast(grip, tagGround(p, p.panes[0].?, false));
// ponytail: dark and helix have always had a faint grip (1.2:1); flagged, not changed here.
const floor: f32 = if (std.mem.eql(u8, theme.name, "dark") or std.mem.eql(u8, theme.name, "helix")) 1.2 else 1.5;
// Against a reading tint (UX D4's, lighter on a dark tag) a little
// less: forestbones_dark's 1.486, nordbones' 1.488, monokai's 1.4999.
if (ratio < floor - 0.02) std.debug.print("{s}: grip {d:.4}\n", .{ theme.name, ratio });
ok = ok and ratio >= floor - 0.02;
}
try std.testing.expect(ok);
}
test "a grip's four states, focused or not and clean or dirty, are each its own in every native theme" {
const p = try Pardes.init(std.testing.allocator, .{ .cols = 100, .rows = 30 });
defer p.deinit();
const pane = try p.setTestFile("text\n");
p.newScratchBelow(0);
var arena: std.heap.ArenaAllocator = .init(std.testing.allocator);
defer arena.deinit();
const State = struct { first: Cell, second: Cell, dirty: bool };
for (0..colors.native_count) |index| {
colors.setThemeIndex(p, index);
var states: [4]State = undefined;
for (&states, 0..) |*state, i| {
p.view().active = if (i & 1 == 0) 0 else 1;
const saved = pane.file.?.saved_revision;
pane.file.?.revision = if (i & 2 != 0) saved + 1 else saved;
_ = arena.reset(.retain_capacity);
const s = try p.render(arena.allocator());
const r = p.view().rects[0];
const y = p.tagTop(pane, r);
state.* = .{ .first = s.at(r.x, y).*, .second = s.at(r.x + 1, y).*, .dirty = s.region(.grip, 0).?.dirty };
try std.testing.expectEqual(i & 2 != 0, state.dirty);
if (i & 2 == 0) continue;
// Dirty is more than a shade: the grid prints a bold `*` (one
// grid for every shell; a pixel shell draws its button over it).
try std.testing.expectEqualStrings("*", state.second.grapheme());
try std.testing.expect(state.second.style.bold);
}
for (states, 0..) |a, i| for (states[i + 1 ..]) |b| {
const same = std.meta.eql(a.first.style.bg, b.first.style.bg) and a.second.visuallyEqual(&b.second);
try std.testing.expect(!same);
};
}
pane.file.?.revision = pane.file.?.saved_revision;
}
test "every theme draws a file's name, a +Errors and a Term word in a tint, never the tag's plain ink" {
if (pardes.Term.ops == null) return error.SkipZigTest; // a shell's Term word
const gpa = std.testing.allocator;
const p = try Pardes.init(gpa, .{ .cols = 120, .rows = 40, .tty_only = true });
defer p.deinit();
_ = try p.setTestFile("text\n");
try std.testing.expect(p.executeBuiltinLine(0, "Term+sh"));
const terminal = p.view().active;
p.acknowledgeShell(.at(terminal), "/bin/sh", false);
try @import("Output.zig").openErrors(p, 0, try gpa.dupe(u8, "oops\n"));
while (p.nextEffect()) |_| {}
const errors = for (p.panes, 0..) |slot, id| {
const pane = slot orelse continue;
if (pane.file) |f| if (std.mem.endsWith(u8, f.path, "+Errors")) break id;
} else return error.NoErrorsPane;
const Probe = struct { id: usize, word: []const u8 };
const probes = [_]Probe{ .{ .id = 0, .word = "test.txt" }, .{ .id = terminal, .word = "Term+sh" }, .{ .id = errors, .word = "+Errors" } };
var arena: std.heap.ArenaAllocator = .init(gpa);
defer arena.deinit();
for (pardes.themes, 0..) |theme, index| {
colors.setThemeIndex(p, index);
// Each pane focused in turn: every word on both grounds.
for (probes) |focused| {
p.view().active = focused.id;
_ = arena.reset(.retain_capacity);
const s = try p.render(arena.allocator());
for (probes) |probe| {
const r = p.view().rects[probe.id];
const y = p.tagTop(p.panes[probe.id].?, r);
// The row's text, and the column each byte of it is drawn at.
var row: [512]u8 = undefined;
var cols: [512]u16 = undefined;
var len: usize = 0;
for (r.x..r.x + r.w) |x| {
const g = s.at(@intCast(x), y).grapheme();
for (if (g.len == 0) " " else g) |byte| if (len < row.len) {
row[len] = byte;
cols[len] = @intCast(x);
len += 1;
};
}
const name = std.mem.indexOf(u8, row[0..len], probe.word) orelse return error.WordNotDrawn;
const plain = std.mem.indexOf(u8, row[0..len], " Del") orelse return error.WordNotDrawn;
const tint = s.at(cols[name], y).style.fg;
const ink = s.at(cols[plain + 1], y).style.fg;
if (std.meta.eql(tint, ink)) std.debug.print("{s}: {s} drawn in the tag's ink\n", .{ theme.name, probe.word });
try std.testing.expect(!std.meta.eql(tint, ink));
// The whole word, in the one tint.
try std.testing.expect(std.meta.eql(s.at(cols[name + probe.word.len - 1], y).style.fg, tint));
}
}
}
}
test "a tag of three rows has its grip on the first, and band under it that still grabs the pane" {
const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 100, .rows = 30 });
defer p.deinit();
const pane = try p.setTestFile("text\n");
pane.tag.own = try std.testing.allocator.dupe(u8, "Second\nthird\nfourth");
var arena: std.heap.ArenaAllocator = .init(std.testing.allocator);
defer arena.deinit();
p.sync();
_ = arena.reset(.retain_capacity);
const s = try p.render(arena.allocator());
const r = p.view().rects[0];
const top = p.tagTop(pane, r);
try std.testing.expect(pane.tag_rows >= 3);
const chrome = p.chromeTheme();
const band = tagGround(p, pane, true);
for (0..config.GUTTER) |dx| {
const x = r.x + @as(u16, @intCast(dx));
try std.testing.expectEqual(Color{ .rgb = chrome.box }, s.at(x, top).style.bg);
for (1..pane.tag_rows) |line| {
const cell = s.at(x, top + @as(u16, @intCast(line)));
try std.testing.expectEqual(Color{ .rgb = band }, cell.style.bg);
try std.testing.expectEqualStrings(" ", cell.grapheme());
}
}
try std.testing.expectEqual(pane.tag_rows, s.region(.grip, 0).?.rect.h);
}
test "place puts every part where the painters and layers put it, in every layout" {
const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 100, .rows = 30 });
defer p.deinit();
_ = try p.setTestFile(std.mem.asBytes(&@as([80][7]u8, @splat("placed\n".*))));
layout.newColumn(p, 0);
layout.newScratchIn(p, 0, layout.activeColumn(p).?);
p.newScratchBelow(0);
p.sync();
var arena: std.heap.ArenaAllocator = .init(std.testing.allocator);
defer arena.deinit();
for (0..16) |step| {
if (step & 4 != 0) try std.testing.expect(p.executeBuiltinLine(p.view().active, "Msg a placed notice"));
if (step == 8) try std.testing.expect(p.executeBuiltinLine(1, "Collapse"));
if (step == 12) try std.testing.expect(p.executeBuiltinLine(1, "Collapse"));
if (step == 10) {
const pane = p.panes[p.view().active].?;
pane.tag.own = try std.testing.allocator.dupe(u8, "Second\nthird");
}
p.sync();
_ = arena.reset(.retain_capacity);
const s = try p.render(arena.allocator());
try std.testing.expectEqual(Region.Kind.page, s.regions[0].kind);
for (p.panes, 0..) |slot, id| {
const pane = slot orelse continue;
const r = p.view().rects[id];
if (r.w <= config.GUTTER or r.h == 0) continue;
const grip = s.region(.grip, id).?;
try std.testing.expectEqual(Rect{ .x = r.x, .y = p.tagTop(pane, r), .w = config.GUTTER, .h = pane.tag_rows }, grip.rect);
// The grid's grip cell is the grip's.
try std.testing.expectEqual(FontRole.tagline, s.at(grip.rect.x, grip.rect.y).style.font_role);
const tag = s.region(.tag, id).?;
try std.testing.expectEqual(Rect{ .x = r.x + config.GUTTER, .y = p.tagTop(pane, r), .w = r.w - config.GUTTER, .h = pane.tag_rows }, tag.rect);
// One layer, a row to each line of the tag.
if (r.w > TAG_TEXT_INSET) {
try std.testing.expectEqual(Rect{ .x = r.x + TAG_TEXT_INSET, .y = p.tagTop(pane, r), .w = r.w - TAG_TEXT_INSET, .h = pane.tag_rows }, s.tag_layers[id].viewport);
try std.testing.expectEqual(pane.tag_rows, s.tag_layers[id].rows);
}
if (pane.collapsed) {
try std.testing.expect(s.region(.body, id) == null);
try std.testing.expect(s.region(.rail, id) == null);
continue;
}
const body = s.region(.body, id).?;
try std.testing.expectEqual(Rect{ .x = r.x + config.GUTTER, .y = p.bodyTop(pane, r), .w = r.w - config.GUTTER, .h = r.h - pane.tag_rows }, body.rect);
const rail = s.region(.rail, id).?;
// The painted thumb is exactly the placed one.
for (0..rail.rect.h) |row| {
const y = rail.rect.y + @as(u16, @intCast(row));
const thumb = row >= rail.thumb_y and row < rail.thumb_y + rail.thumb_h;
const want = if (thumb) p.chromeTheme().scroll_thumb else p.chromeTheme().scroll_track;
try std.testing.expectEqual(Color{ .rgb = want }, s.at(rail.rect.x, y).style.bg);
}
var notices: usize = 0;
for (s.regionList()) |region| {
if (region.kind != .notice or region.owner != id) continue;
notices += 1;
try std.testing.expectEqual(pane.notices.left[region.notice], region.rect.x);
try std.testing.expectEqual(r.x + r.w, region.rect.x + region.rect.w);
try std.testing.expectEqual(p.bodyTop(pane, r) + pane.notices.row[region.notice], region.rect.y);
try std.testing.expectEqual(region.rect, s.tag_layers[NOTICE_LAYER_BASE + id * Pane.Notices.max + region.notice].viewport);
}
try std.testing.expectEqual(@as(usize, pane.notices.len), notices);
}
for (0..p.view().ncol) |c| {
const region = s.region(.column_tag, c);
if (layout.columnBarHeight(p) == 0) {
try std.testing.expect(region == null);
continue;
}
try std.testing.expectEqual(Rect{ .x = p.view().col_x[c] + TAG_TEXT_INSET, .y = p.topBarHeight(), .w = p.view().col_w[c] - TAG_TEXT_INSET, .h = layout.columnBarHeight(p) }, region.?.rect);
}
}
}
test "the word under the pointer on the notice row is lit in its layer and in the grid's copy" {
const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 61, .rows = 12 });
defer p.deinit();
_ = try p.setTestFile("text\n");
var arena: std.heap.ArenaAllocator = .init(std.testing.allocator);
defer arena.deinit();
try std.testing.expect(p.executeBuiltinLine(p.view().active, "Msg Look here"));
p.sync();
var s = try p.render(arena.allocator());
const foot = s.region(.notice_row, Region.none) orelse return error.TestExpectedNoticeRow;
try std.testing.expectEqual(s.rows - 1, foot.rect.y);
const row = s.cells[@as(usize, foot.rect.y) * s.cols ..][0..s.cols];
// "here": the message's last word, `· /test.txt Look here`, 17
// columns in.
const here: u16 = TAG_TEXT_INSET + 17;
try std.testing.expectEqualStrings("h", row[here].grapheme());
p.view().pointer_inside = true;
p.view().hover_col = here + 1;
p.view().hover_row = foot.rect.y;
_ = arena.reset(.retain_capacity);
s = try p.render(arena.allocator());
const lit = s.cells[@as(usize, foot.rect.y) * s.cols ..][0..s.cols];
const sel: Color = .{ .rgb = p.theme().tag_sel_bg orelse p.theme().sel_bg };
for (lit[here..][0..4]) |cell| try std.testing.expectEqual(sel, cell.style.bg);
try std.testing.expect(!std.meta.eql(sel, lit[here - 2].style.bg));
const layer = &s.tag_layers[pardes.NOTICE_ROW_LAYER];
try std.testing.expectEqual(TagKind.notice_row, layer.kind);
const lit_in_layer = for (layer.cells) |cell| {
if (std.meta.eql(sel, cell.style.bg)) break true;
} else false;
try std.testing.expect(lit_in_layer);
}
test "a message wider than the notice row, as a shader's compile error is, gives way to the row's right end and draws nothing past the row" {
const p = try Pardes.init(std.testing.allocator, .{ .cols = 80, .rows = 20 });
defer p.deinit();
// A gui-shaped core: its tagline is narrower than the body, so the
// row's layer holds more columns than the grid has cells.
p.update(.{ .resize = .{ .cols = 80, .rows = 20, .row_metrics = .{ .body_w = 10, .body_h = 20, .tagline_w = 6, .tagline_h = 12 } } });
_ = try p.setTestFile("text\n");
var arena: std.heap.ArenaAllocator = .init(std.testing.allocator);
defer arena.deinit();
const long = std.mem.asBytes(&@as([3][126]u8, @splat("Shader /home/someone/shaders/test_shadertoy_invalid.glsl: error: Linking fragment stage: No function definition (body) found: ".*)));
for ([_][]const u8{ "Msg " ++ long, "" }) |line| {
// The Msg builtin, and the path a failed compile takes (Post.zig).
if (line.len > 0) try std.testing.expect(p.executeBuiltinLine(p.view().active, line)) else p.setMessage(.at(p.view().active), long);
p.sync();
for (0..30) |_| p.update(.tick);
_ = arena.reset(.retain_capacity);
const s = try p.render(arena.allocator());
const layer = &s.tag_layers[pardes.NOTICE_ROW_LAYER];
try std.testing.expectEqual(layout.tagCapacity(p, s.cols - TAG_TEXT_INSET), layer.cols);
// The counts at the row's end, whole, a blank cell short of it.
const cells = layer.cells[0..layer.cols];
try std.testing.expectEqualStrings(" ", cells[cells.len - 1].grapheme());
try std.testing.expectEqualStrings("m", cells[cells.len - 2].grapheme());
}
}
test "a monkey over notices, prompts, tags and tiny panes at a gui's metrics never panics" {
// The snapshot harness runs the tty, whose tagline pitch is the body's,
// so it never reaches what a narrower tagline does to a notice's band:
// this drives the core at a gui's metrics instead. PARDES_FUZZ_STEPS
// runs it longer (the suite's run is short).
const steps: usize = if (std.c.getenv("PARDES_FUZZ_STEPS")) |v| std.fmt.parseInt(usize, std.mem.span(v), 10) catch 400 else 400;
const seed: u64 = if (std.c.getenv("PARDES_FUZZ_SEED")) |v| std.fmt.parseInt(u64, std.mem.span(v), 10) catch 7 else 7;
var prng = std.Random.DefaultPrng.init(seed);
const random = prng.random();
const p = try Pardes.init(std.testing.allocator, .{ .cols = 80, .rows = 24 });
defer p.deinit();
_ = try p.setTestFile(std.mem.asBytes(&@as([20][17]u8, @splat("fuzz\n\u{754c}\u{754c} wide\n".*))));
var arena: std.heap.ArenaAllocator = .init(std.testing.allocator);
defer arena.deinit();
const words = [_][]const u8{ "error:", "\u{754c}", "x", "Shader /tmp/a.glsl:", "e\u{301}", "\t", std.mem.asBytes(&@as([20][5]u8, @splat("long-".*))), " " };
const commands = [_][]const u8{ "New", "Newcol", "Collapse", "TreeContext", "Wrap", "Find", "Del", "Debug" };
var text: std.ArrayListUnmanaged(u8) = .empty;
defer text.deinit(std.testing.allocator);
for (0..steps) |_| {
switch (random.uintLessThan(u8, 8)) {
// A resize, down to a cell, at a gui's metrics: a tagline no
// wider and no taller than the body's cell, often narrower.
0 => {
const body_w = random.intRangeAtMost(u16, 6, 12);
const body_h = random.intRangeAtMost(u16, 12, 24);
const cols = if (random.boolean()) random.intRangeAtMost(u16, 1, 12) else random.intRangeAtMost(u16, 13, 140);
const rows = if (random.boolean()) random.intRangeAtMost(u16, 1, 6) else random.intRangeAtMost(u16, 7, 50);
p.update(.{ .resize = .{ .cols = cols, .rows = rows, .row_metrics = .{ .body_w = body_w, .body_h = body_h, .tagline_w = random.intRangeAtMost(u16, 2, body_w), .tagline_h = random.intRangeAtMost(u16, 6, body_h) } } });
},
// A message, as long as a compile error or longer.
1, 2 => {
text.clearRetainingCapacity();
for (0..random.uintAtMost(usize, 60)) |_| try text.appendSlice(std.testing.allocator, words[random.uintLessThan(usize, words.len)]);
if (random.boolean()) p.setMessage(.at(p.view().active), text.items) else {
const line = try std.fmt.allocPrint(arena.allocator(), "Msg {s}", .{text.items});
_ = p.executeBuiltinLine(p.view().active, line);
}
},
3 => _ = p.executeBuiltinLine(p.view().active, commands[random.uintLessThan(usize, commands.len)]),
// Typing, escaping and entering, into a prompt when one is up.
4 => p.update(.{ .key = .{ .cp = switch (random.uintLessThan(u8, 9)) {
0 => pardes.Key.escape,
1 => pardes.Key.enter,
2 => 0x754c,
3 => pardes.Key.end,
4 => pardes.Key.home,
5 => pardes.Key.up,
6 => pardes.Key.down,
else => 'a' + random.uintLessThan(u21, 26),
} } }),
// A tag of several rows.
5 => if (p.panes[p.view().active]) |pane| {
text.clearRetainingCapacity();
for (0..random.uintAtMost(usize, 5)) |_| try text.appendSlice(std.testing.allocator, if (random.boolean()) " Look\nmore \u{754c}" else "\n" ++ &@as([150]u8, @splat('x')));
if (pane.tag.own) |own| std.testing.allocator.free(own);
pane.tag.own = try std.testing.allocator.dupe(u8, text.items);
},
// Hover, and clicks: into a tag, a header, a body, a grip.
6 => {
const col = random.uintAtMost(u16, p.view().screen_w);
const row = random.uintAtMost(u16, p.view().screen_h);
if (random.boolean()) p.update(.{ .mouse = .{ .button = .none, .kind = .motion, .col = col, .row = row } }) else {
p.update(.{ .mouse = .{ .button = .left, .kind = .press, .col = col, .row = row } });
p.update(.{ .mouse = .{ .button = .left, .kind = .release, .col = col, .row = row } });
}
},
else => for (0..random.uintAtMost(usize, 20)) |_| p.update(.tick),
}
p.sync();
_ = arena.reset(.retain_capacity);
_ = try p.render(arena.allocator());
p.presented();
}
}
test "a wide glyph cut by a tag's or a notice's edge is joined into the grid as blank, never half drawn" {
const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 41, .rows = 12 });
defer p.deinit();
_ = try p.setTestFile("wide\n");
var arena: std.heap.ArenaAllocator = .init(std.testing.allocator);
defer arena.deinit();
const pane = p.panes[p.view().active].?;
// Both alignments, so one of them puts a wide glyph across each edge.
for ([_][]const u8{ std.mem.asBytes(&@as([30][3]u8, @splat("\u{754c}".*))), "x" ++ std.mem.asBytes(&@as([30][3]u8, @splat("\u{754c}".*))) }) |wide| {
if (pane.tag.own) |own| std.testing.allocator.free(own);
pane.tag.own = try std.testing.allocator.dupe(u8, wide);
var message: [200]u8 = undefined;
try std.testing.expect(p.executeBuiltinLine(p.view().active, try std.fmt.bufPrint(&message, "Msg {s}", .{wide})));
p.sync();
_ = arena.reset(.retain_capacity);
const s = try p.render(arena.allocator());
for (s.regionList()) |region| {
if (region.kind != .tag and region.kind != .notice) continue;
const row = s.cells[@as(usize, region.rect.y) * s.cols ..][region.rect.x..][0..region.rect.w];
for (row, 0..) |cell, i| {
const spacer = !cell.default and cell.len == 0;
const head = !cell.default and cell.len > 1 and i + 1 < row.len and !row[i + 1].default and row[i + 1].len == 0;
// A spacer has its head before it; a head its spacer after.
if (spacer) try std.testing.expect(i > 0 and row[i - 1].len > 1);
if (i + 1 == row.len) try std.testing.expect(!head and !(cell.len > 1 and vaxis.gwidth.gwidth(cell.grapheme(), .unicode) == 2));
}
// The right edge of each is the band, blank where a glyph was cut.
try std.testing.expectEqual(FontRole.tagline, row[row.len - 1].style.font_role);
}
}
}
test "a body with context rows is painted once, and the grid's rows are what a plain paint of it gives" {
const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 60, .rows = 16 });
defer p.deinit();
const pane = try p.setTestFile("pub const Box = struct {\n pub fn run() void {\n" ++ (std.mem.asBytes(&@as([30][21]u8, @splat(" // body line\n".*)))) ++ " }\n};\n");
p.gpa.free(pane.file.?.path);
pane.file.?.path = try p.gpa.dupe(u8, "/test.zig");
if (!panes.File.supportsContext(pane)) return error.SkipZigTest;
pane.file.?.tree_context = true;
pane.file.?.scroll = 8;
var arena: std.heap.ArenaAllocator = .init(std.testing.allocator);
defer arena.deinit();
// A selection and the cursor on body rows, painted in the one pass.
pane.body.cur_row = 12;
pane.body.cur_col = 3;
pane.body.vsel = .{ .active = true, .row = 10, .col = 2 };
pane.body.mode = .normal;
p.sync();
_ = arena.reset(.retain_capacity);
const s = try p.render(arena.allocator());
const layer = &s.body_layers[0];
try std.testing.expect(layer.context_rows > 0);
const body = s.region(.body, 0).?;
// The plain paint the grid used to take, of the same frame.
const cells = try arena.allocator().alloc(Cell, s.cells.len);
@memset(cells, .{});
var plain: Surface = .{ .cols = s.cols, .rows = s.rows, .cells = cells };
pane.view.layered = false;
try body_layer.renderBody(p, &plain, arena.allocator(), pane, p.view().rects[0], 0, true, null);
pane.view.layered = true;
for (0..body.rect.h) |row| {
const y = body.rect.y + row;
for (0..body.rect.w) |col| {
const x = body.rect.x + col;
const got = s.cells[y * s.cols + x];
const want = plain.cells[y * s.cols + x];
try std.testing.expect(got.visuallyEqual(&want));
}
}
try std.testing.expectEqual(plain.cursor, s.cursor);
// The layer's own rows run past the grid's.
try std.testing.expect(layer.rows > body.rect.h);
}
// The class kept closed: a frame changes nothing a step reads. What a frame
// writes of a core is `.local` or `.derived` (in no digest and no dump: a
// pane's notice bands, the geometry's wrap map and context rows, a markdown
// text's tables), so a step that reads one must find what its own state
// makes. Here one core draws after every input, each frame at its own time
// after its step's, and one joined from its dump never draws: each input's
// step leaves the two with one digest. A field a frame leaves that a step
// then reads otherwise than a core that did not draw makes it, differs here.
// Not seen: a core joined later (its authority's reads are the tape's, which
// this has none of: the soak's late joins), and what no input here reaches.
test "a frame changes nothing a step reads: a core drawing after every input and one never drawing agree" {
const gpa = std.testing.allocator;
const steps: usize = if (std.c.getenv("PARDES_FUZZ_STEPS")) |v| std.fmt.parseInt(usize, std.mem.span(v), 10) catch 600 else 600;
const seed: u64 = if (std.c.getenv("PARDES_FUZZ_SEED")) |v| std.fmt.parseInt(u64, std.mem.span(v), 10) catch 11 else 11;
var prng = std.Random.DefaultPrng.init(seed);
const random = prng.random();
var text: std.ArrayList(u8) = .empty;
defer text.deinit(gpa);
for (0..4500) |i| try text.print(gpa, "line {d} {s}\n", .{ i, std.mem.asBytes(&@as([20][6]u8, @splat("words ".*)))[0 .. 6 * (i % 20)] });
try text.appendSlice(gpa, "\n| name | qty | note |\n|:--|--:|:-:|\n| fig | 1 | ripe |\n| apple || |\n| a longer name | 12345 | x |\n\n## end\n");
const metrics: pardes.RowMetrics = .{ .body_w = 10, .body_h = 20, .tagline_w = 6, .tagline_h = 12 };
const a = try Pardes.init(gpa, .{ .tty_only = true, .cols = 90, .rows = 30 });
defer a.deinit();
a.update(.{ .resize = .{ .cols = 90, .rows = 30, .row_metrics = metrics } });
const pane = try a.setTestFile(text.items);
a.gpa.free(pane.file.?.path);
pane.file.?.path = try a.gpa.dupe(u8, "/notes.md");
a.newScratchBelow(0);
a.view().active = 0;
pane.file.?.scroll = 4490;
pane.body.cur_row = 4502;
a.sync();
const first = try pardes.dump.dumpBytes(a);
defer gpa.free(first);
const b = try pardes.dump.load(gpa, a.opts, first, null);
defer b.deinit();
var arena: std.heap.ArenaAllocator = .init(gpa);
defer arena.deinit();
const commands = [_][]const u8{ "Wrap", "Collapse", "Del", "Undo", "Redo" };
var now: u64 = std.time.ns_per_s;
for (0..steps) |i| {
const v = a.view();
// Most presses on the body around the table at the text's end.
const body = v.rects[0];
const ev: pardes.Event = switch (random.uintLessThan(u8, 13)) {
0...4 => .{ .key = .{ .cp = switch (random.uintLessThan(u8, 16)) {
0 => pardes.Key.escape,
1 => 'i',
2 => 'G',
3 => 'g',
4 => ' ',
5 => 'd',
6 => 'w',
7 => 'b',
8 => 'u',
9, 10 => 'j',
11, 12 => 'k',
13 => 'h',
14 => 'l',
else => 'x',
} } },
5...7 => .{ .mouse = .{
.button = if (random.uintLessThan(u8, 4) == 0) .wheel_up else if (random.uintLessThan(u8, 4) == 0) .wheel_down else .left,
.kind = switch (random.uintLessThan(u8, 3)) {
0 => .press,
1 => .drag,
else => .release,
},
.col = if (random.boolean()) random.uintLessThan(u16, v.screen_w) else body.x + random.uintLessThan(u16, @max(1, @min(body.w, 40))),
.row = if (random.boolean()) random.uintLessThan(u16, v.screen_h) else body.y + random.uintLessThan(u16, @max(1, body.h)),
} },
8 => .{ .command = commands[random.uintLessThan(usize, commands.len)] },
9 => .{ .paste = "| new | row |\n" },
// The text changed on disk: a step changes it whole.
10 => .{ .file_changed = .{ .pane = .at(0), .bytes = text.items[0 .. text.items.len - random.uintLessThan(usize, 40)] } },
else => blk: {
now += random.uintLessThan(u64, 2 * std.time.ns_per_s);
break :blk .tick;
},
};
for ([_]*Pardes{ a, b }) |p| {
p.clock.now_ns = now;
p.clock.stepped_ns = now;
p.update(ev);
}
// A frame is drawn at its own time, after its step's.
now += random.uintLessThan(u64, std.time.ns_per_s);
a.clock.now_ns = now;
b.clock.now_ns = now;
_ = arena.reset(.retain_capacity);
_ = try a.render(arena.allocator());
a.presented();
b.presented();
if (apart(a, b)) {
std.debug.print("seed {d}, step {d}: a {s} left them apart\n", .{ seed, i, @tagName(ev) });
return error.DigestsDiffer;
}
}
}
/// Whether two cores' digests differ, field by field (the panes' and their
/// files' too), the fields that do named; but for the three that differ
/// between two cores in one test process whatever they do: a message's
/// stamp and a text's mtime, read off the wall clock with no tape, and a
/// scratch pane's cwd, inherited by pointer on one and owned bytes on one
/// loaded from a dump.
fn apart(a: *const Pardes, b: *const Pardes) bool {
var any = false;
const names = comptime pardes.walk.fieldNames(Pardes);
var da: [names.len]u64 = undefined;
var db: [names.len]u64 = undefined;
_ = pardes.walk.fieldDigests(Pardes, a, &da);
_ = pardes.walk.fieldDigests(Pardes, b, &db);
for (names, da, db) |name, x, y| if (x != y and !std.mem.eql(u8, name, "panes")) {
std.debug.print(" {s}\n", .{name});
any = true;
};
const pane_names = comptime pardes.walk.fieldNames(Pane);
for (a.panes, b.panes, 0..) |pa, pb, id| {
if ((pa == null) != (pb == null)) {
std.debug.print(" pane {d} open on one\n", .{id});
any = true;
}
const x = pa orelse continue;
const y = pb orelse continue;
var pda: [pane_names.len]u64 = undefined;
var pdb: [pane_names.len]u64 = undefined;
_ = pardes.walk.fieldDigests(Pane, x, &pda);
_ = pardes.walk.fieldDigests(Pane, y, &pdb);
for (pane_names, pda, pdb) |name, u, v| if (u != v and !std.mem.eql(u8, name, "cwd")) {
if (std.mem.eql(u8, name, "file")) if (x.file) |*fx| if (y.file) |*fy| {
const file_names = comptime pardes.walk.fieldNames(panes.File.State);
var fda: [file_names.len]u64 = undefined;
var fdb: [file_names.len]u64 = undefined;
_ = pardes.walk.fieldDigests(panes.File.State, fx, &fda);
_ = pardes.walk.fieldDigests(panes.File.State, fy, &fdb);
for (file_names, fda, fdb) |file_name, q, r| if (q != r and !std.mem.eql(u8, file_name, "mtime")) {
std.debug.print(" pane {d}.file.{s}\n", .{ id, file_name });
any = true;
};
continue;
};
std.debug.print(" pane {d}.{s}\n", .{ id, name });
any = true;
};
}
return any;
}
test "progress is said at the right end of its tag: a pane's at its tag's, a server's at the workspace tag's, and it goes when it ends" {
const p = try Pardes.init(std.testing.allocator, .{ .cols = 160, .rows = 20 });
defer p.deinit();
const pane = try p.setTestFile("text\n");
const id = p.view().active;
var frame = std.heap.ArenaAllocator.init(std.testing.allocator);
defer frame.deinit();
var buf: [512]u8 = undefined;
const Row = struct {
fn of(s: *const Surface, y: u16, out: []u8) []const u8 {
var n: usize = 0;
for (0..s.cols) |x| {
const g = s.cells[@as(usize, y) * s.cols + x].grapheme();
@memcpy(out[n..][0..g.len], g);
n += g.len;
}
return std.mem.trimEnd(u8, out[0..n], " ");
}
};
p.setStatus(.at(id), "Changes: jj\u{2026}");
p.setStatus(.at(pardes.MAX_PANES), "--:--:-- lsp zls: indexing 3/10");
p.sync();
var s = try p.render(frame.allocator());
const tag_y = p.tagTop(pane, p.view().rects[id]);
try std.testing.expect(std.mem.endsWith(u8, Row.of(s, tag_y, &buf), "\u{22ef} Changes: jj\u{2026}"));
try std.testing.expect(std.mem.endsWith(u8, Row.of(s, 0, &buf), "\u{22ef} lsp zls: indexing 3/10"));
// Its mark meanwhile, and neither is the notice row's.
try std.testing.expectEqualStrings("\u{22ef}", s.at(p.view().rects[id].x + config.GUTTER - 1, tag_y).grapheme());
try std.testing.expect(p.messages.headline() == null);
p.setStatus(.at(id), "");
_ = frame.reset(.retain_capacity);
s = try p.render(frame.allocator());
try std.testing.expect(std.mem.indexOf(u8, Row.of(s, tag_y, &buf), "\u{22ef}") == null);
}
test "a tag out of rest draws its keys bold in the key ink and its hint in the dim ink, never faint (UX D4)" {
const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 200, .rows = 30 });
defer p.deinit();
const pane = try p.setTestFile("one\n");
var frame = std.heap.ArenaAllocator.init(std.testing.allocator);
defer frame.deinit();
const a = frame.allocator();
const roles = &p.chromeTheme().roles;
const inks = roles.on(if (pardes.Trail.reading(p, pane)) .reading_focused else .focused);
// SPC's level rolled, after its delay: `␣ ▸ … l lsp… … ↓ unroll · ? help`.
p.clock.now_ns = 5 * std.time.ns_per_s;
p.clock.stepped_ns = p.clock.now_ns;
p.update(.{ .key = .{ .cp = ' ' } });
p.advance(p.clock.now_ns);
p.clock.now_ns = p.view().tag_state.due_ns;
p.sync();
const s = try p.render(a);
const r = p.view().rects[p.view().active];
const drawn = pardes.Band.covered(p, pane, p.view().active);
const row = s.cells[@as(usize, r.y + drawn.tag) * s.cols ..][0 .. @as(usize, s.cols) * drawn.head];
const Row = struct {
fn find(cells: []const Cell, word: []const u8) ?usize {
var at: usize = 0;
while (at + word.len <= cells.len) : (at += 1) {
for (word, cells[at..][0..word.len]) |c, cell| {
if (cell.len != 1 or cell.text[0] != c) break;
} else return at;
}
return null;
}
};
const hint = Row.find(row, "unroll").?;
try std.testing.expectEqual(Color{ .rgb = inks.dim }, row[hint].style.fg);
for (row) |cell| try std.testing.expect(!cell.style.dim);
var keys: usize = 0;
for (row) |cell| if (std.meta.eql(cell.style.fg, Color{ .rgb = roles.key })) {
try std.testing.expect(cell.style.bold);
keys += 1;
};
try std.testing.expect(keys > 3);
}
test "an unfocused reading pane's box in the grid is its ring round the reading tint where the box's fill is the tag's own ground (acme's), as a window draws it; a held pane's, or another theme's, as before" {
const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 100, .rows = 30 });
defer p.deinit();
const pane = try p.setTestFile("one\n");
const reader = p.view().active;
p.newScratchBelow(reader);
colors.setThemeIndex(p, colors.themeIndex("acme").?);
p.sync();
var frame = std.heap.ArenaAllocator.init(std.testing.allocator);
defer frame.deinit();
try std.testing.expect(pardes.Trail.reading(p, pane) and p.view().active != reader);
var s = try p.render(frame.allocator());
const r = p.view().rects[reader];
const want = colors.mix(boxBorder(p), p.chromeTheme().tag_reading_bg);
for (0..config.GUTTER) |x| try std.testing.expectEqual(Color{ .rgb = want }, s.at(r.x + @as(u16, @intCast(x)), r.y).style.bg);
// Held, the ring round the plain box.
pane.trail.held = true;
s = try p.render(frame.allocator());
try std.testing.expectEqual(Color{ .rgb = colors.mix(boxBorder(p), p.chromeTheme().tag_bg) }, s.at(r.x, r.y).style.bg);
// orchard's box has a fill of its own: a reader's keeps it.
pane.trail.held = false;
colors.setThemeIndex(p, colors.themeIndex("orchard").?);
s = try p.render(frame.allocator());
try std.testing.expectEqual(Color{ .rgb = colors.mix(boxBorder(p), p.chromeTheme().box_dim) }, s.at(r.x, r.y).style.bg);
}
|