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
// Copyright (c) 2019 Nicholas Marriott <nicholas.marriott@gmail.com>
//
// Permission to use, copy, modify, and distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
// WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
// IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
// OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
use crate::*;
/// Format range.
struct format_range {
index: u32,
s: *mut screen,
start: u32,
end: u32,
type_: style_range_type,
argument: u32,
string: [u8; 16],
entry: tailq_entry<format_range>,
}
type format_ranges = tailq_head<format_range>;
impl_tailq_entry!(format_range, entry, tailq_entry<format_range>);
/// Does this range match this style?
/// C `vendor/tmux/format-draw.c:44`: `static int format_is_type(struct format_range *fr, struct style *sy)`
fn format_is_type(fr: &format_range, sy: &style) -> bool {
if fr.type_ != sy.range_type {
return false;
}
match fr.type_ {
style_range_type::STYLE_RANGE_NONE
| style_range_type::STYLE_RANGE_LEFT
| style_range_type::STYLE_RANGE_RIGHT => true,
style_range_type::STYLE_RANGE_PANE
| style_range_type::STYLE_RANGE_WINDOW
| style_range_type::STYLE_RANGE_SESSION => fr.argument == sy.range_argument,
style_range_type::STYLE_RANGE_USER => unsafe {
libc::strcmp(
(&raw const fr.string).cast(),
(&raw const sy.range_string).cast(),
) == 0
},
}
}
/// Free a range.
/// C `vendor/tmux/format-draw.c:66`: `static void format_free_range(struct format_ranges *frs, struct format_range *fr)`
unsafe fn format_free_range(frs: *mut format_ranges, fr: *mut format_range) {
unsafe {
tailq_remove(frs, fr);
free(fr.cast());
}
}
/// Fix range positions.
/// C `vendor/tmux/format-draw.c:74`: `static void format_update_ranges(struct format_ranges *frs, struct screen *s, u_int offset, u_int start, u_int width)`
unsafe fn format_update_ranges(
frs: *mut format_ranges,
s: *mut screen,
offset: u32,
start: u32,
width: u32,
) {
unsafe {
if frs.is_null() {
return;
}
for fr in tailq_foreach(frs).map(NonNull::as_ptr) {
if (*fr).s != s {
continue;
}
if (*fr).end <= start || (*fr).start >= start + width {
format_free_range(frs, fr);
continue;
}
if (*fr).start < start {
(*fr).start = start;
}
if (*fr).end > start + width {
(*fr).end = start + width;
}
if (*fr).start == (*fr).end {
format_free_range(frs, fr);
continue;
}
(*fr).start -= start;
(*fr).end -= start;
(*fr).start += offset;
(*fr).end += offset;
}
}
}
/// Draw a part of the format.
/// C `vendor/tmux/format-draw.c:110`: `static void format_draw_put(struct screen_write_ctx *octx, u_int ocx, u_int ocy, struct screen *s, struct format_ranges *frs, u_int offset, u_int start, u_int width)`
unsafe fn format_draw_put(
octx: *mut screen_write_ctx,
ocx: u32,
ocy: u32,
s: *mut screen,
frs: *mut format_ranges,
offset: u32,
start: u32,
width: u32,
) {
unsafe {
// The offset is how far from the cursor on the target screen; start
// and width how much to copy from the source screen.
screen_write_cursormove(octx, (ocx + offset) as c_int, ocy as c_int, 0);
screen_write_fast_copy(octx, s, start, 0, width, 1);
format_update_ranges(frs, s, offset, start, width);
}
}
/// Draw list part of format.
/// C `vendor/tmux/format-draw.c:125`: `static void format_draw_put_list(struct screen_write_ctx *octx, u_int ocx, u_int ocy, u_int offset, u_int width, struct screen *list, struct screen *list_left, struct screen *list_right, int focus_start, int focus_end, struct format_ranges *frs)`
unsafe fn format_draw_put_list(
octx: *mut screen_write_ctx,
ocx: u32,
ocy: u32,
mut offset: u32,
mut width: u32,
list: *mut screen,
list_left: *mut screen,
list_right: *mut screen,
focus_start: i32,
focus_end: i32,
frs: *mut format_ranges,
) {
unsafe {
// If there is enough space for the list, draw it entirely.
if width >= (*list).cx {
format_draw_put(octx, ocx, ocy, list, frs, offset, 0, width);
return;
}
// The list needs to be trimmed. Try to keep the focus visible.
let focus_centre: u32 = (focus_start + (focus_end - focus_start) / 2) as u32;
let mut start: u32 = focus_centre.saturating_sub(width / 2);
if start + width > (*list).cx {
start = (*list).cx - width;
}
// Draw <> markers at either side if needed.
if start != 0 && width > (*list_left).cx {
screen_write_cursormove(octx, (ocx + offset) as c_int, ocy as c_int, 0);
screen_write_fast_copy(octx, list_left, 0, 0, (*list_left).cx, 1);
offset += (*list_left).cx;
start += (*list_left).cx;
width -= (*list_left).cx;
}
if start + width < (*list).cx && width > (*list_right).cx {
screen_write_cursormove(
octx,
(ocx + offset + width - (*list_right).cx) as c_int,
ocy as c_int,
0,
);
screen_write_fast_copy(octx, list_right, 0, 0, (*list_right).cx, 1);
width -= (*list_right).cx;
}
// Draw the list screen itself.
format_draw_put(octx, ocx, ocy, list, frs, offset, start, width);
}
}
/// Draw format with no list.
/// C `vendor/tmux/format-draw.c:169`: `static void format_draw_none(struct screen_write_ctx *octx, u_int available, u_int ocx, u_int ocy, struct screen *left, struct screen *centre, struct screen *right, struct screen *abs_centre, struct format_ranges *frs)`
unsafe fn format_draw_none(
octx: *mut screen_write_ctx,
available: u32,
ocx: u32,
ocy: u32,
left: *mut screen,
centre: *mut screen,
right: *mut screen,
abs_centre: *mut screen,
frs: *mut format_ranges,
) {
unsafe {
let mut width_left: u32 = (*left).cx;
let mut width_centre: u32 = (*centre).cx;
let mut width_right: u32 = (*right).cx;
let mut width_abs_centre: u32 = (*abs_centre).cx;
// Try to keep as much of the left and right as possible at the expense * of the centre.
while width_left + width_centre + width_right > available {
if width_centre > 0 {
width_centre -= 1;
} else if width_right > 0 {
width_right -= 1;
} else {
width_left -= 1;
}
}
// Write left.
format_draw_put(octx, ocx, ocy, left, frs, 0, 0, width_left);
// Write right at available - width_right.
format_draw_put(
octx,
ocx,
ocy,
right,
frs,
available - width_right,
(*right).cx - width_right,
width_right,
);
// Write centre halfway between
// width_left
// and
// available - width_right.
format_draw_put(
octx,
ocx,
ocy,
centre,
frs,
width_left + ((available - width_right) - width_left) / 2 - width_centre / 2,
(*centre).cx / 2 - width_centre / 2,
width_centre,
);
// Write abs_centre in the perfect centre of all horizontal space.
if width_abs_centre > available {
width_abs_centre = available;
}
format_draw_put(
octx,
ocx,
ocy,
abs_centre,
frs,
(available - width_abs_centre) / 2,
0,
width_abs_centre,
);
}
}
/// Draw format with list on the left.
/// C `vendor/tmux/format-draw.c:228`: `static void format_draw_left(struct screen_write_ctx *octx, u_int available, u_int ocx, u_int ocy, struct screen *left, struct screen *centre, struct screen *right, struct screen *abs_centre, struct screen *list, struct screen *list_left, struct screen *list_right, struct screen *after, int focus_start, int focus_end, struct format_ranges *frs)`
unsafe fn format_draw_left(
octx: *mut screen_write_ctx,
available: u32,
ocx: u32,
ocy: u32,
left: *mut screen,
centre: *mut screen,
right: *mut screen,
abs_centre: *mut screen,
list: *mut screen,
list_left: *mut screen,
list_right: *mut screen,
after: *mut screen,
mut focus_start: i32,
mut focus_end: i32,
frs: *mut format_ranges,
) {
unsafe {
let mut width_left: u32 = (*left).cx;
let mut width_centre: u32 = (*centre).cx;
let mut width_right: u32 = (*right).cx;
let mut width_abs_centre: u32 = (*abs_centre).cx;
let mut width_list: u32 = (*list).cx;
let mut width_after: u32 = (*after).cx;
let mut ctx: screen_write_ctx = std::mem::zeroed(); // TODO use uninit
// Trim first the centre, then the list, then the right, then after the
// list, then the left.
while width_left + width_centre + width_right + width_list + width_after > available {
if width_centre > 0 {
width_centre -= 1;
} else if width_list > 0 {
width_list -= 1;
} else if width_right > 0 {
width_right -= 1;
} else if width_after > 0 {
width_after -= 1;
} else {
width_left -= 1;
}
}
// If there is no list left, pass off to the no list function.
if width_list == 0 {
screen_write_start(&raw mut ctx, left);
screen_write_fast_copy(&raw mut ctx, after, 0, 0, width_after, 1);
screen_write_stop(&raw mut ctx);
format_draw_none(
octx, available, ocx, ocy, left, centre, right, abs_centre, frs,
);
return;
}
// Write left at 0.
format_draw_put(octx, ocx, ocy, left, frs, 0, 0, width_left);
// Write right at available - width_right.
format_draw_put(
octx,
ocx,
ocy,
right,
frs,
available - width_right,
(*right).cx - width_right,
width_right,
);
// Write after at width_left + width_list.
format_draw_put(
octx,
ocx,
ocy,
after,
frs,
width_left + width_list,
0,
width_after,
);
// Write centre halfway between
// width_left + width_list + width_after
// and
// available - width_right.
format_draw_put(
octx,
ocx,
ocy,
centre,
frs,
(width_left + width_list + width_after)
+ ((available - width_right) - (width_left + width_list + width_after)) / 2
- width_centre / 2,
(*centre).cx / 2 - width_centre / 2,
width_centre,
);
// The list now goes from
// width_left
// to
// width_left + width_list.
// If there is no focus given, keep the left in focus.
if focus_start == -1 || focus_end == -1 {
focus_start = 0;
focus_end = 0;
}
format_draw_put_list(
octx,
ocx,
ocy,
width_left,
width_list,
list,
list_left,
list_right,
focus_start,
focus_end,
frs,
);
// Write abs_centre in the perfect centre of all horizontal space.
if width_abs_centre > available {
width_abs_centre = available;
}
format_draw_put(
octx,
ocx,
ocy,
abs_centre,
frs,
(available - width_abs_centre) / 2,
0,
width_abs_centre,
);
}
}
/// Draw format with list in the centre.
/// C `vendor/tmux/format-draw.c:331`: `static void format_draw_centre(struct screen_write_ctx *octx, u_int available, u_int ocx, u_int ocy, struct screen *left, struct screen *centre, struct screen *right, struct screen *abs_centre, struct screen *list, struct screen *list_left, struct screen *list_right, struct screen *after, int focus_start, int focus_end, struct format_ranges *frs)`
unsafe fn format_draw_centre(
octx: *mut screen_write_ctx,
available: u32,
ocx: u32,
ocy: u32,
left: *mut screen,
centre: *mut screen,
right: *mut screen,
abs_centre: *mut screen,
list: *mut screen,
list_left: *mut screen,
list_right: *mut screen,
after: *mut screen,
mut focus_start: i32,
mut focus_end: i32,
frs: *mut format_ranges,
) {
unsafe {
let mut width_left: u32 = (*left).cx;
let mut width_centre: u32 = (*centre).cx;
let mut width_right: u32 = (*right).cx;
let mut width_list: u32 = (*list).cx;
let mut width_after: u32 = (*after).cx;
let mut width_abs_centre: u32 = (*abs_centre).cx;
let mut ctx: screen_write_ctx = std::mem::zeroed(); // TODO use uninit
// Trim first the list, then after the list, then the centre, then the
// right, then the left.
while width_left + width_centre + width_right + width_list + width_after > available {
if width_list > 0 {
width_list -= 1;
} else if width_after > 0 {
width_after -= 1;
} else if width_centre > 0 {
width_centre -= 1;
} else if width_right > 0 {
width_right -= 1;
} else {
width_left -= 1;
}
}
// If there is no list left, pass off to the no list function.
if width_list == 0 {
screen_write_start(&raw mut ctx, centre);
screen_write_fast_copy(&raw mut ctx, after, 0, 0, width_after, 1);
screen_write_stop(&raw mut ctx);
format_draw_none(
octx, available, ocx, ocy, left, centre, right, abs_centre, frs,
);
return;
}
// Write left at 0.
format_draw_put(octx, ocx, ocy, left, frs, 0, 0, width_left);
// Write right at available - width_right.
format_draw_put(
octx,
ocx,
ocy,
right,
frs,
available - width_right,
(*right).cx - width_right,
width_right,
);
// All three centre sections are offset from the middle of the
// available space.
let middle = width_left + ((available - width_right) - width_left) / 2;
// Write centre at
// middle - width_list / 2 - width_centre.
format_draw_put(
octx,
ocx,
ocy,
centre,
frs,
middle - width_list / 2 - width_centre,
0,
width_centre,
);
// Write after at
// middle - width_list / 2 + width_list
format_draw_put(
octx,
ocx,
ocy,
after,
frs,
middle - width_list / 2 + width_list,
0,
width_after,
);
// The list now goes from
// middle - width_list / 2
// to
// middle + width_list / 2
// If there is no focus given, keep the centre in focus.
if focus_start == -1 || focus_end == -1 {
focus_start = (*list).cx as i32 / 2;
focus_end = (*list).cx as i32 / 2;
}
format_draw_put_list(
octx,
ocx,
ocy,
middle - width_list / 2,
width_list,
list,
list_left,
list_right,
focus_start,
focus_end,
frs,
);
// Write abs_centre in the perfect centre of all horizontal space.
if width_abs_centre > available {
width_abs_centre = available;
}
format_draw_put(
octx,
ocx,
ocy,
abs_centre,
frs,
(available - width_abs_centre) / 2,
0,
width_abs_centre,
);
}
}
/// Draw format with list on the right.
/// C `vendor/tmux/format-draw.c:439`: `static void format_draw_right(struct screen_write_ctx *octx, u_int available, u_int ocx, u_int ocy, struct screen *left, struct screen *centre, struct screen *right, struct screen *abs_centre, struct screen *list, struct screen *list_left, struct screen *list_right, struct screen *after, int focus_start, int focus_end, struct format_ranges *frs)`
unsafe fn format_draw_right(
octx: *mut screen_write_ctx,
available: u32,
ocx: u32,
ocy: u32,
left: *mut screen,
centre: *mut screen,
right: *mut screen,
abs_centre: *mut screen,
list: *mut screen,
list_left: *mut screen,
list_right: *mut screen,
after: *mut screen,
mut focus_start: i32,
mut focus_end: i32,
frs: *mut format_ranges,
) {
unsafe {
let mut width_left: u32 = (*left).cx;
let mut width_centre: u32 = (*centre).cx;
let mut width_right: u32 = (*right).cx;
let mut width_list: u32 = (*list).cx;
let mut width_after: u32 = (*after).cx;
let mut width_abs_centre: u32 = (*abs_centre).cx;
let mut ctx: screen_write_ctx = std::mem::zeroed(); // TODO use uninit
// Trim first the centre, then the list, then the right, then
// after the list, then the left.
while width_left + width_centre + width_right + width_list + width_after > available {
if width_centre > 0 {
width_centre -= 1;
} else if width_list > 0 {
width_list -= 1;
} else if width_right > 0 {
width_right -= 1;
} else if width_after > 0 {
width_after -= 1;
} else {
width_left -= 1;
}
}
// If there is no list left, pass off to the no list function.
if width_list == 0 {
screen_write_start(&raw mut ctx, right);
screen_write_fast_copy(&raw mut ctx, after, 0, 0, width_after, 1);
screen_write_stop(&raw mut ctx);
format_draw_none(
octx, available, ocx, ocy, left, centre, right, abs_centre, frs,
);
return;
}
// Write left at 0.
format_draw_put(octx, ocx, ocy, left, frs, 0, 0, width_left);
// Write after at available - width_after.
format_draw_put(
octx,
ocx,
ocy,
after,
frs,
available - width_after,
(*after).cx - width_after,
width_after,
);
// Write right at
// available - width_right - width_list - width_after.
format_draw_put(
octx,
ocx,
ocy,
right,
frs,
available - width_right - width_list - width_after,
0,
width_right,
);
// Write centre halfway between
// width_left
// and
// available - width_right - width_list - width_after.
format_draw_put(
octx,
ocx,
ocy,
centre,
frs,
width_left + ((available - width_right - width_list - width_after) - width_left) / 2
- width_centre / 2,
(*centre).cx / 2 - width_centre / 2,
width_centre,
);
// The list now goes from
// available - width_list - width_after
// to
// available - width_after
// If there is no focus given, keep the right in focus.
if focus_start == -1 || focus_end == -1 {
focus_start = 0;
focus_end = 0;
}
format_draw_put_list(
octx,
ocx,
ocy,
available - width_list - width_after,
width_list,
list,
list_left,
list_right,
focus_start,
focus_end,
frs,
);
// Write abs_centre in the perfect centre of all horizontal space.
if width_abs_centre > available {
width_abs_centre = available;
}
format_draw_put(
octx,
ocx,
ocy,
abs_centre,
frs,
(available - width_abs_centre) / 2,
0,
width_abs_centre,
);
}
}
/// C `vendor/tmux/format-draw.c:545`: `static void format_draw_absolute_centre(struct screen_write_ctx *octx, u_int available, u_int ocx, u_int ocy, struct screen *left, struct screen *centre, struct screen *right, struct screen *abs_centre, struct screen *list, struct screen *list_left, struct screen *list_right, struct screen *after, int focus_start, int focus_end, struct format_ranges *frs)`
unsafe fn format_draw_absolute_centre(
octx: *mut screen_write_ctx,
available: u32,
ocx: u32,
ocy: u32,
left: *mut screen,
centre: *mut screen,
right: *mut screen,
abs_centre: *mut screen,
list: *mut screen,
list_left: *mut screen,
list_right: *mut screen,
after: *mut screen,
mut focus_start: i32,
mut focus_end: i32,
frs: *mut format_ranges,
) {
unsafe {
let mut width_left: u32 = (*left).cx;
let mut width_centre: u32 = (*centre).cx;
let mut width_right: u32 = (*right).cx;
let mut width_abs_centre: u32 = (*abs_centre).cx;
let mut width_list: u32 = (*list).cx;
let mut width_after: u32 = (*after).cx;
// Trim first centre, then the right, then the left.
while width_left + width_centre + width_right > available {
if width_centre > 0 {
width_centre -= 1;
} else if width_right > 0 {
width_right -= 1;
} else {
width_left -= 1;
}
}
// We trim list after and abs_centre independently, as we are drawing
// them over the rest. Trim first the list, then after the list, then
// abs_centre.
while width_list + width_after + width_abs_centre > available {
if width_list > 0 {
width_list -= 1;
} else if width_after > 0 {
width_after -= 1;
} else {
width_abs_centre -= 1;
}
}
// Write left at 0.
format_draw_put(octx, ocx, ocy, left, frs, 0, 0, width_left);
// Write right at available - width_right.
format_draw_put(
octx,
ocx,
ocy,
right,
frs,
available - width_right,
(*right).cx - width_right,
width_right,
);
// Keep writing centre at the relative centre. Only the list is written
// in the absolute centre of the horizontal space.
let middle = width_left + ((available - width_right) - width_left) / 2;
// Write centre at
// middle - width_centre.
format_draw_put(
octx,
ocx,
ocy,
centre,
frs,
middle - width_centre,
0,
width_centre,
);
// If there is no focus given, keep the centre in focus.
if focus_start == -1 || focus_end == -1 {
focus_start = (*list).cx as i32 / 2;
focus_end = (*list).cx as i32 / 2;
}
// We centre abs_centre and the list together, so their shared centre is
// in the perfect centre of horizontal space.
let mut abs_centre_offset = (available - width_list - width_abs_centre) / 2;
// Write abs_centre before the list.
format_draw_put(
octx,
ocx,
ocy,
abs_centre,
frs,
abs_centre_offset,
0,
width_abs_centre,
);
abs_centre_offset += width_abs_centre;
// Draw the list in the absolute centre
format_draw_put_list(
octx,
ocx,
ocy,
abs_centre_offset,
width_list,
list,
list_left,
list_right,
focus_start,
focus_end,
frs,
);
abs_centre_offset += width_list;
// Write after at the end of the centre
format_draw_put(
octx,
ocx,
ocy,
after,
frs,
abs_centre_offset,
0,
width_after,
);
}
}
/// Get width and count of any leading #s.
/// C `vendor/tmux/format-draw.c:648`: `static const char *format_leading_hashes(const char *cp, u_int *n, u_int *width)`
unsafe fn format_leading_hashes(cp: *const u8, n: *mut u32, width: *mut u32) -> *const u8 {
unsafe {
*n = 0;
while *cp.add(*n as usize) == b'#' {
*n += 1;
}
if *n == 0 {
*width = 0;
return cp;
}
if *cp.add(*n as usize) != b'[' {
if (*n).is_multiple_of(2) {
*width = *n / 2;
} else {
*width = *n / 2 + 1;
}
return cp.add(*n as usize);
}
*width = *n / 2;
if (*n).is_multiple_of(2) {
// An even number of #s means that all #s are escaped, so not a
// style. The caller should not skip this. Return pointing to
// the [.
return cp.add(*n as usize);
}
// This is a style, so return pointing to the #.
cp.add(*n as usize - 1)
}
}
/// Draw multiple characters.
/// C `vendor/tmux/format-draw.c:678`: `static void format_draw_many(struct screen_write_ctx *ctx, struct style *sy, char ch, u_int n)`
unsafe fn format_draw_many(ctx: *mut screen_write_ctx, sy: *mut style, ch: u8, n: u32) {
unsafe {
utf8_set(&raw mut (*sy).gc.data, ch);
for _ in 0..n {
screen_write_cell(ctx, &raw mut (*sy).gc);
}
}
}
/// Draw a format to a screen.
/// C `vendor/tmux/format-draw.c:690`: `void format_draw(struct screen_write_ctx *octx, const struct grid_cell *base, u_int available, const char *expanded, struct style_ranges *srs, int default_colours)`
pub unsafe fn format_draw(
octx: *mut screen_write_ctx,
base: *const grid_cell,
available: c_uint,
expanded: &str,
srs: *mut style_ranges,
default_colours: c_int,
) {
let size = expanded.len() as u32;
let expanded = CString::new(expanded).unwrap(); // TODO FIXME extra allocation to
// avoid rewritting rest of
// function now
let func = "format_draw";
let mut __func__ = c!("format_draw");
unsafe {
#[derive(Copy, Clone, Eq, PartialEq)]
#[repr(u32)]
enum Current {
Left,
Centre,
Right,
AbsoluteCentre,
List,
ListLeft,
ListRight,
After,
}
const TOTAL: usize = Current::After as usize + 1;
let mut current = Current::Left;
let mut last = Current::Left;
static NAMES: [&str; TOTAL] = [
"LEFT",
"CENTRE",
"RIGHT",
"ABSOLUTE_CENTRE",
"LIST",
"LIST_LEFT",
"LIST_RIGHT",
"AFTER",
];
let os: *mut screen = (*octx).s;
let mut s: [screen; TOTAL] = zeroed();
let mut ctx: [screen_write_ctx; TOTAL] = zeroed();
let ocx: u32 = (*os).cx;
let ocy: u32 = (*os).cy;
let mut width: [u32; TOTAL] = [0; TOTAL];
let mut map: [Current; 5] = [
Current::Left,
Current::Left,
Current::Centre,
Current::Right,
Current::AbsoluteCentre,
];
let mut focus_start: i32 = -1;
let mut focus_end: i32 = -1;
let mut list_state: i32 = -1;
let mut fill = -1;
let mut list_align = style_align::STYLE_ALIGN_DEFAULT;
let mut gc: grid_cell = zeroed();
let mut current_default: grid_cell = zeroed();
let mut sy: style = zeroed();
let mut saved_sy: style = zeroed();
let ud: *mut utf8_data = &raw mut sy.gc.data;
let mut fr = null_mut();
let mut frs: format_ranges = zeroed();
memcpy__(&raw mut current_default, base);
style_set(&raw mut sy, &raw mut current_default);
tailq_init(&raw mut frs);
// log_debug("%s: %s", __func__, expanded);
// We build three screens for left, right, centre alignment, one for
// the list, one for anything after the list and two for the list left
// and right markers.
for i in 0..TOTAL {
screen_init(&raw mut s[i], size, 1, 0);
screen_write_start(&raw mut ctx[i], &raw mut s[i]);
screen_write_clearendofline(&raw mut ctx[i], current_default.bg as u32);
width[i] = 0;
}
'out: {
// Walk the string and add to the corresponding screens,
// parsing styles as we go.
let mut cp: *const u8 = expanded.as_ptr().cast();
while *cp != b'\0' {
// Handle sequences of #.
if *cp == b'#' && *cp.add(1) != b'[' && *cp.add(1) != b'\0' {
let mut n: u32 = 1;
while *cp.add(n as usize) == b'#' {
n += 1;
}
let even = n.is_multiple_of(2);
if *cp.add(n as usize) != b'[' {
cp = cp.add(n as usize);
n = n.div_ceil(2);
width[current as usize] += n;
format_draw_many(&raw mut ctx[current as usize], &raw mut sy, b'#', n);
continue;
}
cp = cp.add(if even { n as usize + 1 } else { n as usize - 1 });
if sy.ignore != 0 {
continue;
}
format_draw_many(&raw mut ctx[current as usize], &raw mut sy, b'#', n / 2);
width[current as usize] += n / 2;
if even {
utf8_set(ud, b'[');
screen_write_cell(&raw mut ctx[current as usize], &raw mut sy.gc);
width[current as usize] += 1;
}
continue;
}
// Is this not a style?
if *cp != b'#' || *cp.add(1) != b'[' || sy.ignore != 0 {
// See if this is a UTF-8 character.
let mut more = utf8_open(ud, *cp);
if more == utf8_state::UTF8_MORE {
while ({
cp = cp.add(1);
*cp != b'\0'
}) && more == utf8_state::UTF8_MORE
{
more = utf8_append(ud, *cp);
}
if more != utf8_state::UTF8_DONE {
cp = cp.wrapping_sub((*ud).have as usize);
}
}
// Not a UTF-8 character - ASCII or not valid.
if more != utf8_state::UTF8_DONE {
if *cp < 0x20 || *cp > 0x7e {
// Ignore nonprintable characters.
cp = cp.add(1);
continue;
}
utf8_set(ud, *cp);
cp = cp.add(1);
}
// Draw the cell to the current screen.
screen_write_cell(&raw mut ctx[current as u32 as usize], &raw mut sy.gc);
width[current as usize] += (*ud).width as u32;
continue;
}
// This is a style. Work out where the end is and parse it.
let end = format_skip(cp.add(2), c!("]"));
if end.is_null() {
// log_debug("%s: no terminating ] at '%s'", __func__, cp + 2);
for fr_ in tailq_foreach(&raw mut frs).map(NonNull::as_ptr) {
fr = fr_;
// TODO warning this seems to break the aliasing rules
format_free_range(&raw mut frs, fr);
}
break 'out;
}
let tmp: *mut u8 = xstrndup(cp.add(2), end.offset_from(cp.add(2)) as usize)
.as_ptr()
.cast();
style_copy(&raw mut saved_sy, &raw const sy);
if style_parse(&raw mut sy, &raw mut current_default, tmp) != 0 {
log_debug!("{}: invalid style '{}'", func, _s(tmp));
free_(tmp);
cp = end.add(1);
continue;
}
log_debug!(
"{}: style '{}' -> '{}'",
func,
_s(tmp),
_s(style_tostring(&raw const sy))
);
free_(tmp);
if default_colours != 0 {
sy.gc.bg = (*base).bg;
sy.gc.fg = (*base).fg;
}
// If this style has a fill colour, store it for later.
if sy.fill != 8 {
fill = sy.fill;
}
// If this style pushed or popped the default, update it.
if sy.default_type == style_default_type::STYLE_DEFAULT_PUSH {
memcpy__(&raw mut current_default, &raw const saved_sy.gc);
sy.default_type = style_default_type::STYLE_DEFAULT_BASE;
} else if sy.default_type == style_default_type::STYLE_DEFAULT_POP {
memcpy__(&raw mut current_default, base);
sy.default_type = style_default_type::STYLE_DEFAULT_BASE;
}
// Check the list state.
match sy.list {
style_list::STYLE_LIST_ON => {
// Entering the list, exiting a marker, or exiting the
// focus.
if list_state != 0 {
if !fr.is_null() {
// abort any region
free_(fr);
fr = null_mut();
}
list_state = 0;
list_align = sy.align;
}
// End the focus if started.
if focus_start != -1 && focus_end == -1 {
focus_end = s[Current::List as usize].cx as i32;
}
current = Current::List;
}
style_list::STYLE_LIST_FOCUS => {
// Entering the focus.
// note conditions are flipped from original c source because of break
// (list_state != 0 => not inside the list; focus already started => skip)
if list_state == 0 && focus_start == -1 {
focus_start = s[Current::List as usize].cx as i32;
}
}
style_list::STYLE_LIST_OFF => {
// Exiting or outside the list.
if list_state == 0 {
if !fr.is_null() {
// abort any region
free_(fr);
fr = null_mut();
}
if focus_start != -1 && focus_end == -1 {
focus_end = s[Current::List as usize].cx as i32;
}
map[list_align as usize] = Current::After;
if list_align == style_align::STYLE_ALIGN_LEFT {
map[style_align::STYLE_ALIGN_DEFAULT as usize] = Current::After;
}
list_state = 1;
}
current = map[sy.align as usize];
}
style_list::STYLE_LIST_LEFT_MARKER => {
// Entering left marker.
// note conditions are flipped from original c source because of break
// (list_state != 0 => not inside the list; cx != 0 => already have marker)
if list_state == 0 && s[Current::ListLeft as usize].cx == 0 {
if !fr.is_null() {
// abort any region
free_(fr);
fr = null_mut();
}
if focus_start != -1 && focus_end == -1 {
focus_start = -1;
focus_end = -1;
}
current = Current::ListLeft;
}
}
style_list::STYLE_LIST_RIGHT_MARKER => {
// note conditions are flipped from original c source because of break
if list_state == 0 && s[Current::ListRight as usize].cx == 0 {
if !fr.is_null() {
// abort any region
free_(fr);
fr = null_mut();
}
if focus_start != -1 && focus_end == -1 {
focus_start = -1;
focus_end = -1;
}
current = Current::ListRight;
}
}
}
if current != last {
log_debug!(
"{}: change {} -> {}",
func,
NAMES[last as usize],
NAMES[current as usize]
);
last = current;
}
// Check if the range style has changed and if so end the
// current range and start a new one if needed.
if !srs.is_null() {
if !fr.is_null() && !format_is_type(&*fr, &sy) {
if s[current as usize].cx != (*fr).start {
(*fr).end = s[current as usize].cx + 1;
tailq_insert_tail(&raw mut frs, fr);
} else {
free_(fr);
}
fr = null_mut();
}
if fr.is_null() && sy.range_type != style_range_type::STYLE_RANGE_NONE {
fr = xcalloc_(1).as_ptr();
(*fr).index = current as u32;
(*fr).s = &raw mut s[current as usize];
(*fr).start = s[current as usize].cx;
(*fr).type_ = sy.range_type;
(*fr).argument = sy.range_argument;
strlcpy(
(*fr).string.as_mut_ptr(),
sy.range_string.as_ptr(),
size_of::<[u8; 16]>(),
);
}
}
cp = end.add(1);
}
free_(fr);
for i in 0..TOTAL {
screen_write_stop(&raw mut ctx[i]);
log_debug!("{}: width {} is {}", func, NAMES[i], width[i]);
}
if focus_start != -1 && focus_end != -1 {
log_debug!("{}: focus {}-{}", func, focus_start, focus_end);
}
for fr in tailq_foreach(&raw mut frs).map(NonNull::as_ptr) {
log_debug!(
"{}: range {}|{} is {} {}-{}",
func,
(*fr).type_ as u32,
(*fr).argument,
NAMES[(*fr).index as usize],
(*fr).start,
(*fr).end
);
}
// Clear the available area.
if fill != -1 {
memcpy__(&raw mut gc, &raw const GRID_DEFAULT_CELL);
gc.bg = fill;
for _ in 0..available {
screen_write_putc(octx, &raw mut gc, b' ');
}
}
// Draw the screens. How they are arranged depends on where the list appears.
match list_align {
// No list.
style_align::STYLE_ALIGN_DEFAULT => format_draw_none(
octx,
available,
ocx,
ocy,
&raw mut s[Current::Left as usize],
&raw mut s[Current::Centre as usize],
&raw mut s[Current::Right as usize],
&raw mut s[Current::AbsoluteCentre as usize],
&raw mut frs,
),
// List is part of the left.
style_align::STYLE_ALIGN_LEFT => format_draw_left(
octx,
available,
ocx,
ocy,
&raw mut s[Current::Left as usize],
&raw mut s[Current::Centre as usize],
&raw mut s[Current::Right as usize],
&raw mut s[Current::AbsoluteCentre as usize],
&raw mut s[Current::List as usize],
&raw mut s[Current::ListLeft as usize],
&raw mut s[Current::ListRight as usize],
&raw mut s[Current::After as usize],
focus_start,
focus_end,
&raw mut frs,
),
// List is part of the centre.
style_align::STYLE_ALIGN_CENTRE => format_draw_centre(
octx,
available,
ocx,
ocy,
&raw mut s[Current::Left as usize],
&raw mut s[Current::Centre as usize],
&raw mut s[Current::Right as usize],
&raw mut s[Current::AbsoluteCentre as usize],
&raw mut s[Current::List as usize],
&raw mut s[Current::ListLeft as usize],
&raw mut s[Current::ListRight as usize],
&raw mut s[Current::After as usize],
focus_start,
focus_end,
&raw mut frs,
),
// List is part of the right.
style_align::STYLE_ALIGN_RIGHT => format_draw_right(
octx,
available,
ocx,
ocy,
&raw mut s[Current::Left as usize],
&raw mut s[Current::Centre as usize],
&raw mut s[Current::Right as usize],
&raw mut s[Current::AbsoluteCentre as usize],
&raw mut s[Current::List as usize],
&raw mut s[Current::ListLeft as usize],
&raw mut s[Current::ListRight as usize],
&raw mut s[Current::After as usize],
focus_start,
focus_end,
&raw mut frs,
),
// List is in the centre of the entire horizontal space.
style_align::STYLE_ALIGN_ABSOLUTE_CENTRE => format_draw_absolute_centre(
octx,
available,
ocx,
ocy,
&raw mut s[Current::Left as usize],
&raw mut s[Current::Centre as usize],
&raw mut s[Current::Right as usize],
&raw mut s[Current::AbsoluteCentre as usize],
&raw mut s[Current::List as usize],
&raw mut s[Current::ListLeft as usize],
&raw mut s[Current::ListRight as usize],
&raw mut s[Current::After as usize],
focus_start,
focus_end,
&raw mut frs,
),
}
// Create ranges to return.
for fr in tailq_foreach(&mut frs).map(NonNull::as_ptr) {
let sr = xcalloc1::<style_range>();
sr.type_ = (*fr).type_;
sr.argument = (*fr).argument;
strlcpy(
sr.string.as_mut_ptr(),
(*fr).string.as_ptr(),
size_of::<[u8; 16]>(),
);
sr.start = (*fr).start;
sr.end = (*fr).end;
tailq_insert_tail(srs, sr);
match sr.type_ {
style_range_type::STYLE_RANGE_NONE => (),
style_range_type::STYLE_RANGE_LEFT => {
log_debug!("{}: range left at {}-{}", func, sr.start, sr.end);
}
style_range_type::STYLE_RANGE_RIGHT => {
log_debug!("{}: range right at {}-{}", func, sr.start, sr.end);
}
style_range_type::STYLE_RANGE_PANE => {
log_debug!(
"{}: range pane|%%{} at {}-{}",
func,
sr.argument,
sr.start,
sr.end
);
}
style_range_type::STYLE_RANGE_WINDOW => {
log_debug!(
"{}: range window|{} at {}-{}",
func,
sr.argument,
sr.start,
sr.end
);
}
style_range_type::STYLE_RANGE_SESSION => {
log_debug!(
"{}: range session|${} at {}-{}",
func,
sr.argument,
sr.start,
sr.end
);
}
style_range_type::STYLE_RANGE_USER => {
log_debug!(
"{}: range user|{} at {}-{}",
func,
sr.argument,
sr.start,
sr.end
);
}
}
format_free_range(&raw mut frs, fr);
}
} // out:
// Free the screens.
for s_i in &mut s {
screen_free(s_i);
}
// Restore the original cursor position.
screen_write_cursormove(octx, ocx as i32, ocy as i32, 0);
}
}
/// Get width, taking #[] into account.
/// C `vendor/tmux/format-draw.c:1100`: `u_int format_width(const char *expanded)`
pub unsafe fn format_width(expanded: &str) -> u32 {
unsafe {
let expanded = CString::new(expanded).unwrap(); // TODO FIXME extra allocation to
// avoid rewritting rest of
// function now
let mut cp: *const u8 = expanded.as_ptr().cast();
let mut n: u32 = 0;
let mut leading_width: u32 = 0;
let mut width: u32 = 0;
let mut ud: utf8_data = zeroed();
while *cp != b'\0' {
if *cp == b'#' {
let mut end = format_leading_hashes(cp, &raw mut n, &raw mut leading_width);
width += leading_width;
cp = end;
if *cp == b'#' {
end = format_skip(cp.add(2), c!("]"));
if end.is_null() {
return 0;
}
cp = end.add(1);
}
} else if let mut more = utf8_open(&raw mut ud, *cp)
&& more == utf8_state::UTF8_MORE
{
while ({
cp = cp.add(1);
*cp != b'\0'
} && more == utf8_state::UTF8_MORE)
{
more = utf8_append(&raw mut ud, *cp);
}
if more == utf8_state::UTF8_DONE {
width += ud.width as u32;
}
} else if *cp > 0x1f && *cp < 0x7f {
width += 1;
cp = cp.add(1);
} else {
cp = cp.add(1);
}
}
width
}
}
/// Trim on the left, taking #[] into account.
///
/// Note, we copy the whole set of unescaped #s, but only add their escaped size to width.
/// This is because the `format_draw` function will actually do the escaping when it runs
/// C `vendor/tmux/format-draw.c:1139`: `char *format_trim_left(const char *expanded, u_int limit)`
pub unsafe fn format_trim_left(expanded: *const u8, limit: u32) -> *mut u8 {
unsafe {
let mut cp = expanded;
let mut n: u32 = 0;
let mut width: u32 = 0;
let mut leading_width: u32 = 0;
let mut ud: utf8_data = zeroed();
let mut out: *mut u8 = xcalloc(2, strlen(expanded) + 1).as_ptr().cast();
let copy = out;
while *cp != b'\0' {
if width >= limit {
break;
}
if *cp == b'#' {
let mut end = format_leading_hashes(cp, &raw mut n, &raw mut leading_width);
if leading_width > limit - width {
leading_width = limit - width;
}
if leading_width != 0 {
if n == 1 {
*out = b'#';
out = out.add(1);
} else {
libc::memset(out.cast(), b'#' as i32, 2 * leading_width as usize);
out = out.add(2 * leading_width as usize);
}
width += leading_width;
}
cp = end;
if *cp == b'#' {
end = format_skip(cp.add(2), c!("]"));
if end.is_null() {
break;
}
libc::memcpy(out.cast(), cp.cast(), end.add(1).offset_from(cp) as usize);
out = out.offset(end.add(1).offset_from(cp));
cp = end.add(1);
}
} else if let mut more = utf8_open(&raw mut ud, *cp)
&& more == utf8_state::UTF8_MORE
{
while ({
cp = cp.add(1);
*cp != b'\0'
}) && more == utf8_state::UTF8_MORE
{
more = utf8_append(&raw mut ud, *cp);
}
if more == utf8_state::UTF8_DONE {
if width + ud.width as u32 <= limit {
libc::memcpy(out.cast(), ud.data.as_ptr().cast(), ud.size as usize);
out = out.add(ud.size as usize);
}
width += ud.width as u32;
} else {
cp = cp.wrapping_sub(ud.have as usize).add(1);
}
} else if *cp > 0x1f && *cp < 0x7f {
if width < limit {
*out = *cp;
out = out.add(1);
}
width += 1;
cp = cp.add(1);
} else {
cp = cp.add(1);
}
}
*out = b'\0';
copy
}
}
/// Trim on the right, taking #[] into account.
/// C `vendor/tmux/format-draw.c:1200`: `char *format_trim_right(const char *expanded, u_int limit)`
pub unsafe fn format_trim_right(expanded: *const u8, limit: u32) -> *mut u8 {
unsafe {
let mut ud: utf8_data = std::mem::zeroed();
let mut width: u32 = 0;
let mut n: u32 = 0;
let mut leading_width: u32 = 0;
let mut copy_width: u32;
let mut cp = expanded;
// Invalid UTF-8: fall back to byte length (C's byte-based width counts
// each stray byte as ~1 column) instead of panicking in cstr_to_str.
let total_width: u32 = match cstr_to_str_(expanded) {
Some(v) => format_width(v),
None => strlen(expanded) as u32,
};
if total_width <= limit {
return xstrdup(expanded).as_ptr();
}
let skip: u32 = total_width - limit;
let mut out: *mut u8 = xcalloc(2, strlen(expanded) + 1).as_ptr().cast();
let copy: *mut u8 = out;
while *cp != b'\0' {
if *cp == b'#' {
let mut end: *const u8 =
format_leading_hashes(cp, &raw mut n, &raw mut leading_width);
copy_width = leading_width;
if width <= skip {
if skip - width >= copy_width {
copy_width = 0;
} else {
copy_width -= skip - width;
}
}
if copy_width != 0 {
if n == 1 {
*out = b'#';
out = out.add(1);
} else {
libc::memset(out.cast(), b'#' as i32, 2 * copy_width as usize);
out = out.add(2 * copy_width as usize);
}
}
width += leading_width;
cp = end;
if *cp == b'#' {
end = format_skip(cp.add(2), c!("]"));
if end.is_null() {
break;
}
libc::memcpy(out.cast(), cp.cast(), end.add(1).offset_from(cp) as usize);
out = out.offset(end.add(1).offset_from(cp));
cp = end.add(1);
}
} else if let mut more = utf8_open(&raw mut ud, *cp)
&& more == utf8_state::UTF8_MORE
{
while ({
cp = cp.add(1);
*(cp) != b'\0'
}) && more == utf8_state::UTF8_MORE
{
more = utf8_append(&raw mut ud, *cp);
}
if more == utf8_state::UTF8_DONE {
if width >= skip {
libc::memcpy(out.cast(), ud.data.as_ptr().cast(), ud.size as usize);
out = out.add(ud.size as usize);
}
width += ud.width as u32;
} else {
cp = cp.wrapping_sub(ud.have as usize).add(1);
}
} else if *cp > 0x1f && *cp < 0x7f {
if width >= skip {
*out = *cp;
out = out.add(1);
}
width += 1;
cp = cp.add(1);
} else {
cp = cp.add(1);
}
}
*out = b'\0';
copy
}
}
#[cfg(test)]
mod tests {
use super::*;
// Copy a NUL-terminated C string produced by a trim helper into an owned
// Vec<u8> (excluding the terminator), then free the xmalloc'd buffer.
unsafe fn take(p: *mut u8) -> Vec<u8> {
unsafe {
let v = CStr::from_ptr(p.cast()).to_bytes().to_vec();
free_(p);
v
}
}
// format_width counts display columns: plain ASCII is one column each, and
// control characters (< 0x20) contribute nothing.
#[test]
fn test_format_width_plain() {
unsafe {
assert_eq!(format_width(""), 0);
assert_eq!(format_width("hello"), 5);
// A tab (0x09) and other C0 controls are zero-width.
assert_eq!(format_width("a\tb"), 2);
}
}
// format_width skips a `#[...]` style block entirely (zero width) but
// counts the surrounding text.
#[test]
fn test_format_width_style_block() {
unsafe {
assert_eq!(format_width("#[fg=red]hi"), 2);
assert_eq!(format_width("a#[bold]b#[default]c"), 3);
}
}
// format_width: an escaped `##` (even run, not a style) collapses to n/2
// columns; the C draw pass renders it as a literal '#'.
#[test]
fn test_format_width_escaped_hashes() {
unsafe {
assert_eq!(format_width("a##b"), 3); // 'a' + '#' + 'b'
assert_eq!(format_width("##"), 1);
assert_eq!(format_width("####"), 2);
}
}
// format_trim_left keeps the leftmost `limit` columns.
#[test]
fn test_format_trim_left() {
unsafe {
assert_eq!(take(format_trim_left(crate::c!("hello"), 3)), b"hel");
// Limit at/above the width leaves the string untouched.
assert_eq!(take(format_trim_left(crate::c!("hello"), 5)), b"hello");
assert_eq!(take(format_trim_left(crate::c!("hello"), 10)), b"hello");
// Zero limit yields an empty string.
assert_eq!(take(format_trim_left(crate::c!("hello"), 0)), b"");
}
}
// format_trim_right keeps the rightmost `limit` columns; when the text
// already fits it is returned verbatim.
#[test]
fn test_format_trim_right() {
unsafe {
assert_eq!(take(format_trim_right(crate::c!("hello"), 3)), b"llo");
assert_eq!(take(format_trim_right(crate::c!("hello"), 5)), b"hello");
assert_eq!(take(format_trim_right(crate::c!("hello"), 10)), b"hello");
}
}
// A style block survives trimming even when it sits before the kept region:
// it has zero width, so trim_left copies it through without consuming limit.
#[test]
fn test_format_trim_left_preserves_style() {
unsafe {
let out = take(format_trim_left(crate::c!("#[fg=red]hello"), 3));
assert_eq!(out, b"#[fg=red]hel");
}
}
// Call format_leading_hashes on a C string and report (return offset, n,
// width) so the private helper's three outputs can be pinned.
unsafe fn leading(s: &[u8]) -> (isize, u32, u32) {
unsafe {
let cs = std::ffi::CString::new(s).unwrap();
let cp: *const u8 = cs.as_ptr().cast();
let mut n: u32 = 0;
let mut width: u32 = 0;
let end = format_leading_hashes(cp, &raw mut n, &raw mut width);
(end.offset_from(cp), n, width)
}
}
// format_leading_hashes (format-draw.c:1096): no leading '#' -> n=0, width=0,
// pointer unchanged.
#[test]
fn test_format_leading_hashes_none() {
unsafe {
assert_eq!(leading(b"abc"), (0, 0, 0));
}
}
// A run of hashes NOT followed by '[' is literal text: an even run of 2k is
// k columns, an odd run of 2k+1 is k+1 columns, and the pointer advances
// past the whole run.
#[test]
fn test_format_leading_hashes_literal_runs() {
unsafe {
// "#x": n=1 (odd), width = 1/2 + 1 = 1, end past the run (offset 1).
assert_eq!(leading(b"#x"), (1, 1, 1));
// "##x": n=2 (even), width = 1, end at offset 2.
assert_eq!(leading(b"##x"), (2, 2, 1));
// "####x": n=4 (even), width = 2, end at offset 4.
assert_eq!(leading(b"####x"), (4, 4, 2));
}
}
// A '[' after the hash run marks a style. An odd run is a real style: width
// = n/2 and the pointer is left on the LAST '#' (offset n-1) so the caller
// still sees a '#['. An even run is fully escaped (not a style): the pointer
// stops on the '[' (offset n).
#[test]
fn test_format_leading_hashes_style_vs_escaped() {
unsafe {
// "#[": n=1 (odd) -> style, width 0, pointer on the '#' (offset 0).
assert_eq!(leading(b"#["), (0, 1, 0));
// "###[": n=3 (odd) -> style, width 1, pointer on last '#' (offset 2).
assert_eq!(leading(b"###["), (2, 3, 1));
// "##[": n=2 (even) -> escaped, width 1, pointer on '[' (offset 2).
assert_eq!(leading(b"##["), (2, 2, 1));
}
}
// format_width returns 0 for an unterminated style block: format_skip finds
// no closing ']' and the function bails (format-draw.c:1122).
#[test]
fn test_format_width_unterminated_style() {
unsafe {
assert_eq!(format_width("#[fg=red"), 0);
assert_eq!(format_width("a#[nope"), 0);
}
}
// A style block on its own has zero display width.
#[test]
fn test_format_width_style_only() {
unsafe {
assert_eq!(format_width("#[fg=red,bold]"), 0);
assert_eq!(format_width("#[default]"), 0);
}
}
// format_width: control characters (< 0x20) and DEL-range bytes (>= 0x7f)
// are zero-width; only printable ASCII 0x20..0x7e counts.
#[test]
fn test_format_width_control_bytes() {
unsafe {
// Bell, backspace, escape are all zero-width.
assert_eq!(format_width("a\x07\x08\x1bb"), 2);
// A leading space (0x20) does count as one column.
assert_eq!(format_width(" x "), 3);
}
}
// format_trim_left keeps an escaped "##" run: it counts as one column but is
// emitted as two bytes because format_draw will re-escape it later
// (format-draw.c:1155 memset of 2*leading_width).
#[test]
fn test_format_trim_left_escaped_hashes() {
unsafe {
// 'a' (1 col) + "##" (1 col) fills limit 2; output keeps both hashes.
assert_eq!(take(format_trim_left(crate::c!("a##b"), 2)), b"a##");
}
}
// format_trim_right keeps the rightmost columns while carrying a leading
// zero-width style block through untouched (format-draw.c:1200).
#[test]
fn test_format_trim_right_preserves_style() {
unsafe {
let out = take(format_trim_right(crate::c!("#[fg=red]hello"), 3));
assert_eq!(out, b"#[fg=red]llo");
}
}
// format_trim_right with a zero limit drops all display columns, leaving an
// empty string (every char is skipped because width < skip throughout).
#[test]
fn test_format_trim_right_zero_limit() {
unsafe {
assert_eq!(take(format_trim_right(crate::c!("hello"), 0)), b"");
}
}
// format_trim_left at exactly the string width is a full copy; one column
// short drops the last character.
#[test]
fn test_format_trim_left_boundary() {
unsafe {
assert_eq!(take(format_trim_left(crate::c!("abcd"), 4)), b"abcd");
assert_eq!(take(format_trim_left(crate::c!("abcd"), 3)), b"abc");
}
}
// format_width and format_trim agree: trimming to the full width yields the
// original, and format_width of the trimmed result never exceeds the limit.
#[test]
fn test_format_width_trim_consistency() {
unsafe {
let s = crate::c!("hello world");
assert_eq!(format_width("hello world"), 11);
for limit in 0u32..=11 {
let left = take(format_trim_left(s, limit));
let ls = std::str::from_utf8(&left).unwrap();
assert!(format_width(ls) <= limit, "left limit {limit}: {ls:?}");
}
}
}
}