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
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
|
"""
Container for the layout.
(Containers can contain other containers or user interface controls.)
"""
from __future__ import annotations
from abc import ABCMeta, abstractmethod
from enum import Enum
from functools import partial
from typing import TYPE_CHECKING, Callable, Sequence, Union, cast
from prompt_toolkit.application.current import get_app
from prompt_toolkit.cache import SimpleCache
from prompt_toolkit.data_structures import Point
from prompt_toolkit.filters import (
FilterOrBool,
emacs_insert_mode,
to_filter,
vi_insert_mode,
)
from prompt_toolkit.formatted_text import (
AnyFormattedText,
StyleAndTextTuples,
to_formatted_text,
)
from prompt_toolkit.formatted_text.utils import (
fragment_list_to_text,
fragment_list_width,
)
from prompt_toolkit.key_binding import KeyBindingsBase
from prompt_toolkit.mouse_events import MouseEvent, MouseEventType
from prompt_toolkit.utils import get_cwidth, take_using_weights, to_int, to_str
from .controls import (
DummyControl,
FormattedTextControl,
GetLinePrefixCallable,
UIContent,
UIControl,
)
from .dimension import (
AnyDimension,
Dimension,
max_layout_dimensions,
sum_layout_dimensions,
to_dimension,
)
from .margins import Margin
from .mouse_handlers import MouseHandlers
from .screen import _CHAR_CACHE, Screen, WritePosition
from .utils import explode_text_fragments
if TYPE_CHECKING:
from typing_extensions import Protocol, TypeGuard
from prompt_toolkit.key_binding.key_bindings import NotImplementedOrNone
__all__ = [
"AnyContainer",
"Container",
"HorizontalAlign",
"VerticalAlign",
"HSplit",
"VSplit",
"FloatContainer",
"Float",
"WindowAlign",
"Window",
"WindowRenderInfo",
"ConditionalContainer",
"ScrollOffsets",
"ColorColumn",
"to_container",
"to_window",
"is_container",
"DynamicContainer",
]
class Container(metaclass=ABCMeta):
"""
Base class for user interface layout.
"""
@abstractmethod
def reset(self) -> None:
"""
Reset the state of this container and all the children.
(E.g. reset scroll offsets, etc...)
"""
@abstractmethod
def preferred_width(self, max_available_width: int) -> Dimension:
"""
Return a :class:`~prompt_toolkit.layout.Dimension` that represents the
desired width for this container.
"""
@abstractmethod
def preferred_height(self, width: int, max_available_height: int) -> Dimension:
"""
Return a :class:`~prompt_toolkit.layout.Dimension` that represents the
desired height for this container.
"""
@abstractmethod
def write_to_screen(
self,
screen: Screen,
mouse_handlers: MouseHandlers,
write_position: WritePosition,
parent_style: str,
erase_bg: bool,
z_index: int | None,
) -> None:
"""
Write the actual content to the screen.
:param screen: :class:`~prompt_toolkit.layout.screen.Screen`
:param mouse_handlers: :class:`~prompt_toolkit.layout.mouse_handlers.MouseHandlers`.
:param parent_style: Style string to pass to the :class:`.Window`
object. This will be applied to all content of the windows.
:class:`.VSplit` and :class:`.HSplit` can use it to pass their
style down to the windows that they contain.
:param z_index: Used for propagating z_index from parent to child.
"""
def is_modal(self) -> bool:
"""
When this container is modal, key bindings from parent containers are
not taken into account if a user control in this container is focused.
"""
return False
def get_key_bindings(self) -> KeyBindingsBase | None:
"""
Returns a :class:`.KeyBindings` object. These bindings become active when any
user control in this container has the focus, except if any containers
between this container and the focused user control is modal.
"""
return None
@abstractmethod
def get_children(self) -> list[Container]:
"""
Return the list of child :class:`.Container` objects.
"""
return []
if TYPE_CHECKING:
class MagicContainer(Protocol):
"""
Any object that implements ``__pt_container__`` represents a container.
"""
def __pt_container__(self) -> AnyContainer: ...
AnyContainer = Union[Container, "MagicContainer"]
def _window_too_small() -> Window:
"Create a `Window` that displays the 'Window too small' text."
return Window(
FormattedTextControl(text=[("class:window-too-small", " Window too small... ")])
)
class VerticalAlign(Enum):
"Alignment for `HSplit`."
TOP = "TOP"
CENTER = "CENTER"
BOTTOM = "BOTTOM"
JUSTIFY = "JUSTIFY"
class HorizontalAlign(Enum):
"Alignment for `VSplit`."
LEFT = "LEFT"
CENTER = "CENTER"
RIGHT = "RIGHT"
JUSTIFY = "JUSTIFY"
class _Split(Container):
"""
The common parts of `VSplit` and `HSplit`.
"""
def __init__(
self,
children: Sequence[AnyContainer],
window_too_small: Container | None = None,
padding: AnyDimension = Dimension.exact(0),
padding_char: str | None = None,
padding_style: str = "",
width: AnyDimension = None,
height: AnyDimension = None,
z_index: int | None = None,
modal: bool = False,
key_bindings: KeyBindingsBase | None = None,
style: str | Callable[[], str] = "",
) -> None:
self.children = [to_container(c) for c in children]
self.window_too_small = window_too_small or _window_too_small()
self.padding = padding
self.padding_char = padding_char
self.padding_style = padding_style
self.width = width
self.height = height
self.z_index = z_index
self.modal = modal
self.key_bindings = key_bindings
self.style = style
def is_modal(self) -> bool:
return self.modal
def get_key_bindings(self) -> KeyBindingsBase | None:
return self.key_bindings
def get_children(self) -> list[Container]:
return self.children
class HSplit(_Split):
"""
Several layouts, one stacked above/under the other. ::
+--------------------+
| |
+--------------------+
| |
+--------------------+
By default, this doesn't display a horizontal line between the children,
but if this is something you need, then create a HSplit as follows::
HSplit(children=[ ... ], padding_char='-',
padding=1, padding_style='#ffff00')
:param children: List of child :class:`.Container` objects.
:param window_too_small: A :class:`.Container` object that is displayed if
there is not enough space for all the children. By default, this is a
"Window too small" message.
:param align: `VerticalAlign` value.
:param width: When given, use this width instead of looking at the children.
:param height: When given, use this height instead of looking at the children.
:param z_index: (int or None) When specified, this can be used to bring
element in front of floating elements. `None` means: inherit from parent.
:param style: A style string.
:param modal: ``True`` or ``False``.
:param key_bindings: ``None`` or a :class:`.KeyBindings` object.
:param padding: (`Dimension` or int), size to be used for the padding.
:param padding_char: Character to be used for filling in the padding.
:param padding_style: Style to applied to the padding.
"""
def __init__(
self,
children: Sequence[AnyContainer],
window_too_small: Container | None = None,
align: VerticalAlign = VerticalAlign.JUSTIFY,
padding: AnyDimension = 0,
padding_char: str | None = None,
padding_style: str = "",
width: AnyDimension = None,
height: AnyDimension = None,
z_index: int | None = None,
modal: bool = False,
key_bindings: KeyBindingsBase | None = None,
style: str | Callable[[], str] = "",
) -> None:
super().__init__(
children=children,
window_too_small=window_too_small,
padding=padding,
padding_char=padding_char,
padding_style=padding_style,
width=width,
height=height,
z_index=z_index,
modal=modal,
key_bindings=key_bindings,
style=style,
)
self.align = align
self._children_cache: SimpleCache[tuple[Container, ...], list[Container]] = (
SimpleCache(maxsize=1)
)
self._remaining_space_window = Window() # Dummy window.
def preferred_width(self, max_available_width: int) -> Dimension:
if self.width is not None:
return to_dimension(self.width)
if self.children:
dimensions = [c.preferred_width(max_available_width) for c in self.children]
return max_layout_dimensions(dimensions)
else:
return Dimension()
def preferred_height(self, width: int, max_available_height: int) -> Dimension:
if self.height is not None:
return to_dimension(self.height)
dimensions = [
c.preferred_height(width, max_available_height) for c in self._all_children
]
return sum_layout_dimensions(dimensions)
def reset(self) -> None:
for c in self.children:
c.reset()
@property
def _all_children(self) -> list[Container]:
"""
List of child objects, including padding.
"""
def get() -> list[Container]:
result: list[Container] = []
# Padding Top.
if self.align in (VerticalAlign.CENTER, VerticalAlign.BOTTOM):
result.append(Window(width=Dimension(preferred=0)))
# The children with padding.
for child in self.children:
result.append(child)
result.append(
Window(
height=self.padding,
char=self.padding_char,
style=self.padding_style,
)
)
if result:
result.pop()
# Padding right.
if self.align in (VerticalAlign.CENTER, VerticalAlign.TOP):
result.append(Window(width=Dimension(preferred=0)))
return result
return self._children_cache.get(tuple(self.children), get)
def write_to_screen(
self,
screen: Screen,
mouse_handlers: MouseHandlers,
write_position: WritePosition,
parent_style: str,
erase_bg: bool,
z_index: int | None,
) -> None:
"""
Render the prompt to a `Screen` instance.
:param screen: The :class:`~prompt_toolkit.layout.screen.Screen` class
to which the output has to be written.
"""
sizes = self._divide_heights(write_position)
style = parent_style + " " + to_str(self.style)
z_index = z_index if self.z_index is None else self.z_index
if sizes is None:
self.window_too_small.write_to_screen(
screen, mouse_handlers, write_position, style, erase_bg, z_index
)
else:
#
ypos = write_position.ypos
xpos = write_position.xpos
width = write_position.width
# Draw child panes.
for s, c in zip(sizes, self._all_children):
c.write_to_screen(
screen,
mouse_handlers,
WritePosition(xpos, ypos, width, s),
style,
erase_bg,
z_index,
)
ypos += s
# Fill in the remaining space. This happens when a child control
# refuses to take more space and we don't have any padding. Adding a
# dummy child control for this (in `self._all_children`) is not
# desired, because in some situations, it would take more space, even
# when it's not required. This is required to apply the styling.
remaining_height = write_position.ypos + write_position.height - ypos
if remaining_height > 0:
self._remaining_space_window.write_to_screen(
screen,
mouse_handlers,
WritePosition(xpos, ypos, width, remaining_height),
style,
erase_bg,
z_index,
)
def _divide_heights(self, write_position: WritePosition) -> list[int] | None:
"""
Return the heights for all rows.
Or None when there is not enough space.
"""
if not self.children:
return []
width = write_position.width
height = write_position.height
# Calculate heights.
dimensions = [c.preferred_height(width, height) for c in self._all_children]
# Sum dimensions
sum_dimensions = sum_layout_dimensions(dimensions)
# If there is not enough space for both.
# Don't do anything.
if sum_dimensions.min > height:
return None
# Find optimal sizes. (Start with minimal size, increase until we cover
# the whole height.)
sizes = [d.min for d in dimensions]
child_generator = take_using_weights(
items=list(range(len(dimensions))), weights=[d.weight for d in dimensions]
)
i = next(child_generator)
# Increase until we meet at least the 'preferred' size.
preferred_stop = min(height, sum_dimensions.preferred)
preferred_dimensions = [d.preferred for d in dimensions]
while sum(sizes) < preferred_stop:
if sizes[i] < preferred_dimensions[i]:
sizes[i] += 1
i = next(child_generator)
# Increase until we use all the available space. (or until "max")
if not get_app().is_done:
max_stop = min(height, sum_dimensions.max)
max_dimensions = [d.max for d in dimensions]
while sum(sizes) < max_stop:
if sizes[i] < max_dimensions[i]:
sizes[i] += 1
i = next(child_generator)
return sizes
class VSplit(_Split):
"""
Several layouts, one stacked left/right of the other. ::
+---------+----------+
| | |
| | |
+---------+----------+
By default, this doesn't display a vertical line between the children, but
if this is something you need, then create a HSplit as follows::
VSplit(children=[ ... ], padding_char='|',
padding=1, padding_style='#ffff00')
:param children: List of child :class:`.Container` objects.
:param window_too_small: A :class:`.Container` object that is displayed if
there is not enough space for all the children. By default, this is a
"Window too small" message.
:param align: `HorizontalAlign` value.
:param width: When given, use this width instead of looking at the children.
:param height: When given, use this height instead of looking at the children.
:param z_index: (int or None) When specified, this can be used to bring
element in front of floating elements. `None` means: inherit from parent.
:param style: A style string.
:param modal: ``True`` or ``False``.
:param key_bindings: ``None`` or a :class:`.KeyBindings` object.
:param padding: (`Dimension` or int), size to be used for the padding.
:param padding_char: Character to be used for filling in the padding.
:param padding_style: Style to applied to the padding.
"""
def __init__(
self,
children: Sequence[AnyContainer],
window_too_small: Container | None = None,
align: HorizontalAlign = HorizontalAlign.JUSTIFY,
padding: AnyDimension = 0,
padding_char: str | None = None,
padding_style: str = "",
width: AnyDimension = None,
height: AnyDimension = None,
z_index: int | None = None,
modal: bool = False,
key_bindings: KeyBindingsBase | None = None,
style: str | Callable[[], str] = "",
) -> None:
super().__init__(
children=children,
window_too_small=window_too_small,
padding=padding,
padding_char=padding_char,
padding_style=padding_style,
width=width,
height=height,
z_index=z_index,
modal=modal,
key_bindings=key_bindings,
style=style,
)
self.align = align
self._children_cache: SimpleCache[tuple[Container, ...], list[Container]] = (
SimpleCache(maxsize=1)
)
self._remaining_space_window = Window() # Dummy window.
def preferred_width(self, max_available_width: int) -> Dimension:
if self.width is not None:
return to_dimension(self.width)
dimensions = [
c.preferred_width(max_available_width) for c in self._all_children
]
return sum_layout_dimensions(dimensions)
def preferred_height(self, width: int, max_available_height: int) -> Dimension:
if self.height is not None:
return to_dimension(self.height)
# At the point where we want to calculate the heights, the widths have
# already been decided. So we can trust `width` to be the actual
# `width` that's going to be used for the rendering. So,
# `divide_widths` is supposed to use all of the available width.
# Using only the `preferred` width caused a bug where the reported
# height was more than required. (we had a `BufferControl` which did
# wrap lines because of the smaller width returned by `_divide_widths`.
sizes = self._divide_widths(width)
children = self._all_children
if sizes is None:
return Dimension()
else:
dimensions = [
c.preferred_height(s, max_available_height)
for s, c in zip(sizes, children)
]
return max_layout_dimensions(dimensions)
def reset(self) -> None:
for c in self.children:
c.reset()
@property
def _all_children(self) -> list[Container]:
"""
List of child objects, including padding.
"""
def get() -> list[Container]:
result: list[Container] = []
# Padding left.
if self.align in (HorizontalAlign.CENTER, HorizontalAlign.RIGHT):
result.append(Window(width=Dimension(preferred=0)))
# The children with padding.
for child in self.children:
result.append(child)
result.append(
Window(
width=self.padding,
char=self.padding_char,
style=self.padding_style,
)
)
if result:
result.pop()
# Padding right.
if self.align in (HorizontalAlign.CENTER, HorizontalAlign.LEFT):
result.append(Window(width=Dimension(preferred=0)))
return result
return self._children_cache.get(tuple(self.children), get)
def _divide_widths(self, width: int) -> list[int] | None:
"""
Return the widths for all columns.
Or None when there is not enough space.
"""
children = self._all_children
if not children:
return []
# Calculate widths.
dimensions = [c.preferred_width(width) for c in children]
preferred_dimensions = [d.preferred for d in dimensions]
# Sum dimensions
sum_dimensions = sum_layout_dimensions(dimensions)
# If there is not enough space for both.
# Don't do anything.
if sum_dimensions.min > width:
return None
# Find optimal sizes. (Start with minimal size, increase until we cover
# the whole width.)
sizes = [d.min for d in dimensions]
child_generator = take_using_weights(
items=list(range(len(dimensions))), weights=[d.weight for d in dimensions]
)
i = next(child_generator)
# Increase until we meet at least the 'preferred' size.
preferred_stop = min(width, sum_dimensions.preferred)
while sum(sizes) < preferred_stop:
if sizes[i] < preferred_dimensions[i]:
sizes[i] += 1
i = next(child_generator)
# Increase until we use all the available space.
max_dimensions = [d.max for d in dimensions]
max_stop = min(width, sum_dimensions.max)
while sum(sizes) < max_stop:
if sizes[i] < max_dimensions[i]:
sizes[i] += 1
i = next(child_generator)
return sizes
def write_to_screen(
self,
screen: Screen,
mouse_handlers: MouseHandlers,
write_position: WritePosition,
parent_style: str,
erase_bg: bool,
z_index: int | None,
) -> None:
"""
Render the prompt to a `Screen` instance.
:param screen: The :class:`~prompt_toolkit.layout.screen.Screen` class
to which the output has to be written.
"""
if not self.children:
return
children = self._all_children
sizes = self._divide_widths(write_position.width)
style = parent_style + " " + to_str(self.style)
z_index = z_index if self.z_index is None else self.z_index
# If there is not enough space.
if sizes is None:
self.window_too_small.write_to_screen(
screen, mouse_handlers, write_position, style, erase_bg, z_index
)
return
# Calculate heights, take the largest possible, but not larger than
# write_position.height.
heights = [
child.preferred_height(width, write_position.height).preferred
for width, child in zip(sizes, children)
]
height = max(write_position.height, min(write_position.height, max(heights)))
#
ypos = write_position.ypos
xpos = write_position.xpos
# Draw all child panes.
for s, c in zip(sizes, children):
c.write_to_screen(
screen,
mouse_handlers,
WritePosition(xpos, ypos, s, height),
style,
erase_bg,
z_index,
)
xpos += s
# Fill in the remaining space. This happens when a child control
# refuses to take more space and we don't have any padding. Adding a
# dummy child control for this (in `self._all_children`) is not
# desired, because in some situations, it would take more space, even
# when it's not required. This is required to apply the styling.
remaining_width = write_position.xpos + write_position.width - xpos
if remaining_width > 0:
self._remaining_space_window.write_to_screen(
screen,
mouse_handlers,
WritePosition(xpos, ypos, remaining_width, height),
style,
erase_bg,
z_index,
)
class FloatContainer(Container):
"""
Container which can contain another container for the background, as well
as a list of floating containers on top of it.
Example Usage::
FloatContainer(content=Window(...),
floats=[
Float(xcursor=True,
ycursor=True,
content=CompletionsMenu(...))
])
:param z_index: (int or None) When specified, this can be used to bring
element in front of floating elements. `None` means: inherit from parent.
This is the z_index for the whole `Float` container as a whole.
"""
def __init__(
self,
content: AnyContainer,
floats: list[Float],
modal: bool = False,
key_bindings: KeyBindingsBase | None = None,
style: str | Callable[[], str] = "",
z_index: int | None = None,
) -> None:
self.content = to_container(content)
self.floats = floats
self.modal = modal
self.key_bindings = key_bindings
self.style = style
self.z_index = z_index
def reset(self) -> None:
self.content.reset()
for f in self.floats:
f.content.reset()
def preferred_width(self, max_available_width: int) -> Dimension:
return self.content.preferred_width(max_available_width)
def preferred_height(self, width: int, max_available_height: int) -> Dimension:
"""
Return the preferred height of the float container.
(We don't care about the height of the floats, they should always fit
into the dimensions provided by the container.)
"""
return self.content.preferred_height(width, max_available_height)
def write_to_screen(
self,
screen: Screen,
mouse_handlers: MouseHandlers,
write_position: WritePosition,
parent_style: str,
erase_bg: bool,
z_index: int | None,
) -> None:
style = parent_style + " " + to_str(self.style)
z_index = z_index if self.z_index is None else self.z_index
self.content.write_to_screen(
screen, mouse_handlers, write_position, style, erase_bg, z_index
)
for number, fl in enumerate(self.floats):
# z_index of a Float is computed by summing the z_index of the
# container and the `Float`.
new_z_index = (z_index or 0) + fl.z_index
style = parent_style + " " + to_str(self.style)
# If the float that we have here, is positioned relative to the
# cursor position, but the Window that specifies the cursor
# position is not drawn yet, because it's a Float itself, we have
# to postpone this calculation. (This is a work-around, but good
# enough for now.)
postpone = fl.xcursor is not None or fl.ycursor is not None
if postpone:
new_z_index = (
number + 10**8
) # Draw as late as possible, but keep the order.
screen.draw_with_z_index(
z_index=new_z_index,
draw_func=partial(
self._draw_float,
fl,
screen,
mouse_handlers,
write_position,
style,
erase_bg,
new_z_index,
),
)
else:
self._draw_float(
fl,
screen,
mouse_handlers,
write_position,
style,
erase_bg,
new_z_index,
)
def _draw_float(
self,
fl: Float,
screen: Screen,
mouse_handlers: MouseHandlers,
write_position: WritePosition,
style: str,
erase_bg: bool,
z_index: int | None,
) -> None:
"Draw a single Float."
# When a menu_position was given, use this instead of the cursor
# position. (These cursor positions are absolute, translate again
# relative to the write_position.)
# Note: This should be inside the for-loop, because one float could
# set the cursor position to be used for the next one.
cpos = screen.get_menu_position(
fl.attach_to_window or get_app().layout.current_window
)
cursor_position = Point(
x=cpos.x - write_position.xpos, y=cpos.y - write_position.ypos
)
fl_width = fl.get_width()
fl_height = fl.get_height()
width: int
height: int
xpos: int
ypos: int
# Left & width given.
if fl.left is not None and fl_width is not None:
xpos = fl.left
width = fl_width
# Left & right given -> calculate width.
elif fl.left is not None and fl.right is not None:
xpos = fl.left
width = write_position.width - fl.left - fl.right
# Width & right given -> calculate left.
elif fl_width is not None and fl.right is not None:
xpos = write_position.width - fl.right - fl_width
width = fl_width
# Near x position of cursor.
elif fl.xcursor:
if fl_width is None:
width = fl.content.preferred_width(write_position.width).preferred
width = min(write_position.width, width)
else:
width = fl_width
xpos = cursor_position.x
if xpos + width > write_position.width:
xpos = max(0, write_position.width - width)
# Only width given -> center horizontally.
elif fl_width:
xpos = int((write_position.width - fl_width) / 2)
width = fl_width
# Otherwise, take preferred width from float content.
else:
width = fl.content.preferred_width(write_position.width).preferred
if fl.left is not None:
xpos = fl.left
elif fl.right is not None:
xpos = max(0, write_position.width - width - fl.right)
else: # Center horizontally.
xpos = max(0, int((write_position.width - width) / 2))
# Trim.
width = min(width, write_position.width - xpos)
# Top & height given.
if fl.top is not None and fl_height is not None:
ypos = fl.top
height = fl_height
# Top & bottom given -> calculate height.
elif fl.top is not None and fl.bottom is not None:
ypos = fl.top
height = write_position.height - fl.top - fl.bottom
# Height & bottom given -> calculate top.
elif fl_height is not None and fl.bottom is not None:
ypos = write_position.height - fl_height - fl.bottom
height = fl_height
# Near cursor.
elif fl.ycursor:
ypos = cursor_position.y + (0 if fl.allow_cover_cursor else 1)
if fl_height is None:
height = fl.content.preferred_height(
width, write_position.height
).preferred
else:
height = fl_height
# Reduce height if not enough space. (We can use the height
# when the content requires it.)
if height > write_position.height - ypos:
if write_position.height - ypos + 1 >= ypos:
# When the space below the cursor is more than
# the space above, just reduce the height.
height = write_position.height - ypos
else:
# Otherwise, fit the float above the cursor.
height = min(height, cursor_position.y)
ypos = cursor_position.y - height
# Only height given -> center vertically.
elif fl_height:
ypos = int((write_position.height - fl_height) / 2)
height = fl_height
# Otherwise, take preferred height from content.
else:
height = fl.content.preferred_height(width, write_position.height).preferred
if fl.top is not None:
ypos = fl.top
elif fl.bottom is not None:
ypos = max(0, write_position.height - height - fl.bottom)
else: # Center vertically.
ypos = max(0, int((write_position.height - height) / 2))
# Trim.
height = min(height, write_position.height - ypos)
# Write float.
# (xpos and ypos can be negative: a float can be partially visible.)
if height > 0 and width > 0:
wp = WritePosition(
xpos=xpos + write_position.xpos,
ypos=ypos + write_position.ypos,
width=width,
height=height,
)
if not fl.hide_when_covering_content or self._area_is_empty(screen, wp):
fl.content.write_to_screen(
screen,
mouse_handlers,
wp,
style,
erase_bg=not fl.transparent(),
z_index=z_index,
)
def _area_is_empty(self, screen: Screen, write_position: WritePosition) -> bool:
"""
Return True when the area below the write position is still empty.
(For floats that should not hide content underneath.)
"""
wp = write_position
for y in range(wp.ypos, wp.ypos + wp.height):
if y in screen.data_buffer:
row = screen.data_buffer[y]
for x in range(wp.xpos, wp.xpos + wp.width):
c = row[x]
if c.char != " ":
return False
return True
def is_modal(self) -> bool:
return self.modal
def get_key_bindings(self) -> KeyBindingsBase | None:
return self.key_bindings
def get_children(self) -> list[Container]:
children = [self.content]
children.extend(f.content for f in self.floats)
return children
class Float:
"""
Float for use in a :class:`.FloatContainer`.
Except for the `content` parameter, all other options are optional.
:param content: :class:`.Container` instance.
:param width: :class:`.Dimension` or callable which returns a :class:`.Dimension`.
:param height: :class:`.Dimension` or callable which returns a :class:`.Dimension`.
:param left: Distance to the left edge of the :class:`.FloatContainer`.
:param right: Distance to the right edge of the :class:`.FloatContainer`.
:param top: Distance to the top of the :class:`.FloatContainer`.
:param bottom: Distance to the bottom of the :class:`.FloatContainer`.
:param attach_to_window: Attach to the cursor from this window, instead of
the current window.
:param hide_when_covering_content: Hide the float when it covers content underneath.
:param allow_cover_cursor: When `False`, make sure to display the float
below the cursor. Not on top of the indicated position.
:param z_index: Z-index position. For a Float, this needs to be at least
one. It is relative to the z_index of the parent container.
:param transparent: :class:`.Filter` indicating whether this float needs to be
drawn transparently.
"""
def __init__(
self,
content: AnyContainer,
top: int | None = None,
right: int | None = None,
bottom: int | None = None,
left: int | None = None,
width: int | Callable[[], int] | None = None,
height: int | Callable[[], int] | None = None,
xcursor: bool = False,
ycursor: bool = False,
attach_to_window: AnyContainer | None = None,
hide_when_covering_content: bool = False,
allow_cover_cursor: bool = False,
z_index: int = 1,
transparent: bool = False,
) -> None:
assert z_index >= 1
self.left = left
self.right = right
self.top = top
self.bottom = bottom
self.width = width
self.height = height
self.xcursor = xcursor
self.ycursor = ycursor
self.attach_to_window = (
to_window(attach_to_window) if attach_to_window else None
)
self.content = to_container(content)
self.hide_when_covering_content = hide_when_covering_content
self.allow_cover_cursor = allow_cover_cursor
self.z_index = z_index
self.transparent = to_filter(transparent)
def get_width(self) -> int | None:
if callable(self.width):
return self.width()
return self.width
def get_height(self) -> int | None:
if callable(self.height):
return self.height()
return self.height
def __repr__(self) -> str:
return f"Float(content={self.content!r})"
class WindowRenderInfo:
"""
Render information for the last render time of this control.
It stores mapping information between the input buffers (in case of a
:class:`~prompt_toolkit.layout.controls.BufferControl`) and the actual
render position on the output screen.
(Could be used for implementation of the Vi 'H' and 'L' key bindings as
well as implementing mouse support.)
:param ui_content: The original :class:`.UIContent` instance that contains
the whole input, without clipping. (ui_content)
:param horizontal_scroll: The horizontal scroll of the :class:`.Window` instance.
:param vertical_scroll: The vertical scroll of the :class:`.Window` instance.
:param window_width: The width of the window that displays the content,
without the margins.
:param window_height: The height of the window that displays the content.
:param configured_scroll_offsets: The scroll offsets as configured for the
:class:`Window` instance.
:param visible_line_to_row_col: Mapping that maps the row numbers on the
displayed screen (starting from zero for the first visible line) to
(row, col) tuples pointing to the row and column of the :class:`.UIContent`.
:param rowcol_to_yx: Mapping that maps (row, column) tuples representing
coordinates of the :class:`UIContent` to (y, x) absolute coordinates at
the rendered screen.
"""
def __init__(
self,
window: Window,
ui_content: UIContent,
horizontal_scroll: int,
vertical_scroll: int,
window_width: int,
window_height: int,
configured_scroll_offsets: ScrollOffsets,
visible_line_to_row_col: dict[int, tuple[int, int]],
rowcol_to_yx: dict[tuple[int, int], tuple[int, int]],
x_offset: int,
y_offset: int,
wrap_lines: bool,
) -> None:
self.window = window
self.ui_content = ui_content
self.vertical_scroll = vertical_scroll
self.window_width = window_width # Width without margins.
self.window_height = window_height
self.configured_scroll_offsets = configured_scroll_offsets
self.visible_line_to_row_col = visible_line_to_row_col
self.wrap_lines = wrap_lines
self._rowcol_to_yx = rowcol_to_yx # row/col from input to absolute y/x
# screen coordinates.
self._x_offset = x_offset
self._y_offset = y_offset
@property
def visible_line_to_input_line(self) -> dict[int, int]:
return {
visible_line: rowcol[0]
for visible_line, rowcol in self.visible_line_to_row_col.items()
}
@property
def cursor_position(self) -> Point:
"""
Return the cursor position coordinates, relative to the left/top corner
of the rendered screen.
"""
cpos = self.ui_content.cursor_position
try:
y, x = self._rowcol_to_yx[cpos.y, cpos.x]
except KeyError:
# For `DummyControl` for instance, the content can be empty, and so
# will `_rowcol_to_yx` be. Return 0/0 by default.
return Point(x=0, y=0)
else:
return Point(x=x - self._x_offset, y=y - self._y_offset)
@property
def applied_scroll_offsets(self) -> ScrollOffsets:
"""
Return a :class:`.ScrollOffsets` instance that indicates the actual
offset. This can be less than or equal to what's configured. E.g, when
the cursor is completely at the top, the top offset will be zero rather
than what's configured.
"""
if self.displayed_lines[0] == 0:
top = 0
else:
# Get row where the cursor is displayed.
y = self.input_line_to_visible_line[self.ui_content.cursor_position.y]
top = min(y, self.configured_scroll_offsets.top)
return ScrollOffsets(
top=top,
bottom=min(
self.ui_content.line_count - self.displayed_lines[-1] - 1,
self.configured_scroll_offsets.bottom,
),
# For left/right, it probably doesn't make sense to return something.
# (We would have to calculate the widths of all the lines and keep
# double width characters in mind.)
left=0,
right=0,
)
@property
def displayed_lines(self) -> list[int]:
"""
List of all the visible rows. (Line numbers of the input buffer.)
The last line may not be entirely visible.
"""
return sorted(row for row, col in self.visible_line_to_row_col.values())
@property
def input_line_to_visible_line(self) -> dict[int, int]:
"""
Return the dictionary mapping the line numbers of the input buffer to
the lines of the screen. When a line spans several rows at the screen,
the first row appears in the dictionary.
"""
result: dict[int, int] = {}
for k, v in self.visible_line_to_input_line.items():
if v in result:
result[v] = min(result[v], k)
else:
result[v] = k
return result
def first_visible_line(self, after_scroll_offset: bool = False) -> int:
"""
Return the line number (0 based) of the input document that corresponds
with the first visible line.
"""
if after_scroll_offset:
return self.displayed_lines[self.applied_scroll_offsets.top]
else:
return self.displayed_lines[0]
def last_visible_line(self, before_scroll_offset: bool = False) -> int:
"""
Like `first_visible_line`, but for the last visible line.
"""
if before_scroll_offset:
return self.displayed_lines[-1 - self.applied_scroll_offsets.bottom]
else:
return self.displayed_lines[-1]
def center_visible_line(
self, before_scroll_offset: bool = False, after_scroll_offset: bool = False
) -> int:
"""
Like `first_visible_line`, but for the center visible line.
"""
return (
self.first_visible_line(after_scroll_offset)
+ (
self.last_visible_line(before_scroll_offset)
- self.first_visible_line(after_scroll_offset)
)
// 2
)
@property
def content_height(self) -> int:
"""
The full height of the user control.
"""
return self.ui_content.line_count
@property
def full_height_visible(self) -> bool:
"""
True when the full height is visible (There is no vertical scroll.)
"""
return (
self.vertical_scroll == 0
and self.last_visible_line() == self.content_height
)
@property
def top_visible(self) -> bool:
"""
True when the top of the buffer is visible.
"""
return self.vertical_scroll == 0
@property
def bottom_visible(self) -> bool:
"""
True when the bottom of the buffer is visible.
"""
return self.last_visible_line() == self.content_height - 1
@property
def vertical_scroll_percentage(self) -> int:
"""
Vertical scroll as a percentage. (0 means: the top is visible,
100 means: the bottom is visible.)
"""
if self.bottom_visible:
return 100
else:
return 100 * self.vertical_scroll // self.content_height
def get_height_for_line(self, lineno: int) -> int:
"""
Return the height of the given line.
(The height that it would take, if this line became visible.)
"""
if self.wrap_lines:
return self.ui_content.get_height_for_line(
lineno, self.window_width, self.window.get_line_prefix
)
else:
return 1
class ScrollOffsets:
"""
Scroll offsets for the :class:`.Window` class.
Note that left/right offsets only make sense if line wrapping is disabled.
"""
def __init__(
self,
top: int | Callable[[], int] = 0,
bottom: int | Callable[[], int] = 0,
left: int | Callable[[], int] = 0,
right: int | Callable[[], int] = 0,
) -> None:
self._top = top
self._bottom = bottom
self._left = left
self._right = right
@property
def top(self) -> int:
return to_int(self._top)
@property
def bottom(self) -> int:
return to_int(self._bottom)
@property
def left(self) -> int:
return to_int(self._left)
@property
def right(self) -> int:
return to_int(self._right)
def __repr__(self) -> str:
return f"ScrollOffsets(top={self._top!r}, bottom={self._bottom!r}, left={self._left!r}, right={self._right!r})"
class ColorColumn:
"""
Column for a :class:`.Window` to be colored.
"""
def __init__(self, position: int, style: str = "class:color-column") -> None:
self.position = position
self.style = style
_in_insert_mode = vi_insert_mode | emacs_insert_mode
class WindowAlign(Enum):
"""
Alignment of the Window content.
Note that this is different from `HorizontalAlign` and `VerticalAlign`,
which are used for the alignment of the child containers in respectively
`VSplit` and `HSplit`.
"""
LEFT = "LEFT"
RIGHT = "RIGHT"
CENTER = "CENTER"
class Window(Container):
"""
Container that holds a control.
:param content: :class:`.UIControl` instance.
:param width: :class:`.Dimension` instance or callable.
:param height: :class:`.Dimension` instance or callable.
:param z_index: When specified, this can be used to bring element in front
of floating elements.
:param dont_extend_width: When `True`, don't take up more width then the
preferred width reported by the control.
:param dont_extend_height: When `True`, don't take up more width then the
preferred height reported by the control.
:param ignore_content_width: A `bool` or :class:`.Filter` instance. Ignore
the :class:`.UIContent` width when calculating the dimensions.
:param ignore_content_height: A `bool` or :class:`.Filter` instance. Ignore
the :class:`.UIContent` height when calculating the dimensions.
:param left_margins: A list of :class:`.Margin` instance to be displayed on
the left. For instance: :class:`~prompt_toolkit.layout.NumberedMargin`
can be one of them in order to show line numbers.
:param right_margins: Like `left_margins`, but on the other side.
:param scroll_offsets: :class:`.ScrollOffsets` instance, representing the
preferred amount of lines/columns to be always visible before/after the
cursor. When both top and bottom are a very high number, the cursor
will be centered vertically most of the time.
:param allow_scroll_beyond_bottom: A `bool` or
:class:`.Filter` instance. When True, allow scrolling so far, that the
top part of the content is not visible anymore, while there is still
empty space available at the bottom of the window. In the Vi editor for
instance, this is possible. You will see tildes while the top part of
the body is hidden.
:param wrap_lines: A `bool` or :class:`.Filter` instance. When True, don't
scroll horizontally, but wrap lines instead.
:param get_vertical_scroll: Callable that takes this window
instance as input and returns a preferred vertical scroll.
(When this is `None`, the scroll is only determined by the last and
current cursor position.)
:param get_horizontal_scroll: Callable that takes this window
instance as input and returns a preferred vertical scroll.
:param always_hide_cursor: A `bool` or
:class:`.Filter` instance. When True, never display the cursor, even
when the user control specifies a cursor position.
:param cursorline: A `bool` or :class:`.Filter` instance. When True,
display a cursorline.
:param cursorcolumn: A `bool` or :class:`.Filter` instance. When True,
display a cursorcolumn.
:param colorcolumns: A list of :class:`.ColorColumn` instances that
describe the columns to be highlighted, or a callable that returns such
a list.
:param align: :class:`.WindowAlign` value or callable that returns an
:class:`.WindowAlign` value. alignment of content.
:param style: A style string. Style to be applied to all the cells in this
window. (This can be a callable that returns a string.)
:param char: (string) Character to be used for filling the background. This
can also be a callable that returns a character.
:param get_line_prefix: None or a callable that returns formatted text to
be inserted before a line. It takes a line number (int) and a
wrap_count and returns formatted text. This can be used for
implementation of line continuations, things like Vim "breakindent" and
so on.
"""
def __init__(
self,
content: UIControl | None = None,
width: AnyDimension = None,
height: AnyDimension = None,
z_index: int | None = None,
dont_extend_width: FilterOrBool = False,
dont_extend_height: FilterOrBool = False,
ignore_content_width: FilterOrBool = False,
ignore_content_height: FilterOrBool = False,
left_margins: Sequence[Margin] | None = None,
right_margins: Sequence[Margin] | None = None,
scroll_offsets: ScrollOffsets | None = None,
allow_scroll_beyond_bottom: FilterOrBool = False,
wrap_lines: FilterOrBool = False,
get_vertical_scroll: Callable[[Window], int] | None = None,
get_horizontal_scroll: Callable[[Window], int] | None = None,
always_hide_cursor: FilterOrBool = False,
cursorline: FilterOrBool = False,
cursorcolumn: FilterOrBool = False,
colorcolumns: (
None | list[ColorColumn] | Callable[[], list[ColorColumn]]
) = None,
align: WindowAlign | Callable[[], WindowAlign] = WindowAlign.LEFT,
style: str | Callable[[], str] = "",
char: None | str | Callable[[], str] = None,
get_line_prefix: GetLinePrefixCallable | None = None,
) -> None:
self.allow_scroll_beyond_bottom = to_filter(allow_scroll_beyond_bottom)
self.always_hide_cursor = to_filter(always_hide_cursor)
self.wrap_lines = to_filter(wrap_lines)
self.cursorline = to_filter(cursorline)
self.cursorcolumn = to_filter(cursorcolumn)
self.content = content or DummyControl()
self.dont_extend_width = to_filter(dont_extend_width)
self.dont_extend_height = to_filter(dont_extend_height)
self.ignore_content_width = to_filter(ignore_content_width)
self.ignore_content_height = to_filter(ignore_content_height)
self.left_margins = left_margins or []
self.right_margins = right_margins or []
self.scroll_offsets = scroll_offsets or ScrollOffsets()
self.get_vertical_scroll = get_vertical_scroll
self.get_horizontal_scroll = get_horizontal_scroll
self.colorcolumns = colorcolumns or []
self.align = align
self.style = style
self.char = char
self.get_line_prefix = get_line_prefix
self.width = width
self.height = height
self.z_index = z_index
# Cache for the screens generated by the margin.
self._ui_content_cache: SimpleCache[tuple[int, int, int], UIContent] = (
SimpleCache(maxsize=8)
)
self._margin_width_cache: SimpleCache[tuple[Margin, int], int] = SimpleCache(
maxsize=1
)
self.reset()
def __repr__(self) -> str:
return f"Window(content={self.content!r})"
def reset(self) -> None:
self.content.reset()
#: Scrolling position of the main content.
self.vertical_scroll = 0
self.horizontal_scroll = 0
# Vertical scroll 2: this is the vertical offset that a line is
# scrolled if a single line (the one that contains the cursor) consumes
# all of the vertical space.
self.vertical_scroll_2 = 0
#: Keep render information (mappings between buffer input and render
#: output.)
self.render_info: WindowRenderInfo | None = None
def _get_margin_width(self, margin: Margin) -> int:
"""
Return the width for this margin.
(Calculate only once per render time.)
"""
# Margin.get_width, needs to have a UIContent instance.
def get_ui_content() -> UIContent:
return self._get_ui_content(width=0, height=0)
def get_width() -> int:
return margin.get_width(get_ui_content)
key = (margin, get_app().render_counter)
return self._margin_width_cache.get(key, get_width)
def _get_total_margin_width(self) -> int:
"""
Calculate and return the width of the margin (left + right).
"""
return sum(self._get_margin_width(m) for m in self.left_margins) + sum(
self._get_margin_width(m) for m in self.right_margins
)
def preferred_width(self, max_available_width: int) -> Dimension:
"""
Calculate the preferred width for this window.
"""
def preferred_content_width() -> int | None:
"""Content width: is only calculated if no exact width for the
window was given."""
if self.ignore_content_width():
return None
# Calculate the width of the margin.
total_margin_width = self._get_total_margin_width()
# Window of the content. (Can be `None`.)
preferred_width = self.content.preferred_width(
max_available_width - total_margin_width
)
if preferred_width is not None:
# Include width of the margins.
preferred_width += total_margin_width
return preferred_width
# Merge.
return self._merge_dimensions(
dimension=to_dimension(self.width),
get_preferred=preferred_content_width,
dont_extend=self.dont_extend_width(),
)
def preferred_height(self, width: int, max_available_height: int) -> Dimension:
"""
Calculate the preferred height for this window.
"""
def preferred_content_height() -> int | None:
"""Content height: is only calculated if no exact height for the
window was given."""
if self.ignore_content_height():
return None
total_margin_width = self._get_total_margin_width()
wrap_lines = self.wrap_lines()
return self.content.preferred_height(
width - total_margin_width,
max_available_height,
wrap_lines,
self.get_line_prefix,
)
return self._merge_dimensions(
dimension=to_dimension(self.height),
get_preferred=preferred_content_height,
dont_extend=self.dont_extend_height(),
)
@staticmethod
def _merge_dimensions(
dimension: Dimension | None,
get_preferred: Callable[[], int | None],
dont_extend: bool = False,
) -> Dimension:
"""
Take the Dimension from this `Window` class and the received preferred
size from the `UIControl` and return a `Dimension` to report to the
parent container.
"""
dimension = dimension or Dimension()
# When a preferred dimension was explicitly given to the Window,
# ignore the UIControl.
preferred: int | None
if dimension.preferred_specified:
preferred = dimension.preferred
else:
# Otherwise, calculate the preferred dimension from the UI control
# content.
preferred = get_preferred()
# When a 'preferred' dimension is given by the UIControl, make sure
# that it stays within the bounds of the Window.
if preferred is not None:
if dimension.max_specified:
preferred = min(preferred, dimension.max)
if dimension.min_specified:
preferred = max(preferred, dimension.min)
# When a `dont_extend` flag has been given, use the preferred dimension
# also as the max dimension.
max_: int | None
min_: int | None
if dont_extend and preferred is not None:
max_ = min(dimension.max, preferred)
else:
max_ = dimension.max if dimension.max_specified else None
min_ = dimension.min if dimension.min_specified else None
return Dimension(
min=min_, max=max_, preferred=preferred, weight=dimension.weight
)
def _get_ui_content(self, width: int, height: int) -> UIContent:
"""
Create a `UIContent` instance.
"""
def get_content() -> UIContent:
return self.content.create_content(width=width, height=height)
key = (get_app().render_counter, width, height)
return self._ui_content_cache.get(key, get_content)
def _get_digraph_char(self) -> str | None:
"Return `False`, or the Digraph symbol to be used."
app = get_app()
if app.quoted_insert:
return "^"
if app.vi_state.waiting_for_digraph:
if app.vi_state.digraph_symbol1:
return app.vi_state.digraph_symbol1
return "?"
return None
def write_to_screen(
self,
screen: Screen,
mouse_handlers: MouseHandlers,
write_position: WritePosition,
parent_style: str,
erase_bg: bool,
z_index: int | None,
) -> None:
"""
Write window to screen. This renders the user control, the margins and
copies everything over to the absolute position at the given screen.
"""
# If dont_extend_width/height was given. Then reduce width/height in
# WritePosition if the parent wanted us to paint in a bigger area.
# (This happens if this window is bundled with another window in a
# HSplit/VSplit, but with different size requirements.)
write_position = WritePosition(
xpos=write_position.xpos,
ypos=write_position.ypos,
width=write_position.width,
height=write_position.height,
)
if self.dont_extend_width():
write_position.width = min(
write_position.width,
self.preferred_width(write_position.width).preferred,
)
if self.dont_extend_height():
write_position.height = min(
write_position.height,
self.preferred_height(
write_position.width, write_position.height
).preferred,
)
# Draw
z_index = z_index if self.z_index is None else self.z_index
draw_func = partial(
self._write_to_screen_at_index,
screen,
mouse_handlers,
write_position,
parent_style,
erase_bg,
)
if z_index is None or z_index <= 0:
# When no z_index is given, draw right away.
draw_func()
else:
# Otherwise, postpone.
screen.draw_with_z_index(z_index=z_index, draw_func=draw_func)
def _write_to_screen_at_index(
self,
screen: Screen,
mouse_handlers: MouseHandlers,
write_position: WritePosition,
parent_style: str,
erase_bg: bool,
) -> None:
# Don't bother writing invisible windows.
# (We save some time, but also avoid applying last-line styling.)
if write_position.height <= 0 or write_position.width <= 0:
return
# Calculate margin sizes.
left_margin_widths = [self._get_margin_width(m) for m in self.left_margins]
right_margin_widths = [self._get_margin_width(m) for m in self.right_margins]
total_margin_width = sum(left_margin_widths + right_margin_widths)
# Render UserControl.
ui_content = self.content.create_content(
write_position.width - total_margin_width, write_position.height
)
assert isinstance(ui_content, UIContent)
# Scroll content.
wrap_lines = self.wrap_lines()
self._scroll(
ui_content, write_position.width - total_margin_width, write_position.height
)
# Erase background and fill with `char`.
self._fill_bg(screen, write_position, erase_bg)
# Resolve `align` attribute.
align = self.align() if callable(self.align) else self.align
# Write body
visible_line_to_row_col, rowcol_to_yx = self._copy_body(
ui_content,
screen,
write_position,
sum(left_margin_widths),
write_position.width - total_margin_width,
self.vertical_scroll,
self.horizontal_scroll,
wrap_lines=wrap_lines,
highlight_lines=True,
vertical_scroll_2=self.vertical_scroll_2,
always_hide_cursor=self.always_hide_cursor(),
has_focus=get_app().layout.current_control == self.content,
align=align,
get_line_prefix=self.get_line_prefix,
)
# Remember render info. (Set before generating the margins. They need this.)
x_offset = write_position.xpos + sum(left_margin_widths)
y_offset = write_position.ypos
render_info = WindowRenderInfo(
window=self,
ui_content=ui_content,
horizontal_scroll=self.horizontal_scroll,
vertical_scroll=self.vertical_scroll,
window_width=write_position.width - total_margin_width,
window_height=write_position.height,
configured_scroll_offsets=self.scroll_offsets,
visible_line_to_row_col=visible_line_to_row_col,
rowcol_to_yx=rowcol_to_yx,
x_offset=x_offset,
y_offset=y_offset,
wrap_lines=wrap_lines,
)
self.render_info = render_info
# Set mouse handlers.
def mouse_handler(mouse_event: MouseEvent) -> NotImplementedOrNone:
"""
Wrapper around the mouse_handler of the `UIControl` that turns
screen coordinates into line coordinates.
Returns `NotImplemented` if no UI invalidation should be done.
"""
# Don't handle mouse events outside of the current modal part of
# the UI.
if self not in get_app().layout.walk_through_modal_area():
return NotImplemented
# Find row/col position first.
yx_to_rowcol = {v: k for k, v in rowcol_to_yx.items()}
y = mouse_event.position.y
x = mouse_event.position.x
# If clicked below the content area, look for a position in the
# last line instead.
max_y = write_position.ypos + len(visible_line_to_row_col) - 1
y = min(max_y, y)
result: NotImplementedOrNone
while x >= 0:
try:
row, col = yx_to_rowcol[y, x]
except KeyError:
# Try again. (When clicking on the right side of double
# width characters, or on the right side of the input.)
x -= 1
else:
# Found position, call handler of UIControl.
result = self.content.mouse_handler(
MouseEvent(
position=Point(x=col, y=row),
event_type=mouse_event.event_type,
button=mouse_event.button,
modifiers=mouse_event.modifiers,
)
)
break
else:
# nobreak.
# (No x/y coordinate found for the content. This happens in
# case of a DummyControl, that does not have any content.
# Report (0,0) instead.)
result = self.content.mouse_handler(
MouseEvent(
position=Point(x=0, y=0),
event_type=mouse_event.event_type,
button=mouse_event.button,
modifiers=mouse_event.modifiers,
)
)
# If it returns NotImplemented, handle it here.
if result == NotImplemented:
result = self._mouse_handler(mouse_event)
return result
mouse_handlers.set_mouse_handler_for_range(
x_min=write_position.xpos + sum(left_margin_widths),
x_max=write_position.xpos + write_position.width - total_margin_width,
y_min=write_position.ypos,
y_max=write_position.ypos + write_position.height,
handler=mouse_handler,
)
# Render and copy margins.
move_x = 0
def render_margin(m: Margin, width: int) -> UIContent:
"Render margin. Return `Screen`."
# Retrieve margin fragments.
fragments = m.create_margin(render_info, width, write_position.height)
# Turn it into a UIContent object.
# already rendered those fragments using this size.)
return FormattedTextControl(fragments).create_content(
width + 1, write_position.height
)
for m, width in zip(self.left_margins, left_margin_widths):
if width > 0: # (ConditionalMargin returns a zero width. -- Don't render.)
# Create screen for margin.
margin_content = render_margin(m, width)
# Copy and shift X.
self._copy_margin(margin_content, screen, write_position, move_x, width)
move_x += width
move_x = write_position.width - sum(right_margin_widths)
for m, width in zip(self.right_margins, right_margin_widths):
# Create screen for margin.
margin_content = render_margin(m, width)
# Copy and shift X.
self._copy_margin(margin_content, screen, write_position, move_x, width)
move_x += width
# Apply 'self.style'
self._apply_style(screen, write_position, parent_style)
# Tell the screen that this user control has been painted at this
# position.
screen.visible_windows_to_write_positions[self] = write_position
def _copy_body(
self,
ui_content: UIContent,
new_screen: Screen,
write_position: WritePosition,
move_x: int,
width: int,
vertical_scroll: int = 0,
horizontal_scroll: int = 0,
wrap_lines: bool = False,
highlight_lines: bool = False,
vertical_scroll_2: int = 0,
always_hide_cursor: bool = False,
has_focus: bool = False,
align: WindowAlign = WindowAlign.LEFT,
get_line_prefix: Callable[[int, int], AnyFormattedText] | None = None,
) -> tuple[dict[int, tuple[int, int]], dict[tuple[int, int], tuple[int, int]]]:
"""
Copy the UIContent into the output screen.
Return (visible_line_to_row_col, rowcol_to_yx) tuple.
:param get_line_prefix: None or a callable that takes a line number
(int) and a wrap_count (int) and returns formatted text.
"""
xpos = write_position.xpos + move_x
ypos = write_position.ypos
line_count = ui_content.line_count
new_buffer = new_screen.data_buffer
empty_char = _CHAR_CACHE["", ""]
# Map visible line number to (row, col) of input.
# 'col' will always be zero if line wrapping is off.
visible_line_to_row_col: dict[int, tuple[int, int]] = {}
# Maps (row, col) from the input to (y, x) screen coordinates.
rowcol_to_yx: dict[tuple[int, int], tuple[int, int]] = {}
def copy_line(
line: StyleAndTextTuples,
lineno: int,
x: int,
y: int,
is_input: bool = False,
) -> tuple[int, int]:
"""
Copy over a single line to the output screen. This can wrap over
multiple lines in the output. It will call the prefix (prompt)
function before every line.
"""
if is_input:
current_rowcol_to_yx = rowcol_to_yx
else:
current_rowcol_to_yx = {} # Throwaway dictionary.
# Draw line prefix.
if is_input and get_line_prefix:
prompt = to_formatted_text(get_line_prefix(lineno, 0))
x, y = copy_line(prompt, lineno, x, y, is_input=False)
# Scroll horizontally.
skipped = 0 # Characters skipped because of horizontal scrolling.
if horizontal_scroll and is_input:
h_scroll = horizontal_scroll
line = explode_text_fragments(line)
while h_scroll > 0 and line:
h_scroll -= get_cwidth(line[0][1])
skipped += 1
del line[:1] # Remove first character.
x -= h_scroll # When scrolling over double width character,
# this can end up being negative.
# Align this line. (Note that this doesn't work well when we use
# get_line_prefix and that function returns variable width prefixes.)
if align == WindowAlign.CENTER:
line_width = fragment_list_width(line)
if line_width < width:
x += (width - line_width) // 2
elif align == WindowAlign.RIGHT:
line_width = fragment_list_width(line)
if line_width < width:
x += width - line_width
col = 0
wrap_count = 0
for style, text, *_ in line:
new_buffer_row = new_buffer[y + ypos]
# Remember raw VT escape sequences. (E.g. FinalTerm's
# escape sequences.)
if "[ZeroWidthEscape]" in style:
new_screen.zero_width_escapes[y + ypos][x + xpos] += text
continue
for c in text:
char = _CHAR_CACHE[c, style]
char_width = char.width
# Wrap when the line width is exceeded.
if wrap_lines and x + char_width > width:
visible_line_to_row_col[y + 1] = (
lineno,
visible_line_to_row_col[y][1] + x,
)
y += 1
wrap_count += 1
x = 0
# Insert line prefix (continuation prompt).
if is_input and get_line_prefix:
prompt = to_formatted_text(
get_line_prefix(lineno, wrap_count)
)
x, y = copy_line(prompt, lineno, x, y, is_input=False)
new_buffer_row = new_buffer[y + ypos]
if y >= write_position.height:
return x, y # Break out of all for loops.
# Set character in screen and shift 'x'.
if x >= 0 and y >= 0 and x < width:
new_buffer_row[x + xpos] = char
# When we print a multi width character, make sure
# to erase the neighbors positions in the screen.
# (The empty string if different from everything,
# so next redraw this cell will repaint anyway.)
if char_width > 1:
for i in range(1, char_width):
new_buffer_row[x + xpos + i] = empty_char
# If this is a zero width characters, then it's
# probably part of a decomposed unicode character.
# See: https://en.wikipedia.org/wiki/Unicode_equivalence
# Merge it in the previous cell.
elif char_width == 0:
# Handle all character widths. If the previous
# character is a multiwidth character, then
# merge it two positions back.
for pw in [2, 1]: # Previous character width.
if (
x - pw >= 0
and new_buffer_row[x + xpos - pw].width == pw
):
prev_char = new_buffer_row[x + xpos - pw]
char2 = _CHAR_CACHE[
prev_char.char + c, prev_char.style
]
new_buffer_row[x + xpos - pw] = char2
# Keep track of write position for each character.
current_rowcol_to_yx[lineno, col + skipped] = (
y + ypos,
x + xpos,
)
col += 1
x += char_width
return x, y
# Copy content.
def copy() -> int:
y = -vertical_scroll_2
lineno = vertical_scroll
while y < write_position.height and lineno < line_count:
# Take the next line and copy it in the real screen.
line = ui_content.get_line(lineno)
visible_line_to_row_col[y] = (lineno, horizontal_scroll)
# Copy margin and actual line.
x = 0
x, y = copy_line(line, lineno, x, y, is_input=True)
lineno += 1
y += 1
return y
copy()
def cursor_pos_to_screen_pos(row: int, col: int) -> Point:
"Translate row/col from UIContent to real Screen coordinates."
try:
y, x = rowcol_to_yx[row, col]
except KeyError:
# Normally this should never happen. (It is a bug, if it happens.)
# But to be sure, return (0, 0)
return Point(x=0, y=0)
# raise ValueError(
# 'Invalid position. row=%r col=%r, vertical_scroll=%r, '
# 'horizontal_scroll=%r, height=%r' %
# (row, col, vertical_scroll, horizontal_scroll, write_position.height))
else:
return Point(x=x, y=y)
# Set cursor and menu positions.
if ui_content.cursor_position:
screen_cursor_position = cursor_pos_to_screen_pos(
ui_content.cursor_position.y, ui_content.cursor_position.x
)
if has_focus:
new_screen.set_cursor_position(self, screen_cursor_position)
if always_hide_cursor:
new_screen.show_cursor = False
else:
new_screen.show_cursor = ui_content.show_cursor
self._highlight_digraph(new_screen)
if highlight_lines:
self._highlight_cursorlines(
new_screen,
screen_cursor_position,
xpos,
ypos,
width,
write_position.height,
)
# Draw input characters from the input processor queue.
if has_focus and ui_content.cursor_position:
self._show_key_processor_key_buffer(new_screen)
# Set menu position.
if ui_content.menu_position:
new_screen.set_menu_position(
self,
cursor_pos_to_screen_pos(
ui_content.menu_position.y, ui_content.menu_position.x
),
)
# Update output screen height.
new_screen.height = max(new_screen.height, ypos + write_position.height)
return visible_line_to_row_col, rowcol_to_yx
def _fill_bg(
self, screen: Screen, write_position: WritePosition, erase_bg: bool
) -> None:
"""
Erase/fill the background.
(Useful for floats and when a `char` has been given.)
"""
char: str | None
if callable(self.char):
char = self.char()
else:
char = self.char
if erase_bg or char:
wp = write_position
char_obj = _CHAR_CACHE[char or " ", ""]
for y in range(wp.ypos, wp.ypos + wp.height):
row = screen.data_buffer[y]
for x in range(wp.xpos, wp.xpos + wp.width):
row[x] = char_obj
def _apply_style(
self, new_screen: Screen, write_position: WritePosition, parent_style: str
) -> None:
# Apply `self.style`.
style = parent_style + " " + to_str(self.style)
new_screen.fill_area(write_position, style=style, after=False)
# Apply the 'last-line' class to the last line of each Window. This can
# be used to apply an 'underline' to the user control.
wp = WritePosition(
write_position.xpos,
write_position.ypos + write_position.height - 1,
write_position.width,
1,
)
new_screen.fill_area(wp, "class:last-line", after=True)
def _highlight_digraph(self, new_screen: Screen) -> None:
"""
When we are in Vi digraph mode, put a question mark underneath the
cursor.
"""
digraph_char = self._get_digraph_char()
if digraph_char:
cpos = new_screen.get_cursor_position(self)
new_screen.data_buffer[cpos.y][cpos.x] = _CHAR_CACHE[
digraph_char, "class:digraph"
]
def _show_key_processor_key_buffer(self, new_screen: Screen) -> None:
"""
When the user is typing a key binding that consists of several keys,
display the last pressed key if the user is in insert mode and the key
is meaningful to be displayed.
E.g. Some people want to bind 'jj' to escape in Vi insert mode. But the
first 'j' needs to be displayed in order to get some feedback.
"""
app = get_app()
key_buffer = app.key_processor.key_buffer
if key_buffer and _in_insert_mode() and not app.is_done:
# The textual data for the given key. (Can be a VT100 escape
# sequence.)
data = key_buffer[-1].data
# Display only if this is a 1 cell width character.
if get_cwidth(data) == 1:
cpos = new_screen.get_cursor_position(self)
new_screen.data_buffer[cpos.y][cpos.x] = _CHAR_CACHE[
data, "class:partial-key-binding"
]
def _highlight_cursorlines(
self, new_screen: Screen, cpos: Point, x: int, y: int, width: int, height: int
) -> None:
"""
Highlight cursor row/column.
"""
cursor_line_style = " class:cursor-line "
cursor_column_style = " class:cursor-column "
data_buffer = new_screen.data_buffer
# Highlight cursor line.
if self.cursorline():
row = data_buffer[cpos.y]
for x in range(x, x + width):
original_char = row[x]
row[x] = _CHAR_CACHE[
original_char.char, original_char.style + cursor_line_style
]
# Highlight cursor column.
if self.cursorcolumn():
for y2 in range(y, y + height):
row = data_buffer[y2]
original_char = row[cpos.x]
row[cpos.x] = _CHAR_CACHE[
original_char.char, original_char.style + cursor_column_style
]
# Highlight color columns
colorcolumns = self.colorcolumns
if callable(colorcolumns):
colorcolumns = colorcolumns()
for cc in colorcolumns:
assert isinstance(cc, ColorColumn)
column = cc.position
if column < x + width: # Only draw when visible.
color_column_style = " " + cc.style
for y2 in range(y, y + height):
row = data_buffer[y2]
original_char = row[column + x]
row[column + x] = _CHAR_CACHE[
original_char.char, original_char.style + color_column_style
]
def _copy_margin(
self,
margin_content: UIContent,
new_screen: Screen,
write_position: WritePosition,
move_x: int,
width: int,
) -> None:
"""
Copy characters from the margin screen to the real screen.
"""
xpos = write_position.xpos + move_x
ypos = write_position.ypos
margin_write_position = WritePosition(xpos, ypos, width, write_position.height)
self._copy_body(margin_content, new_screen, margin_write_position, 0, width)
def _scroll(self, ui_content: UIContent, width: int, height: int) -> None:
"""
Scroll body. Ensure that the cursor is visible.
"""
if self.wrap_lines():
func = self._scroll_when_linewrapping
else:
func = self._scroll_without_linewrapping
func(ui_content, width, height)
def _scroll_when_linewrapping(
self, ui_content: UIContent, width: int, height: int
) -> None:
"""
Scroll to make sure the cursor position is visible and that we maintain
the requested scroll offset.
Set `self.horizontal_scroll/vertical_scroll`.
"""
scroll_offsets_bottom = self.scroll_offsets.bottom
scroll_offsets_top = self.scroll_offsets.top
# We don't have horizontal scrolling.
self.horizontal_scroll = 0
def get_line_height(lineno: int) -> int:
return ui_content.get_height_for_line(lineno, width, self.get_line_prefix)
# When there is no space, reset `vertical_scroll_2` to zero and abort.
# This can happen if the margin is bigger than the window width.
# Otherwise the text height will become "infinite" (a big number) and
# the copy_line will spend a huge amount of iterations trying to render
# nothing.
if width <= 0:
self.vertical_scroll = ui_content.cursor_position.y
self.vertical_scroll_2 = 0
return
# If the current line consumes more than the whole window height,
# then we have to scroll vertically inside this line. (We don't take
# the scroll offsets into account for this.)
# Also, ignore the scroll offsets in this case. Just set the vertical
# scroll to this line.
line_height = get_line_height(ui_content.cursor_position.y)
if line_height > height - scroll_offsets_top:
# Calculate the height of the text before the cursor (including
# line prefixes).
text_before_height = ui_content.get_height_for_line(
ui_content.cursor_position.y,
width,
self.get_line_prefix,
slice_stop=ui_content.cursor_position.x,
)
# Adjust scroll offset.
self.vertical_scroll = ui_content.cursor_position.y
self.vertical_scroll_2 = min(
text_before_height - 1, # Keep the cursor visible.
line_height
- height, # Avoid blank lines at the bottom when scrolling up again.
self.vertical_scroll_2,
)
self.vertical_scroll_2 = max(
0, text_before_height - height, self.vertical_scroll_2
)
return
else:
self.vertical_scroll_2 = 0
# Current line doesn't consume the whole height. Take scroll offsets into account.
def get_min_vertical_scroll() -> int:
# Make sure that the cursor line is not below the bottom.
# (Calculate how many lines can be shown between the cursor and the .)
used_height = 0
prev_lineno = ui_content.cursor_position.y
for lineno in range(ui_content.cursor_position.y, -1, -1):
used_height += get_line_height(lineno)
if used_height > height - scroll_offsets_bottom:
return prev_lineno
else:
prev_lineno = lineno
return 0
def get_max_vertical_scroll() -> int:
# Make sure that the cursor line is not above the top.
prev_lineno = ui_content.cursor_position.y
used_height = 0
for lineno in range(ui_content.cursor_position.y - 1, -1, -1):
used_height += get_line_height(lineno)
if used_height > scroll_offsets_top:
return prev_lineno
else:
prev_lineno = lineno
return prev_lineno
def get_topmost_visible() -> int:
"""
Calculate the upper most line that can be visible, while the bottom
is still visible. We should not allow scroll more than this if
`allow_scroll_beyond_bottom` is false.
"""
prev_lineno = ui_content.line_count - 1
used_height = 0
for lineno in range(ui_content.line_count - 1, -1, -1):
used_height += get_line_height(lineno)
if used_height > height:
return prev_lineno
else:
prev_lineno = lineno
return prev_lineno
# Scroll vertically. (Make sure that the whole line which contains the
# cursor is visible.
topmost_visible = get_topmost_visible()
# Note: the `min(topmost_visible, ...)` is to make sure that we
# don't require scrolling up because of the bottom scroll offset,
# when we are at the end of the document.
self.vertical_scroll = max(
self.vertical_scroll, min(topmost_visible, get_min_vertical_scroll())
)
self.vertical_scroll = min(self.vertical_scroll, get_max_vertical_scroll())
# Disallow scrolling beyond bottom?
if not self.allow_scroll_beyond_bottom():
self.vertical_scroll = min(self.vertical_scroll, topmost_visible)
def _scroll_without_linewrapping(
self, ui_content: UIContent, width: int, height: int
) -> None:
"""
Scroll to make sure the cursor position is visible and that we maintain
the requested scroll offset.
Set `self.horizontal_scroll/vertical_scroll`.
"""
cursor_position = ui_content.cursor_position or Point(x=0, y=0)
# Without line wrapping, we will never have to scroll vertically inside
# a single line.
self.vertical_scroll_2 = 0
if ui_content.line_count == 0:
self.vertical_scroll = 0
self.horizontal_scroll = 0
return
else:
current_line_text = fragment_list_to_text(
ui_content.get_line(cursor_position.y)
)
def do_scroll(
current_scroll: int,
scroll_offset_start: int,
scroll_offset_end: int,
cursor_pos: int,
window_size: int,
content_size: int,
) -> int:
"Scrolling algorithm. Used for both horizontal and vertical scrolling."
# Calculate the scroll offset to apply.
# This can obviously never be more than have the screen size. Also, when the
# cursor appears at the top or bottom, we don't apply the offset.
scroll_offset_start = int(
min(scroll_offset_start, window_size / 2, cursor_pos)
)
scroll_offset_end = int(
min(scroll_offset_end, window_size / 2, content_size - 1 - cursor_pos)
)
# Prevent negative scroll offsets.
if current_scroll < 0:
current_scroll = 0
# Scroll back if we scrolled to much and there's still space to show more of the document.
if (
not self.allow_scroll_beyond_bottom()
and current_scroll > content_size - window_size
):
current_scroll = max(0, content_size - window_size)
# Scroll up if cursor is before visible part.
if current_scroll > cursor_pos - scroll_offset_start:
current_scroll = max(0, cursor_pos - scroll_offset_start)
# Scroll down if cursor is after visible part.
if current_scroll < (cursor_pos + 1) - window_size + scroll_offset_end:
current_scroll = (cursor_pos + 1) - window_size + scroll_offset_end
return current_scroll
# When a preferred scroll is given, take that first into account.
if self.get_vertical_scroll:
self.vertical_scroll = self.get_vertical_scroll(self)
assert isinstance(self.vertical_scroll, int)
if self.get_horizontal_scroll:
self.horizontal_scroll = self.get_horizontal_scroll(self)
assert isinstance(self.horizontal_scroll, int)
# Update horizontal/vertical scroll to make sure that the cursor
# remains visible.
offsets = self.scroll_offsets
self.vertical_scroll = do_scroll(
current_scroll=self.vertical_scroll,
scroll_offset_start=offsets.top,
scroll_offset_end=offsets.bottom,
cursor_pos=ui_content.cursor_position.y,
window_size=height,
content_size=ui_content.line_count,
)
if self.get_line_prefix:
current_line_prefix_width = fragment_list_width(
to_formatted_text(self.get_line_prefix(ui_content.cursor_position.y, 0))
)
else:
current_line_prefix_width = 0
self.horizontal_scroll = do_scroll(
current_scroll=self.horizontal_scroll,
scroll_offset_start=offsets.left,
scroll_offset_end=offsets.right,
cursor_pos=get_cwidth(current_line_text[: ui_content.cursor_position.x]),
window_size=width - current_line_prefix_width,
# We can only analyze the current line. Calculating the width off
# all the lines is too expensive.
content_size=max(
get_cwidth(current_line_text), self.horizontal_scroll + width
),
)
def _mouse_handler(self, mouse_event: MouseEvent) -> NotImplementedOrNone:
"""
Mouse handler. Called when the UI control doesn't handle this
particular event.
Return `NotImplemented` if nothing was done as a consequence of this
key binding (no UI invalidate required in that case).
"""
if mouse_event.event_type == MouseEventType.SCROLL_DOWN:
self._scroll_down()
return None
elif mouse_event.event_type == MouseEventType.SCROLL_UP:
self._scroll_up()
return None
return NotImplemented
def _scroll_down(self) -> None:
"Scroll window down."
info = self.render_info
if info is None:
return
if self.vertical_scroll < info.content_height - info.window_height:
if info.cursor_position.y <= info.configured_scroll_offsets.top:
self.content.move_cursor_down()
self.vertical_scroll += 1
def _scroll_up(self) -> None:
"Scroll window up."
info = self.render_info
if info is None:
return
if info.vertical_scroll > 0:
# TODO: not entirely correct yet in case of line wrapping and long lines.
if (
info.cursor_position.y
>= info.window_height - 1 - info.configured_scroll_offsets.bottom
):
self.content.move_cursor_up()
self.vertical_scroll -= 1
def get_key_bindings(self) -> KeyBindingsBase | None:
return self.content.get_key_bindings()
def get_children(self) -> list[Container]:
return []
class ConditionalContainer(Container):
"""
Wrapper around any other container that can change the visibility. The
received `filter` determines whether the given container should be
displayed or not.
:param content: :class:`.Container` instance.
:param filter: :class:`.Filter` instance.
"""
def __init__(self, content: AnyContainer, filter: FilterOrBool) -> None:
self.content = to_container(content)
self.filter = to_filter(filter)
def __repr__(self) -> str:
return f"ConditionalContainer({self.content!r}, filter={self.filter!r})"
def reset(self) -> None:
self.content.reset()
def preferred_width(self, max_available_width: int) -> Dimension:
if self.filter():
return self.content.preferred_width(max_available_width)
else:
return Dimension.zero()
def preferred_height(self, width: int, max_available_height: int) -> Dimension:
if self.filter():
return self.content.preferred_height(width, max_available_height)
else:
return Dimension.zero()
def write_to_screen(
self,
screen: Screen,
mouse_handlers: MouseHandlers,
write_position: WritePosition,
parent_style: str,
erase_bg: bool,
z_index: int | None,
) -> None:
if self.filter():
return self.content.write_to_screen(
screen, mouse_handlers, write_position, parent_style, erase_bg, z_index
)
def get_children(self) -> list[Container]:
return [self.content]
class DynamicContainer(Container):
"""
Container class that dynamically returns any Container.
:param get_container: Callable that returns a :class:`.Container` instance
or any widget with a ``__pt_container__`` method.
"""
def __init__(self, get_container: Callable[[], AnyContainer]) -> None:
self.get_container = get_container
def _get_container(self) -> Container:
"""
Return the current container object.
We call `to_container`, because `get_container` can also return a
widget with a ``__pt_container__`` method.
"""
obj = self.get_container()
return to_container(obj)
def reset(self) -> None:
self._get_container().reset()
def preferred_width(self, max_available_width: int) -> Dimension:
return self._get_container().preferred_width(max_available_width)
def preferred_height(self, width: int, max_available_height: int) -> Dimension:
return self._get_container().preferred_height(width, max_available_height)
def write_to_screen(
self,
screen: Screen,
mouse_handlers: MouseHandlers,
write_position: WritePosition,
parent_style: str,
erase_bg: bool,
z_index: int | None,
) -> None:
self._get_container().write_to_screen(
screen, mouse_handlers, write_position, parent_style, erase_bg, z_index
)
def is_modal(self) -> bool:
return False
def get_key_bindings(self) -> KeyBindingsBase | None:
# Key bindings will be collected when `layout.walk()` finds the child
# container.
return None
def get_children(self) -> list[Container]:
# Here we have to return the current active container itself, not its
# children. Otherwise, we run into issues where `layout.walk()` will
# never see an object of type `Window` if this contains a window. We
# can't/shouldn't proxy the "isinstance" check.
return [self._get_container()]
def to_container(container: AnyContainer) -> Container:
"""
Make sure that the given object is a :class:`.Container`.
"""
if isinstance(container, Container):
return container
elif hasattr(container, "__pt_container__"):
return to_container(container.__pt_container__())
else:
raise ValueError(f"Not a container object: {container!r}")
def to_window(container: AnyContainer) -> Window:
"""
Make sure that the given argument is a :class:`.Window`.
"""
if isinstance(container, Window):
return container
elif hasattr(container, "__pt_container__"):
return to_window(cast("MagicContainer", container).__pt_container__())
else:
raise ValueError(f"Not a Window object: {container!r}.")
def is_container(value: object) -> TypeGuard[AnyContainer]:
"""
Checks whether the given value is a container object
(for use in assert statements).
"""
if isinstance(value, Container):
return True
if hasattr(value, "__pt_container__"):
return is_container(cast("MagicContainer", value).__pt_container__())
return False
|