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
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
6995
6996
6997
6998
6999
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
7028
7029
7030
7031
7032
7033
7034
7035
7036
7037
7038
7039
7040
7041
7042
7043
7044
7045
7046
7047
7048
7049
7050
7051
7052
7053
7054
7055
7056
7057
7058
7059
7060
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097
7098
7099
7100
7101
7102
7103
7104
7105
7106
7107
7108
7109
7110
7111
7112
7113
7114
7115
7116
7117
7118
7119
7120
7121
7122
7123
7124
7125
7126
7127
7128
7129
7130
7131
7132
7133
7134
7135
7136
7137
7138
7139
7140
7141
7142
7143
7144
7145
7146
7147
7148
7149
7150
7151
7152
7153
7154
7155
7156
7157
7158
7159
7160
7161
7162
7163
7164
7165
7166
7167
7168
7169
7170
7171
7172
7173
7174
7175
7176
7177
7178
7179
7180
7181
7182
7183
7184
7185
7186
7187
7188
7189
7190
7191
7192
7193
7194
7195
7196
7197
7198
7199
7200
7201
7202
7203
7204
7205
7206
7207
7208
7209
7210
7211
7212
7213
7214
7215
7216
7217
7218
7219
7220
7221
7222
7223
7224
7225
7226
7227
7228
7229
7230
7231
7232
7233
7234
7235
7236
7237
7238
7239
7240
7241
7242
7243
7244
7245
7246
7247
7248
7249
7250
7251
7252
7253
7254
7255
7256
7257
7258
7259
7260
7261
7262
7263
7264
7265
7266
7267
7268
7269
7270
7271
7272
7273
7274
7275
7276
7277
7278
7279
7280
7281
7282
7283
7284
7285
7286
7287
7288
7289
7290
7291
7292
7293
7294
7295
7296
7297
7298
7299
7300
7301
7302
7303
7304
7305
7306
7307
7308
7309
7310
7311
7312
7313
7314
7315
7316
7317
7318
7319
7320
7321
7322
7323
7324
7325
7326
7327
7328
7329
7330
7331
7332
7333
7334
7335
7336
7337
7338
7339
7340
7341
7342
7343
7344
7345
7346
7347
7348
7349
7350
7351
7352
7353
7354
7355
7356
7357
7358
7359
7360
7361
7362
7363
7364
7365
7366
7367
7368
7369
7370
7371
7372
7373
7374
7375
7376
7377
7378
7379
7380
7381
7382
7383
7384
7385
7386
7387
7388
7389
7390
7391
7392
7393
7394
7395
7396
7397
7398
7399
7400
7401
7402
7403
7404
7405
7406
7407
7408
7409
7410
7411
7412
7413
7414
7415
7416
7417
7418
7419
7420
7421
7422
7423
7424
7425
7426
7427
7428
7429
7430
7431
7432
7433
7434
7435
7436
7437
7438
7439
7440
7441
7442
7443
7444
7445
7446
7447
7448
7449
7450
7451
7452
7453
7454
7455
7456
7457
7458
7459
7460
7461
7462
7463
7464
7465
7466
7467
7468
7469
7470
7471
7472
7473
7474
7475
7476
7477
7478
7479
7480
7481
7482
7483
7484
7485
7486
7487
7488
7489
7490
7491
7492
7493
7494
7495
7496
7497
7498
7499
7500
7501
7502
7503
7504
7505
7506
7507
7508
7509
7510
7511
7512
7513
7514
7515
7516
7517
7518
7519
7520
7521
7522
7523
7524
7525
7526
7527
7528
7529
7530
7531
7532
7533
7534
7535
7536
7537
7538
7539
7540
7541
7542
7543
7544
7545
7546
7547
7548
7549
7550
7551
7552
7553
7554
7555
7556
7557
7558
7559
7560
7561
7562
7563
7564
7565
7566
7567
7568
7569
7570
7571
7572
7573
7574
7575
7576
7577
7578
7579
7580
7581
7582
7583
7584
7585
7586
7587
7588
7589
7590
7591
7592
7593
7594
7595
7596
7597
7598
7599
7600
7601
7602
7603
7604
7605
7606
7607
7608
7609
7610
7611
7612
7613
7614
7615
7616
7617
7618
7619
7620
7621
7622
7623
7624
7625
7626
7627
7628
7629
7630
7631
7632
7633
7634
7635
7636
7637
7638
7639
7640
7641
7642
7643
7644
7645
7646
7647
7648
7649
7650
7651
7652
7653
7654
7655
7656
7657
7658
7659
7660
7661
7662
7663
7664
7665
7666
7667
7668
7669
7670
7671
7672
7673
7674
7675
7676
7677
7678
7679
7680
7681
7682
7683
7684
7685
7686
7687
7688
7689
7690
7691
7692
7693
7694
7695
7696
7697
7698
7699
7700
7701
7702
7703
7704
7705
7706
7707
7708
7709
7710
7711
7712
7713
7714
7715
7716
7717
7718
7719
7720
7721
7722
7723
7724
7725
7726
7727
7728
7729
7730
7731
7732
7733
7734
7735
7736
7737
7738
7739
7740
7741
7742
7743
7744
7745
7746
7747
7748
7749
7750
7751
7752
7753
7754
7755
7756
7757
7758
7759
7760
7761
7762
7763
7764
7765
7766
7767
7768
7769
7770
7771
7772
7773
7774
7775
7776
7777
7778
7779
7780
7781
7782
7783
7784
7785
7786
7787
7788
7789
7790
7791
7792
7793
7794
7795
7796
7797
7798
7799
7800
7801
7802
7803
7804
7805
7806
7807
7808
7809
7810
7811
7812
7813
7814
7815
7816
7817
7818
7819
7820
7821
7822
7823
7824
7825
7826
7827
7828
7829
7830
7831
7832
7833
7834
7835
7836
7837
7838
7839
7840
7841
7842
7843
7844
7845
7846
7847
7848
7849
7850
7851
7852
7853
7854
7855
7856
7857
7858
7859
7860
7861
7862
7863
7864
7865
7866
7867
7868
7869
7870
7871
7872
7873
7874
7875
7876
7877
7878
7879
7880
7881
7882
7883
7884
7885
7886
7887
7888
7889
7890
7891
7892
7893
7894
7895
7896
7897
7898
7899
7900
7901
7902
7903
7904
7905
7906
7907
7908
7909
7910
7911
7912
7913
7914
7915
7916
7917
7918
7919
7920
7921
7922
7923
7924
7925
7926
7927
7928
7929
7930
7931
7932
7933
7934
7935
7936
7937
7938
7939
7940
7941
7942
7943
7944
7945
7946
7947
7948
7949
7950
7951
7952
7953
7954
7955
7956
7957
7958
7959
7960
7961
7962
7963
7964
7965
7966
7967
7968
7969
7970
7971
7972
7973
7974
7975
7976
7977
7978
7979
7980
7981
7982
7983
7984
7985
7986
7987
7988
7989
7990
7991
7992
7993
7994
7995
7996
7997
7998
7999
8000
8001
8002
8003
8004
8005
8006
8007
8008
8009
8010
8011
8012
8013
8014
8015
8016
8017
8018
8019
8020
8021
8022
8023
8024
8025
8026
8027
8028
8029
8030
8031
8032
8033
8034
8035
8036
8037
8038
8039
8040
8041
8042
8043
8044
8045
8046
8047
8048
8049
8050
8051
8052
8053
8054
8055
8056
8057
8058
8059
8060
8061
8062
8063
8064
8065
8066
8067
8068
8069
8070
8071
8072
8073
8074
8075
8076
8077
8078
8079
8080
8081
8082
8083
8084
8085
8086
8087
8088
8089
8090
8091
8092
8093
8094
8095
8096
8097
8098
8099
8100
8101
8102
8103
8104
8105
8106
8107
8108
8109
8110
8111
8112
8113
8114
8115
8116
8117
8118
8119
8120
8121
8122
8123
8124
8125
8126
8127
8128
8129
8130
8131
8132
8133
8134
8135
8136
8137
8138
8139
8140
8141
8142
8143
8144
8145
8146
8147
8148
8149
8150
8151
8152
8153
8154
8155
8156
8157
8158
8159
8160
8161
8162
8163
8164
8165
8166
8167
8168
8169
8170
8171
8172
8173
8174
8175
8176
8177
8178
8179
8180
8181
8182
8183
8184
8185
8186
8187
8188
8189
8190
8191
8192
8193
8194
8195
8196
8197
8198
8199
8200
8201
8202
8203
8204
8205
8206
8207
8208
8209
8210
8211
8212
8213
8214
8215
8216
8217
8218
8219
8220
8221
8222
8223
8224
8225
8226
8227
8228
8229
8230
8231
8232
8233
8234
8235
8236
8237
8238
8239
8240
8241
8242
8243
8244
8245
8246
8247
8248
8249
8250
8251
8252
8253
8254
8255
8256
8257
8258
8259
8260
8261
8262
8263
8264
8265
8266
8267
8268
8269
8270
8271
8272
8273
8274
8275
8276
8277
8278
8279
8280
8281
8282
8283
8284
8285
8286
8287
8288
8289
8290
8291
8292
8293
8294
8295
8296
8297
8298
8299
8300
8301
8302
8303
8304
8305
8306
8307
8308
8309
8310
8311
8312
8313
8314
8315
8316
8317
8318
8319
8320
8321
8322
8323
8324
8325
8326
8327
8328
8329
8330
8331
8332
8333
8334
8335
8336
8337
8338
8339
8340
8341
8342
8343
8344
8345
8346
8347
8348
8349
8350
8351
8352
8353
8354
8355
8356
8357
8358
8359
8360
8361
8362
8363
8364
8365
8366
8367
8368
8369
8370
8371
8372
8373
8374
8375
8376
8377
8378
8379
8380
8381
8382
8383
8384
8385
8386
8387
8388
8389
8390
8391
8392
8393
8394
8395
8396
8397
8398
8399
8400
8401
8402
8403
8404
8405
8406
8407
8408
8409
8410
8411
8412
8413
8414
8415
8416
8417
8418
8419
8420
8421
8422
8423
8424
8425
8426
8427
8428
8429
8430
8431
8432
8433
8434
8435
8436
8437
8438
8439
8440
8441
8442
8443
8444
8445
8446
8447
8448
8449
8450
8451
8452
8453
8454
8455
8456
8457
8458
8459
8460
8461
8462
8463
8464
8465
8466
8467
8468
8469
8470
8471
8472
8473
8474
8475
8476
8477
8478
8479
8480
8481
8482
8483
8484
8485
8486
8487
8488
8489
8490
8491
8492
8493
8494
8495
8496
8497
8498
8499
8500
8501
8502
8503
8504
8505
8506
8507
8508
8509
8510
8511
8512
8513
8514
8515
8516
8517
8518
8519
8520
8521
8522
8523
8524
8525
8526
8527
8528
8529
8530
8531
8532
8533
8534
8535
8536
8537
8538
8539
8540
8541
8542
8543
8544
8545
8546
8547
8548
8549
8550
8551
8552
8553
8554
8555
8556
8557
8558
8559
8560
8561
8562
8563
8564
8565
8566
8567
8568
8569
8570
8571
8572
8573
8574
8575
8576
8577
8578
8579
8580
8581
8582
8583
8584
8585
8586
8587
8588
8589
8590
8591
8592
8593
8594
8595
8596
8597
8598
8599
8600
8601
8602
8603
8604
8605
8606
8607
8608
8609
8610
8611
8612
8613
8614
8615
8616
8617
8618
8619
8620
8621
8622
8623
8624
8625
8626
8627
8628
8629
8630
8631
8632
8633
8634
8635
8636
8637
8638
8639
8640
8641
8642
8643
8644
8645
8646
8647
8648
8649
8650
8651
8652
8653
8654
8655
8656
8657
8658
8659
8660
8661
8662
8663
8664
8665
8666
8667
8668
8669
8670
8671
8672
8673
8674
8675
8676
8677
8678
8679
8680
8681
8682
8683
8684
8685
8686
8687
8688
8689
8690
8691
8692
8693
8694
8695
8696
8697
8698
8699
8700
8701
8702
8703
8704
8705
8706
8707
8708
8709
8710
8711
8712
8713
8714
8715
8716
8717
8718
8719
8720
8721
8722
8723
8724
8725
8726
8727
8728
8729
8730
8731
8732
8733
8734
8735
8736
8737
8738
8739
8740
8741
8742
8743
8744
8745
8746
8747
8748
8749
8750
8751
8752
8753
8754
8755
8756
8757
8758
8759
8760
8761
8762
8763
8764
8765
8766
8767
8768
8769
8770
8771
8772
8773
8774
8775
8776
8777
8778
8779
8780
8781
8782
8783
8784
8785
8786
8787
8788
8789
8790
8791
8792
8793
8794
8795
8796
8797
8798
8799
8800
8801
8802
8803
8804
8805
8806
8807
8808
8809
8810
8811
8812
8813
8814
8815
8816
8817
8818
8819
8820
8821
8822
8823
8824
8825
8826
8827
8828
8829
8830
8831
8832
8833
8834
8835
8836
8837
8838
8839
8840
8841
8842
8843
8844
8845
8846
8847
8848
8849
8850
8851
8852
8853
8854
8855
8856
8857
8858
8859
8860
8861
8862
8863
8864
8865
8866
8867
8868
8869
8870
8871
8872
8873
8874
8875
8876
8877
8878
8879
8880
8881
8882
8883
8884
8885
8886
8887
8888
8889
8890
8891
8892
8893
8894
8895
8896
8897
8898
8899
8900
8901
8902
8903
8904
8905
8906
8907
8908
8909
8910
8911
8912
8913
8914
8915
8916
8917
8918
8919
8920
8921
8922
8923
8924
8925
8926
8927
8928
8929
8930
8931
8932
8933
8934
8935
8936
8937
8938
8939
8940
8941
8942
8943
8944
8945
8946
8947
8948
8949
8950
8951
8952
8953
8954
8955
8956
8957
8958
8959
8960
8961
8962
8963
8964
8965
8966
8967
8968
8969
8970
8971
8972
8973
8974
8975
8976
8977
8978
8979
8980
8981
8982
8983
8984
8985
8986
8987
8988
8989
8990
8991
8992
8993
8994
8995
8996
8997
8998
8999
9000
9001
9002
9003
9004
9005
9006
9007
9008
9009
9010
9011
9012
9013
9014
9015
9016
9017
9018
9019
9020
9021
9022
9023
9024
9025
9026
9027
9028
9029
9030
9031
9032
9033
9034
9035
9036
9037
9038
9039
9040
9041
9042
9043
9044
9045
9046
9047
9048
9049
9050
9051
9052
9053
9054
9055
9056
9057
9058
9059
9060
9061
9062
9063
9064
9065
9066
9067
9068
9069
9070
9071
9072
9073
9074
9075
9076
9077
9078
9079
9080
9081
9082
9083
9084
9085
9086
9087
9088
9089
9090
9091
9092
9093
9094
9095
9096
9097
9098
9099
9100
9101
9102
9103
9104
9105
9106
9107
9108
9109
9110
9111
9112
9113
9114
9115
9116
9117
9118
9119
9120
9121
9122
9123
9124
9125
9126
9127
9128
9129
9130
9131
9132
9133
9134
9135
9136
9137
9138
9139
9140
9141
9142
9143
9144
9145
9146
9147
9148
9149
9150
9151
9152
9153
9154
9155
9156
9157
9158
9159
9160
9161
9162
9163
9164
9165
9166
9167
9168
9169
9170
9171
9172
9173
9174
9175
9176
9177
9178
9179
9180
9181
9182
9183
9184
9185
9186
9187
9188
9189
9190
9191
9192
9193
9194
9195
9196
9197
9198
9199
9200
9201
9202
9203
9204
9205
9206
9207
9208
9209
9210
9211
9212
9213
9214
9215
9216
9217
9218
9219
9220
9221
9222
9223
9224
9225
9226
9227
9228
9229
9230
9231
9232
9233
9234
9235
9236
9237
9238
9239
9240
9241
9242
9243
9244
9245
9246
9247
9248
9249
9250
9251
9252
9253
9254
9255
9256
9257
9258
9259
9260
9261
9262
9263
9264
9265
9266
9267
9268
9269
9270
9271
9272
9273
9274
9275
9276
9277
9278
9279
9280
9281
9282
9283
9284
9285
9286
9287
9288
9289
9290
9291
9292
9293
9294
9295
9296
9297
9298
9299
9300
9301
9302
9303
9304
9305
9306
9307
9308
9309
9310
9311
9312
9313
9314
9315
9316
9317
9318
9319
9320
9321
9322
9323
9324
9325
9326
9327
9328
9329
9330
9331
9332
9333
9334
9335
9336
9337
9338
9339
9340
9341
9342
9343
9344
9345
9346
9347
9348
9349
9350
9351
9352
9353
9354
9355
9356
9357
9358
9359
9360
9361
9362
9363
9364
9365
9366
9367
9368
9369
9370
9371
9372
9373
9374
9375
9376
9377
9378
9379
9380
9381
9382
9383
9384
9385
9386
9387
9388
9389
9390
9391
9392
9393
9394
9395
9396
9397
9398
9399
9400
9401
9402
9403
9404
9405
9406
9407
9408
9409
9410
9411
9412
9413
9414
9415
9416
9417
9418
9419
9420
9421
9422
9423
9424
9425
9426
9427
9428
9429
9430
9431
9432
9433
9434
9435
9436
9437
9438
9439
9440
9441
9442
9443
9444
9445
9446
9447
9448
9449
9450
9451
9452
9453
9454
9455
9456
9457
9458
9459
9460
9461
9462
9463
9464
9465
9466
9467
9468
9469
9470
9471
9472
9473
9474
9475
9476
9477
9478
9479
9480
9481
9482
9483
9484
9485
9486
9487
9488
9489
9490
9491
9492
9493
9494
9495
9496
9497
9498
9499
9500
9501
9502
9503
9504
9505
9506
9507
9508
9509
9510
9511
9512
9513
9514
9515
9516
9517
9518
9519
9520
9521
9522
9523
9524
9525
9526
9527
9528
9529
9530
9531
9532
9533
9534
9535
9536
9537
9538
9539
9540
9541
9542
9543
9544
9545
9546
9547
9548
9549
9550
9551
9552
9553
9554
9555
9556
9557
9558
9559
9560
9561
9562
9563
9564
9565
9566
9567
9568
9569
9570
9571
9572
9573
9574
9575
9576
9577
9578
9579
9580
9581
9582
9583
9584
9585
9586
9587
9588
9589
9590
9591
9592
9593
9594
9595
9596
9597
9598
9599
9600
9601
9602
9603
9604
9605
9606
9607
9608
9609
9610
9611
9612
9613
9614
9615
9616
9617
9618
9619
9620
9621
9622
9623
9624
9625
9626
9627
9628
9629
9630
9631
9632
9633
9634
9635
9636
9637
9638
9639
9640
9641
9642
9643
9644
9645
9646
9647
9648
9649
9650
9651
9652
9653
9654
9655
9656
9657
9658
9659
9660
9661
9662
9663
9664
9665
9666
9667
9668
9669
9670
9671
9672
9673
9674
9675
9676
9677
9678
9679
9680
9681
9682
9683
9684
9685
9686
9687
9688
9689
9690
9691
9692
9693
9694
9695
9696
9697
9698
9699
9700
9701
9702
9703
9704
9705
9706
9707
9708
9709
9710
9711
9712
9713
9714
9715
9716
9717
9718
9719
9720
9721
9722
9723
9724
9725
9726
9727
9728
9729
9730
9731
9732
9733
9734
9735
9736
9737
9738
9739
9740
9741
9742
9743
9744
9745
9746
9747
9748
9749
9750
9751
9752
9753
9754
9755
9756
9757
9758
9759
9760
9761
9762
9763
9764
9765
9766
9767
9768
9769
9770
9771
9772
9773
9774
9775
9776
9777
9778
9779
9780
9781
9782
9783
9784
9785
9786
9787
9788
9789
9790
9791
9792
9793
9794
9795
9796
9797
9798
9799
9800
9801
9802
9803
9804
9805
9806
9807
9808
9809
9810
9811
9812
9813
9814
9815
9816
9817
9818
9819
9820
9821
9822
9823
9824
9825
9826
9827
9828
9829
9830
9831
9832
9833
9834
9835
9836
9837
9838
9839
9840
9841
9842
9843
9844
9845
9846
9847
9848
9849
9850
9851
9852
9853
9854
9855
9856
9857
9858
9859
9860
9861
9862
9863
9864
9865
9866
9867
9868
9869
9870
9871
9872
9873
9874
9875
9876
9877
9878
9879
9880
9881
9882
9883
9884
9885
9886
9887
9888
9889
9890
9891
9892
9893
9894
9895
9896
9897
9898
9899
9900
9901
9902
9903
9904
9905
9906
9907
9908
9909
9910
9911
9912
9913
9914
9915
9916
9917
9918
9919
9920
9921
9922
9923
9924
9925
9926
9927
9928
9929
9930
9931
9932
9933
9934
9935
9936
9937
9938
9939
9940
9941
9942
9943
9944
9945
9946
9947
9948
9949
9950
9951
9952
9953
9954
9955
9956
9957
9958
9959
9960
9961
9962
9963
9964
9965
9966
9967
9968
9969
9970
9971
9972
9973
9974
9975
9976
9977
9978
9979
9980
9981
9982
9983
9984
9985
9986
9987
9988
9989
9990
9991
9992
9993
9994
9995
9996
9997
9998
9999
10000
10001
10002
10003
10004
10005
10006
10007
10008
10009
10010
10011
10012
10013
10014
10015
10016
10017
10018
10019
10020
10021
10022
10023
10024
10025
10026
10027
10028
10029
10030
10031
10032
10033
10034
10035
10036
10037
10038
10039
10040
10041
10042
10043
10044
10045
10046
10047
10048
10049
10050
10051
10052
10053
10054
10055
10056
10057
10058
10059
10060
10061
10062
10063
10064
10065
10066
10067
10068
10069
10070
10071
10072
10073
10074
10075
10076
10077
10078
10079
10080
10081
10082
10083
10084
10085
10086
10087
10088
10089
10090
10091
10092
10093
10094
10095
10096
10097
10098
10099
10100
10101
10102
10103
10104
10105
10106
10107
10108
10109
10110
10111
10112
10113
10114
10115
10116
10117
10118
10119
10120
10121
10122
10123
10124
10125
10126
10127
10128
10129
10130
10131
10132
10133
10134
10135
10136
10137
10138
10139
10140
10141
10142
10143
10144
10145
10146
10147
10148
10149
10150
10151
10152
10153
10154
10155
10156
10157
10158
10159
10160
10161
10162
10163
10164
10165
10166
10167
10168
10169
10170
10171
10172
10173
10174
10175
10176
10177
10178
10179
10180
10181
10182
10183
10184
10185
10186
10187
10188
10189
10190
10191
10192
10193
10194
10195
10196
10197
10198
10199
10200
10201
10202
10203
10204
10205
10206
10207
10208
10209
10210
10211
10212
10213
10214
10215
10216
10217
10218
10219
10220
10221
10222
10223
10224
10225
10226
10227
10228
10229
10230
10231
10232
10233
10234
10235
10236
10237
10238
10239
10240
10241
10242
10243
10244
10245
10246
10247
10248
10249
10250
10251
10252
10253
10254
10255
10256
10257
10258
10259
10260
10261
10262
10263
10264
10265
10266
10267
10268
10269
10270
10271
10272
10273
10274
10275
10276
10277
10278
10279
10280
10281
10282
10283
10284
10285
10286
10287
10288
10289
10290
10291
10292
10293
10294
10295
10296
10297
10298
10299
10300
10301
10302
10303
10304
10305
10306
10307
10308
10309
10310
10311
10312
10313
10314
10315
10316
10317
10318
10319
10320
10321
10322
10323
10324
10325
10326
10327
10328
10329
10330
10331
10332
10333
10334
10335
10336
10337
10338
10339
10340
10341
10342
10343
10344
10345
10346
10347
10348
10349
10350
10351
10352
10353
10354
10355
10356
10357
10358
10359
10360
10361
10362
10363
10364
10365
10366
10367
10368
10369
10370
10371
10372
10373
10374
10375
10376
10377
10378
10379
10380
10381
10382
10383
10384
10385
10386
10387
10388
10389
10390
10391
10392
10393
10394
10395
10396
10397
10398
10399
10400
10401
10402
10403
10404
10405
10406
10407
10408
10409
10410
10411
10412
10413
10414
10415
10416
10417
10418
10419
10420
10421
10422
10423
10424
10425
10426
10427
10428
10429
10430
10431
10432
10433
10434
10435
10436
10437
10438
10439
10440
10441
10442
10443
10444
10445
10446
10447
10448
10449
10450
10451
10452
10453
10454
10455
10456
10457
10458
10459
10460
10461
10462
10463
10464
10465
10466
10467
10468
10469
10470
10471
10472
10473
10474
10475
10476
10477
10478
10479
10480
10481
10482
10483
10484
10485
10486
10487
10488
10489
10490
10491
10492
10493
10494
10495
10496
10497
10498
10499
10500
10501
10502
10503
10504
10505
10506
10507
10508
10509
10510
10511
10512
10513
10514
10515
10516
10517
10518
10519
10520
10521
10522
10523
10524
10525
10526
10527
10528
10529
10530
10531
10532
10533
10534
10535
10536
10537
10538
10539
10540
10541
10542
10543
10544
10545
10546
10547
10548
10549
10550
10551
10552
10553
10554
10555
10556
10557
10558
10559
10560
10561
10562
10563
10564
10565
10566
10567
10568
10569
10570
10571
10572
10573
10574
10575
10576
10577
10578
10579
10580
10581
10582
10583
10584
10585
10586
10587
10588
10589
10590
10591
10592
10593
10594
10595
10596
10597
10598
10599
10600
10601
10602
10603
10604
10605
10606
10607
10608
10609
10610
10611
10612
10613
10614
10615
10616
10617
10618
10619
10620
10621
10622
10623
10624
10625
10626
10627
10628
10629
10630
10631
10632
10633
10634
10635
10636
10637
10638
10639
10640
10641
10642
10643
10644
10645
10646
10647
10648
10649
10650
10651
10652
10653
10654
10655
10656
10657
10658
10659
10660
10661
10662
10663
10664
10665
10666
10667
10668
10669
10670
10671
10672
10673
10674
10675
10676
10677
10678
10679
10680
10681
10682
10683
10684
10685
10686
10687
10688
10689
10690
10691
10692
10693
10694
10695
10696
10697
10698
10699
10700
10701
10702
10703
10704
10705
10706
10707
10708
10709
10710
10711
10712
10713
10714
10715
10716
10717
10718
10719
10720
10721
10722
10723
10724
10725
10726
10727
10728
10729
10730
10731
10732
10733
10734
10735
10736
10737
10738
10739
10740
10741
10742
10743
10744
10745
10746
10747
10748
10749
10750
10751
10752
10753
10754
10755
10756
10757
10758
10759
10760
10761
10762
10763
10764
10765
10766
10767
10768
10769
10770
10771
10772
10773
10774
10775
10776
10777
10778
10779
10780
10781
10782
10783
10784
10785
10786
10787
10788
10789
10790
10791
10792
10793
10794
10795
10796
10797
10798
10799
10800
10801
10802
10803
10804
10805
10806
10807
10808
10809
10810
10811
10812
10813
10814
10815
10816
10817
10818
10819
10820
10821
10822
10823
10824
10825
10826
10827
10828
10829
10830
10831
10832
10833
10834
10835
10836
10837
10838
10839
10840
10841
10842
10843
10844
10845
10846
10847
10848
10849
10850
10851
10852
10853
10854
10855
10856
10857
10858
10859
10860
10861
10862
10863
10864
10865
10866
10867
10868
10869
10870
10871
10872
10873
10874
10875
10876
10877
10878
10879
10880
10881
10882
10883
10884
10885
10886
10887
10888
10889
10890
10891
10892
10893
10894
10895
10896
10897
10898
10899
10900
10901
10902
10903
10904
10905
10906
10907
10908
10909
10910
10911
10912
10913
10914
10915
10916
10917
10918
10919
10920
10921
10922
10923
10924
10925
10926
10927
10928
10929
10930
10931
10932
10933
10934
10935
10936
10937
10938
10939
10940
10941
10942
10943
10944
10945
10946
10947
10948
10949
10950
10951
10952
10953
10954
10955
10956
10957
10958
10959
10960
10961
10962
10963
10964
10965
10966
10967
10968
10969
10970
10971
10972
10973
10974
10975
10976
10977
10978
10979
10980
10981
10982
10983
10984
10985
10986
10987
10988
10989
10990
10991
10992
10993
10994
10995
10996
10997
10998
10999
11000
11001
11002
11003
11004
11005
11006
11007
11008
11009
11010
11011
11012
11013
11014
11015
11016
11017
11018
11019
11020
11021
11022
11023
11024
11025
11026
11027
11028
11029
11030
11031
11032
11033
11034
11035
11036
11037
11038
11039
11040
11041
11042
11043
11044
11045
11046
11047
11048
11049
11050
11051
11052
11053
11054
11055
11056
11057
11058
11059
11060
11061
11062
11063
11064
11065
11066
11067
11068
11069
11070
11071
11072
11073
11074
11075
11076
11077
11078
11079
11080
11081
11082
11083
11084
11085
11086
11087
11088
11089
11090
11091
11092
11093
11094
11095
11096
11097
11098
11099
11100
11101
11102
11103
11104
11105
11106
11107
11108
11109
11110
11111
11112
11113
11114
11115
11116
11117
11118
11119
11120
11121
11122
11123
11124
11125
11126
11127
11128
11129
11130
11131
11132
11133
11134
11135
11136
11137
11138
11139
11140
11141
11142
11143
11144
11145
11146
11147
11148
11149
11150
11151
11152
11153
11154
11155
11156
11157
11158
11159
11160
11161
11162
11163
11164
11165
11166
11167
11168
11169
11170
11171
11172
11173
11174
11175
11176
11177
11178
11179
11180
11181
11182
11183
11184
11185
11186
11187
11188
11189
11190
11191
11192
11193
11194
11195
11196
11197
11198
11199
11200
11201
11202
11203
11204
11205
11206
11207
11208
11209
11210
11211
11212
11213
11214
11215
11216
11217
11218
11219
11220
11221
11222
11223
11224
11225
11226
11227
11228
11229
11230
11231
11232
11233
11234
11235
11236
11237
11238
11239
11240
11241
11242
11243
11244
11245
11246
11247
11248
11249
11250
11251
11252
11253
11254
11255
11256
11257
11258
11259
11260
11261
11262
11263
11264
11265
11266
11267
11268
11269
11270
11271
11272
11273
11274
11275
11276
11277
11278
11279
11280
11281
11282
11283
11284
11285
11286
11287
11288
11289
11290
11291
11292
11293
11294
11295
11296
11297
11298
11299
11300
11301
11302
11303
11304
11305
11306
11307
11308
11309
11310
11311
11312
11313
11314
11315
11316
11317
11318
11319
11320
11321
11322
11323
11324
11325
11326
11327
11328
11329
11330
11331
11332
11333
11334
11335
11336
11337
11338
11339
11340
11341
11342
11343
11344
11345
11346
11347
11348
11349
11350
11351
11352
11353
11354
11355
11356
11357
11358
11359
11360
11361
11362
11363
11364
11365
11366
11367
11368
11369
11370
11371
11372
11373
11374
11375
11376
11377
11378
11379
11380
11381
11382
11383
11384
11385
11386
11387
11388
11389
11390
11391
11392
11393
11394
11395
11396
11397
11398
11399
11400
11401
11402
11403
11404
11405
11406
11407
11408
11409
11410
11411
11412
11413
11414
11415
11416
11417
11418
11419
11420
11421
11422
11423
11424
11425
11426
11427
11428
11429
11430
11431
11432
11433
11434
11435
11436
11437
11438
11439
11440
11441
11442
11443
11444
11445
11446
11447
11448
11449
11450
11451
11452
11453
11454
11455
11456
11457
11458
11459
11460
11461
11462
11463
11464
11465
11466
11467
11468
11469
11470
11471
11472
11473
11474
11475
11476
11477
11478
11479
11480
11481
11482
11483
11484
11485
11486
11487
11488
11489
11490
11491
11492
11493
11494
11495
11496
11497
11498
11499
11500
11501
11502
11503
11504
11505
11506
11507
11508
11509
11510
11511
11512
11513
11514
11515
11516
11517
11518
11519
11520
11521
11522
11523
11524
11525
11526
11527
11528
11529
11530
11531
11532
11533
11534
11535
11536
11537
11538
11539
11540
11541
11542
11543
11544
11545
11546
11547
11548
11549
11550
11551
11552
11553
11554
11555
11556
11557
11558
11559
11560
11561
11562
11563
11564
11565
11566
11567
11568
11569
11570
11571
11572
11573
11574
11575
11576
11577
11578
11579
11580
11581
11582
11583
11584
11585
11586
11587
11588
11589
11590
11591
11592
11593
11594
11595
11596
11597
11598
11599
11600
11601
11602
11603
11604
11605
11606
11607
11608
11609
11610
11611
11612
11613
11614
11615
11616
11617
11618
11619
11620
11621
11622
11623
11624
11625
11626
11627
11628
11629
11630
11631
11632
11633
11634
11635
11636
11637
11638
11639
11640
11641
11642
11643
11644
11645
11646
11647
11648
11649
11650
11651
11652
11653
11654
11655
11656
11657
11658
11659
11660
11661
11662
11663
11664
11665
11666
11667
11668
11669
11670
11671
11672
11673
11674
11675
11676
11677
11678
11679
11680
11681
11682
11683
11684
11685
11686
11687
11688
11689
11690
11691
11692
11693
11694
11695
11696
11697
11698
11699
11700
11701
11702
11703
11704
11705
11706
11707
11708
11709
11710
11711
11712
11713
11714
11715
11716
11717
11718
11719
11720
11721
11722
11723
11724
11725
11726
11727
11728
11729
11730
11731
11732
11733
11734
11735
11736
11737
11738
11739
11740
11741
11742
11743
11744
11745
11746
11747
11748
11749
11750
11751
11752
11753
11754
11755
11756
11757
11758
11759
11760
11761
11762
11763
11764
11765
11766
11767
11768
11769
11770
11771
11772
11773
11774
11775
11776
11777
11778
11779
11780
11781
11782
11783
11784
11785
11786
11787
11788
11789
11790
11791
11792
11793
11794
11795
11796
11797
11798
11799
11800
11801
11802
11803
11804
11805
11806
11807
11808
11809
11810
11811
11812
11813
11814
11815
11816
11817
11818
11819
11820
11821
11822
11823
11824
11825
11826
11827
11828
11829
11830
11831
11832
11833
11834
11835
11836
11837
11838
11839
11840
11841
11842
11843
11844
11845
11846
11847
11848
11849
11850
11851
11852
11853
11854
11855
11856
11857
11858
11859
11860
11861
11862
11863
11864
11865
11866
11867
11868
11869
11870
11871
11872
11873
11874
11875
11876
11877
11878
11879
11880
11881
11882
11883
11884
11885
11886
11887
11888
11889
11890
11891
11892
11893
11894
11895
11896
11897
11898
11899
11900
11901
11902
11903
11904
11905
11906
11907
11908
11909
11910
11911
11912
11913
11914
11915
11916
11917
11918
11919
11920
11921
11922
11923
11924
11925
11926
11927
11928
11929
11930
11931
11932
11933
11934
11935
11936
11937
11938
11939
11940
11941
11942
11943
11944
11945
11946
11947
11948
11949
11950
11951
11952
11953
11954
11955
11956
11957
11958
11959
11960
11961
11962
11963
11964
11965
11966
11967
11968
11969
11970
11971
11972
11973
11974
11975
11976
11977
11978
11979
11980
11981
11982
11983
11984
11985
11986
11987
11988
11989
11990
11991
11992
11993
11994
11995
11996
11997
11998
11999
12000
12001
12002
12003
12004
12005
12006
12007
12008
12009
12010
12011
12012
12013
12014
12015
12016
12017
12018
12019
12020
12021
12022
12023
12024
12025
12026
12027
12028
12029
12030
12031
12032
12033
12034
12035
12036
12037
12038
12039
12040
12041
12042
12043
12044
12045
12046
12047
12048
12049
12050
12051
12052
12053
12054
12055
12056
12057
12058
12059
12060
12061
12062
12063
12064
12065
12066
12067
12068
12069
12070
12071
12072
12073
12074
12075
12076
12077
12078
12079
12080
12081
12082
12083
12084
12085
12086
12087
12088
12089
12090
12091
12092
12093
12094
12095
12096
12097
12098
12099
12100
12101
12102
12103
12104
12105
12106
12107
12108
12109
12110
12111
12112
12113
12114
12115
12116
12117
12118
12119
12120
12121
12122
12123
12124
12125
12126
12127
12128
12129
12130
12131
12132
12133
12134
12135
12136
12137
12138
12139
12140
12141
12142
12143
12144
12145
12146
12147
12148
12149
12150
12151
12152
12153
12154
12155
12156
12157
12158
12159
12160
12161
12162
12163
12164
12165
12166
12167
12168
12169
12170
12171
12172
12173
12174
12175
12176
12177
12178
12179
12180
12181
12182
12183
12184
12185
12186
12187
12188
12189
12190
12191
12192
12193
12194
12195
12196
12197
12198
12199
12200
12201
12202
12203
12204
12205
12206
12207
12208
12209
12210
12211
12212
12213
12214
12215
12216
12217
12218
12219
12220
12221
12222
12223
12224
12225
12226
12227
12228
12229
12230
12231
12232
12233
12234
12235
12236
12237
12238
12239
12240
12241
12242
12243
12244
12245
12246
12247
12248
12249
12250
12251
12252
12253
12254
12255
12256
12257
12258
12259
12260
12261
12262
12263
12264
12265
12266
12267
12268
12269
12270
12271
12272
12273
12274
12275
12276
12277
12278
12279
12280
12281
12282
12283
12284
12285
12286
12287
12288
12289
12290
12291
12292
12293
12294
12295
12296
12297
12298
12299
12300
12301
12302
12303
12304
12305
12306
12307
12308
12309
12310
12311
12312
12313
12314
12315
12316
12317
12318
12319
12320
12321
12322
12323
12324
12325
12326
12327
12328
12329
12330
12331
12332
12333
12334
12335
12336
12337
12338
12339
12340
12341
12342
12343
12344
12345
12346
12347
12348
12349
12350
12351
12352
12353
12354
12355
12356
12357
12358
12359
12360
12361
12362
12363
12364
12365
12366
12367
12368
12369
12370
12371
12372
12373
12374
12375
12376
12377
12378
12379
12380
12381
12382
12383
12384
12385
12386
12387
12388
12389
12390
12391
12392
12393
12394
12395
12396
12397
12398
12399
12400
12401
12402
12403
12404
12405
12406
12407
12408
12409
12410
12411
12412
12413
12414
12415
12416
12417
12418
12419
12420
12421
12422
12423
12424
12425
12426
12427
12428
12429
12430
12431
12432
12433
12434
12435
12436
12437
12438
12439
12440
12441
12442
12443
12444
12445
12446
12447
12448
12449
12450
12451
12452
12453
12454
12455
12456
12457
12458
12459
12460
12461
12462
12463
12464
12465
12466
12467
12468
12469
12470
12471
12472
12473
12474
12475
12476
12477
12478
12479
12480
12481
12482
12483
12484
12485
12486
12487
12488
12489
12490
12491
12492
12493
12494
12495
12496
12497
12498
12499
12500
12501
12502
12503
12504
12505
12506
12507
12508
12509
12510
12511
12512
12513
12514
12515
12516
12517
12518
12519
12520
12521
12522
12523
12524
12525
12526
12527
12528
12529
12530
12531
12532
12533
12534
12535
12536
12537
12538
12539
12540
12541
12542
12543
12544
12545
12546
12547
12548
12549
12550
12551
12552
12553
12554
12555
12556
12557
12558
12559
12560
12561
12562
12563
12564
12565
12566
12567
12568
12569
12570
12571
12572
12573
12574
12575
12576
12577
12578
12579
12580
12581
12582
12583
12584
12585
12586
12587
12588
12589
12590
12591
12592
12593
12594
12595
12596
12597
12598
12599
12600
12601
12602
12603
12604
12605
12606
12607
12608
12609
12610
12611
12612
12613
12614
12615
12616
12617
12618
12619
12620
12621
12622
12623
12624
12625
12626
12627
12628
12629
12630
12631
12632
12633
12634
12635
12636
12637
12638
12639
12640
12641
12642
12643
12644
12645
12646
12647
12648
12649
12650
12651
12652
12653
12654
12655
12656
12657
12658
12659
12660
12661
12662
12663
12664
12665
12666
12667
12668
12669
12670
12671
12672
12673
12674
12675
12676
12677
12678
12679
12680
12681
12682
12683
12684
12685
12686
12687
12688
12689
12690
12691
12692
12693
12694
12695
12696
12697
12698
12699
12700
12701
12702
12703
12704
12705
12706
12707
12708
12709
12710
12711
12712
12713
12714
12715
12716
12717
12718
12719
12720
12721
12722
12723
12724
12725
12726
12727
12728
12729
12730
12731
12732
12733
12734
12735
12736
12737
12738
12739
12740
12741
12742
12743
12744
12745
12746
12747
12748
12749
12750
12751
12752
12753
12754
12755
12756
12757
12758
12759
12760
12761
12762
12763
12764
12765
12766
12767
12768
12769
12770
12771
12772
12773
12774
12775
12776
12777
12778
12779
12780
12781
12782
12783
12784
12785
12786
12787
12788
12789
12790
12791
12792
12793
12794
12795
12796
12797
12798
12799
12800
12801
12802
12803
12804
12805
12806
12807
12808
12809
12810
12811
12812
12813
12814
12815
12816
12817
12818
12819
12820
12821
12822
12823
12824
12825
12826
12827
12828
12829
12830
12831
12832
12833
12834
12835
12836
12837
12838
12839
12840
12841
12842
12843
12844
12845
12846
12847
12848
12849
12850
12851
12852
12853
12854
12855
12856
12857
12858
12859
12860
12861
12862
12863
12864
12865
12866
12867
12868
12869
12870
12871
12872
12873
12874
12875
12876
12877
12878
12879
12880
12881
12882
12883
12884
12885
12886
12887
12888
12889
12890
12891
12892
12893
12894
12895
12896
12897
12898
12899
12900
12901
12902
12903
12904
12905
12906
12907
12908
12909
12910
12911
12912
12913
12914
12915
12916
12917
12918
12919
12920
12921
12922
12923
12924
12925
12926
12927
12928
12929
12930
12931
12932
12933
12934
12935
12936
12937
12938
12939
12940
12941
12942
12943
12944
12945
12946
12947
12948
12949
12950
12951
12952
12953
12954
12955
12956
12957
12958
12959
12960
12961
12962
12963
12964
12965
12966
12967
12968
12969
12970
12971
12972
12973
12974
12975
12976
12977
12978
12979
12980
12981
12982
12983
12984
12985
12986
12987
12988
12989
12990
12991
12992
12993
12994
12995
12996
12997
12998
12999
13000
13001
13002
13003
13004
13005
13006
13007
13008
13009
13010
13011
13012
13013
13014
13015
13016
13017
13018
13019
13020
13021
13022
13023
13024
13025
13026
13027
13028
13029
13030
13031
13032
13033
13034
13035
13036
13037
13038
13039
13040
13041
13042
13043
13044
13045
13046
13047
13048
13049
13050
13051
13052
13053
13054
13055
13056
13057
13058
13059
13060
13061
13062
13063
13064
13065
13066
13067
13068
13069
13070
13071
13072
13073
13074
13075
13076
13077
13078
13079
13080
13081
13082
13083
13084
13085
13086
13087
13088
13089
13090
13091
13092
13093
13094
13095
13096
13097
13098
13099
13100
13101
13102
13103
13104
13105
13106
13107
13108
13109
13110
13111
13112
13113
13114
13115
13116
13117
13118
13119
13120
13121
13122
13123
13124
13125
13126
13127
13128
13129
13130
13131
13132
13133
13134
13135
13136
13137
13138
13139
13140
13141
13142
13143
13144
13145
13146
13147
13148
13149
13150
13151
13152
13153
13154
13155
13156
13157
13158
13159
13160
13161
13162
13163
13164
13165
13166
13167
13168
13169
13170
13171
13172
13173
13174
13175
13176
13177
13178
13179
13180
13181
13182
13183
13184
13185
13186
13187
13188
13189
13190
13191
13192
13193
13194
13195
13196
13197
13198
13199
13200
13201
13202
13203
13204
13205
13206
13207
13208
13209
13210
13211
13212
13213
13214
13215
13216
13217
13218
13219
13220
13221
13222
13223
13224
13225
13226
13227
13228
13229
13230
13231
13232
13233
13234
13235
13236
13237
13238
13239
13240
13241
13242
13243
13244
13245
13246
13247
13248
13249
13250
13251
13252
13253
13254
13255
13256
13257
13258
13259
13260
13261
13262
13263
13264
13265
13266
13267
13268
13269
13270
13271
13272
13273
13274
13275
13276
13277
13278
13279
13280
13281
13282
13283
13284
13285
13286
13287
13288
13289
13290
13291
13292
13293
13294
13295
13296
13297
13298
13299
13300
13301
13302
13303
13304
13305
13306
13307
13308
13309
13310
13311
13312
13313
13314
13315
13316
13317
13318
13319
13320
13321
13322
13323
13324
13325
13326
13327
13328
13329
13330
13331
13332
13333
13334
13335
13336
13337
13338
13339
13340
13341
13342
13343
13344
13345
13346
13347
13348
13349
13350
13351
13352
13353
13354
13355
13356
13357
13358
13359
13360
13361
13362
13363
13364
13365
13366
13367
13368
13369
13370
13371
13372
13373
13374
13375
13376
13377
13378
13379
13380
13381
13382
13383
13384
13385
13386
13387
13388
13389
13390
13391
13392
13393
13394
13395
13396
13397
13398
13399
13400
13401
13402
13403
13404
13405
13406
13407
13408
13409
13410
13411
13412
13413
13414
13415
13416
13417
13418
13419
13420
13421
13422
13423
13424
13425
13426
13427
13428
13429
13430
13431
13432
13433
13434
13435
13436
13437
13438
13439
13440
13441
13442
13443
13444
13445
13446
13447
13448
13449
13450
13451
13452
13453
13454
13455
13456
13457
13458
13459
13460
13461
13462
13463
13464
13465
13466
13467
13468
13469
13470
13471
13472
13473
13474
13475
13476
13477
13478
13479
13480
13481
13482
13483
13484
13485
13486
13487
13488
13489
13490
13491
13492
13493
13494
13495
13496
13497
13498
13499
13500
13501
13502
13503
13504
13505
13506
13507
13508
13509
13510
13511
13512
13513
13514
13515
13516
13517
13518
13519
13520
13521
13522
13523
13524
13525
13526
13527
13528
13529
13530
13531
13532
13533
13534
13535
13536
13537
13538
13539
13540
13541
13542
13543
13544
13545
13546
13547
13548
13549
13550
13551
13552
13553
13554
13555
13556
13557
13558
13559
13560
13561
13562
13563
13564
13565
13566
13567
13568
13569
13570
13571
13572
13573
13574
13575
13576
13577
13578
13579
13580
13581
13582
13583
13584
13585
13586
13587
13588
13589
13590
13591
13592
13593
13594
13595
13596
13597
13598
13599
13600
13601
13602
13603
13604
13605
13606
13607
13608
13609
13610
13611
13612
13613
13614
13615
13616
13617
13618
13619
13620
13621
13622
13623
13624
13625
13626
13627
13628
13629
13630
13631
13632
13633
13634
13635
13636
13637
13638
13639
13640
13641
13642
13643
13644
13645
13646
13647
13648
13649
13650
13651
13652
13653
13654
13655
13656
13657
13658
13659
13660
13661
13662
13663
13664
13665
13666
13667
13668
13669
13670
13671
13672
13673
13674
13675
13676
13677
13678
13679
13680
13681
13682
13683
13684
13685
13686
13687
13688
13689
13690
13691
13692
13693
13694
13695
13696
13697
13698
13699
13700
13701
13702
13703
13704
13705
13706
13707
13708
13709
13710
13711
13712
13713
13714
13715
13716
13717
13718
13719
13720
13721
13722
13723
13724
13725
13726
13727
13728
13729
13730
13731
13732
13733
13734
13735
13736
13737
13738
13739
13740
13741
13742
13743
13744
13745
13746
13747
13748
13749
13750
13751
13752
13753
13754
13755
13756
13757
13758
13759
13760
13761
13762
13763
13764
13765
13766
13767
13768
13769
13770
13771
13772
13773
13774
13775
13776
13777
13778
13779
13780
13781
13782
13783
13784
13785
13786
13787
13788
13789
13790
13791
13792
13793
13794
13795
13796
13797
13798
13799
13800
13801
13802
13803
13804
13805
13806
13807
13808
13809
13810
13811
13812
13813
13814
13815
13816
13817
13818
13819
13820
13821
13822
13823
13824
13825
13826
13827
13828
13829
13830
13831
13832
13833
13834
13835
13836
13837
13838
13839
13840
13841
13842
13843
13844
13845
13846
13847
13848
13849
13850
13851
13852
13853
13854
13855
13856
13857
13858
13859
13860
13861
13862
13863
13864
13865
13866
13867
13868
13869
13870
13871
13872
13873
13874
13875
13876
13877
13878
13879
13880
13881
13882
13883
13884
13885
13886
13887
13888
13889
13890
13891
13892
13893
13894
13895
13896
13897
13898
13899
13900
13901
13902
13903
13904
13905
13906
13907
13908
13909
13910
13911
13912
13913
13914
13915
13916
13917
13918
13919
13920
13921
13922
13923
13924
13925
13926
13927
13928
13929
13930
13931
13932
13933
13934
13935
13936
13937
13938
13939
13940
13941
13942
13943
13944
13945
13946
13947
13948
13949
13950
13951
13952
13953
13954
13955
13956
13957
13958
13959
13960
13961
13962
13963
13964
13965
13966
13967
13968
13969
13970
13971
13972
13973
13974
13975
13976
13977
13978
13979
13980
13981
13982
13983
13984
13985
13986
13987
13988
13989
13990
13991
13992
13993
13994
13995
13996
13997
13998
13999
14000
14001
14002
14003
14004
14005
14006
14007
14008
14009
14010
14011
14012
14013
14014
14015
14016
14017
14018
14019
14020
14021
14022
14023
14024
14025
14026
14027
14028
14029
14030
14031
14032
14033
14034
14035
14036
14037
14038
14039
14040
14041
14042
14043
14044
14045
14046
14047
14048
14049
14050
14051
14052
14053
14054
14055
14056
14057
14058
14059
14060
14061
14062
14063
14064
14065
14066
14067
14068
14069
14070
14071
14072
14073
14074
14075
14076
14077
14078
14079
14080
14081
14082
14083
14084
14085
14086
14087
14088
14089
14090
14091
14092
14093
14094
14095
14096
14097
14098
14099
14100
14101
14102
14103
14104
14105
14106
14107
14108
14109
14110
14111
14112
14113
14114
14115
14116
14117
14118
14119
14120
14121
14122
14123
14124
14125
14126
14127
14128
14129
14130
14131
14132
14133
14134
14135
14136
14137
14138
14139
14140
14141
14142
14143
14144
14145
14146
14147
14148
14149
14150
14151
14152
14153
14154
14155
14156
14157
14158
14159
14160
14161
14162
14163
14164
14165
14166
14167
14168
14169
14170
14171
14172
14173
14174
14175
14176
14177
14178
14179
14180
14181
14182
14183
14184
14185
14186
14187
14188
14189
14190
14191
14192
14193
14194
14195
14196
14197
14198
14199
14200
14201
14202
14203
14204
14205
14206
14207
14208
14209
14210
14211
14212
14213
14214
14215
14216
14217
14218
14219
14220
14221
14222
14223
14224
14225
14226
14227
14228
14229
14230
14231
14232
14233
14234
14235
14236
14237
14238
14239
14240
14241
14242
14243
14244
14245
14246
14247
14248
14249
14250
14251
14252
14253
14254
14255
14256
14257
14258
14259
14260
14261
14262
14263
14264
14265
14266
14267
14268
14269
14270
14271
14272
14273
14274
14275
14276
14277
14278
14279
14280
14281
14282
14283
14284
14285
14286
14287
14288
14289
14290
14291
14292
14293
14294
14295
14296
14297
14298
14299
14300
14301
14302
14303
14304
14305
14306
14307
14308
14309
14310
14311
14312
14313
14314
14315
14316
14317
14318
14319
14320
14321
14322
14323
14324
14325
14326
14327
14328
14329
14330
14331
14332
14333
14334
14335
14336
14337
14338
14339
14340
14341
14342
14343
14344
14345
14346
14347
14348
14349
14350
14351
14352
14353
14354
14355
14356
14357
14358
14359
14360
14361
14362
14363
14364
14365
14366
14367
14368
14369
14370
14371
14372
14373
14374
14375
14376
14377
14378
14379
14380
14381
14382
14383
14384
14385
14386
14387
14388
14389
14390
14391
14392
14393
14394
14395
14396
14397
14398
14399
14400
14401
14402
14403
14404
14405
14406
14407
14408
14409
14410
14411
14412
14413
14414
14415
14416
14417
14418
14419
14420
14421
14422
14423
14424
14425
14426
14427
14428
14429
14430
14431
14432
14433
14434
14435
14436
14437
14438
14439
14440
14441
14442
14443
14444
14445
14446
14447
14448
14449
14450
14451
14452
14453
14454
14455
14456
14457
14458
14459
14460
14461
14462
14463
14464
14465
14466
14467
14468
14469
14470
14471
14472
14473
14474
14475
14476
14477
14478
14479
14480
14481
14482
14483
14484
14485
14486
14487
14488
14489
14490
14491
14492
14493
14494
14495
14496
14497
14498
14499
14500
14501
14502
14503
14504
14505
14506
14507
14508
14509
14510
14511
14512
14513
14514
14515
14516
14517
14518
14519
14520
14521
14522
14523
14524
14525
14526
14527
14528
14529
14530
14531
14532
14533
14534
14535
14536
14537
14538
14539
14540
14541
14542
14543
14544
14545
14546
14547
14548
14549
14550
14551
14552
14553
14554
14555
14556
14557
14558
14559
14560
14561
14562
14563
14564
14565
14566
14567
14568
14569
14570
14571
14572
14573
14574
14575
14576
14577
14578
14579
14580
14581
14582
14583
14584
14585
14586
14587
14588
14589
14590
14591
14592
14593
14594
14595
14596
14597
14598
14599
14600
14601
14602
14603
14604
14605
14606
14607
14608
14609
14610
14611
14612
14613
14614
14615
14616
14617
14618
14619
14620
14621
14622
14623
14624
14625
14626
14627
14628
14629
14630
14631
14632
14633
14634
14635
14636
14637
14638
14639
14640
14641
14642
14643
14644
14645
14646
14647
14648
14649
14650
14651
14652
14653
14654
14655
14656
14657
14658
14659
14660
14661
14662
14663
14664
14665
14666
14667
14668
14669
14670
14671
14672
14673
14674
14675
14676
14677
14678
14679
14680
14681
14682
14683
14684
14685
14686
14687
14688
14689
14690
14691
14692
14693
14694
14695
14696
14697
14698
14699
14700
14701
14702
14703
14704
14705
14706
14707
14708
14709
14710
14711
14712
14713
14714
14715
14716
14717
14718
14719
14720
14721
14722
14723
14724
14725
14726
14727
14728
14729
14730
14731
14732
14733
14734
14735
14736
14737
14738
14739
14740
14741
14742
14743
14744
14745
14746
14747
14748
14749
14750
14751
14752
14753
14754
14755
14756
14757
14758
14759
14760
14761
14762
14763
14764
14765
14766
14767
14768
14769
14770
14771
14772
14773
14774
14775
14776
14777
14778
14779
14780
14781
14782
14783
14784
14785
14786
14787
14788
14789
14790
14791
14792
14793
14794
14795
14796
14797
14798
14799
14800
14801
14802
14803
14804
14805
14806
14807
14808
14809
14810
14811
14812
14813
14814
14815
14816
14817
14818
14819
14820
14821
14822
14823
14824
14825
14826
14827
14828
14829
14830
14831
14832
14833
14834
14835
14836
14837
14838
14839
14840
14841
14842
14843
14844
14845
14846
14847
14848
14849
14850
14851
14852
14853
14854
14855
14856
14857
14858
14859
14860
14861
14862
14863
14864
14865
14866
14867
14868
14869
14870
14871
14872
14873
14874
14875
14876
14877
14878
14879
14880
14881
14882
14883
14884
14885
14886
14887
14888
14889
14890
14891
14892
14893
14894
14895
14896
14897
14898
14899
14900
14901
14902
14903
14904
14905
14906
14907
14908
14909
14910
14911
14912
14913
14914
14915
14916
14917
14918
14919
14920
14921
14922
14923
14924
14925
14926
14927
14928
14929
14930
14931
14932
14933
14934
14935
14936
14937
14938
14939
14940
14941
14942
14943
14944
14945
14946
14947
14948
14949
14950
14951
14952
14953
14954
14955
14956
14957
14958
14959
14960
14961
14962
14963
14964
14965
14966
14967
14968
14969
14970
14971
14972
14973
14974
14975
14976
14977
14978
14979
14980
14981
14982
14983
14984
14985
14986
14987
14988
14989
14990
14991
14992
14993
14994
14995
14996
14997
14998
14999
15000
15001
15002
15003
15004
15005
15006
15007
15008
15009
15010
15011
15012
15013
15014
15015
15016
15017
15018
15019
15020
15021
15022
15023
15024
15025
15026
15027
15028
15029
15030
15031
15032
15033
15034
15035
15036
15037
15038
15039
15040
15041
15042
15043
15044
15045
15046
15047
15048
15049
15050
15051
15052
15053
15054
15055
15056
15057
15058
15059
15060
15061
15062
15063
15064
15065
15066
15067
15068
15069
15070
15071
15072
15073
15074
15075
15076
15077
15078
15079
15080
15081
15082
15083
15084
15085
15086
15087
15088
15089
15090
15091
15092
15093
15094
15095
15096
15097
15098
15099
15100
15101
15102
15103
15104
15105
15106
15107
15108
15109
15110
15111
15112
15113
15114
15115
15116
15117
15118
15119
15120
15121
15122
15123
15124
15125
15126
15127
15128
15129
15130
15131
15132
15133
15134
15135
15136
15137
15138
15139
15140
15141
15142
15143
15144
15145
15146
15147
15148
15149
15150
15151
15152
15153
15154
15155
15156
15157
15158
15159
15160
15161
15162
15163
15164
15165
15166
15167
15168
15169
15170
15171
15172
15173
15174
15175
15176
15177
15178
15179
15180
15181
15182
15183
15184
15185
15186
15187
15188
15189
15190
15191
15192
15193
15194
15195
15196
15197
15198
15199
15200
15201
15202
15203
15204
15205
15206
15207
15208
15209
15210
15211
15212
15213
15214
15215
15216
15217
15218
15219
15220
15221
15222
15223
15224
15225
15226
15227
15228
15229
15230
15231
15232
15233
15234
15235
15236
15237
15238
15239
15240
15241
15242
15243
15244
15245
15246
15247
15248
15249
15250
15251
15252
15253
15254
15255
15256
15257
15258
15259
15260
15261
15262
15263
15264
15265
15266
15267
15268
15269
15270
15271
15272
15273
15274
15275
15276
15277
15278
15279
15280
15281
15282
15283
15284
15285
15286
15287
15288
15289
15290
15291
15292
15293
15294
15295
15296
15297
15298
15299
15300
15301
15302
15303
15304
15305
15306
15307
15308
15309
15310
15311
15312
15313
15314
15315
15316
15317
15318
15319
15320
15321
15322
15323
15324
15325
15326
15327
15328
15329
15330
15331
15332
15333
15334
15335
15336
15337
15338
15339
15340
15341
15342
15343
15344
15345
15346
15347
15348
15349
15350
15351
15352
15353
15354
15355
15356
15357
15358
15359
15360
15361
15362
15363
15364
15365
15366
15367
15368
15369
15370
15371
15372
15373
15374
15375
15376
15377
15378
15379
15380
15381
15382
15383
15384
15385
15386
15387
15388
15389
15390
15391
15392
15393
15394
15395
15396
15397
15398
15399
15400
15401
15402
15403
15404
15405
15406
15407
15408
15409
15410
15411
15412
15413
15414
15415
15416
15417
15418
15419
15420
15421
15422
15423
15424
15425
15426
15427
15428
15429
15430
15431
15432
15433
15434
15435
15436
15437
15438
15439
15440
15441
15442
15443
15444
15445
15446
15447
15448
15449
15450
15451
15452
15453
15454
15455
15456
15457
15458
15459
15460
15461
15462
15463
15464
15465
15466
15467
15468
15469
15470
15471
15472
15473
15474
15475
15476
15477
15478
15479
15480
15481
15482
15483
15484
15485
15486
15487
15488
15489
15490
15491
15492
15493
15494
15495
15496
15497
15498
15499
15500
15501
15502
15503
15504
15505
15506
15507
15508
15509
15510
15511
15512
15513
15514
15515
15516
15517
15518
15519
15520
15521
15522
15523
15524
15525
15526
15527
15528
15529
15530
15531
15532
15533
15534
15535
15536
15537
15538
15539
15540
15541
15542
15543
15544
15545
15546
15547
15548
15549
15550
15551
15552
15553
15554
15555
15556
15557
15558
15559
15560
15561
15562
15563
15564
15565
15566
15567
15568
15569
15570
15571
15572
15573
15574
15575
15576
15577
15578
15579
15580
15581
15582
15583
15584
15585
15586
15587
15588
15589
15590
15591
15592
15593
15594
15595
15596
15597
15598
15599
15600
15601
15602
15603
15604
15605
15606
15607
15608
15609
15610
15611
15612
15613
15614
15615
15616
15617
15618
15619
15620
15621
15622
15623
15624
15625
15626
15627
15628
15629
15630
15631
15632
15633
15634
15635
15636
15637
15638
15639
15640
15641
15642
15643
15644
15645
15646
15647
15648
15649
15650
15651
15652
15653
15654
15655
15656
15657
15658
15659
15660
15661
15662
15663
15664
15665
15666
15667
15668
15669
15670
15671
15672
15673
15674
15675
15676
15677
15678
15679
15680
15681
15682
15683
15684
15685
15686
15687
15688
15689
15690
15691
15692
15693
15694
15695
15696
15697
15698
15699
15700
15701
15702
15703
15704
15705
15706
15707
15708
15709
15710
15711
15712
15713
15714
15715
15716
15717
15718
15719
15720
15721
15722
15723
15724
15725
15726
15727
15728
15729
15730
15731
15732
15733
15734
15735
15736
15737
15738
15739
15740
15741
15742
15743
15744
15745
15746
15747
15748
15749
15750
15751
15752
15753
15754
15755
15756
15757
15758
15759
15760
15761
15762
15763
15764
15765
15766
15767
15768
15769
15770
15771
15772
15773
15774
15775
15776
15777
15778
15779
15780
15781
15782
15783
15784
15785
15786
15787
15788
15789
15790
15791
15792
15793
15794
15795
15796
15797
15798
15799
15800
15801
15802
15803
15804
15805
15806
15807
15808
15809
15810
15811
15812
15813
15814
15815
15816
15817
15818
15819
15820
15821
15822
15823
15824
15825
15826
15827
15828
15829
15830
15831
15832
15833
15834
15835
15836
15837
15838
15839
15840
15841
15842
15843
15844
15845
15846
15847
15848
15849
15850
15851
15852
15853
15854
15855
15856
15857
15858
15859
15860
15861
15862
15863
15864
15865
15866
15867
15868
15869
15870
15871
15872
15873
15874
15875
15876
15877
15878
15879
15880
15881
15882
15883
15884
15885
15886
15887
15888
15889
15890
15891
15892
15893
15894
15895
15896
15897
15898
15899
15900
15901
15902
15903
15904
15905
15906
15907
15908
15909
15910
15911
15912
15913
15914
15915
15916
15917
15918
15919
15920
15921
15922
15923
15924
15925
15926
15927
15928
15929
15930
15931
15932
15933
15934
15935
15936
15937
15938
15939
15940
15941
15942
15943
15944
15945
15946
15947
15948
15949
15950
15951
15952
15953
15954
15955
15956
15957
15958
15959
15960
15961
15962
15963
15964
15965
15966
15967
15968
15969
15970
15971
15972
15973
15974
15975
15976
15977
15978
15979
15980
15981
15982
15983
15984
15985
15986
15987
15988
15989
15990
15991
15992
15993
15994
15995
15996
15997
15998
15999
16000
16001
16002
16003
16004
16005
16006
16007
16008
16009
16010
16011
16012
16013
16014
16015
16016
16017
16018
16019
16020
16021
16022
16023
16024
16025
16026
16027
16028
16029
16030
16031
16032
16033
16034
16035
16036
16037
16038
16039
16040
16041
16042
16043
16044
16045
16046
16047
16048
16049
16050
16051
16052
16053
16054
16055
16056
16057
16058
16059
16060
16061
16062
16063
16064
16065
16066
16067
16068
16069
16070
16071
16072
16073
16074
16075
16076
16077
16078
16079
16080
16081
16082
16083
16084
16085
16086
16087
16088
16089
16090
16091
16092
16093
16094
16095
16096
16097
16098
16099
16100
16101
16102
16103
16104
16105
16106
16107
16108
16109
16110
16111
16112
16113
16114
16115
16116
16117
16118
16119
16120
16121
16122
16123
16124
16125
16126
16127
16128
16129
16130
16131
16132
16133
16134
16135
16136
16137
16138
16139
16140
16141
16142
16143
16144
16145
16146
16147
16148
16149
16150
16151
16152
16153
16154
16155
16156
16157
16158
16159
16160
16161
16162
16163
16164
16165
16166
16167
16168
16169
16170
16171
16172
16173
16174
16175
16176
16177
16178
16179
16180
16181
16182
16183
16184
16185
16186
16187
16188
16189
16190
16191
16192
16193
16194
16195
16196
16197
16198
16199
16200
16201
16202
16203
16204
16205
16206
16207
16208
16209
16210
16211
16212
16213
16214
16215
16216
16217
16218
16219
16220
16221
16222
16223
16224
16225
16226
16227
16228
16229
16230
16231
16232
16233
16234
16235
16236
16237
16238
16239
16240
16241
16242
16243
16244
16245
16246
16247
16248
16249
16250
16251
16252
16253
16254
16255
16256
16257
16258
16259
16260
16261
16262
16263
16264
16265
16266
16267
16268
16269
16270
16271
16272
16273
16274
16275
16276
16277
16278
16279
16280
16281
16282
16283
16284
16285
16286
16287
16288
16289
16290
16291
16292
16293
16294
16295
16296
16297
16298
16299
16300
16301
16302
16303
16304
16305
16306
16307
16308
16309
16310
16311
16312
16313
16314
16315
16316
16317
16318
16319
16320
16321
16322
16323
16324
16325
16326
16327
16328
16329
16330
16331
16332
16333
16334
16335
16336
16337
16338
16339
16340
16341
16342
16343
16344
16345
16346
16347
16348
16349
16350
16351
16352
16353
16354
16355
16356
16357
16358
16359
16360
16361
16362
16363
16364
16365
16366
16367
16368
16369
16370
16371
16372
16373
16374
16375
16376
16377
16378
16379
16380
16381
16382
16383
16384
16385
16386
16387
16388
16389
16390
16391
16392
16393
16394
16395
16396
16397
16398
16399
16400
16401
16402
16403
16404
16405
16406
16407
16408
16409
16410
16411
16412
16413
16414
16415
16416
16417
16418
16419
16420
16421
16422
16423
16424
16425
16426
16427
16428
16429
16430
16431
16432
16433
16434
16435
16436
16437
16438
16439
16440
16441
16442
16443
16444
16445
16446
16447
16448
16449
16450
16451
16452
16453
16454
16455
16456
16457
16458
16459
16460
16461
16462
16463
16464
16465
16466
16467
16468
16469
16470
16471
16472
16473
16474
16475
16476
16477
16478
16479
16480
16481
16482
16483
16484
16485
16486
16487
16488
16489
16490
16491
16492
16493
16494
16495
16496
16497
16498
16499
16500
16501
16502
16503
16504
16505
16506
16507
16508
16509
16510
16511
16512
16513
16514
16515
16516
16517
16518
16519
16520
16521
16522
16523
16524
16525
16526
16527
16528
16529
16530
16531
16532
16533
16534
16535
16536
16537
16538
16539
16540
16541
16542
16543
16544
16545
16546
16547
16548
16549
16550
16551
16552
16553
16554
16555
16556
16557
16558
16559
16560
16561
16562
16563
16564
16565
16566
16567
16568
16569
16570
16571
16572
16573
16574
16575
16576
16577
16578
16579
16580
16581
16582
16583
16584
16585
16586
16587
16588
16589
16590
16591
16592
16593
16594
16595
16596
16597
16598
16599
16600
16601
16602
16603
16604
16605
16606
16607
16608
16609
16610
16611
16612
16613
16614
16615
16616
16617
16618
16619
16620
16621
16622
16623
16624
16625
16626
16627
16628
16629
16630
16631
16632
16633
16634
16635
16636
16637
16638
16639
16640
16641
16642
16643
16644
16645
16646
16647
16648
16649
16650
16651
16652
16653
16654
16655
16656
16657
16658
16659
16660
16661
16662
16663
16664
16665
16666
16667
16668
16669
16670
16671
16672
16673
16674
16675
16676
16677
16678
16679
16680
16681
16682
16683
16684
16685
16686
16687
16688
16689
16690
16691
16692
16693
16694
16695
16696
16697
16698
16699
16700
16701
16702
16703
16704
16705
16706
16707
16708
16709
16710
16711
16712
16713
16714
16715
16716
16717
16718
16719
16720
16721
16722
16723
16724
16725
16726
16727
16728
16729
16730
16731
16732
16733
16734
16735
16736
16737
16738
16739
16740
16741
16742
16743
16744
16745
16746
16747
16748
16749
16750
16751
16752
16753
16754
16755
16756
16757
16758
16759
16760
16761
16762
16763
16764
16765
16766
16767
16768
16769
16770
16771
16772
16773
16774
16775
16776
16777
16778
16779
16780
16781
16782
16783
16784
16785
16786
16787
16788
16789
16790
16791
16792
16793
16794
16795
16796
16797
16798
16799
16800
16801
16802
16803
16804
16805
16806
16807
16808
16809
16810
16811
16812
16813
16814
16815
16816
16817
16818
16819
16820
16821
16822
16823
16824
16825
16826
16827
16828
16829
16830
16831
16832
16833
16834
16835
16836
16837
16838
16839
16840
16841
16842
16843
16844
16845
16846
16847
16848
16849
16850
16851
16852
16853
16854
16855
16856
16857
16858
16859
16860
16861
16862
16863
16864
16865
16866
16867
16868
16869
16870
16871
16872
16873
16874
16875
16876
16877
16878
16879
16880
16881
16882
16883
16884
16885
16886
16887
16888
16889
16890
16891
16892
16893
16894
16895
16896
16897
16898
16899
16900
16901
16902
16903
16904
16905
16906
16907
16908
16909
16910
16911
16912
16913
16914
16915
16916
16917
16918
16919
16920
16921
16922
16923
16924
16925
16926
16927
16928
16929
16930
16931
16932
16933
16934
16935
16936
16937
16938
16939
16940
16941
16942
16943
16944
16945
16946
16947
16948
16949
16950
16951
16952
16953
16954
16955
16956
16957
16958
16959
16960
16961
16962
16963
16964
16965
16966
16967
16968
16969
16970
16971
16972
16973
16974
16975
16976
16977
16978
16979
16980
16981
16982
16983
16984
16985
16986
16987
16988
16989
16990
16991
16992
16993
16994
16995
16996
16997
16998
16999
17000
17001
17002
17003
17004
17005
17006
17007
17008
17009
17010
17011
17012
17013
17014
17015
17016
17017
17018
17019
17020
17021
17022
17023
17024
17025
17026
17027
17028
17029
17030
17031
17032
17033
17034
17035
17036
17037
17038
17039
17040
17041
17042
17043
17044
17045
17046
17047
17048
17049
17050
17051
17052
17053
17054
17055
17056
17057
17058
17059
17060
17061
17062
17063
17064
17065
17066
17067
17068
17069
17070
17071
17072
17073
17074
17075
17076
17077
17078
17079
17080
17081
17082
17083
17084
17085
17086
17087
17088
17089
17090
17091
17092
17093
17094
17095
17096
17097
17098
17099
17100
17101
17102
17103
17104
17105
17106
17107
17108
17109
17110
17111
17112
17113
17114
17115
17116
17117
17118
17119
17120
17121
17122
17123
17124
17125
17126
17127
17128
17129
17130
17131
17132
17133
17134
17135
17136
17137
17138
17139
17140
17141
17142
17143
17144
17145
17146
17147
17148
17149
17150
17151
17152
17153
17154
17155
17156
17157
17158
17159
17160
17161
17162
17163
17164
17165
17166
17167
17168
17169
17170
17171
17172
17173
17174
17175
17176
17177
17178
17179
17180
17181
17182
17183
17184
17185
17186
17187
17188
17189
17190
17191
17192
17193
17194
17195
17196
17197
17198
17199
17200
17201
17202
17203
17204
17205
17206
17207
17208
17209
17210
17211
17212
17213
17214
17215
17216
17217
17218
17219
17220
17221
17222
17223
17224
17225
17226
17227
17228
17229
17230
17231
17232
17233
17234
17235
17236
17237
17238
17239
17240
17241
17242
17243
17244
17245
17246
17247
17248
17249
17250
17251
17252
17253
17254
17255
17256
17257
17258
17259
17260
17261
17262
17263
17264
17265
17266
17267
17268
17269
17270
17271
17272
17273
17274
17275
17276
17277
17278
17279
17280
17281
17282
17283
17284
17285
17286
17287
17288
17289
17290
17291
17292
17293
17294
17295
17296
17297
17298
17299
17300
17301
17302
17303
17304
17305
17306
17307
17308
17309
17310
17311
17312
17313
17314
17315
17316
17317
17318
17319
17320
17321
17322
17323
17324
17325
17326
17327
17328
17329
17330
17331
17332
17333
17334
17335
17336
17337
17338
17339
17340
17341
17342
17343
17344
17345
17346
17347
17348
17349
17350
17351
17352
17353
17354
17355
17356
17357
17358
17359
17360
17361
17362
17363
17364
17365
17366
17367
17368
17369
17370
17371
17372
17373
17374
17375
17376
17377
17378
17379
17380
17381
17382
17383
17384
17385
17386
17387
17388
17389
17390
17391
17392
17393
17394
17395
17396
17397
17398
17399
17400
17401
17402
17403
17404
17405
17406
17407
17408
17409
17410
17411
17412
17413
17414
17415
17416
17417
17418
17419
17420
17421
17422
17423
17424
17425
17426
17427
17428
17429
17430
17431
17432
17433
17434
17435
17436
17437
17438
17439
17440
17441
17442
17443
17444
17445
17446
17447
17448
17449
17450
17451
17452
17453
17454
17455
17456
17457
17458
17459
17460
17461
17462
17463
17464
17465
17466
17467
17468
17469
17470
17471
17472
17473
17474
17475
17476
17477
17478
17479
17480
17481
17482
17483
17484
17485
17486
17487
17488
17489
17490
17491
17492
17493
17494
17495
17496
17497
17498
17499
17500
17501
17502
17503
17504
17505
17506
17507
17508
17509
17510
17511
17512
17513
17514
17515
17516
17517
17518
17519
17520
17521
17522
17523
17524
17525
17526
17527
17528
17529
17530
17531
17532
17533
17534
17535
17536
17537
17538
17539
17540
17541
17542
17543
17544
17545
17546
17547
17548
17549
17550
17551
17552
17553
17554
17555
17556
17557
17558
17559
17560
17561
17562
17563
17564
17565
17566
17567
17568
17569
17570
17571
17572
17573
17574
17575
17576
17577
17578
17579
17580
17581
17582
17583
17584
17585
17586
17587
17588
17589
17590
17591
17592
17593
17594
17595
17596
17597
17598
17599
17600
17601
17602
17603
17604
17605
17606
17607
17608
17609
17610
17611
17612
17613
17614
17615
17616
17617
17618
17619
17620
17621
17622
17623
17624
17625
17626
17627
17628
17629
17630
17631
17632
17633
17634
17635
17636
17637
17638
17639
17640
17641
17642
17643
17644
17645
17646
17647
17648
17649
17650
17651
17652
17653
17654
17655
17656
17657
17658
17659
17660
17661
17662
17663
17664
17665
17666
17667
17668
17669
17670
17671
17672
17673
17674
17675
17676
17677
17678
17679
17680
17681
17682
17683
17684
17685
17686
17687
17688
17689
17690
17691
17692
17693
17694
17695
17696
17697
17698
17699
17700
17701
17702
17703
17704
17705
17706
17707
17708
17709
17710
17711
17712
17713
17714
17715
17716
17717
17718
17719
17720
17721
17722
17723
17724
17725
17726
17727
17728
17729
17730
17731
17732
17733
17734
17735
17736
17737
17738
17739
17740
17741
17742
17743
17744
17745
17746
17747
17748
17749
17750
17751
17752
17753
17754
17755
17756
17757
17758
17759
17760
17761
17762
17763
17764
17765
17766
17767
17768
17769
17770
17771
17772
17773
17774
17775
17776
17777
17778
17779
17780
17781
17782
17783
17784
17785
17786
17787
17788
17789
17790
17791
17792
17793
17794
17795
17796
17797
17798
17799
17800
17801
17802
17803
17804
17805
17806
17807
17808
17809
17810
17811
17812
17813
17814
17815
17816
17817
17818
17819
17820
17821
17822
17823
17824
17825
17826
17827
17828
17829
17830
17831
17832
17833
17834
17835
17836
17837
17838
17839
17840
17841
17842
17843
17844
17845
17846
17847
17848
17849
17850
17851
17852
17853
17854
17855
17856
17857
17858
17859
17860
17861
17862
17863
17864
17865
17866
17867
17868
17869
17870
17871
17872
17873
17874
17875
17876
17877
17878
17879
17880
17881
17882
17883
17884
17885
17886
17887
17888
17889
17890
17891
17892
17893
17894
17895
17896
17897
17898
17899
17900
17901
17902
17903
17904
17905
17906
17907
17908
17909
17910
17911
17912
17913
17914
17915
17916
17917
17918
17919
17920
17921
17922
17923
17924
17925
17926
17927
17928
17929
17930
17931
17932
17933
17934
17935
17936
17937
17938
17939
17940
17941
17942
17943
17944
17945
17946
17947
17948
17949
17950
17951
17952
17953
17954
17955
17956
17957
17958
17959
17960
17961
17962
17963
17964
17965
17966
17967
17968
17969
17970
17971
17972
17973
17974
17975
17976
17977
17978
17979
17980
17981
17982
17983
17984
17985
17986
17987
17988
17989
17990
17991
17992
17993
17994
17995
17996
17997
17998
17999
18000
18001
18002
18003
18004
18005
18006
18007
18008
18009
18010
18011
18012
18013
18014
18015
18016
18017
18018
18019
18020
18021
18022
18023
18024
18025
18026
18027
18028
18029
18030
18031
18032
18033
18034
18035
18036
18037
18038
18039
18040
18041
18042
18043
18044
18045
18046
18047
18048
18049
18050
18051
18052
18053
18054
18055
18056
18057
18058
18059
18060
18061
18062
18063
18064
18065
18066
18067
18068
18069
18070
18071
18072
18073
18074
18075
18076
18077
18078
18079
18080
18081
18082
18083
18084
18085
18086
18087
18088
18089
18090
18091
18092
18093
18094
18095
18096
18097
18098
18099
18100
18101
18102
18103
18104
18105
18106
18107
18108
18109
18110
18111
18112
18113
18114
18115
18116
18117
18118
18119
18120
18121
18122
18123
18124
18125
18126
18127
18128
18129
18130
18131
18132
18133
18134
18135
18136
18137
18138
18139
18140
18141
18142
18143
18144
18145
18146
18147
18148
18149
18150
18151
18152
18153
18154
18155
18156
18157
18158
18159
18160
18161
18162
18163
18164
18165
18166
18167
18168
18169
18170
18171
18172
18173
18174
18175
18176
18177
18178
18179
18180
18181
18182
18183
18184
18185
18186
18187
18188
18189
18190
18191
18192
18193
18194
18195
18196
18197
18198
18199
18200
18201
18202
18203
18204
18205
18206
18207
18208
18209
18210
18211
18212
18213
18214
18215
18216
18217
18218
18219
18220
18221
18222
18223
18224
18225
18226
18227
18228
18229
18230
18231
18232
18233
18234
18235
18236
18237
18238
18239
18240
18241
18242
18243
18244
18245
18246
18247
18248
18249
18250
18251
18252
18253
18254
18255
18256
18257
18258
18259
18260
18261
18262
18263
18264
18265
18266
18267
18268
18269
18270
18271
18272
18273
18274
18275
18276
18277
18278
18279
18280
18281
18282
18283
18284
18285
18286
18287
18288
18289
18290
18291
18292
18293
18294
18295
18296
18297
18298
18299
18300
18301
18302
18303
18304
18305
18306
18307
18308
18309
18310
18311
18312
18313
18314
18315
18316
18317
18318
18319
18320
18321
18322
18323
18324
18325
18326
18327
18328
18329
18330
18331
18332
18333
18334
18335
18336
18337
18338
18339
18340
18341
18342
18343
18344
18345
18346
18347
18348
18349
18350
18351
18352
18353
18354
18355
18356
18357
18358
18359
18360
18361
18362
18363
18364
18365
18366
18367
18368
18369
18370
18371
18372
18373
18374
18375
18376
18377
18378
18379
18380
18381
18382
18383
18384
18385
18386
18387
18388
18389
18390
18391
18392
18393
18394
18395
18396
18397
18398
18399
18400
18401
18402
18403
18404
18405
18406
18407
18408
18409
18410
18411
18412
18413
18414
18415
18416
18417
18418
18419
18420
18421
18422
18423
18424
18425
18426
18427
18428
18429
18430
18431
18432
18433
18434
18435
18436
18437
18438
18439
18440
18441
18442
18443
18444
18445
18446
18447
18448
18449
18450
18451
18452
18453
18454
18455
18456
18457
18458
18459
18460
18461
18462
18463
18464
18465
18466
18467
18468
18469
18470
18471
18472
18473
18474
18475
18476
18477
18478
18479
18480
18481
18482
18483
18484
18485
18486
18487
18488
18489
18490
18491
18492
18493
18494
18495
18496
18497
18498
18499
18500
18501
18502
18503
18504
18505
18506
18507
18508
18509
18510
18511
18512
18513
18514
18515
18516
18517
18518
18519
18520
18521
18522
18523
18524
18525
18526
18527
18528
18529
18530
18531
18532
18533
18534
18535
18536
18537
18538
18539
18540
18541
18542
18543
18544
18545
18546
18547
18548
18549
18550
18551
18552
18553
18554
18555
18556
18557
18558
18559
18560
18561
18562
18563
18564
18565
18566
18567
18568
18569
18570
18571
18572
18573
18574
18575
18576
18577
18578
18579
18580
18581
18582
18583
18584
18585
18586
18587
18588
18589
18590
18591
18592
18593
18594
18595
18596
18597
18598
18599
18600
18601
18602
18603
18604
18605
18606
18607
18608
18609
18610
18611
18612
18613
18614
18615
18616
18617
18618
18619
18620
18621
18622
18623
18624
18625
18626
18627
18628
18629
18630
18631
18632
18633
18634
18635
18636
18637
18638
18639
18640
18641
18642
18643
18644
18645
18646
18647
18648
18649
18650
18651
18652
18653
18654
18655
18656
18657
18658
18659
18660
18661
18662
18663
18664
18665
18666
18667
18668
18669
18670
18671
18672
18673
18674
18675
18676
18677
18678
18679
18680
18681
18682
18683
18684
18685
18686
18687
18688
18689
18690
18691
18692
18693
18694
18695
18696
18697
18698
18699
18700
18701
18702
18703
18704
18705
18706
18707
18708
18709
18710
18711
18712
18713
18714
18715
18716
18717
18718
18719
18720
18721
18722
18723
18724
18725
18726
18727
18728
18729
18730
18731
18732
18733
18734
18735
18736
18737
18738
18739
18740
18741
18742
18743
18744
18745
18746
18747
18748
18749
18750
18751
18752
18753
18754
18755
18756
18757
18758
18759
18760
18761
18762
18763
18764
18765
18766
18767
18768
18769
18770
18771
18772
18773
18774
18775
18776
18777
18778
18779
18780
18781
18782
18783
18784
18785
18786
18787
18788
18789
18790
18791
18792
18793
18794
18795
18796
18797
18798
18799
18800
18801
18802
18803
18804
18805
18806
18807
18808
18809
18810
18811
18812
18813
18814
18815
18816
18817
18818
18819
18820
18821
18822
18823
18824
18825
18826
18827
18828
18829
18830
18831
18832
18833
18834
18835
18836
18837
18838
18839
18840
18841
18842
18843
18844
18845
18846
18847
18848
18849
18850
18851
18852
18853
18854
18855
18856
18857
18858
18859
18860
18861
18862
18863
18864
18865
18866
18867
18868
18869
18870
18871
18872
18873
18874
18875
18876
18877
18878
18879
18880
18881
18882
18883
18884
18885
18886
18887
18888
18889
18890
18891
18892
18893
18894
18895
18896
18897
18898
18899
18900
18901
18902
18903
18904
18905
18906
18907
18908
18909
18910
18911
18912
18913
18914
18915
18916
18917
18918
18919
18920
18921
18922
18923
18924
18925
18926
18927
18928
18929
18930
18931
18932
18933
18934
18935
18936
18937
18938
18939
18940
18941
18942
18943
18944
18945
18946
18947
18948
18949
18950
18951
18952
18953
18954
18955
18956
18957
18958
18959
18960
18961
18962
18963
18964
18965
18966
18967
18968
18969
18970
18971
18972
18973
18974
18975
18976
18977
18978
18979
18980
18981
18982
18983
18984
18985
18986
18987
18988
18989
18990
18991
18992
18993
18994
18995
18996
18997
18998
18999
19000
19001
19002
19003
19004
19005
19006
19007
19008
19009
19010
19011
19012
19013
19014
19015
19016
19017
19018
19019
19020
19021
19022
19023
19024
19025
19026
19027
19028
19029
19030
19031
19032
19033
19034
19035
19036
19037
19038
19039
19040
19041
19042
19043
19044
19045
19046
19047
19048
19049
19050
19051
19052
19053
19054
19055
19056
19057
19058
19059
19060
19061
19062
19063
19064
19065
19066
19067
19068
19069
19070
19071
19072
19073
19074
19075
19076
19077
19078
19079
19080
19081
19082
19083
19084
19085
19086
19087
19088
19089
19090
19091
19092
19093
19094
19095
19096
19097
19098
19099
19100
19101
19102
19103
19104
19105
19106
19107
19108
19109
19110
19111
19112
19113
19114
19115
19116
19117
19118
19119
19120
19121
19122
19123
19124
19125
19126
19127
19128
19129
19130
19131
19132
19133
19134
19135
19136
19137
19138
19139
19140
19141
19142
19143
19144
19145
19146
19147
19148
19149
19150
19151
19152
19153
19154
19155
19156
19157
19158
19159
19160
19161
19162
19163
19164
19165
19166
19167
19168
19169
19170
19171
19172
19173
19174
19175
19176
19177
19178
19179
19180
19181
19182
19183
19184
19185
19186
19187
19188
19189
19190
19191
19192
19193
19194
19195
19196
19197
19198
19199
19200
19201
19202
19203
19204
19205
19206
19207
19208
19209
19210
19211
19212
19213
19214
19215
19216
19217
19218
19219
19220
19221
19222
19223
19224
19225
19226
19227
19228
19229
19230
19231
19232
19233
19234
19235
19236
19237
19238
19239
19240
19241
19242
19243
19244
19245
19246
19247
19248
19249
19250
19251
19252
19253
19254
19255
19256
19257
19258
19259
19260
19261
19262
19263
19264
19265
19266
19267
19268
19269
19270
19271
19272
19273
19274
19275
19276
19277
19278
19279
19280
19281
19282
19283
19284
19285
19286
19287
19288
19289
19290
19291
19292
19293
19294
19295
19296
19297
19298
19299
19300
19301
19302
19303
19304
19305
19306
19307
19308
19309
19310
19311
19312
19313
19314
19315
19316
19317
19318
19319
19320
19321
19322
19323
19324
19325
19326
19327
19328
19329
19330
19331
19332
19333
19334
19335
19336
19337
19338
19339
19340
19341
19342
19343
19344
19345
19346
19347
19348
19349
19350
19351
19352
19353
19354
19355
19356
19357
19358
19359
19360
19361
19362
19363
19364
19365
19366
19367
19368
19369
19370
19371
19372
19373
19374
19375
19376
19377
19378
19379
19380
19381
19382
19383
19384
19385
19386
19387
19388
19389
19390
19391
19392
19393
19394
19395
19396
19397
19398
19399
19400
19401
19402
19403
19404
19405
19406
19407
19408
19409
19410
19411
19412
19413
19414
19415
19416
19417
19418
19419
19420
19421
19422
19423
19424
19425
19426
19427
19428
19429
19430
19431
19432
19433
19434
19435
19436
19437
19438
19439
19440
19441
19442
19443
19444
19445
19446
19447
19448
19449
19450
19451
19452
19453
19454
19455
19456
19457
19458
19459
19460
19461
19462
19463
19464
19465
19466
19467
19468
19469
19470
19471
19472
19473
19474
19475
19476
19477
19478
19479
19480
19481
19482
19483
19484
19485
19486
19487
19488
19489
19490
19491
19492
19493
19494
19495
19496
19497
19498
19499
19500
19501
19502
19503
19504
19505
19506
19507
19508
19509
19510
19511
19512
19513
19514
19515
19516
19517
19518
19519
19520
19521
19522
19523
19524
19525
19526
19527
19528
19529
19530
19531
19532
19533
19534
19535
19536
19537
19538
19539
19540
19541
19542
19543
19544
19545
19546
19547
19548
19549
19550
19551
19552
19553
19554
19555
19556
19557
19558
19559
19560
19561
19562
19563
19564
19565
19566
19567
19568
19569
19570
19571
19572
19573
19574
19575
19576
19577
19578
19579
19580
19581
19582
19583
19584
19585
19586
19587
19588
19589
19590
19591
19592
19593
19594
19595
19596
19597
19598
19599
19600
19601
19602
19603
19604
19605
19606
19607
19608
19609
19610
19611
19612
19613
19614
19615
19616
19617
19618
19619
19620
19621
19622
19623
19624
19625
19626
19627
19628
19629
19630
19631
19632
19633
19634
19635
19636
19637
19638
19639
19640
19641
19642
19643
19644
19645
19646
19647
19648
19649
19650
19651
19652
19653
19654
19655
19656
19657
19658
19659
19660
19661
19662
19663
19664
19665
19666
19667
19668
19669
19670
19671
19672
19673
19674
19675
19676
19677
19678
19679
19680
19681
19682
19683
19684
19685
19686
19687
19688
19689
19690
19691
19692
19693
19694
19695
19696
19697
19698
19699
19700
19701
19702
19703
19704
19705
19706
19707
19708
19709
19710
19711
19712
19713
19714
19715
19716
19717
19718
19719
19720
19721
19722
19723
19724
19725
19726
19727
19728
19729
19730
19731
19732
19733
19734
19735
19736
19737
19738
19739
19740
19741
19742
19743
19744
19745
19746
19747
19748
19749
19750
19751
19752
19753
19754
19755
19756
19757
19758
19759
19760
19761
19762
19763
19764
19765
19766
19767
19768
19769
19770
19771
19772
19773
19774
19775
19776
19777
19778
19779
19780
19781
19782
19783
19784
19785
19786
19787
19788
19789
19790
19791
19792
19793
19794
19795
19796
19797
19798
19799
19800
19801
19802
19803
19804
19805
19806
19807
19808
19809
19810
19811
19812
19813
19814
19815
19816
19817
19818
19819
19820
19821
19822
19823
19824
19825
19826
19827
19828
19829
19830
19831
19832
19833
19834
19835
19836
19837
19838
19839
19840
19841
19842
19843
19844
19845
19846
19847
19848
19849
19850
19851
19852
19853
19854
19855
19856
19857
19858
19859
19860
19861
19862
19863
19864
19865
19866
19867
19868
19869
19870
19871
19872
19873
19874
19875
19876
19877
19878
19879
19880
19881
19882
19883
19884
19885
19886
19887
19888
19889
19890
19891
19892
19893
19894
19895
19896
use color_eyre::Result;
use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
use polars::datatypes::DataType;
#[cfg(feature = "cloud")]
use polars::io::cloud::{AmazonS3ConfigKey, CloudOptions};
use polars::prelude::{DataFrame, LazyFrame, Schema, col};
#[cfg(feature = "cloud")]
use polars::prelude::{PlRefPath, ScanArgsParquet};
use std::collections::HashMap;
/// Rows measured per background pass. Small enough that a slow filesystem shows
/// progress rather than a long silence.
const MEASURE_BATCH: usize = 12;
/// Rows a probe measures while it is already reading a remote directory.
const PROBE_MEASURE_LIMIT: usize = 24;
/// Rows one classification pass looks into.
///
/// A cap on work in flight rather than a budget spent per directory: what gets looked
/// into is what is on screen, and the next pass is chosen from the viewport as it is
/// when the previous one lands. Sized like [`MEASURE_BATCH`], for the same reason —
/// on a share that answers in milliseconds per row, a screenful arriving in pieces
/// reads as filling in, and one long silence reads as broken.
const CLASSIFY_BATCH: usize = 16;
/// Probes allowed at once. A probe of a share that has gone away holds its thread
/// until the process exits, so the number of them has to be bounded.
const MAX_CONCURRENT_PROBES: usize = 4;
use std::path::{Path, PathBuf};
use std::sync::{Arc, Mutex, mpsc::Sender};
use widgets::info::{FileFacts, InfoModal};
use ratatui::style::{Color, Style};
use ratatui::{buffer::Buffer, layout::Rect, widgets::Widget};
use ratatui::widgets::{Block, Clear};
mod analysis_keys;
pub mod analysis_modal;
pub mod audio;
pub mod avro_types;
#[cfg(feature = "cloud")]
pub mod aws_profiles;
#[cfg(feature = "cloud")]
pub mod azure;
mod background;
pub mod cache;
pub mod candump;
pub mod canonical;
pub mod catalog;
pub mod chart_data;
pub mod chart_export;
pub mod chart_export_modal;
mod chart_jobs;
mod chart_keys;
pub mod chart_modal;
mod chart_pdf;
mod chart_recipe;
pub mod cli;
pub mod clipboard;
#[cfg(feature = "cloud")]
mod cloud_arrow;
#[cfg(feature = "cloud")]
pub mod cloud_browse;
#[cfg(feature = "cloud")]
pub mod cloud_command;
pub mod cloud_env;
#[cfg(feature = "cloud")]
mod cloud_hive;
#[cfg(feature = "cloud")]
pub mod cloud_sources;
pub mod codebook;
pub mod column_types;
pub mod commands;
pub mod config;
pub mod config_command;
pub mod context_menu;
mod copy_keys;
pub mod copy_modal;
pub mod csv_dialect;
pub mod data_quality;
pub mod dataflash;
mod dataset_files;
pub mod dbc;
pub mod delimited_spec;
pub mod discover;
pub mod distribution_fit;
pub mod download;
mod editing_keys;
pub mod elf;
pub mod error_display;
pub mod event_pump;
pub mod exact;
pub mod excel;
pub mod export;
mod export_keys;
pub mod export_modal;
pub mod external_open;
mod feedback;
pub mod filter_modal;
pub mod find;
mod first_rows_trace;
pub mod fix;
pub mod fixed_records;
pub mod follow;
mod footer_state;
pub mod form;
pub mod formats;
pub mod framed_records;
pub mod fuzzy;
#[cfg(feature = "cloud")]
pub mod gcloud;
pub mod glyphs;
pub mod gps;
pub mod help;
mod hex_keys;
pub mod hex_view;
pub mod hf_splits;
pub mod home;
pub mod home_preview;
pub mod indexed;
mod info_keys;
pub mod inspector_bytes;
pub mod inspector_drill;
pub mod inspector_modal;
pub mod inspector_reader;
pub mod intent_modal;
pub mod ipc_stream;
mod jobs;
pub mod journal;
pub mod lines;
pub mod link_open;
mod loading;
pub mod local_copy;
pub(crate) mod local_glob;
pub mod locality;
pub mod logging;
pub mod measurements;
pub mod members;
pub mod midi;
pub mod model_files;
pub mod nested_json;
pub mod notes;
pub mod nul_tail;
pub mod numfmt;
pub mod numpy;
mod open_options;
pub mod output_file;
pub mod parquet_footer;
pub mod past_calendar;
mod pivot_melt_keys;
pub mod pivot_melt_modal;
pub mod pointer;
pub mod pushdown;
pub mod python_script;
pub mod quality_export;
pub mod quality_intent;
mod quality_memory;
pub mod quality_report;
pub mod quality_trends;
#[cfg(any(feature = "http", feature = "cloud"))]
mod remote_model;
mod retype_keys;
pub mod retype_modal;
pub mod row_index;
#[cfg(feature = "cloud")]
pub mod s3_tools;
mod sample_keys;
pub mod sample_modal;
pub mod sampling;
pub mod table_sample;
// Public so the fuzz targets in `fuzz/` can reach `parse_query`. The parser is
// hand-written and runs on whatever the user types, so it is fuzzed directly.
pub mod query;
mod readers;
mod render;
pub mod sanitize;
mod scan;
pub mod schema_union;
pub mod sdf;
pub mod search;
pub(crate) mod segments;
mod sort_filter_keys;
pub mod sort_filter_modal;
pub mod sort_modal;
pub mod source;
pub(crate) mod spec_union;
mod sql_assist;
pub mod sqlite;
// Public so the fuzz target `sql_group_plan` can reach `plan`, which reads every SQL
// statement the prompt runs.
#[cfg(feature = "sql")]
pub mod sql_group;
pub mod startup;
pub mod statistics;
pub mod stdin;
pub mod table_switch;
pub mod tee;
mod terminal;
mod terminal_color;
pub mod terminal_input;
pub mod text_formats;
pub mod themes;
pub mod typed_value;
pub mod ulog;
mod unfinished;
pub mod user_agent;
pub mod value_counts;
pub mod value_counts_modal;
pub mod vcd;
pub mod view;
mod view_keys;
pub mod widgets;
pub use cache::CacheManager;
pub use cli::Args;
pub use config::{
AppConfig, ColorParser, ConfigManager, QueryMode, Theme, ThemeMode, rgb_to_256_color,
rgb_to_basic_ansi,
};
use analysis_modal::{AnalysisModal, AnalysisProgress};
use background::{CacheWrites, InflightCollect, LenCount, OwedAnswer, OwedCount};
use chart_export::{ChartExportFormat, ChartExportRequest};
use chart_export_modal::ChartExportModal;
use chart_jobs::{
ChartCache, ChartExportJob, ChartInflight, ChartPrepared, ChartRequest, ChartResultSlot,
log_series,
};
use chart_modal::{ChartColumns, ChartModal};
pub use error_display::{ErrorKindForPython, error_for_python};
pub use export::{ExportOptions, ExportRequest};
use export_modal::{ExportFocus, ExportFormat, ExportModal};
pub use feedback::{ConfirmationModal, ErrorModal, Flash};
use filter_modal::{FilterOperator, FilterStatement, LogicalOperator};
use form::FormKey;
use jobs::{Answer, Job, Jobs, Outcome};
pub use jobs::{JobKind, Progress, Ticket};
use numfmt::NumberFormatSettings;
pub use open_options::{
OpenOptions, ParseStringsTarget, ReadReport, SqliteOpen, TypedDialect, UnaskedDownload,
};
use output_file::Overwrite;
use pivot_melt_modal::{MeltSpec, PivotMeltModal, PivotSpec};
pub use quality_memory::{KeptQualitySample, QUALITY_MEMORY_BUDGET, RetainedCopy};
use quality_memory::{QUALITY_RELEASED_REMEMBERED, QualityCacheEntry, QualityCopyJob};
use scan::Scan;
use sort_filter_modal::SortFilterModal;
use sort_modal::{SortColumn, order_with_hidden};
use terminal::{QuietTerminal, TakenTerminal, follow_focus, push_keyboard_flags, restore_terminal};
pub use unfinished::ExitSweep;
pub use view::{SavedView, ViewManager, Views};
use widgets::column_widths::WidthChoice;
use widgets::datatable::{DataTableState, DrillRow, OpenFacts};
use widgets::debug::DebugState;
use widgets::text_input::TextInput;
use widgets::view_modal::{FormFocus, ViewModal, ViewModalMode, ViewRow};
/// Application name used for cache directory and other app-specific paths
pub const APP_NAME: &str = "datui";
/// What a file no reader takes, and no hex view can show, is told.
pub(crate) const UNSUPPORTED: &str =
"Unsupported file type. --format names the format to read it as.";
/// Re-export compression format and file format from CLI module
pub use cli::{CompressionFormat, FileFormat, ReadMode, RemoteRead, Stored, Summary};
#[cfg(test)]
mod text_input_flows;
#[cfg(test)]
pub mod tests;
pub enum AppEvent {
Key(KeyEvent),
/// A key to take as if typed: what Enter on a help line presses. The event pump
/// offers it as the next typed key, through `classify`, so it is held, converted or
/// dropped as a typed key would be; outside the pump it is a `Key`.
Press(KeyEvent),
/// Read from the terminal by [`terminal_input::TerminalInput`]: a key press or a
/// resize. [`event_pump::EventPump`] takes it off the channel and decides what a
/// key does while the app is busy; the app itself only ever sees `Key`/`Resize`.
Terminal(crossterm::event::Event),
/// Something polled rather than sent changed (a background panic, a Polars
/// warning): the loop should look. Handled as nothing.
Wake,
/// The terminal said what its background is (an OSC 11 reply, taken off the input
/// stream by [`terminal_input`]). Under `theme.mode = "auto"` the palette follows.
TerminalBackground(ThemeMode),
/// The terminal window came back into focus: under `auto` the background is asked
/// again, since the scheme may have changed while it was away.
TerminalFocused,
/// The settings `run` reads on a worker before it can build the app. Never reaches
/// the app: `run` waits for it before there is one.
SettingsRead(Box<Result<startup::Settings>>),
/// The paths named on the command line or by the Python binding: whether each is
/// there and whether one is a directory is asked on a worker
/// ([`JobKind::OpenNamed`]), a local-looking path being no promise of a fast
/// mount.
OpenNamed(Vec<PathBuf>, OpenOptions),
/// A path named on the command line is not there: the session ends as a missing
/// file always has. The continuation of the look's answer.
NamedPathMissing(PathBuf),
/// Open these paths. Its phases are the loading controller's: each worker's answer
/// starts the next, and the dataset is installed when its schema is read.
Open(Vec<PathBuf>, OpenOptions),
/// Open with an existing LazyFrame (e.g. from Python binding); no file load.
OpenLazyFrame(Box<LazyFrame>, OpenOptions),
/// A home listing built off-thread is ready.
HomeListingReady {
generation: u64,
listing: Box<crate::home::Listing>,
/// What earlier runs measured, read from the cache with the listing.
known: std::collections::HashMap<PathBuf, crate::cache::DatasetFacts>,
/// How often and how lately each recent was opened.
visits: std::collections::HashMap<PathBuf, crate::cache::Visits>,
/// The recent opened last, where the cursor lands.
newest: Option<PathBuf>,
/// The saved folds, when entering the home screen asked for them.
folds: Option<std::collections::HashMap<String, bool>>,
},
/// The worker building a home listing panicked, so no listing is coming. The panic
/// is flashed like any other raw worker's.
HomeListingFailed,
/// The directory the `~` prompt is typing, read off-thread.
HomePathListed {
listing: Box<crate::home::PathListing>,
},
/// A completed path, worked out off-thread.
HomePathCompleted {
generation: u64,
/// What was typed when completion was asked for; a later keystroke makes the
/// answer stale.
typed: String,
completed: String,
candidates: usize,
},
/// The first rows of the highlighted file, read off-thread the way its open reads
/// them, and the dataset that read built, for the open to install.
HomePreviewReady {
path: PathBuf,
/// The row's stamp when it was asked for: what the rows are kept under.
stamp: crate::home_preview::Stamp,
/// The file's stamp when it was read: what the dataset is installed under.
read_at: Option<crate::home_preview::Stamp>,
rows: Option<Arc<crate::home_preview::PreviewRows>>,
prepared: crate::home_preview::Handoff,
},
/// A schema read off-thread for the highlighted dataset.
HomeSchemaReady {
generation: u64,
path: PathBuf,
preview: Option<crate::discover::SchemaPreview>,
},
/// Measurements for rows the home screen asked about, sent as each row is read so
/// a slow row does not hold back the ones before it. `done` marks the end of the
/// batch and frees the slot for the next one.
///
/// No generation, unlike its neighbors: what a look found is keyed by path and
/// true of that path whichever listing asked, so an answer that outlives its
/// listing is still the answer.
HomeMeasured {
measured: Vec<(PathBuf, crate::home::Measured)>,
done: bool,
},
/// What a HEAD said an HTTP(S) file on home weighs: its row, measured.
HomeSized {
path: PathBuf,
measured: crate::home::Measured,
},
/// What a HEAD settled about an HTTP(S) file on home: it cannot be had.
HomeWebGone {
path: PathBuf,
gone: crate::error_display::HttpGone,
},
/// What the rows on screen turned out to be. The same payload as
/// [`AppEvent::HomeMeasured`] and folded in the same way: a kind is one of the
/// things a look into a row produces.
HomeClassified {
measured: Vec<(PathBuf, crate::home::Measured)>,
done: bool,
},
/// A batch of datasets found by the background search below the working
/// directory. Sent repeatedly while the walk runs, so a cold tree fills in
/// rather than arriving all at once at the end.
HomeSearchBatch {
generation: u64,
root: PathBuf,
found: Vec<crate::discover::Entry>,
scanned: usize,
},
/// The filter scored against the search's files, for the walk `epoch` names.
HomeSearchScored {
epoch: u64,
/// `None` from a worker that died.
matches: Option<Box<crate::search::Matches>>,
},
/// The background search has stopped, with `limited` saying why if it stopped
/// short of walking everything.
HomeSearchDone {
generation: u64,
root: PathBuf,
scanned: usize,
limited: Option<String>,
},
/// The cloud sources on this machine, with whatever was listed on an earlier run.
/// Sent before anything is fetched, so the rows are there on the first frame.
#[cfg(feature = "cloud")]
HomeCloudSources {
sources: Vec<crate::home::CloudSource>,
},
/// One source's buckets have been listed, or could not be. Each source reports on
/// its own, so a slow endpoint holds up nobody else's row.
#[cfg(feature = "cloud")]
HomeCloudListed {
id: String,
buckets: Vec<PathBuf>,
/// Lines for the details pane of each listed place that has any.
details: Vec<(PathBuf, Vec<(String, String)>)>,
/// `(short, detail)` when the listing failed.
failure: Option<(String, String)>,
listed_at: std::time::SystemTime,
},
/// A network root has been listed off-thread, or could not be.
HomeProbeReady {
root: PathBuf,
rows: Option<Vec<crate::discover::Entry>>,
/// The listing stopped at [`crate::discover::MAX_ENTRIES_PER_DIR`].
cut_short: bool,
},
/// The rows of a network directory read so far, while its listing goes on.
HomeProbeProgress {
root: PathBuf,
rows: Vec<crate::discover::Entry>,
},
/// What peeking inside some directories of a cloud listing found: the ones that are
/// partitioned or Parquet datasets.
HomeCloudKinds {
kinds: Vec<(
PathBuf,
(crate::discover::EntryKind, crate::discover::Holds),
)>,
/// Directories whose peek failed or was lost: not answered, so not labelled as
/// if they were.
failed: Vec<PathBuf>,
},
/// A cloud listing stopped because its place was left. Nothing is known about the
/// place, so it is listed again when it is entered again.
HomeProbeCancelled {
root: PathBuf,
},
/// The names under `prefix` in a cloud directory cut short, asked for by a filter;
/// `None` when the listing failed or was stopped.
HomeNarrowed {
dir: PathBuf,
prefix: String,
listed: Option<(Vec<crate::discover::Entry>, bool)>,
},
/// A cloud listing was refused, with the service's reason.
HomeProbeFailed {
root: PathBuf,
message: String,
},
/// Run the export, from plan to committed file, once the UI has drawn its
/// progress.
DoExport(ExportRequest),
/// A followed file's watcher found more rows, or that the file went.
Followed(crate::follow::News),
/// The Info tab of a piped journal, read again once it ended, for the dataset of
/// that generation.
FollowedDetail {
dataset_generation: u64,
detail: Box<crate::text_formats::Detail>,
},
Exit,
Crash(String),
QQuery(String),
SqlQuery(String),
Filter(Vec<FilterStatement>),
Sort(Vec<String>, Vec<bool>), // Columns, and per column whether it runs descending
ColumnOrder(Vec<String>, usize), // Column order, locked columns count
Pivot(PivotSpec),
Melt(MeltSpec),
Export(ExportRequest),
/// Collect and format the whole view off-thread for a table-scope copy.
CopyTable {
format: crate::clipboard::CopyFormat,
header: bool,
},
ChartExport(ChartExportRequest),
/// A documentation link the user confirmed, checked by `link_open::checked_url`:
/// start the browser on it.
OpenLink(String),
/// Deferred: run the chart export once its phase is drawn.
DoChartExport(ChartExportRequest),
Collect,
Update,
Reset,
Resize(u16, u16), // resized (width, height)
DoScrollDown, // Deferred scroll: perform page_down after one frame (throbber)
DoScrollUp, // Deferred scroll: perform page_up
DoScrollNext, // Deferred scroll: perform select_next (one row down)
DoScrollPrev, // Deferred scroll: perform select_previous (one row up)
DoScrollEnd, // Deferred scroll: jump to last page (throbber)
DoScrollHome, // Deferred scroll: jump to first page (throbber)
DoScrollHalfDown, // Deferred scroll: half page down
DoScrollHalfUp, // Deferred scroll: half page up
GoToLine(usize), // Deferred: jump to line number (when collect needed)
/// Run the next chunk of analysis (describe/distribution); drives per-column progress.
AnalysisChunk,
/// Run distribution analysis (deferred so progress overlay can show first).
AnalysisDistributionCompute,
/// Run correlation matrix (deferred so progress overlay can show first).
AnalysisCorrelationCompute,
/// Run the configured data-quality plan off the UI thread.
AnalysisDataQualityCompute,
/// A Data Quality run that stopped short had already read its sample: kept, so
/// the read it paid for is not thrown away.
BackgroundQualitySampleKept {
kept: KeptQualitySample,
},
/// A full scan finished copying a remote dataset's objects locally: kept for the
/// dataset it was fetched for, whatever becomes of the run. `None` when the copy
/// did not read as the source, so later runs read the source and say why.
BackgroundQualityCopyKept {
dataset_generation: u64,
copy: Option<Arc<crate::local_copy::LocalCopy>>,
},
/// Background task completed: exact row count for the current LazyFrame. Applied to
/// `data_table_state` only if `len_generation` still matches (the data is unchanged).
/// Runs concurrently with — and independently of — the first buffer paint, so the
/// count fills in the scrollbar/total without ever blocking the initial render.
BackgroundLenReady {
len_generation: u64,
num_rows: usize,
/// For a remote dataset of many files, the rows in each row group of each file,
/// from their footers.
file_row_groups: Option<Vec<Vec<usize>>>,
},
/// Background row count failed. Clears the in-flight marker; the total stays
/// provisional and is shown as unknown. Scrolling does not count again; End does.
BackgroundLenFailed {
len_generation: u64,
},
/// A frame was painted. The run loop calls [`App::frame_painted`] itself; a harness
/// that paints nothing sends this when [`App::count_waits_for_a_frame`].
FramePainted,
/// Every footer of a dataset that opened from two of them has now been read. What
/// they say is in `App::pending_footers_result`; the columns they add join the
/// dataset already on screen.
BackgroundFootersJoined {
generation: u64,
},
/// Every line of a text file opened from its first rows is indexed, `rows` of
/// them, for the dataset of `generation`.
LinesIndexed {
generation: u64,
rows: usize,
},
/// Background task completed: chart data for one selection is prepared. The data is
/// in `App::pending_chart_result`; it belongs to `App::chart_inflight`, which says
/// whether it is still wanted.
BackgroundChartReady,
/// Write the Data Quality report on screen to a file, in a form. From the
/// results in memory: nothing is read.
QualityReportExport(PathBuf, crate::quality_export::ReportFormat, Overwrite),
/// A directory named on the command line: look at it on a worker, then do with it
/// whatever `Enter` on its row would do.
///
/// The look reads footers, or the front of a spread of files, which for a directory
/// of large Parquet is seconds. It is an event rather than a call so the first frame
/// is drawn before it starts, and the wait has the directory's name on it, a spinner
/// and a way out.
LookThenOpenDirectory(PathBuf, OpenOptions),
/// Look at a path off the interface thread, then do with it whatever it turns out to
/// need — browse into it, say it is a lake table, or open it.
///
/// `exists`, `is_dir` and `classify_directory` are all filesystem calls, and the home
/// screen is full of paths on mounts that may not answer. Doing them where the keys
/// are read is an uninterruptible freeze with Ctrl+C on the same thread.
ClassifyThenOpen {
path: PathBuf,
/// A jump — a path typed at `~` — rather than a row that was already listed. Esc
/// then comes back from there to the listing, not up through wherever the path
/// happens to sit.
jump: bool,
},
/// A background job's outcome is in its record: [`jobs::Jobs::end`] takes it.
JobEnded(Ticket),
/// A report from a background job still running.
JobProgress {
ticket: Ticket,
progress: Progress,
},
}
impl AppEvent {
/// A report from work still going, sent many times while it runs: the run loop may
/// fold several into one frame. Anything else is drawn as soon as it is handled.
pub fn is_progress(&self) -> bool {
matches!(
self,
AppEvent::HomeMeasured { done: false, .. }
| AppEvent::HomeClassified { done: false, .. }
| AppEvent::HomeSearchBatch { .. }
| AppEvent::HomeProbeProgress { .. }
| AppEvent::JobProgress {
progress: Progress::QualityPhase(_),
..
}
)
}
}
/// Picks the home-screen workers that panic before their work starts, by the answer
/// they would owe; see `App::home_worker_dies`.
#[cfg(test)]
type HomeWorkerDies = Box<dyn FnMut(&AppEvent) -> bool + Send>;
/// Stands in for [`FileFacts::read`]; see `App::file_facts_reader`.
#[cfg(test)]
type FileFactsReader = Arc<
dyn Fn(&Path, Option<crate::readers::Facts>) -> std::result::Result<FileFacts, String>
+ Send
+ Sync,
>;
/// What [`App::handle`] did with an event: `Ok` carries the follow-up event to send,
/// if any; `Err` returns a key that arrived while the app was busy. Nothing was done
/// with that key and it was not dropped: the caller keeps it and offers it again once
/// the app is idle.
pub type EventOutcome = Result<Option<AppEvent>, KeyEvent>;
/// What <kbd>Enter</kbd> will do on the highlighted row.
///
/// Written so the control bar and the details pane can say it before it happens.
/// Every directory has two doors and the labels no longer decide access, which is only
/// worth anything if the screen says which key is which — a bar reading `Enter Open` on
/// a row where `Enter` goes inside teaches the wrong thing on the first try, and the
/// first try is the one that forms the impression.
///
/// A prediction, so it can drift from [`App::home_open_selected`], which is the thing
/// that actually decides. `test_the_bar_says_what_enter_will_really_do` pumps `Enter`
/// on one row of every shape and asserts the two agreed; that test is the reason this
/// is safe to read from the renderer.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum WhatEnter {
/// Load the file on the row.
OpensFile,
/// Read the whole directory as one table: a hive root, a directory whose files are
/// one table, or the `(all files)` row.
OpensDirectory,
/// Step into the directory. What `→` does too, on these rows.
GoesInside,
/// Look at the row first, then do whichever of the above the answer calls for.
LooksFirst,
/// Fold or unfold a section.
FoldsSection,
/// Show the rest of `RECENT`.
ShowsMore,
/// Show the files datui cannot open, as `Ctrl+A` does.
ShowsHidden,
/// Nothing to open and nowhere to go: an HTTP place, which has no listing to
/// browse and says so.
Explains,
/// A local file datui has no reader for: Enter shows its bytes in the hex view.
OpensHex,
/// A remote file datui has no reader for. Enter says so, and the bar offers nothing.
Nothing,
}
impl App {
/// See [`WhatEnter`].
pub fn what_enter_does(&self) -> WhatEnter {
// One walk of the list, not four. Every `selected_*` helper rebuilds it, and this
// runs from the control bar on every frame, beside a
// `selected_directory_to_enter` that walks it once more.
let rows = self.home.visible();
let entry = match rows.get(self.home.selected) {
// A place row browses into the place, which is what `→` does on it too, so
// it is labelled the same and offered once. An HTTP place has no listing to
// browse and says so instead.
Some(home::Row::Place { path, .. }) => {
return if home::place_is_browsable(path) {
WhatEnter::GoesInside
} else {
WhatEnter::Explains
};
}
Some(home::Row::Header { .. }) => return WhatEnter::FoldsSection,
Some(home::Row::More { .. }) => return WhatEnter::ShowsMore,
Some(home::Row::Hidden { .. }) => return WhatEnter::ShowsHidden,
// "No match.": nothing to open and nothing to say about it.
None => return WhatEnter::Nothing,
// The door reads the directory it names whatever that directory is labelled —
// the lake tables included, which is the one row that reads them at all.
Some(home::Row::Door { .. }) => return WhatEnter::OpensDirectory,
Some(home::Row::Entry { entry, .. }) => *entry,
};
// A bookmark opens whole.
if entry.kind != discover::EntryKind::File && self.home.bookmark(&entry.path).is_some() {
return WhatEnter::OpensDirectory;
}
match entry.kind {
discover::EntryKind::Unknown => WhatEnter::LooksFirst,
// A database of several tables lists them.
discover::EntryKind::File if entry.enter_lists_tables() => WhatEnter::GoesInside,
discover::EntryKind::File => WhatEnter::OpensFile,
discover::EntryKind::Other
if matches!(
source::input_source(&entry.path),
source::InputSource::Local(_)
) =>
{
WhatEnter::OpensHex
}
discover::EntryKind::Other => WhatEnter::Nothing,
discover::EntryKind::Hive | discover::EntryKind::MultiFile => WhatEnter::OpensDirectory,
// A plain directory, and a lake table, whose files are not its rows.
discover::EntryKind::Directory
| discover::EntryKind::Delta
| discover::EntryKind::Iceberg
| discover::EntryKind::Hudi => WhatEnter::GoesInside,
}
}
}
impl App {
/// Whether a dataset held `download` because it came from a remote `path` (the
/// URL it is shown by): a local stream's conversion and standard input's spool are
/// held the same way.
fn fetched(download: Option<&crate::download::TempDownload>, path: Option<&Path>) -> bool {
download.is_some() && path.is_some_and(source::is_remote_url)
}
}
/// Input for the shared run loop: open from file paths or from an existing LazyFrame (e.g. Python binding).
#[derive(Clone)]
pub enum RunInput {
/// The command line as parsed. The configuration is read, and the flags applied
/// over it, behind the first frame ([`startup`]).
Cli(Box<Args>),
/// A host program's options, as the command line would give them, with a frame
/// to show instead of its paths when there is one. Read as the command line is,
/// `-c` included, but standard input is the host's, never data.
Host(Box<Args>, Option<Box<LazyFrame>>),
Paths(Vec<PathBuf>, OpenOptions),
LazyFrame(Box<LazyFrame>, OpenOptions),
}
#[derive(Debug, Default, PartialEq, Eq)]
pub enum InputMode {
#[default]
Normal,
/// The home screen: pick a dataset to open. Reachable at startup with no
/// arguments, and from inside a session, which is what makes datui a place you
/// stay rather than a command you re-run.
Home,
SortFilter,
PivotMelt,
Editing,
Export,
/// The copy dialog over the table.
Copy,
/// The row inspector over the table.
Inspect,
/// The column picker over the table: type a column's name to go to it.
GoToColumn,
/// The format picker over a table read through a spec: read it with another.
PickFormat,
/// A column's type, picked over the table: from the Info panel's Schema tab or
/// the cell menu.
Retype,
/// A datetime made from columns, as a spec's derived column.
Combine,
/// The table picker over a table of a file of several: open another.
PickTable,
Info,
Chart,
/// Value Counts: how often each value of one column occurs in the view.
ValueCounts,
/// The hex view: a file's bytes.
Hex,
/// The Sample form over the table (`S`).
Sample,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum InputType {
/// The command line (`:`): a row number, or a query in SQL or q.
Query,
Find,
}
/// A query whose first rows are being read. It planned, but can still fail on the
/// data — a value that will not cast — and until the rows are in, the view it
/// replaced is kept to go back to.
struct QueryRun {
origin: RunOrigin,
/// The `len_generation` of the frame the query installed. Once that frame is gone
/// (a sort, a filter, another dataset) the rollback no longer applies.
frame: u64,
rollback: crate::widgets::datatable::ViewRollback,
/// The App's count markers as they were, for the frame the rollback restores.
/// A count of that frame still running when the query began lands while the
/// query's frame is installed; its answer goes into `rollback`.
len_count_inflight: Option<u64>,
count_after_paint: Option<u64>,
len_count_failed: Option<u64>,
/// Rows `df` holds, when known, so a failure can say "of N".
rows: Option<usize>,
}
/// Where a running query came from, which decides where its failure is said.
enum RunOrigin {
/// The query prompt, or its event sent directly: inline under the prompt while
/// it is open in this mode, else a dialog.
Query(QueryMode),
/// A view applied. Its failure is a dialog, and the view marked applied before
/// it is marked again. Applied for a match rather than picked, `matched` says
/// why once its rows are in.
View {
previous: Option<String>,
matched: Option<(String, view::MatchReason)>,
},
}
/// What the bar says of a recording (`--tee`): `rec` with its size and rate while it
/// goes on, `saved` with its size, length and file once it ended (`sent` and no file
/// for `--tee -`), or `stopped` and why, in the warning color, when it ended in an
/// error. The second value is that last.
fn recording_label(spool: &crate::follow::Spool) -> (String, bool) {
let dot = crate::glyphs::get().middot;
let size = crate::discover::format_size(spool.bytes());
match spool.ended() {
None => (
format!(
"rec {size} {dot} {}/s",
crate::discover::format_size(spool.rate() as u64)
),
false,
),
Some(None) => {
let secs = spool.duration().as_secs();
let length = if secs >= 3600 {
format!("{}:{:02}:{:02}", secs / 3600, secs / 60 % 60, secs % 60)
} else {
format!("{}:{:02}", secs / 60, secs % 60)
};
match spool.tee().filter(|tee| !tee.to_stdout()) {
Some(tee) => (
format!("saved {size} {dot} {length} {dot} {}", tee.name()),
false,
),
None => (format!("sent {size} {dot} {length}"), false),
}
}
Some(Some(reason)) => (format!("stopped: {reason}"), true),
}
}
/// Where a key was taking the user when leaving was asked about.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Leaving {
Quit,
Home,
}
/// An export under way, for the control bar: the file, its phase, and the bytes
/// written once writing has started.
#[derive(Clone, Debug)]
pub struct ExportProgress {
pub file_path: PathBuf,
pub current_phase: String,
pub written: Option<u64>,
}
/// In-progress analysis computation state (orchestration in App; modal only displays progress).
#[allow(dead_code)]
struct AnalysisComputationState {
df: Option<DataFrame>,
schema: Option<Arc<Schema>>,
partial_stats: Vec<crate::statistics::ColumnStatistics>,
current: usize,
total: usize,
total_rows: usize,
sample_seed: u64,
sample_size: Option<usize>,
}
/// At most one query type can be active. Returns (query, sql_query, fuzzy_query) with only the
/// active one set (SQL takes precedence over fuzzy over DSL query). Used when saving view settings.
fn active_query_settings(
dsl_query: &str,
sql_query: &str,
fuzzy_query: &str,
) -> (Option<String>, Option<String>, Option<String>) {
let sql_trimmed = sql_query.trim();
let fuzzy_trimmed = fuzzy_query.trim();
let dsl_trimmed = dsl_query.trim();
if !sql_trimmed.is_empty() {
(None, Some(sql_trimmed.to_string()), None)
} else if !fuzzy_trimmed.is_empty() {
(None, None, Some(fuzzy_trimmed.to_string()))
} else if !dsl_trimmed.is_empty() {
(Some(dsl_trimmed.to_string()), None, None)
} else {
(None, None, None)
}
}
/// The steps `state` shows, as a saved view keeps them: the query, filters, sort,
/// columns and reshape.
pub(crate) fn view_settings_of(state: &DataTableState) -> view::ViewSettings {
let (query, sql_query, fuzzy_query) = active_query_settings(
state.get_active_query(),
state.get_active_sql_query(),
state.get_active_fuzzy_query(),
);
view::ViewSettings {
chart: None,
sample: saved_sample_of(state),
query,
sql_query,
fuzzy_query,
filters: state.get_filters().to_vec(),
sort_columns: state.get_sort_columns().to_vec(),
sort_descending: state.get_sort_descending().to_vec(),
sort_ascending: state.get_sort_ascending(),
column_order: state.get_column_order().to_vec(),
locked_columns_count: state.locked_columns_count(),
pivot: state.last_pivot_spec().cloned(),
melt: state.last_melt_spec().cloned(),
reshape_source: state.reshape_source().cloned(),
columns: state.column_changes().to_vec(),
}
}
/// The sample `state` is, as a view keeps it: with the query and filters it was
/// drawn through, when it was drawn from the view's rows.
fn saved_sample_of(state: &DataTableState) -> Option<view::SavedSample> {
let sampled = state.sampled()?;
// The whole view it was drawn through: column types, a reshape and a sort pick
// its rows as much as a query does.
let through = sampled.through().then(|| view::ViewSettings {
sample: None,
chart: None,
..view_settings_of(sampled.source())
});
Some(view::SavedSample::of(
sampled.sample(),
sampled.path(),
through,
))
}
/// How far planning a view's steps got.
pub(crate) enum Replayed {
/// Every step is planned; the view's rows are still to be read.
Planned,
/// Stopped at the pivot, which has to be read before the steps after it can be
/// planned.
Pivot(Box<crate::widgets::datatable::PivotJob>),
}
/// What a cloud open was pointed at: the URL as the user gave it, the prefix to list,
/// and the glob to keep, where they named one.
///
/// Together because they are one thought — where to look — and apart they put this
/// function's signature past the point where a reader can hold it.
#[cfg(feature = "cloud")]
struct CloudTarget<'a> {
/// The URL as typed, which is where the bucket and scheme come from.
full: &'a str,
/// The literal prefix to list: the whole key, or the part of a glob before its star.
key: String,
/// The glob the user named, where they named one. The listing keeps only the keys
/// it matches, so everything downstream sees a plain list of files.
pattern: Option<&'a globset::GlobMatcher>,
}
/// Why Data Quality's Run did not start: a cancelled run has not exited yet. Said
/// on Setup's line until it has.
const QUALITY_RUN_WAITS: &str = "Run waits: the cancelled run is still stopping";
/// Why another read of the source did not start: a cancelled analysis has not
/// exited yet, and a second read beside it is how memory runs out.
const ANALYSIS_READ_WAITS: &str = "A cancelled run is still finishing; try again shortly";
/// How long a cancelled run that stops within a batch may take before the screen
/// says it is still going: time for a batch to finish, and no longer.
const CANCEL_GRACE: std::time::Duration = std::time::Duration::from_secs(1);
pub struct App {
pub data_table_state: Option<DataTableState>,
/// The footer counter of the dataset on screen, which its pass behind the open
/// reports to. Handed over by the load that installed it; a load in flight counts on
/// its own until then. See [`Self::footer_progress`].
footer_progress: Arc<crate::schema_union::FooterProgress>,
/// The count as it stood when this frame began, or `None` if no pass was running.
///
/// Taken once because the pass is running on other threads while the frame is
/// drawn. The loading body and the control bar are painted a millisecond apart,
/// and when each read the counter for itself they printed different numbers for
/// one wait — and the bar could print a phase's flat percentage beside a count
/// that had finished between the two reads.
footers_this_frame: Option<(usize, usize)>,
/// The objects a listing had found when this frame began, for the same reason.
listed_this_frame: Option<usize>,
/// Network roots currently being listed off-thread, so a probe is not started
/// twice. Entries are never removed for a root that never answers — that thread
/// is unreclaimable, and retrying it would only block another one.
home_probes_inflight: Vec<PathBuf>,
/// The stop flag of each cloud listing out, by place: leaving the place sets it, and
/// the listing ends before its next page.
home_listing_cancels: HashMap<PathBuf, Arc<std::sync::atomic::AtomicBool>>,
/// The listing out for the names a filter asked of a cut-short cloud directory:
/// where, the name prefix, and its stop flag.
home_narrowing: Option<(PathBuf, String, Arc<std::sync::atomic::AtomicBool>)>,
/// True once cloud discovery has been started. Enumeration costs a request per
/// provider, so it happens once and its result is kept for the session.
#[cfg(feature = "cloud")]
cloud_discovery_started: bool,
/// True while a recursive search below the working directory is out. One at a
/// time: the walk is bounded, and a second one would only compete for the disk.
home_search_inflight: bool,
/// The home generation the walk out was started in. Its batches and its end are its
/// own, whatever refreshes the listing meanwhile; the root decides whether they
/// still describe where the user is.
home_search_generation: u64,
/// Set while the confirmation modal is asking about forgetting every recent.
pending_clear_recents: bool,
/// The checked link the confirmation modal is asking about opening.
pending_link: Option<String>,
/// Whether a browser opened here opens in front of the user: `o` on a
/// documentation link is offered only then (`link_open::local_desktop`).
pub local_desktop: bool,
/// The place whose recents the confirmation modal is asking about forgetting.
pending_forget_place: Option<PathBuf>,
/// Why the last open failed, shown on the home screen when the error is dismissed
/// and there is nothing to fall back to.
last_load_error: Option<String>,
/// Schema reads currently out, so the same one is not requested every frame.
home_schema_inflight: Vec<PathBuf>,
/// Invalidates listings and measurements from a request the user has moved past.
home_generation: u64,
/// Home screen state. Rebuilt from the filesystem whenever home is entered;
/// nothing here is persisted beyond the recents list.
pub home: home::HomeState,
/// Schema previews, memoised for the session only. Persisting these would be a
/// catalogue by another name, and it would go stale.
home_schema_cache: HashMap<PathBuf, Option<discover::SchemaPreview>>,
/// The home screen's `ROWS` previews, and the dataset the newest one built.
pub home_previews: crate::home_preview::Previews,
/// The reads of data started this session, by kind.
pub reads: crate::home_preview::ReadCounts,
/// The dataset whose downloaded shape is kept already. See
/// [`Self::remember_a_downloads_shape`].
shape_remembered: Option<u64>,
path: Option<PathBuf>,
original_file_format: Option<ExportFormat>, // Track original file format for default export
original_file_delimiter: Option<u8>, // Track original file delimiter for CSV export default
/// What `-` reads in place of standard input: a test's pipe.
stdin_reader: Option<Box<dyn std::io::Read + Send>>,
/// Where `--tee -` passes the stream on: standard output as the process got it.
stdout_pass: Option<Box<dyn std::io::Write + Send>>,
/// The follow mark as last drawn, so its clock redraws only when it changes.
follow_drawn: Option<crate::render::footer::FollowMark>,
/// Leaving was asked about while recording: what the user was doing.
pending_leave: Option<Leaving>,
/// A recording kept going after the user went home or quit, until its stream ends.
recording_on: Option<Arc<crate::follow::SpoolHandle>>,
/// A recording's end has been said: once, in the bar or the error dialog.
recording_end_said: bool,
events: Sender<AppEvent>,
debug: DebugState,
pub info_modal: InfoModal,
/// What the Info panel's read found about the open file, and the
/// `dataset_generation` it belongs to. Asked for when the panel opens, read on a
/// worker ([`Job::FileFacts`], whose record says it is reading), and kept for the
/// dataset however the read ended, so neither drawing nor reopening reads again.
file_facts: Option<(u64, FileFacts)>,
/// What the dataset's columns mean, when a catalog that lists it says.
pub codebook: Option<std::sync::Arc<codebook::Codebook>>,
/// The catalog entry the open dataset is, or is inside, and its catalog's label:
/// what Info's Documentation tab shows.
pub catalog_entry: Option<(String, std::sync::Arc<catalog::Dataset>)>,
/// The Documentation view, full screen over home (Ctrl+E).
pub documentation: widgets::documentation::DocState,
/// The same page for the open dataset, on Info's Documentation tab.
pub info_documentation: widgets::documentation::DocState,
/// The directories Ctrl+D kept in the cache before 0.4.0 have been moved into
/// `catalog.toml`, or there were none.
remembered_moved: bool,
/// Send a HEAD for the HTTP(S) file under the cursor on home, to show its size.
/// Off under `cargo test`, which never reaches the network unless a test asks.
pub head_web_rows: bool,
// One input per command line language, each with its own history. The history
// ids ("query", "sql") name files already on disk; they stay as they are so no
// history is lost or read as another language's.
query_input: TextInput, // q, history id "query"
sql_input: TextInput, // SQL, history id "sql"
/// The find prompt (`/`) and the find `n` and `N` repeat; history id "find".
pub find: find::Find,
/// The column cursor moved last: the footer offers the column's keys.
column_hints: bool,
pub input_mode: InputMode,
input_type: Option<InputType>,
query_mode: QueryMode,
/// The language Ctrl+T last chose, which the command line opens on until a query
/// in effect says otherwise.
query_mode_chosen: Option<QueryMode>,
/// The command line holds the query in effect, selected and untouched: Ctrl+T
/// carries it selected, so typing still replaces it.
query_text_restored: bool,
/// The columns of `df`, for the command line's list and completion. Taken from
/// the schema when it opens.
sql_columns: Vec<(String, DataType)>,
/// A Tab completion in progress in the command line.
sql_completion: Option<sql_assist::Cycle>,
/// A query whose first collect is running, and the view to go back to if it
/// fails. From the prompt, the prompt stays open until it is done.
query_running: Option<QueryRun>,
/// Why the last statement failed once it ran, shown under it in the prompt.
query_run_error: Option<String>,
/// Bumped when a running statement's failure lands in the prompt. Keys typed while
/// it ran were not answers to it; see `EventPump`.
inline_failures: u64,
pub sort_filter_modal: SortFilterModal,
pub pivot_melt_modal: PivotMeltModal,
pub view_modal: ViewModal,
/// Whether the open dataset was reached through the home screen. `q` pops
/// the context: opened from home it returns there, launched straight onto
/// a file it quits — the user's mental stack, not a mode.
opened_from_home: bool,
/// `--view NAME`, waiting for the dataset from the command line to land.
/// Taken on the first install, so datasets opened later are not re-dressed.
startup_view: Option<String>,
pub analysis_modal: AnalysisModal,
/// The Sample form over the table (`S`): the view's sample, the step under its
/// query.
pub sample_form: Option<sample_modal::SampleForm>,
/// Where the memory available now is read from, which a sample is checked
/// against. The system's, unless a test says otherwise.
memory_probe: table_sample::MemoryProbe,
/// How each random sample of a stream was drawn on this dataset, by what it was
/// drawn from: drawn again, the same seed keeps the same rows whether or not the
/// count has come in since.
sample_paths: Vec<(String, table_sample::DrawPath)>,
/// Reports, newest first, within [`QUALITY_MEMORY_BUDGET`].
quality_cache: Vec<QualityCacheEntry>,
/// See [`KeptQualitySample`]. Newest first, within [`QUALITY_MEMORY_BUDGET`].
quality_samples: Vec<KeptQualitySample>,
/// Acquisitions the budget released, newest first: (dataset, view, sample).
quality_released: Vec<(u64, u64, sampling::Sample)>,
/// [`QUALITY_MEMORY_BUDGET`], smaller in a test that fills it.
quality_memory_budget: usize,
/// Local copies Data Quality's full scans read instead of a remote source, newest
/// first, within `analysis.quality_local_copy`. Removed from disk when
/// released, when the dataset is opened again or replaced, and at exit.
quality_copies: Vec<RetainedCopy>,
/// The dataset whose copy was released, so Setup says why Run fetches again.
quality_copy_released: Option<u64>,
/// The dataset whose copy did not read as its source: its full scans read the
/// source, and Setup says why.
quality_copy_unusable: Option<u64>,
/// Free bytes in the cache directory, and when they were asked: Setup redraws
/// often, and the answer only feeds a line of text until Run asks again.
quality_copy_free: std::sync::Mutex<Option<(std::time::Instant, Option<u64>)>>,
/// The table an analysis drill left behind: Data Quality's matching rows or the
/// sample's, shown in its place until Esc brings it back.
quality_evidence_return: Option<Box<DataTableState>>,
pub(crate) quality_evidence_label: Option<String>,
pub chart_modal: ChartModal,
pub chart_export_modal: ChartExportModal,
pub export_modal: ExportModal,
pub copy_modal: copy_modal::CopyModal,
pub inspector_modal: inspector_modal::InspectorModal,
/// A value the inspector wrote for another program, for the run loop to open:
/// it owns the terminal that a waiting program takes over.
external_open: Option<external_open::ExternalOpen>,
/// Where those values are written; removed when the app is.
open_dir: Option<tempfile::TempDir>,
/// The shown columns, narrowed by what is typed, while `g` is choosing one.
pub go_to_column: crate::widgets::ui::PickerState,
/// The Value Counts screen (`F`).
pub value_counts: value_counts_modal::ValueCountsModal,
/// The counts the export dialog writes, when it was opened from Value Counts.
export_counts: Option<polars::prelude::DataFrame>,
/// The specs `b` offers for the dataset on screen.
pub format_picker: crate::widgets::ui::PickerState,
/// The type picker, while it is open.
pub retype: Option<retype_modal::RetypeModal>,
/// The combine form, while it is open.
pub combine: Option<retype_modal::CombineModal>,
/// The type picker or the combine form go back to the Info panel, not the table.
pub(crate) retype_from_info: bool,
/// The tables `T` offers: the picker's lines, and what each opens.
pub table_picker: crate::widgets::ui::PickerState,
pub table_choices: Option<table_switch::Tables>,
/// The hex view (`InputMode::Hex`), kept while it is up.
pub hex: Option<hex_view::HexView>,
/// Bumped per hex view opened, so a find's answer for another is dropped.
hex_serial: u64,
/// Where copies go. Built at the first copy and kept for the run: on
/// Wayland and X11 the clipboard offer dies with the process that owns it,
/// so this handle must live as long as the copy should.
clipboard: Option<Box<dyn clipboard::Destination>>,
/// A table-scope copy waiting on the size confirmation.
pending_copy: Option<(clipboard::CopyFormat, bool)>,
pub(crate) chart_cache: ChartCache,
/// The one chart preparation allowed to run at a time. Render draws only what is in
/// `chart_cache`; this drives the throbber while it is current. Its result is
/// installed only if the record is still current (not `stale`) and the dataset is
/// the one it was computed from. Deliberately not
/// `busy`: the sidebar stays live while the data is computed, and the newest
/// selection is prepared once this one lands.
chart_inflight: Option<ChartInflight>,
/// The selection the chart last asked for, and, when it stepped the aggregate of
/// the one before, until when it waits for the next step before it is prepared.
chart_asked: Option<(ChartRequest, Option<std::time::Instant>)>,
/// The result of the background chart preparation, like `pending_collect_result`:
/// the data stays out of the event.
pending_chart_result: ChartResultSlot,
/// A chart export that asked for data still being prepared. `BackgroundChartReady`
/// picks it up; `busy` stays set until then.
chart_export_waiting: Option<ChartExportRequest>,
error_modal: ErrorModal,
flash: Option<Flash>,
pub confirmation_modal: ConfirmationModal,
/// An export waiting on the overwrite confirmation.
pending_export: Option<ExportRequest>,
/// The saved view `d` asked to delete, by id, while the confirmation is up.
pending_delete_view: Option<String>,
/// Delete on the Example datasets heading asked to hide them.
pending_hide_examples: bool,
pending_chart_export: Option<ChartExportRequest>,
/// A Data Quality report export waiting on the overwrite confirmation.
pending_quality_export: Option<(PathBuf, crate::quality_export::ReportFormat)>,
/// The help overlay, over whatever screen it was opened at.
help: help::Help,
/// What the mouse can land on in the last frame, and the last click.
pointer: pointer::Pointing,
/// The menu a right click on a cell opened, while it is open.
context_menu: Option<context_menu::ContextMenu>,
cache: CacheManager,
/// The recent and the shape an open writes, which the home listing waits on.
cache_writes: CacheWrites,
view_manager: Views,
active_view_id: Option<String>, // ID of currently applied view
/// An export under way, which the control bar reports.
export_progress: Option<ExportProgress>,
theme: Theme, // Color theme for UI rendering
/// `a` is waiting on the confirmation to read every row.
pending_read_all: bool,
history_limit: usize, // History limit for all text inputs (from config.query.history_limit)
table_cell_padding: u16, // Spaces between columns (from config.display.cell_padding)
column_colors: bool, // When true, colorize table cells by column type (from config.display.column_colors)
/// Second header row of column types. Starts from `display.type_row`; `D` flips it.
dtype_row: bool,
// Resolved display-time number formatting. `enabled` is flipped by the F key.
number_format: NumberFormatSettings,
runtime: tokio::runtime::Handle, // Tokio runtime handle for background tasks
/// Every general background operation, and the generation their answers are judged
/// by. See [`jobs`].
jobs: Jobs,
/// The open in flight, from the request to its first rows: its phase, what the
/// loading screen says, and what it holds. See [`loading`]. Going home abandons it;
/// an answer from an open it no longer holds is dropped.
loading: loading::Loader,
/// The paths the dataset on screen was opened from, with the options it installed
/// with: what `H` opens again with its header turned the other way.
opened: Option<(Vec<PathBuf>, OpenOptions)>,
/// Where the last load-ahead was asked from. See [`App::load_ahead`].
loaded_ahead_from: Option<(u64, usize, usize, usize)>,
// `len_generation` of the in-flight background row-count, if any. Prevents re-spawning
// the (potentially minutes-long) count on every scroll while it's still running.
len_count_inflight: Option<u64>,
/// The `len_generation` of a count `len_count_inflight` promises that has not
/// started. A full count of a local frame competes with reading its first page for
/// the disk and the Polars workers, and that page's rows can make it unnecessary,
/// so it starts once a frame has painted them. See [`App::frame_painted`].
count_after_paint: Option<u64>,
/// Counts started, so a test can say none began before the page was painted.
#[cfg(test)]
counts_spawned: std::cell::Cell<usize>,
/// Times an installed dataset's own first rows were asked for, so a test can say a
/// view applied on open read them instead.
#[cfg(test)]
first_rows_asked: usize,
// `len_generation` whose background row-count failed. While this matches the current
// generation (and the count is still invalid) the row count is shown as "?" rather than a
// misleading provisional total.
len_count_failed: Option<u64>,
/// End was pressed on a remote dataset before its rows were counted: go there when
/// the count for this generation arrives, rather than to a guess.
end_after_count: Option<u64>,
/// What the pass behind a staged open found, for the frame that applies it: large
/// enough to be worth keeping out of the event, and discarded if the dataset it
/// belongs to has been replaced.
pending_footers_result: std::sync::Arc<std::sync::Mutex<FootersReported>>,
/// Bumped once per dataset put on screen, which the jobs' generation is not: a collect
/// bumps that, and the pass reading the rest of a dataset's footers outlives
/// several. It is what says whether the columns arriving belong to the dataset the
/// user is looking at.
dataset_generation: u64,
/// End was pressed while a dataset was still reading its footers, which is where
/// its end is coming from. Jump when they land — and only for that dataset, which
/// is what the generation is for: a directory the user pressed End on and then walked
/// away from must not move the view of the one they opened next. `end_after_count`
/// alongside keys itself the same way, to `len_generation`.
end_when_the_footers_land: Option<u64>,
/// End was pressed while a text file's lines were still being indexed: jump when
/// the last of them is, for that dataset alone.
end_when_indexed: Option<u64>,
/// Stops the indexing thread of the dataset on screen's lines.
indexing_stop: Arc<std::sync::atomic::AtomicBool>,
/// The lines being indexed, until they all are.
indexing_lines: Option<Arc<crate::lines::Lines>>,
/// The indexing waits while home is up.
indexing_paused: bool,
/// `:N` past the lines indexed so far, for that dataset: gone to once they all are.
goto_when_indexed: Option<(u64, usize)>,
/// The last count started: what it has read of the footers, and its stop (Esc).
count_progress: Arc<crate::schema_union::FooterProgress>,
/// The dataset (`dataset_generation`) an exact count was asked for (`c` in the
/// Info panel), of more files than the count reads unasked.
exact_count_asked: Option<u64>,
/// `c` was pressed while a stopped count was still winding down: count again when
/// its answer, for this `len_generation`, comes in.
count_after_stop: Option<u64>,
/// What a dataset's footers found while the user was looking at a query, a pivot or
/// a drill-down rather than at the data. Held rather than applied, because widening
/// the scan under a query takes the query's own columns away, and offered again the
/// moment the view comes back to the dataset itself.
footers_held: Option<(u64, crate::widgets::datatable::FootersFound)>,
/// Fields a followed pipe's NDJSON brought after the open, held as footers are
/// until the view is back on the data.
followed_fields_held: Option<(u64, Vec<polars::prelude::Field>)>,
/// A re-read the dataset is owed by a footer pass that came back empty-handed, held
/// back because the collect it goes through would bump the generation out from
/// under work already running. The pass that failed brings no columns to hold, so
/// `footers_held` has nothing to say about it, and the dataset still needs the
/// ordinary count the pass was going to save it — hence an errand of its own, tried
/// again after every event until the work it would cancel is done.
reread_owed: Option<u64>,
/// Which home screen workers panic before their work starts, for tests of what a
/// dying worker leaves behind. The jobs' own is [`Jobs::worker_dies`].
#[cfg(test)]
home_worker_dies: Option<HomeWorkerDies>,
/// Reads the open file's facts in place of [`FileFacts::read`], for tests of a
/// read that is slow or fails.
#[cfg(test)]
file_facts_reader: Option<FileFactsReader>,
/// When true, show the throbber and defer keys (see [`App::handle`]); the main loop
/// holds them until this clears.
busy: bool,
/// Bumped whenever the screen the user was typing at is replaced without a key of
/// theirs asking for it: going home, abandoning a load. Keys held while busy carry
/// the value they were typed under and are dropped if it has moved on.
screen_generation: u64,
/// Set by the main loop when it had to drop a key typed while busy, shown beside a
/// status message while work is running. Cleared once the held keys have been
/// replayed.
input_dropped: bool,
throbber_frame: u8, // Spinner frame index (0..3) for control bar
/// Status text for the control bar, at the table view. Shown whether or not the app
/// is busy: an End waiting on a remote row count parks without setting `busy`.
status_message: Option<String>,
analysis_computation: Option<AnalysisComputationState>,
app_config: AppConfig,
/// The terminal should be asked for its background before the next frame.
background_query: bool,
/// The format specs on the search path, read when the app was built.
formats: Arc<crate::formats::Registry>,
}
impl App {
/// The finding under the cursor's rows: from the rows the run kept, at once, or
/// staged as a read that says what it reads and waits for Enter. A finding with
/// no rows to show opens nothing.
fn open_quality_evidence(&mut self) -> Option<AppEvent> {
let (_, finding) = self.analysis_modal.selected_finding()?;
let results = self.analysis_modal.data_quality_results.as_ref()?;
let rows = finding.evidence(results).ok()?;
let sampled = results.precision == data_quality::QualityPrecision::Sampled;
let count = finding.evidence_count(results);
let label = format!(
"Data Quality / {} / {}",
finding.title,
quality_report::columns_label(&finding.columns, 40)
);
let what = format!(
"{} {} {}",
finding.title,
glyphs::get().middot,
quality_report::columns_label(&finding.columns, 40)
);
self.show_quality_rows(rows, label, sampled, what, count)
}
/// The rows an interval's count under the cursor counted: from the rows the run
/// kept, or staged as a read when it kept none. Nothing opens for a count of none.
fn open_interval_evidence(&mut self) -> Option<AppEvent> {
let schema = self.data_table_state.as_ref().map(|state| state.schema());
let (predicate, label, count) = self
.analysis_modal
.interval_evidence(schema.map(|schema| schema.as_ref()))?;
let sampled = self
.analysis_modal
.data_quality_results
.as_ref()
.is_some_and(|results| results.precision == data_quality::QualityPrecision::Sampled);
let what = label
.trim_start_matches("Data Quality / ")
.replace(" / ", &format!(" {} ", glyphs::get().middot));
self.show_quality_rows(
quality_report::EvidenceRows::Matching(predicate),
label,
sampled,
what,
Some(count),
)
}
/// The rows the report on screen measured, while they are kept: a sampled run's
/// rows, same dataset, view and sample. `None` after a full scan, which keeps
/// none, and once they are released.
pub(crate) fn quality_rows_kept(&self) -> Option<std::sync::Arc<data_quality::QualitySample>> {
self.analysis_modal.data_quality_results.as_ref()?;
let plan = self.analysis_modal.quality_result_plan();
if plan.compute != data_quality::QualityCompute::Sample {
return None;
}
self.kept_quality_sample(&plan.sample())
}
/// Open rows a Data Quality result counted. Kept rows are cut in memory and
/// shown; anything else would read the source, so it is staged with what it
/// reads, and only Enter on that reads.
fn show_quality_rows(
&mut self,
rows: quality_report::EvidenceRows,
label: String,
sampled: bool,
what: String,
count: Option<usize>,
) -> Option<AppEvent> {
let plan = self.analysis_modal.quality_result_plan().clone();
let by_files = matches!(rows, quality_report::EvidenceRows::Files(_));
let label = if sampled {
format!("{label} / sampled")
} else {
label
};
if !by_files && self.quality_rows_kept().is_some() {
return self.read_sample_rows(plan.sample(), Some((rows, label)));
}
let state = self.data_table_state.as_ref()?;
let g = glyphs::get();
let rows_label = |rows: usize| {
format!(
"{} {}",
numfmt::group_chrome(rows),
if rows == 1 { "row" } else { "rows" }
)
};
let scope = match &rows {
quality_report::EvidenceRows::Files(files) => files.clone(),
_ => plan.scope.clone(),
};
// A sample as large as the scope reads every row too, but it was not a full
// scan: its rows were kept, and since released.
let not_kept = if plan.compute == data_quality::QualityCompute::Full {
"a full scan keeps no rows"
} else {
"the rows read are no longer kept"
};
let (why, reads) = match &rows {
quality_report::EvidenceRows::Files(data_quality::QualityScope::SourceFiles(files)) => {
(
"their rows are in the files, not the report".to_string(),
format!(
"the {} named {}",
files.len(),
if files.len() == 1 { "file" } else { "files" }
),
)
}
_ if sampled => (
"the sampled rows are no longer kept".to_string(),
format!(
"the sample again: {} {} {}",
widgets::data_quality::compute_label(&plan),
g.middot,
widgets::data_quality::planned_read_label(state, &plan)
),
),
quality_report::EvidenceRows::Duplicates => (
not_kept.to_string(),
format!(
"every row of {}, once {} {}",
plan.scope.label(),
g.middot,
widgets::data_quality::scope_read_label(state, &plan)
),
),
// The table counts what matches, which reads every row; then it reads the
// rows it shows.
_ => (
not_kept.to_string(),
format!(
"every row of {} to count them, then the rows on screen {} {}",
plan.scope.label(),
g.middot,
widgets::data_quality::scope_read_label(state, &plan)
),
),
};
let shows = match (&rows, count) {
(quality_report::EvidenceRows::Duplicates, Some(count)) => {
format!("{}, copies together", rows_label(count))
}
(_, Some(count)) => rows_label(count),
(_, None) => "the rows that match".to_string(),
};
let source = if state.is_remote_source() {
"remote, read only"
} else {
"local, read only"
};
self.analysis_modal.data_quality_evidence_read = Some(analysis_modal::EvidenceRead {
summary: vec![
("Rows", what),
("Why", why),
("Reads", reads),
("Shows", shows),
("Source", source.to_string()),
],
sample: (sampled && !by_files).then(|| plan.sample()),
scope,
rows,
label,
});
None
}
/// Enter on a staged read: read the rows it named, as it said.
fn confirm_evidence_read(&mut self) -> Option<AppEvent> {
// Beside a cancelled run still reading, the read stays staged for later.
let staged = self.analysis_modal.data_quality_evidence_read.as_ref()?;
let kept = staged
.sample
.as_ref()
.is_some_and(|sample| self.kept_quality_sample(sample).is_some());
if !kept && self.read_waits_for_cancelled() {
return None;
}
let read = self.analysis_modal.data_quality_evidence_read.take()?;
if let Some(sample) = read.sample {
return self.read_sample_rows(sample, Some((read.rows, read.label)));
}
let predicate = match read.rows {
quality_report::EvidenceRows::Matching(predicate) => predicate,
// A column its file never had, or holds in a type the scan cannot read,
// has no value to filter on: its rows are the ones those files hold.
quality_report::EvidenceRows::Files(_) => polars::prelude::lit(true),
quality_report::EvidenceRows::Duplicates => {
return self.read_duplicate_rows(read.scope, read.label);
}
};
self.open_quality_scope_rows(&read.scope, predicate, read.label)
}
/// Every row of `scope` that repeats, read in one pass off the UI thread and
/// shown as a table: what a full scan's duplicate finding opens once asked to.
fn read_duplicate_rows(
&mut self,
scope: data_quality::QualityScope,
label: String,
) -> Option<AppEvent> {
let state = self.data_table_state.as_ref()?;
let (lf, schema) = match state.quality_scope_frame(&scope) {
Ok(frame) => frame,
Err(error) => {
self.error_modal
.show(format!("Cannot open matching rows: {error}"));
return None;
}
};
let keys = schema.iter_names().cloned().collect::<Vec<_>>();
// Binary values as the run grouped them: one stub for all, so they never
// split a group the check counted as copies.
let columns = schema
.iter()
.map(|(name, dtype)| {
if matches!(dtype, polars::prelude::DataType::Binary) {
polars::prelude::lit(widgets::datatable::binary_stub()).alias(name.clone())
} else {
polars::prelude::col(name.clone())
}
})
.collect::<Vec<_>>();
let streaming = self.app_config.performance.streaming;
self.analysis_modal.computing = Some(AnalysisProgress::new("Reading the rows that repeat"));
self.spawn_job(
Job::SampleRows,
Some("Reading the rows that repeat..."),
move |_| {
let df = data_quality::duplicate_rows(lf.select(columns), &keys, streaming)
.map_err(|error| format!("{error}"))?;
Ok(Answer::Sample { df, label })
},
);
None
}
/// The rows of `scope` matching `predicate`, in the table viewer in place of the
/// table; Esc brings the table and the report back.
fn open_quality_scope_rows(
&mut self,
scope: &data_quality::QualityScope,
predicate: polars::prelude::Expr,
label: String,
) -> Option<AppEvent> {
let state = self.data_table_state.as_ref()?;
let view = match state.quality_evidence_view(scope, predicate) {
Ok(view) => view,
Err(error) => {
self.error_modal
.show(format!("Cannot open matching rows: {error}"));
return None;
}
};
if let Some(original) = self.data_table_state.replace(view) {
self.quality_evidence_return = Some(Box::new(original));
self.quality_evidence_label = Some(label);
self.analysis_modal.active = false;
self.forget_the_rows_read();
self.spawn_async_collect("Loading matching rows...");
}
None
}
fn return_from_quality_evidence(&mut self, reopen_analysis: bool) -> bool {
let Some(original) = self.quality_evidence_return.take() else {
return false;
};
self.jobs.advance();
self.len_count_inflight = None;
self.data_table_state = Some(*original);
self.quality_evidence_label = None;
self.analysis_modal.active = reopen_analysis;
self.busy = false;
self.status_message = None;
true
}
fn restore_recent_quality_plan(&mut self) {
let Some(view_generation) = self
.data_table_state
.as_ref()
.map(DataTableState::len_generation)
else {
return;
};
if self.analysis_modal.data_quality_plan != data_quality::DataQualityPlan::default() {
return;
}
if let Some(cached) = self.quality_cache.iter().find(|entry| {
entry.dataset_generation == self.dataset_generation
&& entry.view_generation == view_generation
}) {
self.analysis_modal.data_quality_plan = cached.plan.clone();
}
}
/// What the data offers Setup's choices. Read from the schema and the rows
/// already on screen: nothing here reads the source.
fn quality_plan_context(&self) -> analysis_modal::PlanContext {
let Some(state) = self.data_table_state.as_ref() else {
return analysis_modal::PlanContext::default();
};
let plan = &self.analysis_modal.data_quality_plan;
let scope = &plan.scope;
let schema = state.schema();
let mut partitions = state.partition_columns().unwrap_or_default().to_vec();
// A directory whose files agree opens as one scan and names no partition
// columns; its directory names still do.
if partitions.is_empty()
&& let Some(dir) = self.path.as_ref().filter(|path| path.is_dir())
{
partitions = DataTableState::discover_hive_partition_columns(dir)
.into_iter()
.filter(|column| schema.get(column).is_some())
.collect();
}
// Date and time columns, then text read as time: a window can split by either.
let mut time_columns: Vec<(String, bool)> = state
.quality_temporal_columns(scope)
.into_iter()
.map(|column| {
let has_time =
!matches!(schema.get(&column), Some(polars::prelude::DataType::Date));
(column, has_time)
})
.collect();
for format in &plan.time_formats {
if !time_columns
.iter()
.any(|(column, _)| *column == format.column)
{
time_columns.push((
format.column.clone(),
format.kind == data_quality::TimeKind::Datetime,
));
}
}
analysis_modal::PlanContext {
partitions,
time_columns,
files: state.quality_source_file_count() > 1,
text_columns: state
.quality_text_columns(scope)
.into_iter()
.map(|column| {
let examples = state.buffered_values(&column, 3);
(column, examples)
})
.collect(),
}
}
/// The columns a time role can be given: date and time columns, then text,
/// which a role reads through its Text as time format.
pub(crate) fn quality_time_candidates(&self) -> Vec<String> {
let Some(state) = self.data_table_state.as_ref() else {
return Vec::new();
};
let scope = &self.analysis_modal.data_quality_plan.scope;
let mut columns = state.quality_temporal_columns(scope);
columns.extend(state.quality_text_columns(scope));
columns
}
/// The columns Column intent lists: the draft's scope's, from the schema.
pub(crate) fn quality_intent_columns(&self) -> Vec<(String, polars::prelude::DataType)> {
self.data_table_state
.as_ref()
.map(|state| {
crate::widgets::quality_intent::intent_columns(
state.quality_schema(&self.analysis_modal.data_quality_plan.scope),
)
})
.unwrap_or_default()
}
/// Open the intent form on the column under the cursor of the Column intent list.
fn open_intent_form(&mut self) {
let columns = self.quality_intent_columns();
let modal = &mut self.analysis_modal;
let Some((column, dtype)) = columns.get(modal.data_quality_plan_field) else {
return;
};
let plan = &modal.data_quality_plan;
modal.data_quality_intent_form = Some(intent_modal::IntentForm::new(
column,
dtype.clone(),
plan.time_format(column).cloned(),
&plan.intent,
&self.theme,
));
}
/// Keys while the intent form is open: Tab and ↑↓ walk its rows, Space and ←→
/// change a choice, text fields type, Enter stages the declaration in Setup's
/// draft and Esc drops the form's edits. Nothing here reads.
fn intent_form_key(&mut self, event: &KeyEvent) {
let modal = &mut self.analysis_modal;
let Some(form) = modal.data_quality_intent_form.as_mut() else {
return;
};
match form::key(form, event) {
FormKey::Cancel => modal.data_quality_intent_form = None,
FormKey::Submit => match form.apply(&mut modal.data_quality_plan.intent) {
Ok(()) => modal.data_quality_intent_form = None,
Err(error) => form.error = Some(error),
},
FormKey::Act(_) => form.adjust(true),
FormKey::Step(_, delta) => form.adjust(delta > 0),
FormKey::Text(_) => {
if let Some(input) = form.input_mut() {
let _ = input.handle_key(event, None);
}
form.error = None;
}
FormKey::Moved | FormKey::Other => {}
}
}
/// What a Data Quality run reads from, as far as the app knows without reading:
/// where it was opened from, its files, and what the view does to the rows when
/// the scope is the view.
fn quality_source_identity(
&self,
state: &DataTableState,
scope: &data_quality::QualityScope,
) -> crate::quality_export::SourceIdentity {
let format = self
.original_file_format
.map(|format| format.as_str().to_string())
.or_else(|| {
self.path
.as_ref()
.and_then(|path| path.extension())
.and_then(|extension| extension.to_str())
.map(str::to_string)
});
let mut view = Vec::new();
if !scope.uses_source() {
if !state.get_active_query().is_empty() {
view.push(format!("query: {}", state.get_active_query()));
}
if !state.get_active_sql_query().is_empty() {
view.push(format!("SQL: {}", state.get_active_sql_query()));
}
if !state.get_active_fuzzy_query().is_empty() {
view.push(format!("text: {}", state.get_active_fuzzy_query()));
}
for (index, filter) in state.view_filters().iter().enumerate() {
let join = if index == 0 {
String::new()
} else {
format!("{} ", filter.logical_op.as_str())
};
view.push(format!(
"filter: {join}{} {} {}",
filter.column,
filter.operator.as_str(),
filter.value
));
}
if state.reshape_source().is_some() {
view.push("reshaped: pivot or melt".to_string());
}
}
let remote = state.is_remote_source();
// A local path made whole, so the report names the file wherever it is read;
// no file system access.
let piped = self.reads_stdin();
let location = self.path.as_ref().map(|path| {
match std::path::absolute(path).ok().filter(|_| !remote && !piped) {
Some(path) => path.display().to_string(),
None => path.display().to_string(),
}
});
crate::quality_export::SourceIdentity {
location,
remote,
format,
view,
..crate::quality_export::SourceIdentity::default()
}
.with_files(state.quality_source_file_names())
}
/// The export dialog, on a name made from the dataset's.
fn open_quality_export(&mut self) {
let stem = self
.path
.as_ref()
.and_then(|path| path.file_stem())
.and_then(|stem| stem.to_str())
.filter(|stem| !stem.is_empty())
.unwrap_or("data")
.to_string();
self.analysis_modal.data_quality_export =
Some(crate::quality_export::ExportForm::new(&stem, &self.theme));
}
/// Keys while the export dialog is open: Tab moves between the path and the
/// form, the arrows or Space change the form, Enter writes (asking first over a
/// file that exists), Esc closes it.
fn quality_export_key(&mut self, event: &KeyEvent) -> Option<AppEvent> {
let form = self.analysis_modal.data_quality_export.as_mut()?;
match event.code {
KeyCode::Esc => self.analysis_modal.data_quality_export = None,
KeyCode::Tab | KeyCode::BackTab | KeyCode::Up | KeyCode::Down => form.toggle_focus(),
KeyCode::Left
| KeyCode::Right
| KeyCode::Char(' ')
| KeyCode::Char('h')
| KeyCode::Char('l')
if form.on_format =>
{
form.cycle_format();
}
KeyCode::Enter => match form.target() {
Err(error) => form.error = Some(error),
Ok((path, format)) => {
if path.exists() {
let shown = path.display().to_string();
self.pending_quality_export = Some((path, format));
self.confirmation_modal.show_destructive(
format!("File already exists:\n{shown}\n\nOverwrite it?"),
"Overwrite",
);
} else {
// The dialog stays up while the report is written: a failed
// write says why on its status line, the path still there.
return Some(AppEvent::QualityReportExport(
path,
format,
Overwrite::Forbid,
));
}
}
},
_ if !form.on_format => {
let _ = form.path.handle_key(event, None);
form.error = None;
}
_ => {}
}
None
}
/// Space on a Setup row: the Sample form, the role editor, or the row's choices.
fn open_setup_row(&mut self) -> Option<AppEvent> {
use analysis_modal::SetupRow;
self.analysis_modal.data_quality_setup_note = None;
match self.analysis_modal.setup_row() {
SetupRow::Sample => self.open_quality_sample_form(),
SetupRow::TimeRoles => {
// With no date, time or text column there is no role to assign.
if !self.quality_time_candidates().is_empty() {
self.analysis_modal.data_quality_plan_before_edit =
Some(self.analysis_modal.data_quality_plan.clone());
self.analysis_modal
.set_quality_page(data_quality::QualityPage::TimeRoles);
self.analysis_modal.data_quality_plan_field = 0;
}
}
SetupRow::Intent => {
if !self.quality_intent_columns().is_empty() {
self.analysis_modal.data_quality_plan_before_edit =
Some(self.analysis_modal.data_quality_plan.clone());
self.analysis_modal
.set_quality_page(data_quality::QualityPage::Intent);
self.analysis_modal.data_quality_plan_field = 0;
}
}
SetupRow::Intervals => {
// Two assigned roles make the first pair to choose.
if !self
.analysis_modal
.data_quality_plan
.candidate_pairs()
.is_empty()
{
self.analysis_modal.data_quality_plan_before_edit =
Some(self.analysis_modal.data_quality_plan.clone());
self.analysis_modal
.set_quality_page(data_quality::QualityPage::IntervalPairs);
self.analysis_modal.data_quality_plan_field = 0;
}
}
SetupRow::Expected => {
// Windows are what a gap is counted in; with no time-window grain
// there is nothing to expect yet.
if matches!(
self.analysis_modal.data_quality_plan.grain,
data_quality::QualityGrain::TimeWindows { .. }
) {
self.analysis_modal.data_quality_expected_form =
Some(analysis_modal::ExpectedForm::new(
&self.analysis_modal.data_quality_plan,
&self.theme,
));
self.analysis_modal
.set_quality_page(data_quality::QualityPage::ExpectedWindows);
}
}
row => {
let context = self.quality_plan_context();
self.analysis_modal.open_plan_picker(row, &context);
}
}
None
}
/// Keys in the Expected editor: ↑↓ the row, ←→ the cadence, typing in From and
/// Before. Enter writes it into the draft, or says on its own line why it cannot;
/// Esc leaves the draft as it was. Either way back to Setup's Expected row.
fn expected_form_key(&mut self, event: &KeyEvent) {
let every = match &self.analysis_modal.data_quality_plan.grain {
data_quality::QualityGrain::TimeWindows { every, .. } => every.clone(),
_ => String::new(),
};
let Some(form) = self.analysis_modal.data_quality_expected_form.as_mut() else {
return;
};
match form::key(form, event) {
FormKey::Cancel => {}
FormKey::Submit => match form.expected() {
Ok(expected) => self.analysis_modal.data_quality_plan.expected = expected,
Err(problem) => {
form.error = Some(problem);
return;
}
},
FormKey::Step(_, delta) => {
form.cycle(&every, delta > 0);
return;
}
FormKey::Text(_) => {
if let Some(input) = form.input_mut() {
let _ = input.handle_key(event, None);
form.error = None;
}
return;
}
FormKey::Act(_) | FormKey::Moved | FormKey::Other => return,
}
self.analysis_modal.data_quality_expected_form = None;
self.analysis_modal.data_quality_setup_note = None;
self.analysis_modal
.set_quality_page(data_quality::QualityPage::Setup);
self.analysis_modal.data_quality_plan_field = analysis_modal::SetupRow::Expected.index();
}
/// `w` on Trends: the next coarser grain, staged in Setup with the Grain row under
/// the cursor, for segments the sample reached too thinly. Nothing runs until
/// Enter, and Setup's Read says what that run reads; Esc puts the grain back.
fn stage_coarser_grain(&mut self) {
let Some(coarser) = self.analysis_modal.quality_result_plan().coarser_grain() else {
return;
};
self.open_quality_setup();
let plan = &mut self.analysis_modal.data_quality_plan;
plan.grain = coarser;
plan.baseline_segment = None;
self.analysis_modal.data_quality_plan_field = analysis_modal::SetupRow::Grain.index();
}
/// Enter in a Setup row's list: take the choice, and after a text column, ask
/// for its format with the values on screen beside each one.
fn choose_setup_picker(&mut self) {
self.analysis_modal.data_quality_setup_note = None;
if let Some(column) = self.analysis_modal.choose_plan_picker() {
let examples = self
.data_table_state
.as_ref()
.map(|state| state.buffered_values(&column, 3))
.unwrap_or_default();
self.analysis_modal.open_format_picker(&column, &examples);
}
}
/// The Setup setting the Data Quality page on screen lacks before it can show
/// anything; Enter opens it, and the control bar says so.
pub(crate) fn quality_page_setup(&self) -> Option<data_quality::QualitySetup> {
let modal = &self.analysis_modal;
data_quality::page_setup(
modal.data_quality_page,
modal.quality_result_plan(),
modal.data_quality_results.as_ref(),
self.has_quality_time_columns(),
)
}
/// Whether the plan's scope has a column it reads as time: a date or time
/// column, or text given a format. What an empty Trends page points to.
pub(crate) fn has_quality_time_columns(&self) -> bool {
!self
.analysis_modal
.data_quality_plan
.time_formats
.is_empty()
|| self.data_table_state.as_ref().is_some_and(|state| {
!state
.quality_temporal_columns(&self.analysis_modal.data_quality_plan.scope)
.is_empty()
})
}
/// Whether Data Quality's retained rows are the rows `plan` reads, so a run
/// starts from them rather than from the source. They serve any grain: every
/// column is kept, and where each row sat.
pub(crate) fn quality_kept_serves(&self, plan: &data_quality::DataQualityPlan) -> bool {
plan.compute == data_quality::QualityCompute::Sample
&& self.kept_quality_sample(&plan.sample()).is_some()
}
/// Where a run of `plan` gets its exact segment totals: from the retained rows'
/// counts, from the pass that reads a new sample, or from a read of their own.
pub(crate) fn quality_segment_count(
&self,
plan: &data_quality::DataQualityPlan,
) -> data_quality::SegmentCount {
if plan.compute != data_quality::QualityCompute::Sample {
return data_quality::SegmentCount::NotNeeded;
}
match self.kept_quality_sample(&plan.sample()) {
Some(kept) => kept.segment_count(plan),
None => data_quality::fresh_segment_count(plan, self.quality_may_read_blocks(plan)),
}
}
/// Whether the rows `plan` reads were read this session and released to the
/// memory budget, so a Run reads them again.
pub(crate) fn quality_released(&self, plan: &data_quality::DataQualityPlan) -> bool {
let Some(view_generation) = self
.data_table_state
.as_ref()
.map(DataTableState::len_generation)
else {
return false;
};
let sample = plan.sample();
plan.compute == data_quality::QualityCompute::Sample
&& self
.quality_released
.iter()
.any(|(dataset, view, released)| {
*dataset == self.dataset_generation
&& *view == view_generation
&& *released == sample
})
}
/// Whether the dataset is one Parquet or IPC file, the one kind the sampler may
/// read seeded runs of.
fn quality_one_columnar_file(&self) -> bool {
let Some(state) = self.data_table_state.as_ref() else {
return false;
};
let columnar = matches!(
self.original_file_format,
Some(ExportFormat::Parquet | ExportFormat::Ipc)
) || self.path.as_ref().is_some_and(|path| {
path.extension()
.and_then(|extension| extension.to_str())
.is_some_and(|extension| {
matches!(
extension.to_ascii_lowercase().as_str(),
"parquet" | "pq" | "arrow" | "arrows" | "ipc" | "feather"
)
})
});
columnar && state.loaded_file_count() == 1
}
/// Whether a random sample of `plan` reads seeded runs of one file rather than
/// stream every row, as Setup's Read says. Told from the path and the view, since
/// the sampler's own test needs the plan built. Yes only where the scan is read as
/// loaded: the whole source whatever the view, or a view that picks no rows (a
/// sort does not count: samples read the view unsorted). Where it is not sure,
/// Setup names the longer read.
pub(crate) fn quality_reads_blocks(&self, plan: &data_quality::DataQualityPlan) -> bool {
let Some(state) = self.data_table_state.as_ref() else {
return false;
};
self.quality_one_columnar_file()
&& match plan.scope {
data_quality::QualityScope::WholeSource => true,
data_quality::QualityScope::CurrentView => !state.changes_rows(),
// Read in the order on screen, sort included.
data_quality::QualityScope::FirstRows(_)
| data_quality::QualityScope::ViewRows { .. } => {
state.source_file_count() == Some(1)
}
_ => false,
}
}
/// Whether a random sample of `plan` may read seeded runs: where
/// [`Self::quality_reads_blocks`] is sure, and wherever the view may still read
/// the scan as loaded, a query's included.
///
/// Leans to yes: seeded runs see too few rows to count segments, so a yes is what
/// makes Setup name a count pass, and a run that streams after all counts in its
/// one pass and reads less than Setup said, never more.
pub(crate) fn quality_may_read_blocks(&self, plan: &data_quality::DataQualityPlan) -> bool {
let Some(state) = self.data_table_state.as_ref() else {
return false;
};
self.quality_reads_blocks(plan)
|| (self.quality_one_columnar_file()
&& state.may_keep_scan_rows()
&& matches!(
plan.scope,
data_quality::QualityScope::CurrentView
| data_quality::QualityScope::FirstRows(_)
| data_quality::QualityScope::ViewRows { .. }
))
}
/// Whether the session cache holds a report measuring what `plan` measures on
/// this view: the windows it expects are checked against the report, not read.
pub(crate) fn quality_cached(&self, plan: &data_quality::DataQualityPlan) -> bool {
let Some(view_generation) = self
.data_table_state
.as_ref()
.map(DataTableState::len_generation)
else {
return false;
};
self.quality_cache.iter().any(|entry| {
entry.dataset_generation == self.dataset_generation
&& entry.view_generation == view_generation
&& entry.plan.same_measurement(plan)
})
}
/// Everything Data Quality's runs kept for reuse on this dataset, as `d` in Setup
/// would release it: sampled rows in memory and a full scan's local copy on
/// disk. `None` when there is neither.
pub(crate) fn quality_kept_rows(&self) -> Option<widgets::data_quality::KeptRows> {
let kept = self
.quality_samples
.iter()
.filter(|kept| kept.dataset_generation == self.dataset_generation)
.collect::<Vec<_>>();
let copy_bytes = self
.quality_copies
.iter()
.filter(|kept| kept.dataset_generation == self.dataset_generation)
.map(|kept| kept.copy.bytes())
.sum::<u64>();
(!kept.is_empty() || copy_bytes > 0).then(|| widgets::data_quality::KeptRows {
samples: kept.len(),
rows: kept.iter().map(|kept| kept.rows.df().height()).sum(),
bytes: kept.iter().map(|kept| kept.rows.estimated_bytes()).sum(),
copy_bytes,
})
}
/// `d` in Setup: let go of every row runs kept, as the memory budget would, and
/// the local copy a full scan fetched, whose files go from disk. A run that would
/// have reused either reads again, and Setup's Read says so before Run. Reports
/// stay: they are results, and showing one reads nothing.
fn release_quality_rows(&mut self) {
let Some(kept) = self.quality_kept_rows() else {
self.flash_note("Nothing kept to release".to_string());
return;
};
for released in std::mem::take(&mut self.quality_samples) {
self.quality_released.retain(|(dataset, view, sample)| {
!(*dataset == released.dataset_generation
&& *view == released.view_generation
&& *sample == released.sample)
});
self.quality_released.insert(
0,
(
released.dataset_generation,
released.view_generation,
released.sample,
),
);
}
self.quality_released.truncate(QUALITY_RELEASED_REMEMBERED);
// A run still reading the copy holds it until it ends; then the files go.
let generation = self.dataset_generation;
self.quality_copies
.retain(|kept| kept.dataset_generation != generation);
if kept.copy_bytes > 0 {
self.quality_copy_released = Some(generation);
}
let rows = format!(
"{} kept {} ({})",
numfmt::group_chrome(kept.rows),
if kept.rows == 1 { "row" } else { "rows" },
widgets::info::format_bytes(kept.bytes as u64)
);
let copy = format!(
"the local copy ({})",
widgets::info::format_bytes(kept.copy_bytes)
);
self.flash_note(match (kept.samples > 0, kept.copy_bytes > 0) {
(true, true) => format!("Released {rows} and {copy}; the next run reads again"),
(false, true) => format!("Released {copy}; the next full scan fetches again"),
_ => format!("Released {rows}; the next run reads again"),
});
}
/// Rows a sampled Data Quality run read, when they are the rows `sample` names
/// now: same dataset, same view, same sample.
fn kept_quality_sample(
&self,
sample: &sampling::Sample,
) -> Option<std::sync::Arc<data_quality::QualitySample>> {
self.kept_quality_entry(sample)
.map(|kept| kept.rows.clone())
}
fn kept_quality_entry(&self, sample: &sampling::Sample) -> Option<&KeptQualitySample> {
let view_generation = self.data_table_state.as_ref()?.len_generation();
self.quality_samples.iter().find(|kept| {
kept.dataset_generation == self.dataset_generation
&& kept.view_generation == view_generation
&& &kept.sample == sample
})
}
/// Keep what a run read, newest first, in place of any earlier copy of the same
/// rows: a later run returns them with the counts it added.
fn retain_quality_sample(&mut self, kept: &KeptQualitySample) {
if kept.dataset_generation != self.dataset_generation {
return;
}
self.quality_samples.retain(|entry| !entry.same_rows(kept));
self.quality_released.retain(|(dataset, view, sample)| {
!(*dataset == kept.dataset_generation
&& *view == kept.view_generation
&& *sample == kept.sample)
});
self.quality_samples.insert(0, kept.clone());
self.trim_quality_memory();
}
/// Hold Data Quality's reports and retained rows to [`QUALITY_MEMORY_BUDGET`].
///
/// A report whose rows are still retained goes first: remaking it reads nothing.
/// Then the oldest rows, whose next run reads them again, which Setup says. A
/// report with no rows behind it, a full scan's, goes last: it is the dearest to
/// remake. The newest report and the newest rows always stay, whatever their size,
/// so a finished read is never thrown away to make room for itself.
fn trim_quality_memory(&mut self) {
loop {
let used = self
.quality_cache
.iter()
.map(|entry| entry.bytes)
.sum::<usize>()
+ self
.quality_samples
.iter()
.map(|kept| kept.rows.estimated_bytes())
.sum::<usize>();
if used <= self.quality_memory_budget {
return;
}
let remakeable = self
.quality_cache
.iter()
.enumerate()
.skip(1)
.rev()
.find(|(_, entry)| {
entry.plan.compute == data_quality::QualityCompute::Sample
&& self.quality_samples.iter().any(|kept| {
kept.dataset_generation == entry.dataset_generation
&& kept.view_generation == entry.view_generation
&& kept.sample == entry.plan.sample()
})
})
.map(|(index, _)| index);
if let Some(index) = remakeable {
self.quality_cache.remove(index);
} else if self.quality_samples.len() > 1 {
if let Some(released) = self.quality_samples.pop() {
self.quality_released.insert(
0,
(
released.dataset_generation,
released.view_generation,
released.sample,
),
);
self.quality_released.truncate(QUALITY_RELEASED_REMEMBERED);
}
} else if self.quality_cache.len() > 1 {
self.quality_cache.pop();
} else {
return;
}
}
}
fn restore_cached_quality(&mut self) -> bool {
let Some(view_generation) = self
.data_table_state
.as_ref()
.map(DataTableState::len_generation)
else {
return false;
};
let plan = self.analysis_modal.data_quality_plan.clone();
let Some(cached) = self.quality_cache.iter().find(|entry| {
entry.dataset_generation == self.dataset_generation
&& entry.view_generation == view_generation
&& entry.plan.same_measurement(&plan)
}) else {
return false;
};
let mut results = cached.results.clone();
if cached.plan != plan {
// Compared as the plan compares, and kept under the windows it expects now.
if cached.plan.compares_differently(&plan) {
results.compare_segments(&plan);
}
self.cache_quality_result(&results, plan.clone());
}
self.analysis_modal.data_quality_results = Some(results);
self.analysis_modal.data_quality_last_plan = Some(plan);
self.analysis_modal.data_quality_from_cache = true;
self.analysis_modal
.set_quality_page(data_quality::QualityPage::Overview);
true
}
fn cache_quality_result(
&mut self,
results: &data_quality::DataQualityResults,
plan: data_quality::DataQualityPlan,
) {
let Some(view_generation) = self
.data_table_state
.as_ref()
.map(DataTableState::len_generation)
else {
return;
};
// One report per measurement: a plan that only expects other windows
// replaces it.
self.quality_cache.retain(|entry| {
!(entry.dataset_generation == self.dataset_generation
&& entry.view_generation == view_generation
&& entry.plan.same_measurement(&plan))
});
self.quality_cache.insert(
0,
QualityCacheEntry {
dataset_generation: self.dataset_generation,
view_generation,
plan,
bytes: results.estimated_bytes(),
results: results.clone(),
},
);
self.trim_quality_memory();
}
/// The scope a full scan's passes read: `lf` over a local copy in place of its
/// remote objects, fetched first when `job` says so. `kept` hears whether the
/// copy stands in for the source: the copy to keep, or `None`, after which the
/// dataset's full scans read the source. The copy comes back too, for the caller
/// to hold while the passes read it.
fn quality_scope_on_copy(
lf: LazyFrame,
job: QualityCopyJob,
watch: &data_quality::QualityWatch,
fetch: impl FnOnce(
&[crate::local_copy::RemoteObject],
&Path,
) -> Result<crate::local_copy::LocalCopy>,
kept: impl FnOnce(Option<Arc<crate::local_copy::LocalCopy>>),
) -> Result<(LazyFrame, Option<Arc<crate::local_copy::LocalCopy>>)> {
let (copy, fetched) = match job {
QualityCopyJob::Source => return Ok((lf, None)),
QualityCopyJob::Kept(copy) => (copy, false),
QualityCopyJob::Fetch { objects, root } => {
watch.stage(data_quality::QualityStage::CopyingSource, true, true)?;
let copy = fetch(&objects, &root).map_err(|error| {
if watch.cancelled() {
color_eyre::eyre::eyre!(crate::sampling::CANCELLED)
} else {
error
}
})?;
(Arc::new(copy), true)
}
};
// The copy must read as the source does, or the source is read as before.
let local = copy.redirect(&lf).filter(|local| {
let schemas = (local.clone().collect_schema(), lf.clone().collect_schema());
matches!(schemas, (Ok(local), Ok(source)) if local == source)
});
let Some(local) = local else {
log::warn!(target: "datui", "local copy does not read as the source; reading the source");
kept(None);
return Ok((lf, None));
};
if fetched {
kept(Some(copy.clone()));
}
watch.use_copy(data_quality::CopyRead {
bytes: copy.bytes(),
objects: copy.objects(),
fetched,
});
Ok((local, Some(copy)))
}
/// Copy `objects` under `root`, each streamed from its store and written as it
/// arrives; a cancel stops it at the next chunk and the partial copy is removed.
#[cfg(feature = "cloud")]
fn fetch_quality_copy(
objects: &[crate::local_copy::RemoteObject],
root: &Path,
cloud: &crate::config::CloudConfig,
runtime: &tokio::runtime::Handle,
stop: &crate::sampling::ReadWatch,
) -> Result<crate::local_copy::LocalCopy> {
use crate::download::StreamError;
use object_store::ObjectStoreExt;
crate::local_copy::LocalCopy::fetch(root, objects, stop, |object, write| {
let url = object.url.as_str();
let (_, _, store) = Self::cloud_store_for(Path::new(url), cloud, runtime)?;
let (_, key) = Self::cloud_bucket_and_key(url)?;
let path = crate::cloud_browse::object_path(&key);
let listed = object.etag.clone();
let open = async move {
let got = store.get(&path).await.map_err(|e| e.to_string())?;
// Rewritten since it opened, perhaps at the same size: the copy would
// not be the dataset on screen.
if let (Some(listed), Some(fetched)) = (&listed, &got.meta.e_tag)
&& !crate::local_copy::same_etag(listed, fetched)
{
return Err("it changed since it opened. Open the dataset again".to_string());
}
Ok((got.into_stream(), None))
};
let watch = stop.clone();
crate::download::stream_into(runtime, open, move || watch.stopped(), write)
.map(drop)
.map_err(|error| match error {
StreamError::Write(report) => report,
StreamError::Open(e) | StreamError::Read(e) => {
color_eyre::eyre::eyre!("Could not copy {url}: {e}")
}
StreamError::Short { expected, got } => color_eyre::eyre::eyre!(
"Could not copy {url}: it ended after {got} of {expected} bytes"
),
StreamError::Cut => color_eyre::eyre::eyre!(crate::sampling::CANCELLED),
})
})
}
/// Where Data Quality's local copies are written.
fn quality_copies_root(&self) -> PathBuf {
self.cache.cache_dir().join(crate::local_copy::COPIES_DIR)
}
/// `analysis.quality_local_copy`, in bytes.
fn quality_copy_limit(&self) -> u64 {
self.app_config.analysis.quality_local_copy.bytes()
}
/// Bytes on disk in the copies kept.
pub fn quality_copy_bytes(&self) -> u64 {
self.quality_copies
.iter()
.map(|kept| kept.copy.bytes())
.sum()
}
/// The copy this dataset's objects were fetched into this session, while kept.
fn quality_copy_kept(&self) -> Option<&Arc<crate::local_copy::LocalCopy>> {
let state = self.data_table_state.as_ref()?;
self.quality_copies
.iter()
.find(|kept| {
kept.dataset_generation == self.dataset_generation
&& state.each_remote_object().is_some_and(|mut objects| {
objects.all(|object| {
object.is_some_and(|object| kept.copy.covers(&object.url))
})
})
})
.map(|kept| &kept.copy)
}
/// Free bytes where copies are written, asked at most every few seconds.
fn quality_copy_free_space(&self) -> Option<u64> {
let root = self.quality_copies_root();
let Ok(mut cached) = self.quality_copy_free.lock() else {
return crate::local_copy::free_space(&root);
};
match *cached {
Some((asked, free)) if asked.elapsed() < std::time::Duration::from_secs(5) => free,
_ => {
let free = crate::local_copy::free_space(&root);
*cached = Some((std::time::Instant::now(), free));
free
}
}
}
/// How a run of `plan` gets its rows from a remote source, from what the open
/// learned: no read, and no more than a stat of the cache directory.
pub(crate) fn quality_copy_plan(
&self,
plan: &data_quality::DataQualityPlan,
) -> data_quality::CopyPlan {
use data_quality::{CopyPlan, NoCopy};
let Some(state) = self.data_table_state.as_ref() else {
return CopyPlan::NotApplicable;
};
if plan.compute != data_quality::QualityCompute::Full || !state.is_remote_source() {
return CopyPlan::NotApplicable;
}
if !state.quality_reads_whole_source(&plan.scope) {
return CopyPlan::Passes(NoCopy::PartOfTheSource);
}
if let Some(copy) = self.quality_copy_kept() {
return CopyPlan::Kept {
bytes: copy.bytes(),
objects: copy.objects(),
};
}
let limit = self.quality_copy_limit();
if limit == 0 {
return CopyPlan::Passes(NoCopy::Off);
}
if self.quality_copy_unusable == Some(self.dataset_generation) {
return CopyPlan::Passes(NoCopy::Unusable);
}
let Some((bytes, objects)) = state.remote_objects_size() else {
return CopyPlan::Passes(NoCopy::SizeUnknown);
};
if bytes > limit {
return CopyPlan::Passes(NoCopy::TooLarge { bytes, limit });
}
let free = self.quality_copy_free_space();
if free.is_none_or(|free| bytes > free) {
return CopyPlan::Passes(NoCopy::NoRoom { bytes, free });
}
CopyPlan::Fetch { bytes, objects }
}
/// Whether this dataset's copy was released this session, so Run fetches again.
pub(crate) fn quality_copy_released(&self) -> bool {
self.quality_copy_released == Some(self.dataset_generation)
}
/// Keep a copy a run fetched, newest first. Older copies go past the budget;
/// the newest stays, so a finished fetch is never thrown away for itself. With
/// none, the dataset's copy did not read as its source: any kept one goes too.
fn retain_quality_copy(
&mut self,
dataset_generation: u64,
copy: Option<Arc<crate::local_copy::LocalCopy>>,
) {
if dataset_generation != self.dataset_generation {
return;
}
let Some(copy) = copy else {
self.quality_copies
.retain(|kept| kept.dataset_generation != dataset_generation);
self.quality_copy_unusable = Some(dataset_generation);
return;
};
self.quality_copies.insert(
0,
RetainedCopy {
dataset_generation,
copy,
},
);
self.quality_copy_released = None;
let limit = self.quality_copy_limit();
while self.quality_copies.len() > 1 && self.quality_copy_bytes() > limit {
self.quality_copies.pop();
}
}
/// Whether keys wait: a job the user is waiting on is running or owed, an errand
/// is between its phases, or an open is on its way to its dataset.
pub fn is_busy(&self) -> bool {
self.busy || self.jobs.holds_keys() || self.loading.waits()
}
/// The generation background answers are judged by. Advanced each time work starts
/// that replaces what is in flight.
pub fn task_generation(&self) -> u64 {
self.jobs.generation()
}
/// Whether the job `ticket` names is still running and its answer still wanted.
pub fn job_is_current(&self, ticket: Ticket) -> bool {
self.jobs.is_current(ticket)
}
/// Keep recents, histories and measurements in `cache` from now on. For a test that
/// reads its store back: every test in a process shares one, and fifty opens
/// elsewhere push its entries out of the capped recents list.
pub fn use_cache(&mut self, cache: CacheManager) {
self.cache = cache;
}
/// Read `catalog.toml` from `dir`, and write it there, from now on. For a test whose
/// Ctrl+D must not write into the config directory every test in a process shares.
pub fn use_catalog_dir(&mut self, dir: &Path) -> Result<()> {
self.app_config.read_catalog_files(Some(dir))
}
/// Path of the dataset currently installed, if any. Exposed for tests that need to
/// assert an abandoned load did not swap a dataset in after the fact.
pub fn open_path(&self) -> Option<&Path> {
self.path.as_deref()
}
/// Whether the dataset on screen was piped in: named `stdin`, with no file behind
/// that name.
fn reads_stdin(&self) -> bool {
self.opened
.as_ref()
.is_some_and(|(paths, _)| matches!(paths.as_slice(), [path] if stdin::is_stdin(path)))
}
/// What views are matched against: the dataset's path and the table of its file it
/// is. What was piped in, or a frame handed over (`datui.view(frame)`), is `-`,
/// which no path criterion fits, so it matches by its columns alone.
fn view_dataset(&self) -> Option<view::Dataset<'_>> {
self.data_table_state.as_ref()?;
let path = match self.path.as_deref() {
Some(path) if !self.reads_stdin() => path,
_ => Path::new(stdin::PATH),
};
Some(view::Dataset {
path,
table: self.view_table(),
})
}
/// The table of a file of tables the dataset on screen is: the one named by
/// `--table`, or by a path inside the file (`shop.db/orders`), which opens as the
/// file with `--table`.
fn view_table(&self) -> Option<&str> {
let (_, options) = self.opened.as_ref()?;
options.table.as_deref()
}
/// Whether any leased background work, current or abandoned, has yet to report
/// back. Exposed for tests that wait for abandoned work to finish rather than
/// guessing how long it takes.
pub fn background_work_in_flight(&self) -> bool {
self.jobs.in_flight()
}
/// See the `screen_generation` field.
pub fn screen_generation(&self) -> u64 {
self.screen_generation
}
/// True while a message is in front of the user that has to be dismissed.
pub fn modal_showing(&self) -> bool {
self.error_modal.active || self.confirmation_modal.active
}
/// The analysis on screen was run on a sample: what `r` and `a` act on.
fn analysis_results_are_sampled(&self) -> bool {
self.analysis_modal.view == analysis_modal::AnalysisView::Main
&& self.analysis_modal.computing.is_none()
&& self
.analysis_modal
.current_results()
.is_some_and(|r| r.sample_size.is_some())
}
/// An analysis cancelled whose worker has not exited, and when it was cancelled.
/// While it runs, Data Quality does not start another beside it, and says so.
pub(crate) fn cancelled_analysis_running(&self) -> Option<std::time::Instant> {
self.cancelled_analysis().map(|(since, _)| since)
}
/// The newest cancelled analysis or sample read still running, and whether it was
/// cancelled during a read nothing can stop.
fn cancelled_analysis(&self) -> Option<(std::time::Instant, bool)> {
let (since, job) = self.jobs.cancelled_running(Self::reads_for_analysis)?;
let runs_out = match job {
Job::Analysis(run) => run.runs_out,
_ => true,
};
Some((since, runs_out))
}
/// A read for the Analysis tools: a run, or the sample read to show as a table.
fn reads_for_analysis(job: &Job) -> bool {
matches!(job, Job::Analysis(_) | Job::SampleRows)
}
/// A cancelled run still going that the screen should say is: at once when the
/// cancel came during a read nothing can stop, and otherwise only once it has
/// outlasted the batch it was to stop after.
pub(crate) fn cancelled_run_shown(&self) -> Option<crate::widgets::data_quality::Cancelling> {
let (since, read_runs_out) = self.cancelled_analysis()?;
(read_runs_out || since.elapsed() >= CANCEL_GRACE).then_some(
crate::widgets::data_quality::Cancelling {
since,
read_runs_out,
},
)
}
/// Work a cancel passed that is still running: leased on a generation since left.
fn cancelled_work_running(&self) -> bool {
self.jobs.running_behind()
}
/// Where the value tools (Describe, Distribution, Correlation) read the shared
/// sample from, and what the table already knows of its size. Row ranges are
/// counted in the order the table shows; every other view scope reads without the
/// sort, which no statistic needs and which makes a sampled read read everything.
fn sample_source(&self, state: &DataTableState) -> (sampling::SampleSource, Option<usize>) {
Self::sample_source_for(state, &self.analysis_modal.sample.scope)
}
fn sample_source_for(
state: &DataTableState,
scope: &data_quality::QualityScope,
) -> (sampling::SampleSource, Option<usize>) {
if scope.uses_source() {
let (lf, source) = state.data_quality_source_scan();
return (sampling::SampleSource::loaded(lf, source), None);
}
let lf = match scope {
data_quality::QualityScope::FirstRows(_)
| data_quality::QualityScope::ViewRows { .. } => state.lf().clone(),
_ => state.analysis_lf(),
};
(
sampling::SampleSource::view(lf.select(state.binary_stub_exprs())),
sampling::view_scope_rows(state.num_rows_if_valid(), scope),
)
}
/// Open the Sample form on a copy of the shared sample. A per-partition sample
/// splits by a column; partition columns lead the choices, then the columns a
/// partition is usually made of (text, integers, dates), never floats.
fn open_sample_form(&mut self) {
self.open_sample_form_as(false);
}
/// A tool with nothing to show yet: the Sample form is its pane, as it stands.
/// Where the cursor goes is the caller's: into the form when the tool is picked,
/// back to the tool list when Esc leaves it.
fn open_first_run_form(&mut self) {
self.open_sample_form_as(true);
self.sync_sample_form_focus();
}
/// The scope field shows its cursor only while the form has the cursor.
fn sync_sample_form_focus(&mut self) {
let focused = self.analysis_modal.focus == analysis_modal::AnalysisFocus::Main;
if let Some(form) = self.analysis_modal.sample_form.as_mut() {
let has_cursor = focused || !form.inline;
form.sync_focus(has_cursor);
}
}
/// Run the tool on screen with the Sample form's sample, or say on the form why
/// its scope does not parse. In Data Quality the sample is staged in Setup's
/// draft instead: the form applies, and Run reads.
fn run_sample_form(&mut self) -> Option<AppEvent> {
let quality =
self.analysis_modal.selected_tool == Some(analysis_modal::AnalysisTool::DataQuality);
// The view's sample: it is drawn again, and the tool runs on it once it is.
if self.analysis_modal.sample_form.as_ref()?.view {
let memory = self.memory_check();
let form = self.analysis_modal.sample_form.as_mut()?;
return match Self::submit_view_sample(form, memory) {
sample_keys::Submitted::Stays => None,
sample_keys::Submitted::Clear => {
self.analysis_modal.sample_form = None;
self.clear_table_sample();
if quality {
None
} else {
self.start_analysis_run()
}
}
sample_keys::Submitted::Draw { sample, anyway } => {
self.analysis_modal.sample_form = None;
self.apply_table_sample(sample, None, anyway, !quality);
if !quality {
self.analysis_modal.computing =
Some(AnalysisProgress::new("Drawing the sample"));
}
None
}
};
}
let finished = self.analysis_modal.sample_form.as_mut()?.finish();
match finished {
Ok(sample) if quality => {
self.analysis_modal.sample_form = None;
self.analysis_modal.data_quality_plan.adopt_sample(&sample);
self.analysis_modal.data_quality_setup_note = None;
None
}
// The form stays open, as filled, while a cancelled run finishes.
Ok(_) if self.read_waits_for_cancelled() => None,
Ok(sample) => {
self.analysis_modal.sample_form = None;
self.apply_sample(sample)
}
Err(error) => {
if let Some(form) = self.analysis_modal.sample_form.as_mut() {
form.error = Some(error);
}
None
}
}
}
fn open_sample_form_as(&mut self, inline: bool) {
let sample = self.analysis_modal.sample.clone();
self.open_sample_form_on(&sample, inline);
}
/// `s` in Data Quality: the Sample form over Setup, on the draft's sample. Its
/// Enter stages the sample in the draft and returns to Setup; only Run reads.
fn open_quality_sample_form(&mut self) {
self.open_quality_setup();
let sample = self.analysis_modal.data_quality_plan.sample();
self.open_sample_form_on(&sample, false);
}
fn open_sample_form_on(&mut self, sample: &sampling::Sample, inline: bool) {
let Some(state) = self.data_table_state.as_ref() else {
return;
};
// A view with a sample: the form edits it, and the rows come from the view it
// was drawn from.
let (sample, view) = match state.sampled() {
Some(sampled) => (sampled.sample().clone(), true),
None => (sample.clone(), false),
};
let context = self.sample_context(state.unsampled());
let mut form = sample_modal::SampleForm::new(&sample, context, &self.theme);
form.inline = inline;
form.view = view;
form.bytes_per_row = Some(state.unsampled().sample_row_bytes(false));
form.source_bytes_per_row = Some(state.unsampled().sample_row_bytes(true));
self.analysis_modal.sample_form = Some(form);
self.sync_sample_form_focus();
}
/// What the Sample form offers for `state`'s rows: its partitions, files, time
/// columns and the columns an equal-per-value sample can split by.
pub(crate) fn sample_context(&self, state: &DataTableState) -> sample_modal::SampleContext {
let mut partition_columns = state.partition_columns().unwrap_or_default().to_vec();
let mut partition_values = Vec::new();
// A directory whose files agree opens as one scan and names no partition
// columns; its directory names still do. One branch of the tree is walked for
// the columns and one listing read for the first column's values: local,
// and small next to opening the dataset.
if let Some(dir) = self.path.as_ref().filter(|path| path.is_dir()) {
if partition_columns.is_empty() {
partition_columns = DataTableState::discover_hive_partition_columns(dir)
.into_iter()
.filter(|column| state.schema().get(column).is_some())
.collect();
}
if let Some(first) = partition_columns.first() {
let prefix = format!("{first}=");
let mut values: Vec<String> = std::fs::read_dir(dir)
.into_iter()
.flatten()
.flatten()
.filter_map(|entry| {
let name = entry.file_name().to_string_lossy().to_string();
name.strip_prefix(&prefix).map(str::to_string)
})
.collect();
values.sort();
if !values.is_empty() {
partition_values.push((first.clone(), values));
}
}
}
// An equal-per-value sample splits by a column: partition columns first, then
// text, the usual stuff of a group (a ticker, a region), then dates and
// integers. Never floats.
let mut value_columns = partition_columns.clone();
for kind in 0..3 {
for (name, dtype) in state.schema().iter() {
let rank = match dtype {
DataType::String | DataType::Categorical(..) | DataType::Boolean => 0,
DataType::Date => 1,
dtype if dtype.is_integer() => 2,
_ => continue,
};
if rank == kind && !value_columns.iter().any(|column| column == name.as_str()) {
value_columns.push(name.to_string());
}
}
}
sample_modal::SampleContext {
view_rows: state.num_rows_if_valid(),
filtered: state.changes_rows(),
files: state.quality_source_file_names().to_vec(),
partition_columns,
partition_values,
time_columns: state.quality_temporal_columns(&data_quality::QualityScope::WholeSource),
value_columns,
}
}
fn sample_form_key(&mut self, event: &KeyEvent) -> Option<AppEvent> {
let form = self.analysis_modal.sample_form.as_mut()?;
let file_count = form.context.files.len();
let key = form::key(form, event);
match key {
// In a tool's empty pane the form stays, as it was: Esc discards the
// edit and hands the cursor back to the tool list.
FormKey::Cancel if form.inline => {
self.analysis_modal.focus = analysis_modal::AnalysisFocus::Sidebar;
self.open_first_run_form();
}
FormKey::Cancel => self.analysis_modal.sample_form = None,
FormKey::Submit => return self.run_sample_form(),
FormKey::Step(_, delta) => {
form.adjust(delta > 0);
form.edited();
}
FormKey::Text(sample_modal::SampleField::Files)
if matches!(event.code, KeyCode::PageDown | KeyCode::PageUp) =>
{
form.file_offset = if event.code == KeyCode::PageDown {
(form.file_offset + crate::widgets::sample_form::FILES_SHOWN)
.min(file_count.saturating_sub(1))
} else {
form.file_offset
.saturating_sub(crate::widgets::sample_form::FILES_SHOWN)
};
}
FormKey::Text(_) => {
if let Some(input) = form.input_mut(form.field) {
let _ = input.handle_key(event, None);
}
form.edited();
}
FormKey::Act(_) | FormKey::Moved | FormKey::Other => {}
}
None
}
/// Read the shared sample, as the tool on screen reads it, to show as a table.
///
/// Data Quality's last sample is kept and cut when it is these rows; any other is
/// drawn again from its seed, which makes it the same rows the tool measured.
fn read_sample_view(&mut self) -> Option<AppEvent> {
let sample = self.analysis_modal.sample.clone();
self.read_sample_rows(sample, None)
}
/// Read `sample` off the UI thread and show its rows: all of them, or only a
/// finding's, under the finding's label. The sample is drawn again from its seed,
/// so these are the rows the tool measured.
fn read_sample_rows(
&mut self,
sample: sampling::Sample,
evidence: Option<(quality_report::EvidenceRows, String)>,
) -> Option<AppEvent> {
let state = self.data_table_state.as_ref()?;
let (source, known_total) = Self::sample_source_for(state, &sample.scope);
let streaming = self.app_config.performance.streaming;
// The rows Data Quality just measured, when they are the rows asked for: cut
// from memory rather than drawn again from the files.
let kept = self.kept_quality_sample(&sample).map(|kept| {
let columns: Vec<_> = state
.schema()
.iter_names()
.filter(|name| kept.df().column(name.as_str()).is_ok())
.map(|name| polars::prelude::col(name.clone()))
.collect();
(kept, columns)
});
if kept.is_none() && self.read_waits_for_cancelled() {
return None;
}
self.analysis_modal.computing = Some(AnalysisProgress::new(if evidence.is_some() {
"Reading the matching sampled rows"
} else {
"Reading the sample"
}));
self.spawn_job(Job::SampleRows, Some("Reading the sample..."), move |_| {
// The columns shown are the table's; a finding is cut from every column
// the run read first, so duplicates are judged as the run judged them.
let (rows, columns) = match kept {
Some((kept, columns)) => (Ok(kept.analysis_rows(kept.df().clone())), Some(columns)),
None => (
source
.cut(&sample.scope)
.and_then(|lf| sampling::read(&lf, &sample, known_total, streaming)),
None,
),
};
let shown = |df: polars::prelude::DataFrame| match &columns {
Some(columns) => polars::prelude::IntoLazy::lazy(df)
.select(columns.clone())
.collect()
.map_err(color_eyre::eyre::Report::from),
None => Ok(df),
};
let read = rows.and_then(|rows| {
let label = format!(
"Sample {} {}",
crate::glyphs::get().middot,
sample.outcome(
rows.total_rows,
rows.sample_size,
rows.per_value.as_ref().map(|per_value| per_value.kept),
)
);
match evidence {
Some((quality_report::EvidenceRows::Duplicates, label)) => {
// Every column the run grouped by: the scope's own, not the
// row numbers kept beside them.
let keys = rows
.df
.get_column_names()
.into_iter()
.filter(|name| !name.starts_with("__datui"))
.cloned()
.collect::<Vec<_>>();
let df = data_quality::duplicate_rows(
polars::prelude::IntoLazy::lazy(rows.df),
&keys,
streaming,
)?;
Ok((shown(df)?, label))
}
Some((quality_report::EvidenceRows::Matching(predicate), label)) => {
let df = polars::prelude::IntoLazy::lazy(rows.df)
.filter(predicate)
.collect()?;
Ok((shown(df)?, label))
}
// Files are read from the scope, never from a sample.
Some((quality_report::EvidenceRows::Files(_), label)) => {
Ok((shown(rows.df)?, label))
}
None => Ok((shown(rows.df)?, label)),
}
});
let (df, label) = read.map_err(|error| format!("{error}"))?;
Ok(Answer::Sample { df, label })
});
None
}
/// Put the sample's rows in the table viewer in place of the table, as Data
/// Quality's drill-in does; Esc brings the table and Analysis back.
fn show_sample_view(&mut self, df: polars::prelude::DataFrame, label: String) {
let Some(state) = self.data_table_state.as_ref() else {
return;
};
let view = match state.sample_view(df) {
Ok(view) => view,
Err(error) => {
self.error_modal
.show(format!("Cannot show the sample: {error}"));
return;
}
};
if let Some(original) = self.data_table_state.replace(view) {
self.quality_evidence_return = Some(Box::new(original));
self.quality_evidence_label = Some(label);
self.analysis_modal.active = false;
self.forget_the_rows_read();
self.spawn_async_collect("Loading the sample...");
}
}
/// Mirror the shared sample into the Data Quality plan, which carries it into the
/// engine and into the session cache's key. Metadata-only stays metadata-only.
fn sync_quality_plan(&mut self) {
let sample = self.analysis_modal.sample.clone();
self.analysis_modal.data_quality_plan.adopt_sample(&sample);
}
/// Open Data Quality Setup: the plan, staged. Edits wait for Run, and Esc puts
/// back the plan as it stood here. Opening it again while open changes nothing.
fn open_quality_setup(&mut self) {
use data_quality::QualityPage;
let modal = &mut self.analysis_modal;
if !modal.data_quality_page.is_setup() {
modal.data_quality_setup_return = modal.data_quality_page.tab();
}
if modal.data_quality_setup_before.is_none() {
modal.data_quality_setup_before = Some(modal.data_quality_plan.clone());
}
if modal.data_quality_page != QualityPage::Setup {
modal.set_quality_page(QualityPage::Setup);
modal.data_quality_plan_field = 0;
}
modal.focus = analysis_modal::AnalysisFocus::Main;
}
/// Esc on Setup: every staged edit goes, and the report it came from comes back.
/// With no report yet, Setup stays in the pane and the cursor goes to the tools.
fn leave_quality_setup(&mut self) {
use data_quality::QualityPage;
let modal = &mut self.analysis_modal;
if let Some(before) = modal.data_quality_setup_before.take() {
modal.data_quality_plan = before;
}
modal.data_quality_setup_note = None;
modal.data_quality_confirm_run = false;
modal.data_quality_picker = None;
if modal.data_quality_results.is_some() {
let back = match modal.data_quality_setup_return {
page if page.is_setup() => QualityPage::Overview,
page => page,
};
modal.set_quality_page(back);
} else {
modal.set_quality_page(QualityPage::Setup);
modal.focus = analysis_modal::AnalysisFocus::Sidebar;
}
}
/// The confirmation a full scan asks: what it reads, what it fetches from a
/// remote source, and that it writes nothing there.
fn quality_full_scan_question(&self, plan: &data_quality::DataQualityPlan) -> String {
let mut lines = vec![
"Run a full scan?".to_string(),
String::new(),
"Reads: every eligible row, up to the whole source".to_string(),
];
if let data_quality::CopyPlan::Fetch { bytes, .. } = self.quality_copy_plan(plan) {
lines.push(format!(
"Fetch: {} once, to a local copy",
crate::widgets::info::format_bytes(bytes)
));
}
lines.push("Source writes: none".to_string());
lines.join("\n")
}
/// What stops Setup from running as it stands, said on its own line: a time
/// window on text that has no format to read it with.
fn quality_setup_problem(&self) -> Option<String> {
let plan = &self.analysis_modal.data_quality_plan;
let schema = self.data_table_state.as_ref()?.quality_schema(&plan.scope);
match &plan.grain {
data_quality::QualityGrain::TimeWindows { column, .. }
if plan.compute != data_quality::QualityCompute::Metadata
&& !plan.reads_as_time(column, schema) =>
{
Some(format!(
"{column}: text, no format {} set Text as time",
crate::glyphs::get().middot
))
}
_ => None,
}
}
/// Run, from Setup: the one place a Data Quality run starts. The draft becomes
/// the plan, and its sample the one every tool reads; then the report for it is
/// shown if one is already here, and otherwise read, once.
///
/// Waits, with the reason on Setup, while a cancelled run is still stopping: a
/// second read beside it is how memory runs out. A full scan asks first, and
/// Esc there leaves the draft staged and the last report as it was.
fn run_quality_setup(&mut self) -> Option<AppEvent> {
use data_quality::QualityPage;
if self.cancelled_analysis_running().is_some() {
self.analysis_modal.data_quality_confirm_run = false;
self.analysis_modal.data_quality_setup_note = Some(QUALITY_RUN_WAITS.to_string());
return None;
}
if let Some(problem) = self.quality_setup_problem() {
self.analysis_modal.data_quality_setup_note = Some(problem);
return None;
}
// A report already here, on screen or cached, reads nothing: nothing to confirm.
let plan = &self.analysis_modal.data_quality_plan;
let here = (self.analysis_modal.data_quality_results.is_some()
&& self
.analysis_modal
.data_quality_last_plan
.as_ref()
.is_some_and(|last| last.same_measurement(plan)))
|| self.quality_cached(plan);
if plan.requires_confirmation() && !here && !self.analysis_modal.data_quality_confirm_run {
// Asked with the one confirmation; its Yes comes back here.
let message = self.quality_full_scan_question(plan);
self.analysis_modal.data_quality_confirm_run = true;
self.confirmation_modal.show(message);
self.confirmation_modal.yes_label = "Run";
return None;
}
self.analysis_modal.data_quality_confirm_run = false;
self.commit_quality_plan();
let modal = &mut self.analysis_modal;
if modal.data_quality_results.is_some()
&& modal.data_quality_last_plan.as_ref() == Some(&modal.data_quality_plan)
{
let back = match modal.data_quality_setup_return {
page if page.is_setup() => QualityPage::Overview,
page => page,
};
modal.set_quality_page(back);
return None;
}
// Only the expected windows or the comparison changed: the report on screen
// holds every count the windows are checked against and every segment the
// comparison is worked out from, so it is relabeled, not read again.
if let (Some(results), Some(last)) = (
modal.data_quality_results.as_ref(),
modal.data_quality_last_plan.as_ref(),
) && last.same_measurement(&modal.data_quality_plan)
{
let mut results = results.clone();
let plan = modal.data_quality_plan.clone();
let page = if last.compares_differently(&plan) {
results.compare_segments(&plan);
QualityPage::Segments
} else {
QualityPage::Trends
};
modal.data_quality_results = Some(results.clone());
modal.data_quality_last_plan = Some(plan.clone());
modal.set_quality_page(page);
self.cache_quality_result(&results, plan);
return None;
}
if self.restore_cached_quality() {
return None;
}
self.analysis_modal.data_quality_from_cache = false;
let mut progress = AnalysisProgress::new("Preparing the plan");
if self.quality_kept_serves(&self.analysis_modal.data_quality_plan) {
progress.reuse = Some("Starts from rows a run already read".to_string());
}
self.analysis_modal.computing = Some(progress);
self.busy = true;
Some(AppEvent::AnalysisDataQualityCompute)
}
/// The draft is the plan now: Setup closes on it, and its sample becomes the
/// one every tool reads. The other tools' results were of the old sample, so
/// they go; Data Quality's last report stays, labeled with what it measured,
/// until the run replaces it.
fn commit_quality_plan(&mut self) {
let modal = &mut self.analysis_modal;
let sample = modal.data_quality_plan.sample();
if sample != modal.sample {
modal.describe_results = None;
modal.distribution_results = None;
modal.correlation_results = None;
}
modal.sample = sample;
modal.sample_dataset = Some(self.dataset_generation);
modal.sample_run_for = Some(self.dataset_generation);
modal.data_quality_setup_before = None;
modal.data_quality_setup_note = None;
modal.data_quality_picker = None;
}
/// Adopt a new shared sample: every tool's results were of the old one, so all of
/// them go, and the tool on screen runs again. Data Quality only takes it into
/// its plan: nothing reads until its Run.
fn apply_sample(&mut self, sample: sampling::Sample) -> Option<AppEvent> {
// A run it would start waits for a cancelled one, with every result kept.
if self.analysis_modal.selected_tool != Some(analysis_modal::AnalysisTool::DataQuality)
&& self.read_waits_for_cancelled()
{
return None;
}
// A first run on the sample as it stands takes nothing from the other tools.
if sample != self.analysis_modal.sample {
self.analysis_modal.describe_results = None;
self.analysis_modal.distribution_results = None;
self.analysis_modal.correlation_results = None;
self.analysis_modal.data_quality_results = None;
self.analysis_modal.data_quality_last_plan = None;
self.analysis_modal.data_quality_from_cache = false;
}
self.analysis_modal.sample = sample;
self.analysis_modal.sample_dataset = Some(self.dataset_generation);
self.analysis_modal.sample_run_for = Some(self.dataset_generation);
self.sync_quality_plan();
if self.analysis_modal.selected_tool == Some(analysis_modal::AnalysisTool::DataQuality) {
return None;
}
self.start_analysis_run()
}
/// A cancelled analysis is still reading: say so, and start no read beside it.
fn read_waits_for_cancelled(&mut self) -> bool {
if self.cancelled_analysis_running().is_none() {
return false;
}
self.flash_note(ANALYSIS_READ_WAITS.to_string());
true
}
/// Run the selected tool again from scratch, as `r` and `a` do.
fn start_analysis_run(&mut self) -> Option<AppEvent> {
let tool = self.analysis_modal.selected_tool?;
if tool != analysis_modal::AnalysisTool::DataQuality && self.read_waits_for_cancelled() {
return None;
}
let (phase, event) = match tool {
analysis_modal::AnalysisTool::Describe => {
self.analysis_modal.describe_results = None;
self.analysis_computation = Some(AnalysisComputationState {
df: None,
schema: None,
partial_stats: Vec::new(),
current: 0,
total: 0,
total_rows: 0,
sample_seed: self.analysis_modal.sample.seed,
sample_size: None,
});
("Describing data", AppEvent::AnalysisChunk)
}
analysis_modal::AnalysisTool::DistributionAnalysis => {
self.analysis_modal.distribution_results = None;
(
"Analyzing distributions",
AppEvent::AnalysisDistributionCompute,
)
}
analysis_modal::AnalysisTool::CorrelationMatrix => {
self.analysis_modal.correlation_results = None;
(
"Computing correlations",
AppEvent::AnalysisCorrelationCompute,
)
}
analysis_modal::AnalysisTool::DataQuality => return None,
};
self.analysis_modal.computing = Some(AnalysisProgress::new(phase));
self.busy = true;
Some(event)
}
/// Stop waiting for the analysis in flight.
///
/// The worker's answer is dropped: the bump makes it stale, and its lease no longer
/// holds the table up. A Data Quality run stops at its next batch or stage; a
/// collect nothing watches runs to its end. The tool is put back unchosen, so its
/// view does not sit on a spinner for a run that is not coming; Enter on it runs
/// it again.
fn cancel_analysis(&mut self) {
// The run's record says it is still running until its worker ends, superseded
// below. Only a Data Quality run has a watch to stop it by, and only its stages
// say whether they stop partway.
let reads_out = self
.analysis_modal
.computing
.as_ref()
.is_some_and(AnalysisProgress::read_runs_out);
let mut read_runs_out = true;
if let Some(Job::Analysis(run)) =
self.jobs.current_mut(|job| matches!(job, Job::Analysis(_)))
{
read_runs_out = run.watch.is_none() || reads_out;
run.runs_out = read_runs_out;
if let Some(watch) = &run.watch {
watch.cancel();
}
}
let reading_sample = self
.jobs
.current(|job| matches!(job, Job::SampleRows))
.is_some();
self.jobs.cancel(Self::reads_for_analysis);
self.jobs.advance();
// Keys typed while it ran were typed at the run, which is gone: an impatient
// second Enter replayed now would start it again behind the Esc.
self.screen_generation = self.screen_generation.wrapping_add(1);
self.analysis_modal.computing = None;
self.analysis_computation = None;
self.busy = false;
self.status_message = None;
// Reading the sample to look at changed nothing on screen; the tool stays.
if reading_sample {
self.flash_note("Row view cancelled".to_string());
return;
}
// Data Quality keeps its last report and goes back to Setup, where the plan
// can be edited while the run winds down. A read that runs to its end is
// state the header and Setup hold until the worker exits, which a flash could
// not; a run that stops at its next batch is done, and a flash says so.
if self.analysis_modal.selected_tool == Some(analysis_modal::AnalysisTool::DataQuality) {
self.open_quality_setup();
if !read_runs_out {
self.flash_note("Run cancelled".to_string());
}
return;
}
self.analysis_modal.selected_tool = None;
self.analysis_modal.focus = analysis_modal::AnalysisFocus::Sidebar;
self.flash_note("Analysis cancelled".to_string());
}
/// Whether the Pivot & Melt builder is waiting on a pivot it started.
pub(crate) fn pivot_computing(&self) -> bool {
self.input_mode == InputMode::PivotMelt
&& self.jobs.current(|job| matches!(job, Job::Pivot)).is_some()
}
/// Stop waiting for the pivot in flight. As with an analysis, the worker runs to
/// the end and the bump drops its answer. The form stays open with its spec.
fn cancel_pivot(&mut self) {
self.jobs.advance();
self.screen_generation = self.screen_generation.wrapping_add(1);
self.busy = false;
self.status_message = None;
self.flash_note("Pivot cancelled".to_string());
}
/// Drill into the group on row `group_index` of the table, whose values are `row`,
/// and fetch its rows off the UI thread. A drill that fails says why on the control
/// bar and leaves the grouped view as it was.
fn drill_into(&mut self, group_index: usize, row: &DataFrame) {
let Some(state) = self.data_table_state.as_mut() else {
return;
};
let drilled = state.deferred(|s| s.drill_down_with_row(group_index, row));
match drilled {
Ok(()) => {
self.sync_sort_filter_modal();
self.spawn_async_collect(Self::LOADING_BUFFER);
}
Err(e) => self.flash_note(format!(
"Could not drill in: {}",
crate::error_display::user_message_from_report(&e, None)
)),
}
}
/// Read `reader` where `-` reads standard input: what a test pipes in.
#[doc(hidden)]
pub fn read_stdin_from(&mut self, reader: impl std::io::Read + Send + 'static) {
self.stdin_reader = Some(Box::new(reader));
}
/// Pass the stream on to `out` for `--tee -`: standard output as the process got
/// it, or a test's pipe.
#[doc(hidden)]
pub fn pass_stdout_to(&mut self, out: impl std::io::Write + Send + 'static) {
self.stdout_pass = Some(Box::new(out));
}
/// The follow of the dataset on screen, while it is followed.
pub fn follow(&self) -> Option<&crate::follow::Follow> {
self.data_table_state.as_ref()?.follow()
}
/// Whether the follow's rows are on hand: none read in the background is still out,
/// and the view has taken what was counted. A refresh holds no keys, so a test waits
/// on this rather than on `is_busy`.
#[doc(hidden)]
pub fn follow_settled(&self) -> bool {
self.rows_in_flight().is_none() && !self.follow().is_some_and(|f| f.behind())
}
/// Ask the follow's watcher to look now rather than at the end of its interval.
#[doc(hidden)]
pub fn check_follow_now(&self) {
if let Some(follow) = self.follow() {
follow.check_now();
}
}
/// A followed file's watcher reported: what it counted waits for the view to take
/// it, and what the user has to know is flashed.
fn followed(&mut self, news: &crate::follow::News) {
let Some(state) = self.data_table_state.as_mut() else {
return;
};
let Some(follow) = state.follow_mut().filter(|f| f.id() == news.id) else {
return;
};
let message = follow.take(&news.change);
let fields = follow.take_new_fields();
if let Some(handle) = follow.take_held() {
state.read_followed_through(&handle);
}
if let Some(message) = message {
self.flash_note(message);
}
if !fields.is_empty() {
self.followed_fields_held = Some((self.dataset_generation, fields));
}
self.catch_up_follow();
self.join_followed_fields();
self.describe_ended_journal();
}
/// Read a piped journal's Info tab again once it has ended, over every entry: the
/// one the open read describes the entries that had arrived then. Not a job, which
/// the user would wait on; the table works meanwhile.
fn describe_ended_journal(&mut self) {
let Some(lf) = self
.data_table_state
.as_mut()
.and_then(|state| state.ended_journal_to_describe())
else {
return;
};
let generation = self.dataset_generation;
let tx = self.events.clone();
self.runtime.spawn_blocking(move || {
let detail = logging::catch_panic(|| crate::journal::summary(&lf).ok())
.ok()
.flatten();
if let Some(detail) = detail {
let _ = tx.send(AppEvent::FollowedDetail {
dataset_generation: generation,
detail: Box::new(detail),
});
}
});
}
/// Join the fields a followed pipe brought after the open, if the dataset can
/// take them now, and read the rows on screen through the wider frame. Tried again
/// after every event while they wait, as footers are.
fn join_followed_fields(&mut self) {
let Some((generation, _)) = self.followed_fields_held.as_ref() else {
return;
};
if *generation != self.dataset_generation {
self.followed_fields_held = None;
return;
}
// Not under rows still being taken: the view reads the new rows first.
if self.data_table_state.is_none()
|| self.work_the_join_would_cancel()
|| self.follow().is_some_and(|f| f.behind())
{
return;
}
let Some((generation, fields)) = self.followed_fields_held.take() else {
return;
};
let Some(state) = self.data_table_state.as_mut() else {
return;
};
match state.join_followed_fields(&fields) {
Ok(true) => {
self.spawn_async_collect(Self::LOADING_BUFFER);
}
Ok(false) => {}
Err(()) => self.followed_fields_held = Some((generation, fields)),
}
}
/// Show the rows a follow counted, when the table is on screen with nothing
/// running: a query, a sidebar, a takeover or a read in progress keeps the view it
/// has until it is done. The cursor on the last row stays on the last row; anywhere
/// else it stays put, and the rows below it are counted for the bar.
fn catch_up_follow(&mut self) {
if !self.in_normal_table_view()
|| self.is_busy()
|| self.loading.awaiting_dataset()
|| self.rows_in_flight().is_some()
{
return;
}
self.take_follow_rows(true);
}
/// Give the view's frames the rows the follow counted. With `read`, the rows on
/// screen are read too; without, the table reads them once it is back on screen.
/// Returns whether there were rows to take.
fn take_follow_rows(&mut self, read: bool) -> bool {
let Some(state) = self.data_table_state.as_mut() else {
return false;
};
let on_last_row = state.on_last_row();
let counted = state.is_num_rows_valid();
let drawn = state.visible_rows > 0 && counted;
let Some(follow) = state.follow_mut() else {
return false;
};
// The cursor goes to the last row once the rows that put it there are on hand:
// moved before, the frame drawn meanwhile has fewer rows than the cursor's
// place, and the table puts the cursor back on the last row it has.
let settle = read && drawn && std::mem::take(&mut follow.settle_at_end);
let to_end = settle || (read && counted && std::mem::take(&mut follow.end_pending));
let stale = read && std::mem::take(&mut follow.stale_view);
let at_bottom = to_end || on_last_row || follow.end_pending;
if at_bottom {
follow.new_below = 0;
}
if !follow.behind() {
if (to_end && state.scroll_to_end()) || stale {
self.spawn_collect(None);
}
return false;
}
let before = follow.shown();
let (rows, restarted) = follow.catch_up();
if at_bottom {
follow.end_pending = true;
} else if !restarted {
follow.new_below += rows.saturating_sub(before);
}
follow.stale_view = !read;
state.follow_to(rows, restarted);
if at_bottom && read {
if state.is_num_rows_valid() {
state.aim_at_end();
} else {
// A filtered view's end is known once its count lands.
self.end_after_count = Some(state.len_generation());
}
}
if read && !self.spawn_collect(None) {
// Nothing to read: the rows on hand already reach the end.
self.catch_up_follow();
}
true
}
/// `t` over a surface that keeps the rows it was opened on (Value Counts, Analysis,
/// a chart): whether the follow has rows for it to take.
fn follow_rows_waiting(&self) -> bool {
self.follow().is_some_and(|f| f.behind()) && !self.loading.awaiting_dataset()
}
/// Standard input being recorded to the file `--tee` named, for the dataset on
/// screen.
pub fn recording(&self) -> Option<&Arc<crate::follow::Spool>> {
self.data_table_state.as_ref()?;
self.opened
.as_ref()
.and_then(|(_, options)| options.spool.as_ref())
.map(|handle| handle.spool())
.filter(|spool| spool.tee().is_some())
}
/// Where `key` takes the user out of the dataset: quitting, or home.
fn leaves(&self, key: &KeyEvent) -> Option<Leaving> {
if !key.is_press() {
return None;
}
let ctrl = key.modifiers.contains(KeyModifiers::CONTROL);
match key.code {
KeyCode::Char('q') if ctrl => Some(Leaving::Quit),
KeyCode::Char('c') if ctrl => Some(Leaving::Quit),
KeyCode::Char('o') if ctrl => Some(Leaving::Home),
KeyCode::Char('Q') if !ctrl && self.in_normal_table_view() => Some(Leaving::Quit),
KeyCode::Char('q') if !ctrl && self.in_normal_table_view() => {
Some(if self.opened_from_home {
Leaving::Home
} else {
Leaving::Quit
})
}
_ => None,
}
}
/// Ask whether to stop the recording or keep it going while the user leaves.
fn ask_about_recording(&mut self, leaving: Leaving) {
let Some(tee) = self.recording().and_then(|spool| spool.tee()) else {
return;
};
let doing = if tee.to_stdout() {
"passed on"
} else {
"recorded"
};
let message = format!(
"Standard input is still being {doing} to {}. Stop recording, or keep \
recording until the stream ends?",
tee.name()
);
self.pending_leave = Some(leaving);
self.confirmation_modal
.show_choice(message, "Stop recording", "Keep recording");
}
/// Leave as asked: the recording stopped and its file finished, or kept going
/// until its stream ends, while datui goes home or quits.
fn leave_recording(&mut self, stop: bool) -> Option<AppEvent> {
let leaving = self.pending_leave.take()?;
let handle = self
.opened
.as_ref()
.and_then(|(_, options)| options.spool.clone());
if stop {
if let Some(handle) = &handle {
handle.spool().stop();
}
} else {
// Held past the dataset, so letting it go does not stop the copy.
self.recording_on = handle;
}
match leaving {
Leaving::Quit => Some(AppEvent::Exit),
Leaving::Home => {
self.enter_home();
None
}
}
}
/// The recording to wait for once the terminal is handed back: kept going when
/// the user quit, until its stream ends.
pub fn recording_after_exit(
&mut self,
) -> Option<(crate::follow::Tee, Arc<crate::follow::SpoolHandle>)> {
let handle = self.recording_on.take()?;
let spool = handle.spool();
let tee = spool.tee()?.clone();
spool.live().then_some((tee, handle))
}
/// Say once that the recording ended: saved, or stopped by an error, which the
/// error dialog says too. Returns true when the frame must redraw.
fn notice_recording_end(&mut self) -> bool {
let Some(spool) = self.recording().cloned() else {
return false;
};
let Some(ended) = spool.ended() else {
self.recording_end_said = false;
return false;
};
if std::mem::replace(&mut self.recording_end_said, true) {
return false;
}
let said = match spool.tee() {
Some(tee) if tee.to_stdout() => "Standard input ended".to_string(),
Some(tee) => format!("Saved {}", tee.path.display()),
None => String::new(),
};
match ended {
Some(reason) => self.error_modal.show(reason),
None => self.flash_note(said),
}
true
}
/// Show a completion flash on the control bar.
fn flash_note(&mut self, message: String) {
self.flash = Some(Flash::new(message));
}
/// A completion flash that ends in the path written: `Exported to …/out.csv`.
fn flash_path(&mut self, prefix: &str, path: &std::path::Path) {
self.flash = Some(Flash::path(prefix, path));
}
/// The completion flash on the control bar, if one is showing.
pub fn flash_message(&self) -> Option<&str> {
self.flash.as_ref().map(|f| f.message.as_str())
}
/// The error dialog's message, if one is showing.
pub fn error_message(&self) -> Option<&str> {
self.error_modal
.active
.then_some(self.error_modal.message.as_str())
}
/// When the screen next changes on its own, with no event to say so: the flash
/// expiring. The run loop sleeps until then at most.
pub fn next_deadline(&self) -> Option<std::time::Instant> {
let flash = self.flash.as_ref().map(|f| f.expires);
let clock = self
.follow()
.filter(|f| f.standing == crate::follow::Standing::Following)
.and_then(|f| f.last_append)
.map(crate::follow::next_tick);
// A recording's size and rate move every second until it ends, and its end is
// noticed on that tick.
let recording = self
.recording()
.filter(|spool| spool.live())
.map(|_| std::time::Instant::now() + std::time::Duration::from_secs(1));
flash.into_iter().chain(clock).chain(recording).min()
}
/// Whether the follow chip's clock reads differently now: the frame must redraw.
pub fn tick_follow_clock(&mut self) -> bool {
let ended = self.notice_recording_end();
let now = self.follow_mark();
if now == self.follow_drawn {
return ended;
}
self.follow_drawn = now;
true
}
/// What the control bar says about the follow of the dataset on screen.
fn follow_mark(&self) -> Option<crate::render::footer::FollowMark> {
use crate::follow::Standing;
// The hex view shows a file's bytes, not the table the follow moves.
if self.input_mode == InputMode::Hex {
return None;
}
let state = self.data_table_state.as_ref()?;
let rows = |n: usize, what: &str| format!("{} {what}", crate::numfmt::group_chrome(n));
let (rec, rec_stopped) = match self.recording().map(|spool| recording_label(spool)) {
Some((label, stopped)) => (Some(label), stopped),
None => (None, false),
};
let Some(follow) = state.follow().filter(|f| f.standing != Standing::Ended) else {
return rec.is_some().then(|| crate::render::footer::FollowMark {
rec,
rec_stopped,
..Default::default()
});
};
let waiting = follow.waiting();
let (chip, note) = match follow.standing {
// Standard input read as it arrives: how much has, until it ends.
Standing::Following if follow.is_pipe() => {
let read = follow.spool().map_or(0, |spool| spool.bytes());
let chip = format!(
"reading stdin {} {}",
crate::glyphs::get().middot,
crate::discover::format_size(read)
);
let note = (follow.new_below > 0 && !state.on_last_row())
.then(|| rows(follow.new_below, "new below"));
(chip, note)
}
Standing::Following => {
let chip = match follow.last_append {
Some(at) => format!(
"following {} {}",
crate::glyphs::get().middot,
crate::follow::age(at.elapsed())
),
None => "following".to_string(),
};
let note = if waiting > 0 && !self.in_normal_table_view() {
// A takeover keeps the rows it was opened on.
Some(rows(waiting, "new rows"))
} else if follow.new_below > 0 && !state.on_last_row() {
Some(rows(follow.new_below, "new below"))
} else {
None
};
(chip, note)
}
_ => (
"paused".to_string(),
(waiting > 0).then(|| rows(waiting, "new rows")),
),
};
let misfits = follow.misfits();
// What `t` does on this screen: pause or resume at the table; over a surface
// that keeps the rows it was opened on, read the new ones.
let refreshes = self.input_mode == InputMode::ValueCounts
|| (self.input_mode == InputMode::Chart
&& self.chart_modal.picker.is_none()
&& !self.chart_export_modal.active)
|| (self.analysis_modal.active && self.analysis_modal.current_results().is_some());
let key = if self.in_normal_table_view() {
Some(match follow.standing {
Standing::Paused => "Resume",
_ => "Pause",
})
} else if refreshes && follow.behind() {
Some("Refresh")
} else {
None
};
Some(crate::render::footer::FollowMark {
key,
chip: Some(chip),
note,
warning: (misfits > 0).then(|| {
if misfits == 1 {
"1 row does not fit".to_string()
} else {
rows(misfits, "rows do not fit")
}
}),
rec,
rec_stopped,
})
}
/// `t` at the table: pause or resume the follow, or follow the file, reading it
/// again as `H` does.
fn toggle_follow(&mut self) -> Option<AppEvent> {
use crate::follow::Standing;
if let Some(follow) = self.data_table_state.as_mut().and_then(|s| s.follow_mut()) {
match follow.standing {
Standing::Following => follow.pause(),
Standing::Paused => {
follow.resume();
self.catch_up_follow();
}
Standing::Ended => {}
}
return None;
}
let (paths, options) = self.opened.clone()?;
if paths.iter().any(|path| crate::stdin::is_stdin(path)) {
self.flash_note("Standard input is followed from the start: datui -f -".to_string());
return None;
}
if let Some(refusal) = crate::follow::refuse_paths(&paths, &options) {
self.flash_note(refusal);
return None;
}
let options = OpenOptions {
follow: true,
..options
};
self.set_loading_phase("Scanning input", 10);
self.name_what_is_loading(paths[0].clone());
Some(AppEvent::Open(paths, options))
}
/// Drop an expired flash. Returns true when the frame must redraw.
pub fn tick_flash(&mut self) -> bool {
if self.flash.as_ref().is_some_and(Flash::expired) {
self.flash = None;
return true;
}
false
}
/// Show the next Polars user warning on the control bar, once per session, when the
/// bar is free. Returns true when the frame must redraw.
pub fn flash_polars_warning(&mut self) -> bool {
if !self.bar_is_free() {
return false;
}
match logging::next_polars_warning() {
Some(warning) => {
self.flash_note(format!("Polars: {warning}"));
true
}
None => false,
}
}
/// Say that a background thread panicked when nothing else did; a job's panic ends
/// the job instead, the way its error would. Returns true when the frame must
/// redraw.
pub fn flash_background_panic(&mut self) -> bool {
if !self.bar_is_free() {
return false;
}
match logging::take_unreported_panic() {
Some(message) => {
self.flash_note(message);
true
}
None => false,
}
}
/// Whether a flash would be seen: a busy message outranks it, and a modal would
/// hide it until it expired.
fn bar_is_free(&self) -> bool {
!self.is_busy()
&& self.flash.is_none()
&& !self.error_modal.active
&& !self.confirmation_modal.active
}
/// See the `input_dropped` field.
pub fn set_input_dropped(&mut self, dropped: bool) {
self.input_dropped = dropped;
}
/// The escapes that act at once while busy and jump ahead of anything queued: Ctrl-Q
/// and Ctrl-C quit, Ctrl-O goes home, so a slow load never
/// traps the user; a confirmation modal keeps its keys so it can be answered; and the
/// home screen is never busy on its own account (only work left running behind it sets
/// `busy`), so it keeps every key.
pub fn hard_escape_while_busy(&self, key: &KeyEvent) -> bool {
let ctrl = key.modifiers.contains(KeyModifiers::CONTROL);
let quit = ctrl && matches!(key.code, KeyCode::Char('q' | 'c'));
let home = ctrl && key.code == KeyCode::Char('o');
let cancel_analysis = self.analysis_modal.active
&& self.analysis_modal.computing.is_some()
&& key.code == KeyCode::Esc;
let cancel_pivot = self.pivot_computing() && key.code == KeyCode::Esc;
let leave_quality_evidence = self.quality_evidence_return.is_some()
&& self.input_mode == InputMode::Normal
&& key.code == KeyCode::Esc;
let cancel_view = key.code == KeyCode::Esc && self.view_applying();
let cancel_find = key.code == KeyCode::Esc && self.finding();
let stop_sample =
key.code == KeyCode::Esc && self.sample_drawing() && self.in_normal_table_view();
// The help reads nothing, so it can always be closed, a load's screen included.
let close_help = self.help.is_open()
&& matches!(key.code, KeyCode::Esc | KeyCode::F(1) | KeyCode::Char('?'));
quit || home
|| close_help
|| cancel_analysis
|| cancel_pivot
|| cancel_view
|| cancel_find
|| stop_sample
|| leave_quality_evidence
|| self.confirmation_modal.active
|| self.input_mode == InputMode::Home
}
/// The table's column cursor keys: `h` `l` (←→) a column, `[` `]` (or Shift+←→)
/// a page of columns, `{` `}` the first and last. Never with Ctrl or Alt: Ctrl+[
/// is Esc on a terminal.
fn column_cursor_key(key: &KeyEvent) -> Option<crate::widgets::column_paging::CursorMove> {
use crate::widgets::column_paging::CursorMove;
if key
.modifiers
.intersects(KeyModifiers::CONTROL | KeyModifiers::ALT)
{
return None;
}
let shift = key.modifiers.contains(KeyModifiers::SHIFT);
match key.code {
KeyCode::Left if shift => Some(CursorMove::PageLeft),
KeyCode::Right if shift => Some(CursorMove::PageRight),
KeyCode::Left | KeyCode::Char('h') => Some(CursorMove::Left),
KeyCode::Right | KeyCode::Char('l') => Some(CursorMove::Right),
KeyCode::Char('{') => Some(CursorMove::First),
KeyCode::Char('}') => Some(CursorMove::Last),
_ => None,
}
}
/// Whether a key may act while the app is busy. `App::handle` gates on this; the main
/// loop applies the extra "nothing queued" condition for the second group.
///
/// The hard escapes always qualify. Beyond them, in the plain Normal-mode table view
/// (no text field, no modal), the harmless view keys act — quit, the column cursor
/// and help — because the first key held in that view cannot be part of a typed
/// `/query`. Harmless means reads nothing: the column cursor re-slices the buffer
/// it already holds through `rescroll_columns`, never `collect`, which counts the rows
/// when the count has not landed. Admitting a key that can count would put a
/// metadata read per object of a cloud hive on this very thread —
/// `a_key_that_acts_while_busy_reads_nothing` holds the line. Everything else,
/// letters included, is type-ahead and waits; a bare Enter or Esc there confirms
/// nothing and is dropped by the caller. Nothing is classified by keycode alone:
/// the `h` in a typed `/hello` never scrolls.
pub fn key_acts_while_busy(&self, key: &KeyEvent) -> bool {
if self.hard_escape_while_busy(key) || self.menu_takes(key) {
return true;
}
if self.key_acts_while_sampling(key) {
return true;
}
if !self.in_normal_table_view() {
return false;
}
// One row up or down inside the rows held, while all that is awaited is more
// rows (#646): the table on screen is the one they are for.
let step = match key.code {
KeyCode::Down | KeyCode::Char('j') => Some(1),
KeyCode::Up | KeyCode::Char('k') => Some(-1),
_ => None,
};
if let Some(step) = step {
return !self.busy
&& !self.loading.waits()
&& self
.jobs
.keys_held_only_by(|job| matches!(job, Job::Rows(_) | Job::OwedRows { .. }))
&& self
.data_table_state
.as_ref()
.is_some_and(|s| !s.scroll_would_trigger_collect(step));
}
matches!(
key.code,
KeyCode::Char('q')
| KeyCode::Char('Q')
// Drawn from what the table holds: row numbers, digit grouping, the
// type row (#646), a column's width (#647).
| KeyCode::Char('#')
| KeyCode::Char('<')
| KeyCode::Char('>')
| KeyCode::Char('=')
| KeyCode::Char('w')
| KeyCode::Char(',')
| KeyCode::Char('D')
| KeyCode::Left
| KeyCode::Right
| KeyCode::Char('h')
| KeyCode::Char('l')
| KeyCode::Char('{')
| KeyCode::Char('}')
| KeyCode::F(1)
| KeyCode::Char('?')
)
}
/// The plain table view: Normal mode with no help overlay, modal, or in-view modal
/// (view, analysis) or context menu drawn over it.
pub fn in_normal_table_view(&self) -> bool {
self.input_mode == InputMode::Normal
&& !self.help.is_open()
&& !self.view_modal.active
&& !self.analysis_modal.active
&& !self.error_modal.active
&& !self.confirmation_modal.active
&& self.context_menu.is_none()
}
/// While a header is dragged over another column, a rule on the header where it
/// would land: after that column when it moves right, before it when left.
fn render_drop_mark(&self, buf: &mut Buffer, ctx: &crate::render::context::RenderContext) {
let Some(pointer::Drag::Move { column, over }) = self.pointer.drag() else {
return;
};
let Some(state) = self.data_table_state.as_ref() else {
return;
};
let Some((header, columns)) = state.drawn_header() else {
return;
};
let order = state.get_column_order();
let (Some(from), Some(to)) = (
order.iter().position(|c| c == column),
order.iter().position(|c| c == over),
) else {
return;
};
// Only where a drop would land: frozen among frozen, scrolling among scrolling.
let locked = state.locked_columns_count().min(order.len());
if from == to || (from < locked) != (to < locked) {
return;
}
let Some((left, right, _)) = columns.iter().find(|(_, _, name)| name == over) else {
return;
};
let x = if to > from {
*right
} else {
left.saturating_sub(1)
};
if !(header.x..header.right()).contains(&x) {
return;
}
let g = crate::glyphs::get();
for y in header.y..header.bottom() {
let cell = &mut buf[(x, y)];
cell.set_symbol(g.rule);
cell.set_style(Style::default().fg(ctx.accent));
}
}
/// The context menu is open over the plain table view, nothing over it.
pub(crate) fn menu_showing(&self) -> bool {
self.context_menu.is_some()
&& self.input_mode == InputMode::Normal
&& self.data_table_state.is_some()
&& !self.help.is_open()
&& !self.view_modal.active
&& !self.analysis_modal.active
&& !self.error_modal.active
&& !self.confirmation_modal.active
}
/// Whether `key` is one the open menu answers itself (moving, choosing, closing),
/// which reads nothing and so acts while busy.
pub(crate) fn menu_takes(&self, key: &KeyEvent) -> bool {
self.menu_showing()
&& key.modifiers.is_empty()
&& matches!(
key.code,
KeyCode::Up
| KeyCode::Down
| KeyCode::Char('j')
| KeyCode::Char('k')
| KeyCode::Enter
| KeyCode::Esc
)
}
/// Open the context menu at `at`, over the cell the cursor was just put on.
pub fn open_context_menu(&mut self, at: ratatui::layout::Position) {
// A datetime is made from text, a date or a time.
let combine = self
.data_table_state
.as_ref()
.and_then(|state| {
let column = state.current_column()?;
state.schema().get(column).cloned()
})
.is_some_and(|dtype| {
matches!(dtype, DataType::String | DataType::Date | DataType::Time)
});
self.context_menu = Some(context_menu::ContextMenu::with(
at,
context_menu::column_items(combine),
));
}
/// Close the context menu, if it is open.
pub fn close_context_menu(&mut self) {
self.context_menu = None;
}
/// The line `i` of the open menu, chosen: the menu closes and its key is
/// pressed, offered as typed.
pub fn choose_from_menu(&mut self, i: usize) -> Option<AppEvent> {
let menu = self.context_menu.take()?;
match menu.chosen(i)? {
context_menu::MenuKey::Run(key) => Some(AppEvent::Press(key)),
context_menu::MenuKey::Do(action) => {
self.menu_action(action);
None
}
_ => None,
}
}
/// A header dropped on another column: the order with `column` moved to where
/// `onto` is, as `H` / `L` would leave it pressed that many times. A frozen column
/// moves among the frozen ones only, and a scrolling one among the scrolling.
pub fn drop_column(&mut self, column: &str, onto: &str) -> Option<AppEvent> {
let state = self.data_table_state.as_ref()?;
let mut order = state.headers();
let locked = state.locked_columns_count().min(order.len());
let from = order.iter().position(|c| c == column)?;
let to = order.iter().position(|c| c == onto)?;
if from == to || (from < locked) != (to < locked) {
return None;
}
self.flash = None;
self.data_table_state.as_mut()?.set_current_column(column);
let moving = order.remove(from);
order.insert(to, moving);
// The sidebar places hidden columns by the order it last applied; the column
// moves there too, so the shown order agrees with the table (a hidden one may
// sit otherwise than repeated H / L would leave it).
let applied = &mut self.sort_filter_modal.sort.applied_order;
if let (Some(i), Some(j)) = (
applied.iter().position(|c| c == column),
applied.iter().position(|c| c == onto),
) {
let moving = applied.remove(i);
applied.insert(j, moving);
}
Some(AppEvent::ColumnOrder(order, locked))
}
/// Whether a text field currently owns typed characters, so the wheel and `?` leave
/// it alone. The home filter is deliberately excluded.
pub fn text_field_focused(&self) -> bool {
match self.input_mode {
InputMode::Editing => true,
InputMode::Export => matches!(
self.export_modal.focus,
ExportFocus::PathInput | ExportFocus::CsvDelimiter
),
// The Picker narrows by typing, so it types.
InputMode::Copy => self.copy_modal.picker.is_some(),
// The find line types.
InputMode::Inspect => self.inspector_modal.finding,
// The Picker narrows by typing, so it types.
InputMode::GoToColumn => true,
InputMode::PickFormat | InputMode::Retype => true,
InputMode::Combine => self
.combine
.as_ref()
.is_some_and(|c| c.picker.is_some() || c.focus == retype_modal::CombineField::Name),
InputMode::PickTable => true,
InputMode::Sample => self
.sample_form
.as_ref()
.is_some_and(|form| form.field.is_text()),
// The whole inline editor types (pickers narrow, the value edits), as
// do the add-sort Picker and the Columns tab's find.
InputMode::SortFilter => self.sort_filter_modal.typing(),
InputMode::PivotMelt => {
// The Picker narrows by typing, so it types too.
self.pivot_melt_modal.picker.is_some()
|| self
.pivot_melt_modal
.is_text_row(self.pivot_melt_modal.focus)
}
InputMode::Chart => {
if self.chart_export_modal.active {
self.chart_export_modal
.input(self.chart_export_modal.focus)
.is_some()
} else {
// The open column Picker narrows by typing, so it types.
self.chart_modal.picker.is_some()
}
}
InputMode::Normal => {
self.analysis_modal.sample_scope_typing()
|| self.analysis_modal.quality_expected_typing()
|| self.analysis_modal.intent_typing()
|| self.analysis_modal.export_typing()
|| (self.view_modal.active
&& self.view_modal.mode != ViewModalMode::List
&& matches!(
self.view_modal.form_focus,
FormFocus::Name
| FormFocus::Description
| FormFocus::ExactPath
| FormFocus::RelativePath
| FormFocus::PathPattern
| FormFocus::FilenamePattern
))
}
InputMode::Home | InputMode::Info | InputMode::ValueCounts => false,
// The prompt types, and so does the spec picker's filter.
InputMode::Hex => self
.hex
.as_ref()
.is_some_and(|view| view.prompt.is_some() || view.picker.is_some()),
}
}
pub fn send_event(&mut self, event: AppEvent) -> Result<()> {
self.events.send(event)?;
Ok(())
}
/// Whether the dataset on screen is one that opened before its footers were read
/// and is still waiting for them.
///
/// Not the same question as whether a pass is running. The counter is shared with
/// every open, and abandoning one does not stop it: without this, giving up on a
/// large local directory and going back to the dataset you had would leave that
/// dataset's control bar counting footers belonging to the directory you left.
/// Whether the row count on the control bar is on its way, so a spinner stands in
/// for it. Asked by the bar, and by the run loop, which turns the spinner: the
/// two disagreed while a dataset read its own footers, and the spinner sat still.
pub fn row_count_pending(&self) -> bool {
// A load in flight counts as pending: the number `data_table_state` still holds
// belongs to the dataset being replaced, and printing it beside the incoming
// file's name would read as the new one's.
// A dataset still reading its own footers counts too: it declines the ordinary
// count because that pass is bringing one, so nothing is "in flight" — and the
// number it holds meanwhile is only as far as the buffer reaches. Printed
// plainly, a prefix of six thousand files reads `Rows: 70`.
self.len_count_inflight.is_some()
|| self.loading.awaiting_dataset()
// A re-read owed to a dataset whose footers could not be read is a count
// that is coming: the collect it is waiting to run is what starts one. The
// dataset has already stopped saying it counts itself later (it gave up on
// the pass the moment that pass failed), so without this the bar falls
// through to printing the number it happens to hold — which is only as far
// as the buffer reached. A prefix of six thousand files reads `Rows: 70`,
// plainly, for as long as the work in front of the errand takes.
|| self.reread_owed.is_some()
|| self
.data_table_state
.as_ref()
.is_some_and(|state| state.counts_itself_later())
}
/// Whether a spinner is on screen, so the run loop turns it and redraws.
pub fn something_is_spinning(&self) -> bool {
self.is_busy()
|| (self.row_count_pending() && !self.awaiting_open_confirmation())
// The clock beside "source read finishing" keeps time until it has.
|| (self.analysis_modal.active && self.cancelled_analysis_running().is_some())
|| self.chart_preparing()
|| self.value_counts_computing()
|| (self.input_mode == InputMode::Home
&& (self.home.awaiting_listing().is_some()
|| self.home.sections_waiting()
|| !self.home.peeking.is_empty()))
}
fn dataset_is_still_reading_its_footers(&self) -> bool {
self.data_table_state
.as_ref()
.is_some_and(|state| state.footers_pending().is_some())
}
/// Take the numbers the whole frame will be drawn from.
///
/// Only one so far: the footer count. It is read here rather than where it is
/// shown because two parts of the screen show it, they are painted at different
/// moments, and a background thread is moving it between them.
fn begin_frame(&mut self) {
// Back from home to the table whose lines were being indexed.
if self.indexing_paused && self.input_mode != InputMode::Home {
self.index_lines();
}
self.footers_this_frame = self.footer_progress().reading();
self.listed_this_frame = self.footer_progress().listed();
// Whatever this frame does not draw cannot be clicked.
self.pointer.forget_drawn();
if let Some(state) = self.data_table_state.as_mut() {
state.forget_drawn();
}
}
/// The footer counter the screen reads: the open's own while one is on its way to
/// a dataset, else the dataset on screen's. Each open counts on its own, so one
/// replaced half way through cannot count under the name of the file that replaced
/// it.
pub fn footer_progress(&self) -> &Arc<crate::schema_union::FooterProgress> {
self.loading.progress().unwrap_or(&self.footer_progress)
}
/// Hold past the app: dropped after it, it removes the temp files the app's opens
/// were still writing, giving their workers up to a second to stop first. Without
/// it, a quit mid-download or mid-decompression can end the process before the
/// worker removes its partial file.
pub fn exit_sweep(&self) -> ExitSweep {
ExitSweep(self.loading.unfinished().clone())
}
/// Whether an open is on its way and its dataset not installed yet: whatever table
/// `data_table_state` holds meanwhile belongs to the dataset being replaced, so the
/// main view shows the open's progress instead of it.
pub(crate) fn awaiting_dataset(&self) -> bool {
self.loading.awaiting_dataset()
}
/// What the loading screen and the control bar say about the open in flight: its
/// phase, the flat percentage beside it, the path it names and that path's size.
pub(crate) fn load_shown(&self) -> Option<(&str, u16, Option<&Path>, u64)> {
self.loading.current().map(|load| {
let (phase, percent) = load.phase().label();
(phase, percent, load.path(), load.size())
})
}
/// What the load is doing, for whichever part of the screen is saying so.
///
/// The footer count stands in for the phase while a pass is running: it says the
/// same thing and says how far along it is. Both callers read it from
/// [`Self::footers_this_frame`], one number taken once a frame, so they cannot say
/// two different things about one wait.
pub(crate) fn loading_phase<'a>(&self, phase: &'a str) -> std::borrow::Cow<'a, str> {
match self.footers_this_frame {
Some((read, total)) => std::borrow::Cow::Owned(format!(
"Reading footers: {} of {}",
crate::numfmt::group_chrome(read),
crate::numfmt::group_chrome(total)
)),
// A listing has no total to count towards, so it says how far it has got.
None => match self.listed_this_frame {
Some(listed) => std::borrow::Cow::Owned(format!(
"Listing files: {}",
crate::numfmt::group_chrome(listed)
)),
None => std::borrow::Cow::Borrowed(phase),
},
}
}
/// An open is on its way: the loading screen takes over now, saying `phase`, and keys
/// wait for it. Called before the event that carries the open out — by `run` before
/// the first frame, and by a key before the `Open` it returns — because a frame is
/// drawn between the two and would otherwise show the outgoing dataset.
pub fn set_loading_phase(&mut self, phase: impl Into<String>, progress_percent: u16) {
self.announce_open(false, phase.into(), progress_percent);
}
/// As [`Self::set_loading_phase`], for an open chosen on the home screen when
/// `from_home`: that is where its failure is reported.
fn announce_open(&mut self, from_home: bool, phase: String, percent: u16) {
self.make_way_for_an_open();
self.loading.announce(from_home, phase, percent);
}
/// Put the path on the loading screen, so a wait says what it is waiting for.
pub(crate) fn name_what_is_loading(&mut self, path: PathBuf) {
self.loading.name(path);
}
/// An open is being asked for: a load already doing work is put down for it, unless
/// it has not started any (the look or the frame that leads to this open).
fn make_way_for_an_open(&mut self) {
if let Some(retired) = self.loading.make_way() {
self.put_down_load(retired);
}
}
/// An open has its request: make way for it, and stop what the dataset on screen
/// was still reading for itself.
fn begin_new_dataset(&mut self) {
self.make_way_for_an_open();
// A preview's dataset this open did not take is a page nobody is opening.
self.home_previews.drop_prepared();
self.reset_chart_state();
self.jobs.advance();
// The dataset's footer pass is no longer wanted, and unread, unpaid-for is better
// than read and dropped. The open counts its own footers on a counter of its
// own, which the dataset takes over if it installs.
//
// The meter needs no equivalent: it belongs to the dataset rather than to the
// app, so a load that never reaches the screen never has one installed. See
// `DataTableState::measurements`.
self.footer_progress.cancel();
*self
.pending_footers_result
.lock()
.unwrap_or_else(|e| e.into_inner()) = None;
}
/// Put down what the app keeps for a load the loader has retired: its jobs, whose
/// answers are for a screen nobody is on, their lines on the control bar, and the
/// question about its download.
fn put_down_load(&mut self, retired: loading::Retired) {
let id = retired.id;
let lines = self.jobs.quiet(|job| job.load() == Some(id));
self.jobs.supersede(|job| job.load() == Some(id));
if self
.status_message
.as_ref()
.is_some_and(|status| lines.contains(status))
{
self.status_message = None;
}
if retired.asking {
self.confirmation_modal.hide();
}
}
/// Carry out what the open needs next.
fn run_load_step(&mut self, step: loading::Step) -> Option<AppEvent> {
use loading::Step;
let load = self.loading.id();
match step {
Step::Nothing => None,
Step::Crash(message) => Some(AppEvent::Crash(message)),
Step::Failed(failed) => {
self.load_failed(failed);
None
}
Step::Tables(tables) => {
self.land_on_tables(tables);
None
}
Step::Hex(hex) => {
self.land_on_hex(hex);
None
}
Step::Install(loaded) => {
// The view an open applies reads its own first rows, so the dataset's are
// not read.
if self.install_dataset(*loaded) {
return None;
}
#[cfg(test)]
{
self.first_rows_asked += 1;
}
if !self.spawn_async_collect(Self::LOADING_BUFFER) {
// Nothing to read: the buffer already serves the view.
if self.status_message.as_deref() == Some(Self::LOADING_BUFFER) {
self.status_message = None;
}
self.first_rows_settled();
}
None
}
#[cfg(any(feature = "http", feature = "cloud"))]
Step::Ask(pending) => {
// Nothing runs while the question is up: datui waits on a key, and a
// spinner would read as progress. The loader holds the generation
// meanwhile.
self.confirmation_modal
.show(Self::download_confirmation_message(
&pending,
self.loading.download_note(),
));
None
}
Step::AskRead(read) => {
// As for a download: nothing runs, and the generation is held.
self.loading.hold_while_asking(self.jobs.hold());
self.confirmation_modal
.show(Self::in_memory_confirmation_message(&read));
None
}
step => {
let load = load.expect("a step that runs work belongs to the open in flight");
self.spawn_load_phase(load, step);
None
}
}
}
/// Keep the shape of a downloaded dataset under the URL it was opened from, once its
/// rows are counted: nothing lists a web file, so this is the only way its recent,
/// and its catalog row, can say `344 × 9` (#547 D12). Once per dataset.
fn remember_a_downloads_shape(&mut self) {
if self.shape_remembered == Some(self.dataset_generation) {
return;
}
let Some(url) = self.path.clone().filter(|p| source::is_remote_url(p)) else {
return;
};
let Some(state) = self.data_table_state.as_ref().filter(|s| s.fetched()) else {
return;
};
let Some(rows) = state.num_rows_if_valid().filter(|_| !state.changes_rows()) else {
return;
};
self.shape_remembered = Some(self.dataset_generation);
let columns: Vec<String> = state
.source_schema()
.iter_names()
.map(|name| name.to_string())
.collect();
let facts = crate::cache::DatasetFacts {
mtime: std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.map(|d| d.as_secs())
.unwrap_or_default(),
size: 0,
rows: Some(rows),
cols: Some(columns.len()),
cols_sampled: false,
columns,
kind: Some(discover::EntryKind::File),
classified_by: discover::CLASSIFIER_VERSION,
cost: Default::default(),
holds: Default::default(),
};
// Off the UI thread: the index takes a lock other instances may hold.
let cache = self.cache.clone();
self.cache_writes
.spawn(move || cache.record_dataset_facts(&[(url, facts)]));
}
/// The first rows of an open are on screen, or will not be read: its wait is over.
fn first_rows_settled(&mut self) {
self.loading.first_rows_settled();
}
/// Read the rows on screen again, now that the frame they were read through has
/// been replaced.
///
/// The dataset's own errand, not its open's: this happens long after the open has
/// finished, and after a glance at the home screen just the same. The join has
/// already dropped the buffer, so nothing dropping this leaves the table with no
/// rows to show at the moment it was to show more of them.
fn reread_after_the_footers_joined(&mut self) {
// Any re-read satisfies one that was owed: this is the collect the errand was
// waiting to run, whoever asked for it.
self.reread_owed = None;
// End was pressed while the footers were still coming, and they are what the
// end was waiting on. Taken either way: a flag left from a dataset that is gone
// is not this one's to act on. The jump reads the page it lands on, so reading
// the page here first would be one fetched to be thrown away.
if self.end_when_the_footers_land.take() == Some(self.dataset_generation) {
self.status_message = None;
if let Some(next) = self.jump_key(AppEvent::DoScrollEnd) {
// The jump reads the page it lands on, so reading this one first would
// be a page fetched to be thrown away.
let _ = self.events.send(next);
return;
}
// Unless it asked for no read: the view was already at the end, or the pass
// brought no count and the jump is waiting on the ordinary one. The join has
// dropped the buffer either way, so falling through is the difference
// between a table and an empty one.
}
self.spawn_async_collect(Self::LOADING_BUFFER);
}
/// Run a buffer collect that was asked for while other work was waiting on the
/// generation.
///
/// The same shape as `reread_when_the_work_allows` below, and for the same reason:
/// the collect bumps `task_generation`, so it waits its turn and is tried again
/// after every event.
fn collect_when_the_work_allows(&mut self) {
let Some(&Job::OwedRows { dataset, .. }) = self.jobs.owed(Self::owed_rows) else {
return;
};
if dataset != self.dataset_generation {
// The dataset it was owed to is gone, and so is the view it was filling.
// Only the errand is put down, and the keys it held with it; the status line
// belongs to whatever replaced the dataset.
self.jobs.take_owed(Self::owed_rows);
return;
}
if self.work_a_bump_would_strand() {
return;
}
let Some(Job::OwedRows { status, .. }) = self.jobs.take_owed(Self::owed_rows) else {
return;
};
if !self.spawn_async_collect(&status) {
self.busy = false;
self.status_message = None;
// The collect that was owed may have been an open's first rows. Left waiting
// on them, the bar would read "Loading buffer... 70%" with the app idle for
// the rest of the session.
self.first_rows_settled();
}
}
/// Run the re-read a failed footer pass owes the dataset, once it can be run
/// without throwing another answer away.
///
/// The failure branch of `BackgroundFootersJoined` used to re-read on the spot,
/// which bumped `task_generation` with no check at all — the one path into the
/// collect that never asked `work_the_join_would_cancel`. An export in its collect
/// phase then never wrote its file and said nothing about it. So the errand waits
/// its turn, the way held columns already do.
fn reread_when_the_work_allows(&mut self) {
let Some(generation) = self.reread_owed else {
return;
};
if generation != self.dataset_generation {
// The dataset it was owed to is gone; so is the errand.
self.reread_owed = None;
return;
}
if self.work_the_join_would_cancel() {
return;
}
self.reread_after_the_footers_joined();
}
/// Retire an End that was waiting on a count which can no longer answer it.
///
/// Only the flag and the message it put up: the jump itself is not re-issued. See
/// the caller in `BackgroundLenReady` for why asking again is the wrong repair.
fn retire_the_end_that_was_waiting(&mut self) {
self.end_after_count = None;
self.take_down_the_counting_status();
}
/// Take down "Counting rows to find the end...", and only that.
///
/// Clearing the status outright would wipe whatever else is using the line — a
/// load's phase, an export's progress — on behalf of a key pressed somewhere else.
fn take_down_the_counting_status(&mut self) {
if matches!(
self.status_message.as_deref(),
Some(Self::COUNTING_FOR_END | Self::INDEXING_FOR_ROW)
) {
self.status_message = None;
}
}
/// What the status line says while `:N` waits for the lines to be indexed.
const INDEXING_FOR_ROW: &'static str = "Reading lines to find the row...";
/// What the status line says while an End is waiting on a row count. Named so the
/// paths that retire such an End can take the message back down without reaching
/// for a literal, and without clearing a message that belongs to something else.
const COUNTING_FOR_END: &'static str = "Counting rows to find the end...";
/// What the control bar says while a path is being looked at. Named so the answer can
/// take down its own line without clearing one that belongs to something else.
const LOOKING: &'static str = "Looking...";
/// The wait while a directory named on the command line is looked at: which files it
/// holds, and whether they are one table. Seconds, for a directory of large Parquet.
pub const LOOKING_AT_A_DIRECTORY: &'static str = "Looking at the directory";
/// The wait while the rows for the view are fetched.
pub const LOADING_BUFFER: &'static str = "Loading buffer...";
/// The wait while a view's pivot or first rows are read.
const APPLYING_VIEW: &'static str = "Applying view...";
/// The wait while a grouped row the buffer does not hold is read to drill into.
const READING_GROUP: &'static str = "Reading the group...";
/// The wait while the inspector reads a row's hidden and binary fields.
const READING_FIELDS: &'static str = "Reading fields...";
/// Above this a field is copied off the UI thread: a long list's JSON can take
/// a moment to write.
const FIELD_COPY_INLINE_BYTES: usize = 1024 * 1024;
/// The most JSON a copy of a JSON value writes where the clipboard sets no cap.
const JSON_COPY_MAX_BYTES: usize = 64 * 1024 * 1024;
/// The wait while the inspector parses long text as JSON.
const READING_JSON: &'static str = "Reading JSON...";
/// The wait while a pivot reads the view.
const COMPUTING_PIVOT: &'static str = "Computing pivot...";
/// How long a fetch goes unmentioned. A local page lands well inside it, and the key
/// chips staying put is the difference between paging and a bar that blinks a
/// sentence on every screen.
const A_FETCH_WORTH_SAYING: std::time::Duration = std::time::Duration::from_millis(300);
/// Grow the buffer before the view reaches its end, rather than once it has.
///
/// Nothing waits on it: no `busy`, no message, and keys go on paging through the
/// rows on hand. One at a time — a scroll that outruns it either waits on it, when it
/// is bringing the rows asked for, or supersedes it by the generation, as any newer
/// collect does. Never when a bump would strand other work.
fn load_ahead(&mut self) {
// The generation is asked about before the position is marked asked. Held while
// the app is idle (a download waiting on the user), a frame would otherwise
// spend the position on that refusal and never ask again (#490).
if self.is_busy()
|| self.jobs.owed(Self::owed_rows).is_some()
|| self.rows_in_flight().is_some()
|| self.work_a_bump_would_strand()
{
return;
}
let Some(state) = self.data_table_state.as_ref() else {
return;
};
// Asked of each position once. Planning can give back the buffer on hand — a
// row group too large to add under the caps — and asking again every frame
// would plan it again every frame.
let position = state.buffer_position();
if !state.wants_to_load_ahead() || self.loaded_ahead_from == Some(position) {
return;
}
self.loaded_ahead_from = Some(position);
self.spawn_collect(None);
}
/// Whether the bar is still keeping quiet about a fetch for the view.
fn fetch_too_young_to_mention(&self) -> bool {
self.status_message.as_deref() == Some(Self::LOADING_BUFFER)
&& self
.rows_in_flight()
.is_some_and(|inflight| inflight.began.elapsed() < Self::A_FETCH_WORTH_SAYING)
}
/// Work already running that the re-read after a join would cancel.
///
/// The re-read goes through the ordinary collect, which bumps `task_generation`, so
/// everything a bump would strand has to be done first — and that is
/// [`Jobs::would_strand`]'s job now, rather than a list of the kinds of work that
/// might be running.
///
/// One thing more than a bump, though: a join takes a fresh `len_generation` too. A
/// chart is prepared against the frame rather than the generation
/// (`BackgroundChartReady` carries no generation at all), so a bump cannot strand
/// one but changing the frame under it can.
fn work_the_join_would_cancel(&self) -> bool {
self.work_a_bump_would_strand() || self.chart_preparing()
}
/// Give the dataset what its footers found, if it can take it now.
///
/// It cannot while the user is looking at a query, a pivot, a melt or a drill-down:
/// those make their own result the root, and widening the scan underneath one takes
/// away the columns it is built from. So the columns wait — held, not dropped — and
/// this is tried again after every event, which is the cheapest way to catch the
/// moment the view comes back to the data.
///
/// Returns whether the dataset took them, so the caller can re-read the rows on
/// screen through the wider frame.
fn join_held_footers(&mut self) -> bool {
let Some((generation, _)) = self.footers_held.as_ref() else {
return false;
};
if *generation != self.dataset_generation {
// The dataset they belong to is gone; so are they.
self.footers_held = None;
return false;
}
if self.data_table_state.is_none() || self.work_the_join_would_cancel() {
return false;
}
let Some((generation, found)) = self.footers_held.take() else {
return false;
};
let state = self
.data_table_state
.as_mut()
.expect("checked just above, and nothing since takes it");
// Whether this is the moment is the dataset's call, not this one's: it is the
// frame on screen that knows whether it still grows from the scan.
match state.join_dataset_schema(found) {
Ok(()) => true,
Err(found) => {
self.footers_held = Some((generation, *found));
false
}
}
}
/// Start the pass that reads the rest of a staged open's footers.
///
/// Not a job, which the user would wait on: the whole point of opening
/// before every footer is read is that the dataset works while they are read. The
/// generation is the dataset's rather than the task's, because a collect bumps the
/// task's and this pass outlives several of them.
fn start_pending_footers(&mut self) {
let Some(join) = self
.data_table_state
.as_ref()
.and_then(|state| state.footers_pending())
else {
return;
};
let generation = self.dataset_generation;
let slot = self.pending_footers_result.clone();
let tx = self.events.clone();
let progress = self.footer_progress.clone();
self.runtime.spawn_blocking(move || {
// Reported either way. A pass that could not read them has to say so, or
// the dataset waits for it for the rest of the session — and a waiting
// dataset is one that will not count itself, because the count was what
// the pass was bringing back. A pass that panicked could not read them.
let found = logging::catch_panic(|| join(&progress)).unwrap_or(None);
if !Self::record_footers(&slot, generation, found) {
return;
}
let _ = tx.send(AppEvent::BackgroundFootersJoined { generation });
});
}
/// Index the rest of a text file's lines behind its first rows, and say when they
/// are all in ([`AppEvent::LinesIndexed`]). Not a job, which the user would wait on:
/// the table works meanwhile, and a read of every line waits for them on its own
/// worker. The last dataset's indexing, if it is still going, stops.
fn start_indexing(&mut self) {
self.end_when_indexed = None;
self.goto_when_indexed = None;
self.index_lines();
}
/// Run the indexing of the dataset on screen's lines, if they still have lines to
/// index: a new dataset's, or one paused while home was up. Lines of a dataset no
/// longer on screen stop for good, and the reads waiting on them give up.
fn index_lines(&mut self) {
use std::sync::atomic::Ordering;
self.indexing_stop.store(true, Ordering::Relaxed);
self.indexing_paused = false;
let lines = self
.data_table_state
.as_ref()
.and_then(|state| state.lines_to_index().cloned());
if let Some(old) = self.indexing_lines.take()
&& lines.as_ref().is_none_or(|lines| !Arc::ptr_eq(lines, &old))
{
old.stop_indexing();
}
let Some(lines) = lines.filter(|lines| lines.resume_indexing()) else {
return;
};
self.indexing_lines = Some(lines.clone());
let stop = Arc::new(std::sync::atomic::AtomicBool::new(false));
self.indexing_stop = stop.clone();
let generation = self.dataset_generation;
let tx = self.events.clone();
let waiting = lines.clone();
let spawned = std::thread::Builder::new()
.name("datui-index".to_string())
.spawn(move || {
loop {
// Paused or replaced: whoever stopped it says what becomes of the
// reads waiting on the lines.
if stop.load(Ordering::Relaxed) {
return;
}
// A panic stops it where it is: the rows so far are what there is,
// rather than a count that never comes.
let done =
logging::catch_panic(|| lines.index_more(INDEX_STEP)).unwrap_or(true);
if done {
lines.stop_indexing();
let rows = lines.rows();
let _ = tx.send(AppEvent::LinesIndexed { generation, rows });
return;
}
}
});
// No thread to index them: the lines so far are what there is, and nothing
// waits for more.
if spawned.is_err() {
waiting.stop_indexing();
self.indexing_lines = None;
if let Some(state) = self.data_table_state.as_mut() {
state.lines_indexed(waiting.rows());
}
}
}
/// Home is up: the indexing waits, the reads waiting on it with it, until the
/// table is back ([`Self::begin_frame`]).
fn pause_indexing(&mut self) {
if self.indexing_lines.is_some() {
self.indexing_stop
.store(true, std::sync::atomic::Ordering::Relaxed);
self.indexing_paused = true;
}
}
/// More of the dataset's lines are indexed: its frames take them, and once all are,
/// its count and an End that waited for it.
fn lines_indexed(&mut self, generation: u64, rows: usize) {
if generation != self.dataset_generation {
return;
}
let Some(state) = self.data_table_state.as_mut() else {
return;
};
self.indexing_lines = None;
if !state.lines_indexed(rows) {
// Set aside while the lines finished (the quality evidence view): they
// land on the dataset that comes back.
if let Some(held) = self.quality_evidence_return.as_mut() {
held.lines_indexed(rows);
}
return;
}
if let Some((goto, row)) = self.goto_when_indexed.take()
&& goto == generation
{
self.take_down_the_counting_status();
let _ = self.events.send(AppEvent::GoToLine(row));
}
if self.end_when_indexed.take() == Some(generation) {
self.take_down_the_counting_status();
if let Some(next) = self.jump_key(AppEvent::DoScrollEnd) {
let _ = self.events.send(next);
return;
}
}
// The count the indexing held back starts now, and rows past the first ones
// read are read.
if self.in_normal_table_view() && !self.loading.awaiting_dataset() {
self.spawn_collect(None);
}
}
/// Whether the dataset's count waits to be asked for: it has more files than
/// `[read] exact_count_files` and an estimate to show meanwhile.
fn count_held_at_estimate(&self, state: &DataTableState) -> bool {
let limit = self.app_config.read.exact_count_files;
limit > 0
&& state.files_to_count().is_some_and(|files| files > limit)
&& self.exact_count_asked != Some(self.dataset_generation)
&& state.row_estimate(None).is_some()
}
/// The dataset's row count from a sample of its footers, while it is not counted:
/// the dataset's own, or the one its footer pass has said so far.
pub(crate) fn row_estimate(&self) -> Option<crate::schema_union::RowEstimate> {
self.data_table_state
.as_ref()?
.row_estimate(self.footer_progress.estimate())
}
/// Whether the count running reads footers it can say it has read, and so can be
/// stopped: `(read, of)`.
pub(crate) fn footers_counted(&self) -> Option<(usize, usize)> {
self.len_count_inflight?;
self.count_progress
.reading()
.filter(|_| !self.count_progress.is_cancelled())
}
/// `c` in the Info panel: count every row exactly, though the dataset has more
/// files than the count reads unasked.
pub(crate) fn count_exactly(&mut self) {
let Some(state) = self.data_table_state.as_ref() else {
return;
};
if state.is_num_rows_valid() {
return;
}
let generation = state.len_generation();
self.exact_count_asked = Some(self.dataset_generation);
// The footer pass is still bringing the count; the request holds for when it
// lands.
if state.counts_itself_later() {
return;
}
// A count stopped before is asked again.
if self.len_count_failed == Some(generation) {
self.len_count_failed = None;
}
// One stopped and not yet wound down: again once it has.
if self.len_count_inflight == Some(generation) && self.count_progress.is_cancelled() {
self.count_after_stop = Some(generation);
return;
}
if self.len_count_inflight != Some(generation) {
self.len_count_inflight = Some(generation);
let job = LenCount::for_state(state);
self.spawn_count(job);
}
}
/// Esc while a count reads footers: stop it. What it read is kept for the next.
fn stop_count(&mut self) {
self.count_progress.cancel();
}
/// Put what a pass found in the slot, unless a later dataset's pass has answered
/// first. Returns whether it went in, so a pass that lost does not also announce
/// itself.
///
/// Two passes can be in flight at once — opening a second large prefix does not
/// stop the first one reading — and they finish in whatever order the network
/// gives. Without this the slower, older one overwrites the newer entry, and the
/// generation the event carries then disagrees with the generation in the slot,
/// so both are discarded and the dataset on screen never gets its columns.
fn record_footers(
slot: &std::sync::Mutex<FootersReported>,
generation: u64,
found: Option<crate::widgets::datatable::FootersFound>,
) -> bool {
let mut slot = slot.lock().unwrap_or_else(|e| e.into_inner());
if slot.as_ref().is_some_and(|(held, _)| *held > generation) {
return false;
}
*slot = Some((generation, found));
true
}
/// Install the dataset an open read, and apply the view it opens with, if any.
/// Returns whether that view is reading the first rows, which the caller then leaves
/// to it.
///
/// Only the loader hands one over, and only for the open in flight: an abandoned or
/// replaced open's answer never gets this far.
fn install_dataset(&mut self, loaded: loading::Loaded) -> bool {
let loading::Loaded {
state,
path,
options,
debug_label,
paths,
recent,
from_home,
footers,
} = loaded;
let options = &options;
// A key pressed at the dataset being replaced belongs to it, not to this one.
self.end_when_the_footers_land = None;
// Its companion, for the same reason. This one keys itself to a
// `len_generation`, which says nothing about which dataset it belonged to, so
// without clearing it here an End pressed on the directory the user walked away
// from is still live against the one they opened next.
self.end_after_count = None;
// One per dataset that reaches the screen, rather than one per open started:
// an open that fails leaves the last dataset up, and the pass still reading its
// footers has to be able to finish into it.
self.dataset_generation = self.dataset_generation.wrapping_add(1);
self.quality_cache.clear();
self.quality_samples.clear();
self.quality_released.clear();
// The objects may have changed since they were copied; a run on the dataset
// opened now fetches them again.
self.quality_copies.clear();
self.quality_copy_released = None;
self.quality_copy_unusable = None;
self.quality_evidence_return = None;
self.quality_evidence_label = None;
// The findings narrowed to the last dataset's columns would hide this one's.
self.analysis_modal.data_quality_findings = quality_report::FindingsView::default();
self.analysis_modal.data_quality_evidence_read = None;
// A query still running was over the dataset being replaced; its rollback
// is that dataset's view. So was a view waiting on its pivot.
self.query_running = None;
self.jobs.supersede(|job| matches!(job, Job::ViewPivot(_)));
// A sample being drawn was the last dataset's, and so were its paths.
self.put_down_sample_draw();
self.sample_paths.clear();
// Whatever chart state survived belongs to the dataset being replaced.
self.reset_chart_state();
self.debug.schema_load = debug_label;
// Home is now in the stack, so q pops back to it; never unset, since a
// reread from the table (H) is not a new place.
if from_home {
self.opened_from_home = true;
}
// A frame handed over has no path to go back to.
// Without the spec read: it holds the file's map, and a decompressed copy's map
// keeps its disk space until the map goes, so it goes with the dataset.
self.opened = paths.map(|paths| {
let options = OpenOptions {
format_read: None,
sqlite: None,
// Counted afresh by the next read.
tail: None,
prepared: None,
..options.clone()
};
(paths, options)
});
// Recorded once the dataset is installed: a file that fails to load is not one
// anybody wants to get back to.
if let Some(path) = recent {
// Off the opening path. It takes a lock several instances may be contending
// for -- opening a dataset must not queue behind another instance's
// bookkeeping. Only the next home listing waits on it, on its worker.
let cache = self.cache.clone();
self.cache_writes.spawn(move || {
cache.push_recent(&path);
});
}
self.forget_the_rows_read();
self.file_facts = None;
let shown = home::catalogs(&self.app_config);
self.codebook = path.as_deref().and_then(|p| home::codebook_for(&shown, p));
self.catalog_entry = path
.as_deref()
.and_then(|p| home::catalog_entry_for(&shown, p));
// The footers it still has to read are counted on the open's counter, which is
// the dataset's now; the last dataset's pass, if any is left, stops.
self.footer_progress.cancel();
self.footer_progress = footers;
self.data_table_state = Some(state);
// A followed file's watcher starts with its dataset and stops with it.
if options.follow
&& let Some(state) = self.data_table_state.as_mut()
{
match options.tail.as_deref() {
Some(tail) => {
let follow = crate::follow::Follow::start(
tail.clone(),
self.app_config.read.follow_interval.duration(),
self.events.clone(),
options.spool.clone(),
);
state.start_following(if options.pipe {
follow.as_pipe()
} else {
follow
});
// Counted already, as the scan reads them: no count of its own.
state.follow_to(tail.rows(), false);
}
// A recording of something that cannot be read as it grows.
None => self.flash_note(
"Only text and Arrow streams are followed: this shows what had arrived, and recording goes on"
.to_string(),
),
}
}
// A count still waiting for the last dataset's rows to paint is not owed now.
self.retire_a_count_the_rows_answered();
self.path = path.clone();
// Named for this file, so after its path is set.
self.open_info_documentation();
if let Some(ref p) = path {
let read_as = self
.data_table_state
.as_ref()
.and_then(DataTableState::read_as);
self.original_file_format = Self::export_format_for(p, read_as.or(options.format));
// CSV's delimiter: a comma unless the user named a separator. A `.tsv`
// exports as TSV, whose preset is the tab; a tab in a `.csv` would reopen
// as one column.
self.original_file_delimiter = Some(options.separator_or(b','));
} else {
self.original_file_format = None;
self.original_file_delimiter = None;
}
// A panel still up says what it says about the dataset on screen.
if self.info_modal.active {
self.read_file_facts();
self.count_unfit();
}
// The dataset is on screen now; whatever it still has to learn about itself is
// read behind it.
self.start_pending_footers();
self.start_indexing();
// `#` for text and logs, unless the flag or the config said.
if options.row_numbers_auto
&& let Some(state) = self.data_table_state.as_mut()
&& state.numbered_by_default()
{
state.set_row_numbers(true);
}
self.sort_filter_modal = SortFilterModal::new();
self.pivot_melt_modal = PivotMeltModal::new();
self.status_message = Some(Self::LOADING_BUFFER.to_string());
// The dataset is installed and its schema known, so this is where a view
// meets it. `--view` names one and applies to this first open alone;
// `[views] auto_apply` dresses every open that has a matching view.
// A fresh dataset starts with no view applied: the previous file's view
// must not wear the check mark here, nor count as applied when edited.
self.active_view_id = None;
let (view, reason) = match self.startup_view.take() {
Some(name) => match self.view_manager.get_view_by_name(&name).cloned() {
Some(view) => (Some(view), None),
None => {
self.error_modal.show(format!("No view named \"{name}\""));
(None, None)
}
},
None if self.app_config.views.auto_apply => self
.view_dataset()
.zip(self.data_table_state.as_ref())
.and_then(|(dataset, state)| {
self.view_manager
.get_most_relevant(dataset, state.source_schema())
})
.map_or((None, None), |(view, reason)| (Some(view), Some(reason))),
None => (None, None),
};
let Some(view) = view else {
return false;
};
let applied = match reason {
// Applied unasked, it says which view and why.
Some(why) => self.apply_matched_view(&view, why),
None => self.apply_view(&view),
};
match applied {
// The view reads its own first rows, so the dataset's are never read.
Ok(()) => true,
Err(e) => {
self.error_modal
.show(format!("Error applying view \"{}\": {e}", view.name));
false
}
}
}
/// Say that the view `name` was applied because its criteria fit as `why` says.
fn flash_view_applied(&mut self, name: &str, why: view::MatchReason) {
self.flash_note(format!("View \"{name}\" applied: {}", why.as_str()));
}
/// Ensures file path has an extension when user did not provide one; only adds
/// compression suffix (e.g. .gz) when compression is selected. If the user
/// provided a path with an extension (e.g. foo.feather), that extension is kept.
/// Spawn an async buffer collect if needed. Returns true if a background task was spawned.
/// Increments task_generation to invalidate any in-flight collect from a prior call.
///
/// When the LazyFrame's row count is unknown (e.g. fresh load, or just after a
/// filter/sort/pivot/melt that invalidates the cache), the exact `len()` is computed
/// in the background and applied later via `BackgroundLenReady` — it never gates the
/// buffer paint. `prepare_async_collect` plans a top-of-data window when the count is
/// still unknown, so the first screen renders immediately. For large/partitioned/remote
/// datasets the count can take a long time; it runs silently and concurrently.
pub fn spawn_async_collect(&mut self, status: &str) -> bool {
self.spawn_collect(Some(status))
}
/// As [`Self::spawn_async_collect`]; with no `status`, a load-ahead that nothing
/// waits on: its job holds no keys. See [`InflightCollect`].
fn spawn_collect(&mut self, status: Option<&str>) -> bool {
let held = self
.data_table_state
.as_ref()
.is_some_and(|state| self.count_held_at_estimate(state));
let Some(state) = self.data_table_state.as_mut() else {
return false;
};
// The exact row count, when it isn't known and none is already coming for this
// data version. Independent of `task_generation` (a scroll must not restart it)
// and does not set `busy`. A count that reads only footers runs now: it reads no
// data. On an object store a data count rides in the collect spawned below,
// which answers it outright when the read comes back short and otherwise gets
// the row groups to itself first. On a local frame it waits until the page is
// painted (`count_after_paint`), and is not needed at all when that page came
// back short.
let mut count = None;
let generation = state.len_generation();
if !state.is_num_rows_valid()
&& self.len_count_inflight != Some(generation)
// Marked as running only once it is going to run. A dataset still reading
// its own footers declines this count, because that pass is bringing it —
// and the marker is cleared by a count coming back, so setting it for one
// that was never started leaves it set for the rest of the session: a
// spinner where the row count goes, a redraw on its account every frame,
// and `End` waiting on nothing.
&& !state.counts_itself_later()
// A count that failed is not tried again on every scroll. End asks again.
&& self.len_count_failed != Some(generation)
// A dataset of too many files to count unasked shows its estimate.
&& !held
{
self.len_count_inflight = Some(generation);
count = Some(LenCount::for_state(state));
}
let footers = count.take_if(|job| job.reads_footers());
if count.take_if(|_| !state.is_remote_source()).is_some() {
self.count_after_paint = Some(generation);
}
if let Some(job) = footers {
self.spawn_count(job);
}
// Read before the frame is borrowed: the predicate is over the whole App.
let a_bump_would_strand = self.work_a_bump_would_strand();
let inflight = self.rows_in_flight();
// Plan and spawn the buffer collect. With the count unknown this is a top-of-data
// window (`slice(0, N)`) that touches only the first file(s) of a partitioned set.
let Some(state) = self.data_table_state.as_mut() else {
return false;
};
let covered = inflight.is_some_and(|inflight| inflight.covers(state));
// The rows asked for are already on the way in a load-ahead: wait on that one
// rather than fetch them twice. The keys are its now.
if covered
&& let Some(status) = status
&& !self.jobs.waited_on(Self::reading_rows)
{
self.jobs.wait_on(Self::reading_rows, status);
self.busy = false;
self.status_message = Some(status.to_string());
}
let request = (!covered).then(|| state.prepare_async_collect(None));
let Some(Some(request)) = request else {
// Nothing to ride in: the view is covered, or the buffer on hand serves it.
if let Some(job) = count {
self.spawn_count(job);
}
return covered;
};
// Everything past here advances the generation, so everything that holds it has
// to be done first. The collect the user asked for is queued rather than
// refused: the throbber that was already turning goes on turning, and it is
// tried again after every event until the work in front of it finishes.
//
// This is the door #238 was about. `BackgroundLenReady` answers a count by
// jumping to the end, which reaches here with no key pressed and minutes after
// the one that was — long enough for a dataset to have been opened meanwhile.
// The bump cancelled that open's phase in flight, whose answer was then thrown
// away with the open still waiting on it, and the file never opened, silently,
// for the rest of the session.
if a_bump_would_strand {
// The count that was going to ride in this collect is put down rather than
// run on its own. On an object store it answers itself out of the short read
// the collect comes back with; spawned standalone it is a full remote
// `len()`, which is the expensive thing the riding exists to avoid. Putting
// the marker down with it is what lets the retry ask again.
if count.is_some() {
self.len_count_inflight = None;
}
// A load-ahead is not owed: nobody asked for it.
let Some(status) = status else {
return false;
};
// One page is owed at a time, the newest; the user waits on it.
self.jobs.take_owed(Self::owed_rows);
let owed = Job::OwedRows {
dataset: self.dataset_generation,
status: status.to_string(),
};
self.jobs.owe(owed, Some(status));
self.busy = false;
self.status_message = Some(status.to_string());
return true;
}
self.jobs.advance();
self.reads.pages += 1;
let inflight = InflightCollect {
began: std::time::Instant::now(),
files: state.files_a_page_reads(
request.buffer_start,
request.buffer_end.saturating_sub(request.buffer_start),
),
dataset: state.len_generation(),
columns: InflightCollect::columns_of(state),
start: request.buffer_start,
end: request.buffer_end,
};
// Owed from here, so a worker that dies before it reaches the count still
// answers it.
let count = count.map(|job| OwedCount::new(job, self.events.clone()));
self.spawn_job(Job::Rows(inflight), status, move |_| {
let plan = request.plan;
// The count is answered once the page has gone out: it may need a pass of
// its own, which must not hold the page back.
Ok(
match crate::statistics::collect_lazy(request.lf, request.polars_streaming) {
Ok(df) => {
let returned = df.height();
let requested = request.buffer_end - request.buffer_start;
let start = request.buffer_start;
// Stitched and cut here rather than where it lands: a cut may
// copy up to the byte budget, which the UI thread would stall on
// (#483).
Answer::Rows(plan.fit(df)).then(move || {
if let Some(count) = count {
count.answer(|job| job.after_collect(start, returned, requested));
}
})
}
// A pass over a frame that just failed to collect would fail too: the
// count goes unanswered and so reports itself failed, leaving the
// retry to a later interaction (see `len_count_failed`).
Err(e) => Answer::RowsFailed {
message: crate::error_display::user_message_from_polars(&e),
conversion: crate::error_display::conversion_failure(&e).map(Box::new),
}
.then(move || drop(count)),
},
)
});
true
}
/// Count the rows off the UI thread; the answer comes back as `BackgroundLenReady`
/// or `BackgroundLenFailed`.
fn spawn_count(&mut self, job: LenCount) {
#[cfg(test)]
self.counts_spawned.set(self.counts_spawned.get() + 1);
self.count_progress = job.progress.clone();
let count = OwedCount::new(job, self.events.clone());
self.runtime
.spawn_blocking(move || count.answer(LenCount::run));
}
/// Whether the rows of the frame on screen that someone is waiting for are still
/// being read: the page an open, a query or a scroll asked for. A load-ahead is
/// nobody's wait, so a count does not queue behind one.
fn waited_on_rows_pending(&self, generation: u64) -> bool {
self.loading.awaiting_dataset()
|| self.jobs.owed(Self::owed_rows).is_some()
|| (self.rows_waited_on()
&& self
.rows_in_flight()
.is_some_and(|inflight| inflight.dataset == generation))
}
/// Whether a frame painted now would start, or retire, the count waiting on one.
/// The run loop paints after every update; a test harness, which paints nothing,
/// asks this to know when to say a frame was painted.
pub fn count_waits_for_a_frame(&self) -> bool {
self.count_after_paint
.is_some_and(|generation| !self.waited_on_rows_pending(generation))
}
/// A frame has been painted. Start the count that was waiting for its rows to be on
/// screen, unless they are still being read; retire it if the frame it was for has
/// gone or its rows already said how many there are.
pub fn frame_painted(&mut self) {
self.pointer.painted();
let Some(generation) = self.count_after_paint else {
return;
};
if self.waited_on_rows_pending(generation) {
return;
}
self.count_after_paint = None;
let wanted = self
.data_table_state
.as_ref()
.filter(|state| state.len_generation() == generation && !state.is_num_rows_valid());
match wanted {
Some(state) => {
self.len_count_inflight = Some(generation);
self.spawn_count(LenCount::for_state(state));
}
None => {
if self.len_count_inflight == Some(generation) {
self.len_count_inflight = None;
}
}
}
}
/// The page just installed may have said how many rows there are, or belong to a
/// frame other than the one a count is waiting on: either way that count is not
/// owed any more.
fn retire_a_count_the_rows_answered(&mut self) {
let Some(generation) = self.count_after_paint else {
return;
};
let answered = self
.data_table_state
.as_ref()
.is_none_or(|state| state.len_generation() != generation || state.is_num_rows_valid());
if answered {
self.count_after_paint = None;
if self.len_count_inflight == Some(generation) {
self.len_count_inflight = None;
}
}
}
/// Start `job` on a worker. With a `status` the app is busy with it: the control
/// bar says so and keys wait. The worker returns its answer, or `Err` with a
/// message for the user; that, or a panic, is the job's one outcome, which
/// [`AppEvent::JobEnded`] hands to [`App::job_ended`].
///
/// Does not advance the generation. A caller replacing work in flight advances it
/// first.
fn spawn_job<F, R>(&mut self, job: Job, status: Option<&str>, work: F) -> Ticket
where
F: FnOnce(&jobs::Worker) -> std::result::Result<R, String> + Send + 'static,
R: Into<jobs::Answered>,
{
let started = self.start_job(job, status);
let ticket = started.ticket();
started.run(&self.runtime, work);
ticket
}
/// As [`Self::spawn_job`], for a caller that needs the ticket before the work is
/// built: it runs the job with [`jobs::Started::run`].
fn start_job(&mut self, job: Job, status: Option<&str>) -> jobs::Started {
if let Some(status) = status {
// The errand that led here, if one did, is this job's now: its record holds
// the keys until it ends.
self.busy = false;
self.status_message = Some(status.to_string());
}
self.jobs.start(job, status)
}
fn owed_rows(job: &Job) -> bool {
matches!(job, Job::OwedRows { .. })
}
fn reading_rows(job: &Job) -> bool {
matches!(job, Job::Rows(_))
}
/// The read of the table's rows in flight, if one is and is still wanted: what it
/// will fill, and whether anyone waits on it.
fn rows_in_flight(&self) -> Option<InflightCollect> {
match self.jobs.current(Self::reading_rows) {
Some((_, Job::Rows(inflight))) => Some(*inflight),
_ => None,
}
}
/// Whether the user waits on the read of the table's rows in flight.
fn rows_waited_on(&self) -> bool {
self.jobs.waited_on(Self::reading_rows)
}
/// The rows being read, or owed, are not for the table on screen any more: a view
/// has been put in its place. Their answer is dropped when it comes.
fn forget_the_rows_read(&mut self) {
self.jobs
.supersede(|job| Self::reading_rows(job) || Self::owed_rows(job));
}
/// Hold the generation: a continuation waiting to run, or an errand waiting on the
/// user. See [`jobs::Hold`].
pub(crate) fn hold_the_generation(&self) -> jobs::Hold {
self.jobs.hold()
}
/// A job in flight with no worker, started as `spawn_job` starts one, for tests
/// that decide how it ends.
#[cfg(test)]
pub(crate) fn job_for_tests(&mut self, job: Job, status: Option<&str>) -> jobs::Started {
self.start_job(job, status)
}
/// Whether the bar has no open and no export to report.
#[cfg(test)]
pub(crate) fn nothing_loading(&self) -> bool {
self.loading.current().is_none() && self.export_progress.is_none()
}
/// An open on the loading screen, saying `phase` about `path` of `size` bytes, with
/// nothing running: for tests of what the screen says.
#[cfg(test)]
pub(crate) fn loading_for_tests(
&mut self,
path: Option<PathBuf>,
size: u64,
phase: &str,
percent: u16,
) {
self.announce_open(false, phase.to_string(), percent);
if let Some(path) = path {
self.loading.name(path);
}
self.loading.size_for_tests(size);
}
/// An open of `path` begun and scanning, with no worker: for tests that decide how
/// its phases answer. Its jobs are [`Job::Load`] with the id returned.
#[cfg(test)]
pub(crate) fn open_for_tests(&mut self, path: &str) -> loading::LoadId {
self.make_way_for_an_open();
let _ = self.loading.open(loading::OpenRequest {
paths: vec![PathBuf::from(path)],
options: OpenOptions::default(),
size: 0,
recent: None,
shown: None,
warn_in_memory_above: None,
});
self.loading.id().expect("an open was begun")
}
/// Install `state` as the dataset on screen, the way an open of a frame does: through
/// the loader, so what the load hands over (its footer counter) is handed over for
/// real. Its first rows are not read; the open is done once it is installed.
#[cfg(test)]
pub(crate) fn install_for_tests(
&mut self,
state: DataTableState,
path: Option<PathBuf>,
options: &OpenOptions,
debug_label: Option<String>,
) -> bool {
self.make_way_for_an_open();
let _ = self
.loading
.open_frame(LazyFrame::default(), options.clone());
let load = self.loading.id().expect("an open was begun");
let loading::Step::Install(loaded) = self.loading.answered(
load,
loading::LoadAnswer::SchemaRead {
state: Box::new(state),
path,
options: options.clone(),
debug_label,
},
#[cfg(any(feature = "http", feature = "cloud"))]
&self.jobs,
) else {
unreachable!("a schema read installs");
};
let view = self.install_dataset(*loaded);
self.first_rows_settled();
view
}
/// A page owed to the dataset on screen, as one asked for while the generation was
/// held is.
#[cfg(test)]
pub(crate) fn owe_rows_for_tests(&mut self, status: &str) {
let owed = Job::OwedRows {
dataset: self.dataset_generation,
status: status.to_string(),
};
self.jobs.owe(owed, Some(status));
}
/// Whether a page is owed.
#[cfg(test)]
pub(crate) fn rows_owed(&self) -> bool {
self.jobs.owed(Self::owed_rows).is_some()
}
/// A current `job` answers `answer` at once, and the app handles it: for tests of
/// what an answer does.
#[cfg(test)]
pub(crate) fn answer_for_tests(&mut self, job: Job, answer: Answer) -> Option<AppEvent> {
let started = self.jobs.start(job, None);
let ticket = started.ticket();
started.end(Outcome::answered(answer));
self.job_ended(ticket)
}
/// Whether anything is waiting on the current generation, so that advancing it
/// would throw away an answer nothing will ask for again. See
/// [`Jobs::would_strand`].
fn work_a_bump_would_strand(&self) -> bool {
self.jobs.would_strand()
}
/// Run a scroll on `data_table_state` and resolve the busy/spawn cycle.
/// `scroll` returns true when its movement leaves the buffered window (caller must collect).
/// We clear `busy` ourselves when no collect is needed or the spawn no-ops, otherwise
/// the busy flag set by the key handler would gate further input forever.
/// Home, End and G. A jump may need a fill, so it is deferred behind a frame that
/// shows the throbber — setting `start_row` alone used to leave the old buffer on
/// screen, drawn from its first row — unless the view is already there, in which
/// case only the selection settles and no frame or key is spent.
fn jump_key(&mut self, jump: AppEvent) -> Option<AppEvent> {
// The end of a remote dataset is not known until its rows are counted, and a
// jump to a guess reads every file up to it. Wait for the count instead; keys
// keep working meanwhile.
// A dataset still reading its own footers is already getting a count, and its
// end is known as soon as that lands. Starting one here would read every footer
// a second time — and the join takes a fresh `len_generation` on its way past,
// so the count that came back would be answering a question nobody could match
// it to and the jump would never happen. Wait for the pass instead.
// `scan_is_the_root`, not `counts_itself_later`: the question here is whether a
// join is going to land underneath this frame and take a fresh `len_generation`
// with it, which is what would leave a count answering a question nothing could
// match it to. A filter and a sort are rebuilt over the joined scan, so they are
// on this side of it even though they are not pristine.
// Lines still being indexed: the end is where the indexing ends.
if matches!(jump, AppEvent::DoScrollEnd)
&& let Some(state) = self.data_table_state.as_ref()
&& state.indexing().is_some()
{
self.end_when_indexed = Some(self.dataset_generation);
self.status_message = Some(Self::COUNTING_FOR_END.to_string());
return None;
}
if matches!(jump, AppEvent::DoScrollEnd)
&& let Some(state) = self.data_table_state.as_ref()
&& state.footers_pending().is_some()
&& state.scan_is_the_root()
&& !state.is_num_rows_valid()
{
self.end_when_the_footers_land = Some(self.dataset_generation);
self.status_message = Some(Self::COUNTING_FOR_END.to_string());
return None;
}
// Any other frame whose end is not known yet waits for its count too, rather than
// jumping to the end of the rows read so far. A count waiting on a paint starts
// now; one already running or riding in a collect is waited on.
if matches!(jump, AppEvent::DoScrollEnd)
&& let Some(state) = self.data_table_state.as_ref()
&& !state.is_num_rows_valid()
{
let generation = state.len_generation();
self.end_after_count = Some(generation);
self.status_message = Some(Self::COUNTING_FOR_END.to_string());
let held = self.count_after_paint == Some(generation);
if held {
self.count_after_paint = None;
}
if held || self.len_count_inflight != Some(generation) {
self.len_count_inflight = Some(generation);
self.spawn_count(LenCount::for_state(state));
}
return None;
}
let state = self.data_table_state.as_mut()?;
let (already_there, settle): (bool, fn(&mut DataTableState) -> bool) = match jump {
AppEvent::DoScrollHome => (state.start_row() == 0, DataTableState::scroll_to_start),
_ => (state.at_end(), DataTableState::scroll_to_end),
};
if already_there {
settle(state);
return None;
}
self.busy = true;
Some(jump)
}
fn handle_scroll<F>(&mut self, scroll: F) -> Option<AppEvent>
where
F: FnOnce(&mut crate::widgets::datatable::DataTableState) -> bool,
{
let needs = self.data_table_state.as_mut().is_some_and(scroll);
if !needs || !self.spawn_async_collect(Self::LOADING_BUFFER) {
self.busy = false;
self.status_message = None;
}
None
}
/// Hand the export modal's path input a key, and when the value changed, follow
/// the typed extension with the format radio — the alternative was Parquet bytes
/// in a file named `out.csv`, with nothing on screen saying so. Cursor-only keys
/// change nothing and re-pick nothing, so a format chosen after typing stands.
fn export_path_key(&mut self, event: &KeyEvent) {
let before = self.export_modal.path_input.value().to_string();
self.export_modal
.path_input
.handle_key(event, Some(&self.cache));
if self.export_modal.path_input.value() != before {
self.export_modal.sync_format_to_path();
// Typing is the correction the message asked for.
self.export_modal.path_error = None;
}
}
fn ensure_file_extension(
path: &Path,
format: ExportFormat,
compression: Option<CompressionFormat>,
) -> PathBuf {
let current_ext = path.extension().and_then(|e| e.to_str()).unwrap_or("");
let mut new_path = path.to_path_buf();
if current_ext.is_empty() {
// No extension: use default for format (and add compression if selected)
let desired_ext = if let Some(comp) = compression {
format!("{}.{}", format.extension(), comp.extension())
} else {
format.extension().to_string()
};
new_path.set_extension(&desired_ext);
} else {
// User provided an extension: keep it. Only add compression suffix when compression is selected.
let is_compression_only = matches!(
current_ext.to_lowercase().as_str(),
"gz" | "zst" | "bz2" | "xz"
) && ExportFormat::from_extension(current_ext).is_none();
if is_compression_only {
// Path has only compression ext (e.g. file.gz); stem may have format (file.csv.gz)
let stem = path.file_stem().and_then(|s| s.to_str()).unwrap_or("");
let stem_has_format = stem
.split('.')
.next_back()
.and_then(ExportFormat::from_extension)
.is_some();
if stem_has_format {
if let Some(comp) = compression
&& let Some(format_ext) = stem
.split('.')
.next_back()
.and_then(ExportFormat::from_extension)
.map(|f| f.extension())
{
new_path =
PathBuf::from(stem.rsplit_once('.').map(|x| x.0).unwrap_or(stem));
new_path.set_extension(format!("{}.{}", format_ext, comp.extension()));
}
} else if let Some(comp) = compression {
new_path.set_extension(format!("{}.{}", format.extension(), comp.extension()));
} else {
new_path.set_extension(format.extension());
}
} else if let Some(comp) = compression
&& format.supports_compression()
{
new_path.set_extension(format!("{}.{}", current_ext, comp.extension()));
}
// else: path stays as-is (e.g. foo.feather stays foo.feather)
// else: path with format extension stays as-is
}
new_path
}
pub fn new(events: Sender<AppEvent>, runtime: tokio::runtime::Handle) -> App {
// Create default theme for backward compatibility
let theme = Theme::from_config(&AppConfig::default().theme).unwrap_or_else(|_| {
// Create a minimal fallback theme
Theme {
colors: std::collections::HashMap::new(),
}
});
Self::new_with_config(events, runtime, theme, AppConfig::default())
}
pub fn new_with_theme(
events: Sender<AppEvent>,
runtime: tokio::runtime::Handle,
theme: Theme,
) -> App {
Self::new_with_config(events, runtime, theme, AppConfig::default())
}
/// An app with its saved views read here, before it is returned.
pub fn new_with_config(
events: Sender<AppEvent>,
runtime: tokio::runtime::Handle,
theme: Theme,
app_config: AppConfig,
) -> App {
let views = ViewManager::load_or_empty().into();
Self::new_with_views(events, runtime, theme, app_config, views)
}
/// An app whose saved views may still be on their way ([`Views`]).
pub fn new_with_views(
events: Sender<AppEvent>,
runtime: tokio::runtime::Handle,
theme: Theme,
app_config: AppConfig,
view_manager: Views,
) -> App {
let cache = CacheManager::new(APP_NAME).unwrap_or_else(|_| CacheManager {
cache_dir: std::env::temp_dir().join(APP_NAME),
});
let jobs = Jobs::new(events.clone());
let formats = Arc::new(crate::formats::Registry::load(
&crate::formats::search_path_for(&app_config),
));
for error in &formats.errors {
log::warn!("format spec skipped: {error}");
}
let theme_problem = app_config.theme.fallbacks.first().cloned();
let chart_export_modal = ChartExportModal {
recipe: app_config.chart.export_recipe,
..ChartExportModal::new()
};
let mut app = App {
path: None,
data_table_state: None,
footer_progress: Arc::new(crate::schema_union::FooterProgress::default()),
footers_this_frame: None,
listed_this_frame: None,
home: home::HomeState {
hide_unreadable: !app_config.home.show_unreadable,
formats: formats.clone(),
..Default::default()
},
home_probes_inflight: Vec::new(),
home_listing_cancels: HashMap::new(),
home_narrowing: None,
#[cfg(feature = "cloud")]
cloud_discovery_started: false,
home_search_inflight: false,
home_search_generation: 0,
home_generation: 0,
home_schema_inflight: Vec::new(),
last_load_error: None,
pending_clear_recents: false,
pending_link: None,
local_desktop: link_open::local_desktop(link_open::Platform::current(), |name| {
std::env::var(name).ok()
}),
pending_forget_place: None,
home_schema_cache: HashMap::new(),
home_previews: crate::home_preview::Previews::default(),
reads: crate::home_preview::ReadCounts::default(),
shape_remembered: None,
original_file_format: None,
original_file_delimiter: None,
stdin_reader: None,
stdout_pass: None,
follow_drawn: None,
pending_leave: None,
recording_on: None,
recording_end_said: false,
events,
debug: DebugState::default(),
info_modal: InfoModal::new(),
file_facts: None,
codebook: None,
catalog_entry: None,
documentation: Default::default(),
info_documentation: Default::default(),
remembered_moved: false,
head_web_rows: !cache::running_as_a_cargo_test(),
query_input: TextInput::new()
.with_history_limit(app_config.query.history_limit)
.with_theme(&theme)
.with_history("query".to_string()),
sql_input: TextInput::statement()
.with_history_limit(app_config.query.history_limit)
.with_theme(&theme)
.with_history("sql".to_string()),
find: find::Find::new(
TextInput::new()
.with_history_limit(app_config.query.history_limit)
.with_theme(&theme)
.with_history("find".to_string()),
),
column_hints: false,
input_mode: InputMode::Normal,
input_type: None,
query_mode: QueryMode::default().resolve(),
query_mode_chosen: None,
query_text_restored: false,
sql_columns: Vec::new(),
sql_completion: None,
query_running: None,
query_run_error: None,
inline_failures: 0,
sort_filter_modal: SortFilterModal::new(),
pivot_melt_modal: PivotMeltModal::new(),
view_modal: ViewModal::new(),
opened_from_home: false,
startup_view: None,
analysis_modal: AnalysisModal::with_sample_rows(app_config.analysis.sample_rows),
sample_form: None,
memory_probe: std::sync::Arc::new(table_sample::available_memory),
sample_paths: Vec::new(),
quality_cache: Vec::new(),
quality_samples: Vec::new(),
quality_released: Vec::new(),
quality_memory_budget: QUALITY_MEMORY_BUDGET,
quality_copies: Vec::new(),
quality_copy_released: None,
quality_copy_unusable: None,
quality_copy_free: std::sync::Mutex::new(None),
quality_evidence_return: None,
quality_evidence_label: None,
chart_modal: ChartModal::new(),
chart_export_modal,
export_modal: ExportModal::new(),
copy_modal: copy_modal::CopyModal::new(),
inspector_modal: inspector_modal::InspectorModal::new(),
external_open: None,
open_dir: None,
go_to_column: crate::widgets::ui::PickerState::default(),
value_counts: value_counts_modal::ValueCountsModal::default(),
hex: None,
hex_serial: 0,
export_counts: None,
format_picker: crate::widgets::ui::PickerState::default(),
retype: None,
combine: None,
retype_from_info: false,
table_picker: crate::widgets::ui::PickerState::default(),
table_choices: None,
clipboard: None,
pending_copy: None,
chart_cache: ChartCache::default(),
chart_inflight: None,
chart_asked: None,
pending_chart_result: Arc::new(Mutex::new(None)),
chart_export_waiting: None,
error_modal: ErrorModal::new(),
flash: None,
confirmation_modal: ConfirmationModal::new(),
pending_export: None,
pending_delete_view: None,
pending_hide_examples: false,
pending_chart_export: None,
pending_quality_export: None,
help: help::Help::default(),
pointer: pointer::Pointing::default(),
context_menu: None,
cache,
cache_writes: CacheWrites::default(),
view_manager,
active_view_id: None,
export_progress: None,
theme,
pending_read_all: false,
history_limit: app_config.query.history_limit,
table_cell_padding: app_config.display.cell_padding.cells(),
column_colors: app_config.display.column_colors,
dtype_row: app_config.display.type_row,
number_format: app_config
.display
.number_format
.resolve(app_config.display.right_align_numbers)
// AppConfig::load validates this, but App can be built from an
// unvalidated config (e.g. the Python API): fall back to no
// formatting while still honouring the alignment setting.
.unwrap_or_else(|_| NumberFormatSettings {
align_numeric_right: app_config.display.right_align_numbers,
..Default::default()
}),
jobs,
runtime,
loading: loading::Loader::default(),
opened: None,
loaded_ahead_from: None,
pending_footers_result: std::sync::Arc::new(std::sync::Mutex::new(None)),
dataset_generation: 0,
footers_held: None,
followed_fields_held: None,
reread_owed: None,
#[cfg(test)]
home_worker_dies: None,
#[cfg(test)]
file_facts_reader: None,
end_when_the_footers_land: None,
end_when_indexed: None,
indexing_stop: Arc::default(),
indexing_lines: None,
indexing_paused: false,
goto_when_indexed: None,
count_progress: Arc::default(),
exact_count_asked: None,
count_after_stop: None,
len_count_inflight: None,
count_after_paint: None,
#[cfg(test)]
counts_spawned: std::cell::Cell::new(0),
#[cfg(test)]
first_rows_asked: 0,
len_count_failed: None,
end_after_count: None,
busy: false,
throbber_frame: 0,
screen_generation: 0,
input_dropped: false,
status_message: None,
analysis_computation: None,
app_config,
background_query: false,
formats,
};
// A theme that could not be used: why is said on stderr after exit.
if let Some(problem) = theme_problem {
app.flash_note(problem);
}
app
}
/// Use `registry` as the format specs on the search path, for hosts and tests that
/// have their specs in hand.
pub fn set_formats(&mut self, registry: crate::formats::Registry) {
let registry = Arc::new(registry);
self.home.formats = registry.clone();
self.formats = registry;
}
pub fn enable_debug(&mut self) {
self.debug.enabled = true;
}
// ---- Home screen -----------------------------------------------------
/// Schema for a home-screen entry, read from Parquet metadata and memoised for
/// the session. `None` means "not knowable without a scan", which the UI reports
/// rather than papering over.
pub fn home_schema(&mut self, entry: &discover::Entry) -> Option<discover::SchemaPreview> {
// A schema preview reads a local file. Nothing in an object store is read before
// it is opened: asking would only come back empty, again on every rebuild.
if home::is_cloud_place(&entry.path) || home::is_object_store_url(&entry.path) {
return None;
}
if let Some(cached) = self.home_schema_cache.get(&entry.path) {
return cached.clone();
}
// Reading a schema opens a file, so it is requested rather than done here.
// Until it arrives the preview says so; it never blocks the frame.
self.request_home_schema(entry.clone());
None
}
/// What a home-screen worker owes in place of its answer if it panics.
fn owed_answer(&mut self, instead: AppEvent) -> OwedAnswer {
OwedAnswer {
tx: self.events.clone(),
#[cfg(test)]
dies: self
.home_worker_dies
.as_mut()
.is_some_and(|dies| dies(&instead)),
instead: Some(instead),
}
}
/// List the directory the `~` prompt is typing, when it is not the one listed. A
/// URL is listed from what the screen already knows; a local directory is read on
/// a worker.
fn list_the_typed_directory(&mut self) {
if !self.home.path_input_active {
return;
}
let dir = home::typed_dir(&self.home.path_input).to_string();
if self
.home
.path_listing
.as_ref()
.is_some_and(|l| l.dir == dir)
{
return;
}
if home::typed_dir_is_url(&dir) {
self.home.path_listing = Some(home::names_under(&dir, self.home.known_urls()));
return;
}
// Read off the UI thread: a typed path is where a dead mount gets named.
let tx = self.events.clone();
let owed = self.owed_answer(AppEvent::HomePathListed {
listing: Box::new(home::PathListing {
dir: dir.clone(),
names: Vec::new(),
failed: true,
}),
});
std::thread::spawn(move || {
owed.run(|| {
let listing = home::list_typed_dir(&dir);
let _ = tx.send(AppEvent::HomePathListed {
listing: Box::new(listing),
});
})
});
}
/// Complete the path being typed, on a worker.
fn request_path_completion(&mut self) {
let typed = self.home.path_input.clone();
if typed.is_empty() {
return;
}
let generation = self.home_generation;
let tx = self.events.clone();
std::thread::spawn(move || {
let (completed, candidates) = home::complete_path(&typed);
let _ = tx.send(AppEvent::HomePathCompleted {
generation,
typed,
completed,
candidates,
});
});
}
/// The first rows of a home-screen file for its `ROWS` preview, read on a worker
/// the way its open reads them. `None` until they land, and for a row that is not
/// previewed. `screen_height` sizes the page to the one the table will ask for.
pub fn home_preview_rows(
&mut self,
entry: &discover::Entry,
screen_height: u16,
) -> Option<Arc<crate::home_preview::PreviewRows>> {
let max = self.app_config.home.preview_max.bytes();
if !crate::home_preview::previewable(entry, max) {
return None;
}
let stamp = crate::home_preview::Stamp::of_entry(entry);
if let Some(known) = self.home_previews.rows(&entry.path, stamp) {
return known;
}
if self.home_previews.inflight.is_none() {
self.request_home_preview(entry.path.clone(), stamp, screen_height);
}
None
}
/// Whether `entry` is one the preview reads, before its rows are in.
pub fn home_preview_pending(&self, path: &Path) -> bool {
self.home_previews.reading(path)
}
/// Read a file's first page on a worker, through the open's own scan and schema
/// read, so the open can install what it built.
fn request_home_preview(
&mut self,
path: PathBuf,
stamp: crate::home_preview::Stamp,
screen_height: u16,
) {
self.home_previews.inflight = Some(path.clone());
self.reads.previews += 1;
let tx = self.events.clone();
let cloud = self.app_config.cloud.clone();
let formats = self.formats.clone();
let runtime = self.runtime.clone();
let cache = self.cache.clone();
let writes = self.cache_writes.clone();
// The table's rows: the screen less the title, the header and the control bar.
let visible = (screen_height as usize).saturating_sub(3).max(1);
let owed = self.owed_answer(AppEvent::HomePreviewReady {
path: path.clone(),
stamp,
read_at: None,
rows: None,
prepared: crate::home_preview::Handoff::default(),
});
self.runtime.spawn_blocking(move || {
owed.run(|| {
let began = std::time::Instant::now();
let read_at = crate::home_preview::Stamp::of_file(&path);
let read = Self::read_home_preview(
&path, &cloud, &formats, &runtime, cache, writes, visible,
);
log::debug!(
target: "datui",
"home preview of {}: {:.1?}",
path.display(),
began.elapsed()
);
let (rows, prepared) = match read {
Some((rows, prepared)) => (Some(Arc::new(rows)), Some(Box::new(prepared))),
None => (None, None),
};
let _ = tx.send(AppEvent::HomePreviewReady {
path,
stamp,
read_at,
rows,
prepared: Arc::new(Mutex::new(prepared)),
});
})
});
}
/// What the open of `path` from the home screen reads first: its scan, its schema
/// and the page the table asks for when `visible` rows show. Built by the open's
/// own steps with the options the home screen opens a file with, so the dataset is
/// the one the open would build.
fn read_home_preview(
path: &Path,
cloud: &crate::config::CloudConfig,
formats: &crate::formats::Registry,
runtime: &tokio::runtime::Handle,
cache: CacheManager,
writes: CacheWrites,
visible: usize,
) -> Option<(
crate::home_preview::PreviewRows,
crate::home_preview::Prepared,
)> {
let paths = [path.to_path_buf()];
let scanned = Self::scan_for_open(
cloud,
formats,
&paths,
OpenOptions::default(),
Some(path.to_path_buf()),
)
.ok()?;
let loading::LoadAnswer::Scanned { lf, path, options } = scanned else {
return None;
};
let progress = Arc::<crate::schema_union::FooterProgress>::default();
let report = crate::measurements::OpenReport {
progress: progress.clone(),
meter: Arc::new(crate::measurements::Meter::default()),
remembered: Some(cache),
writes,
};
let read = Self::read_schema_for_open(
*lf,
path,
options,
cloud,
runtime,
&report,
loading::Made::default(),
)
.ok()?;
let loading::LoadAnswer::SchemaRead {
mut state,
options,
debug_label,
..
} = read
else {
return None;
};
// Planned as the table plans its first page, so the page is the one it wants.
state.visible_rows = visible;
let began = std::time::Instant::now();
let request = state.prepare_async_collect(None)?;
let df = crate::statistics::collect_lazy(request.lf, request.polars_streaming).ok()?;
let result = request.plan.fit(df);
let rows = crate::home_preview::PreviewRows::from_frame(result.rows());
state.measurements().read_page(began.elapsed(), Some(1));
state.apply_async_collect(result);
Some((
rows,
crate::home_preview::Prepared {
state,
options,
debug_label,
progress,
},
))
}
/// Whether a schema read is currently out for this path.
pub fn home_schema_pending(&self, path: &Path) -> bool {
self.home_schema_inflight.iter().any(|p| p == path)
}
/// Read the selected dataset's schema on a worker.
fn request_home_schema(&mut self, entry: discover::Entry) {
if self.home_schema_inflight.contains(&entry.path) {
return;
}
self.home_schema_inflight.push(entry.path.clone());
let generation = self.home_generation;
let tx = self.events.clone();
// Remembered as having none, so the preview is not asked for again.
let owed = self.owed_answer(AppEvent::HomeSchemaReady {
generation,
path: entry.path.clone(),
preview: None,
});
self.runtime.spawn_blocking(move || {
owed.run(|| {
let preview = discover::schema_preview(&entry);
let _ = tx.send(AppEvent::HomeSchemaReady {
generation,
path: entry.path,
preview,
});
})
});
}
/// Start listing any network roots that have not answered yet.
///
/// Nothing here waits on the result. A share that has gone away leaves its thread
/// blocked in the kernel — on a `hard` NFS mount that is uninterruptible and the
/// thread never returns — so the task is abandoned rather than joined, exactly as
/// an abandoned dataset load is.
fn spawn_home_probes(&mut self) {
self.stop_listings_left_behind();
for root in self.home.pending_probes() {
if self.home_probes_inflight.contains(&root) {
// Left and come back to before its next page: it goes on.
if let Some(cancelled) = self.home_listing_cancels.get(&root) {
cancelled.store(false, std::sync::atomic::Ordering::Relaxed);
}
continue;
}
// Each probe of an unreachable share costs a thread that will never come
// back. A handful is a rounding error; an unbounded number, on a machine
// with a page of dead mounts, is not.
//
// Except the directory browsed into, which is the whole screen and has
// nothing else to show. Held behind the cap, it waited on roots the user
// had left — a few slow bucket listings kept a share's directory on a
// spinner long after it could have been read. One more thread per
// directory the user opens is bounded by the user.
let browsed = self.home.browsing.as_ref() == Some(&root);
if !browsed && self.home_probes_inflight.len() >= MAX_CONCURRENT_PROBES {
continue;
}
self.home_probes_inflight.push(root.clone());
let tx = self.events.clone();
let cache = self.cache.clone();
let owed = self.owed_answer(AppEvent::HomeProbeFailed {
root: root.clone(),
message: "Could not read it; see the log".to_string(),
});
#[cfg(feature = "cloud")]
let cloud = self.app_config.cloud.clone();
#[cfg(feature = "cloud")]
let runtime = self.runtime.clone();
#[cfg(feature = "cloud")]
let cancelled = {
let flag = std::sync::Arc::new(std::sync::atomic::AtomicBool::new(false));
self.home_listing_cancels.insert(root.clone(), flag.clone());
flag
};
// A detached OS thread, not the runtime's blocking pool. A thread wedged
// on an unreachable `hard` mount never returns, and the pool is shared with
// the work that actually loads data — a few dead shares must not eat into
// the capacity that opening a dataset depends on.
std::thread::spawn(move || {
owed.run(|| {
// A bucket or a prefix inside one. It looks like a network root to
// everything above, and it is, but it is read with an object-store
// listing rather than `read_dir` — which on a `gs://` path fails, which
// is why descending into a bucket used to show nothing at all.
//
// Deliberately metadata-only. A delimited listing returns names, sizes
// and modification times for one level, and nothing here reads an
// object's contents: no footers, no schemas, no row counts. Those are
// what a local listing fills in for free from bytes already on the
// machine, and what would cost a ranged read per row against an object
// store somebody pays egress on.
#[cfg(feature = "cloud")]
if let Some((id, account)) = home::cloud_account(&root) {
let listed = wait_on_runtime(&runtime, async move {
crate::cloud_browse::list_account(&id, &account, &cloud).await
});
match listed {
Some(Ok(rows)) => {
let _ = tx.send(AppEvent::HomeProbeReady {
root,
rows: Some(rows),
cut_short: false,
});
}
Some(Err(message)) => {
log::warn!(
target: "datui::cloud",
"listing {} failed: {message}",
root.display()
);
let _ = tx.send(AppEvent::HomeProbeFailed { root, message });
}
None => {
let _ = tx.send(AppEvent::HomeProbeReady {
root,
rows: None,
cut_short: false,
});
}
}
return;
}
#[cfg(feature = "cloud")]
if crate::cloud_browse::split_bucket_url(&root.to_string_lossy()).is_some()
|| source::azure_parts(&root.to_string_lossy()).is_some()
{
let url = root.to_string_lossy().into_owned();
// Each page's rows are drawn as they come, and leaving the place
// stops the listing before its next page.
let watch = crate::cloud_browse::Watch {
progress: Some(std::sync::Arc::new({
let (tx, root) = (tx.clone(), root.clone());
move |so_far: &[crate::discover::Entry]| {
let _ = tx.send(AppEvent::HomeProbeProgress {
root: root.clone(),
rows: so_far.to_vec(),
});
}
})),
cancelled,
names_from: None,
};
let listed = wait_on_runtime(&runtime, async move {
crate::cloud_browse::list_objects_watched(&url, &cloud, &watch).await
});
// A refused listing says why, rather than reading as a place that
// stopped answering.
match listed {
Some(Err(message)) => {
log::warn!(
target: "datui::cloud",
"listing {} failed: {message}",
root.display()
);
let _ = tx.send(AppEvent::HomeProbeFailed { root, message });
}
Some(Ok(level)) if level.cancelled => {
let _ = tx.send(AppEvent::HomeProbeCancelled { root });
}
other => {
let (rows, cut_short) = match other {
Some(Ok(level)) => (Some(level.rows), level.truncated),
_ => (None, false),
};
let _ = tx.send(AppEvent::HomeProbeReady {
root,
rows,
cut_short,
});
}
}
return;
}
// Nothing to list a bucket with, and `read_dir` on its URL would
// only call it unavailable.
#[cfg(not(feature = "cloud"))]
if source::is_remote_url(&root) {
let message = "cloud support not in this build".to_string();
let _ = tx.send(AppEvent::HomeProbeFailed { root, message });
return;
}
let mut cut_short = false;
let rows = if std::fs::read_dir(&root).is_ok() {
// What has been read shows while the rest is read: a share can take
// seconds over a directory of thousands.
let scan = crate::discover::scan_dir_progressive(&root, |so_far| {
let _ = tx.send(AppEvent::HomeProbeProgress {
root: root.clone(),
rows: so_far.to_vec(),
});
});
cut_short = scan.truncated;
let mut rows = scan.entries;
// Measuring happens here too: it is the same remote filesystem,
// and this thread is already the one allowed to block on it.
for row in rows.iter_mut().take(PROBE_MEASURE_LIMIT) {
crate::discover::enrich(row);
}
// Remote datasets are measured nowhere else, so this is the only
// chance to remember them. Without it a remote row is blank on
// every run, which is exactly backwards: the hardest things to
// reach are the ones most worth remembering.
let mounts = crate::locality::Mounts::current();
for row in rows.iter_mut() {
row.cost.source = Some(mounts.describe(&row.path).fstype);
}
let facts: Vec<_> = rows.iter().filter_map(home::facts_for).collect();
cache.record_dataset_facts(&facts);
Some(rows)
} else {
None
};
let _ = tx.send(AppEvent::HomeProbeReady {
root,
rows,
cut_short,
});
})
});
}
}
/// Stop the cloud listings of places no longer on screen: the one browsed, or the
/// roots of the home listing. One left and come back to is listed again.
fn stop_listings_left_behind(&mut self) {
let home = &self.home;
for (root, cancelled) in &self.home_listing_cancels {
let wanted = match &home.browsing {
Some(dir) => dir == root,
None => home
.sections
.iter()
.any(|s| s.remote_root.as_ref() == Some(root)),
};
if !wanted {
cancelled.store(true, std::sync::atomic::Ordering::Relaxed);
}
}
if let Some((dir, _, cancelled)) = &self.home_narrowing
&& home.browsing.as_ref() != Some(dir)
{
cancelled.store(true, std::sync::atomic::Ordering::Relaxed);
self.home_narrowing = None;
}
if self
.home
.narrowed
.as_ref()
.is_some_and(|n| self.home.browsing.as_ref() != Some(&n.dir))
{
self.home.narrowed = None;
}
}
/// In a cloud directory cut short at the cap, ask the server for the names the
/// filter starts, so a name past the first few thousand can still be found. Nothing
/// asked when the filter is empty or what is held already answers it.
#[cfg(feature = "cloud")]
fn narrow_cloud_listing(&mut self) {
let dir = self.home.browsing.clone();
let prefix = dir.as_ref().and_then(|dir| {
if !self.home.cut_short.contains(dir) {
return None;
}
let rows = self.home.probed.get(dir)?;
let names: Vec<&str> = rows.iter().map(|row| row.name.as_str()).collect();
crate::cloud_browse::narrowing_prefix(&self.home.filter, &names)
});
let (Some(dir), Some(prefix)) = (dir, prefix) else {
if let Some((_, _, cancelled)) = self.home_narrowing.take() {
cancelled.store(true, std::sync::atomic::Ordering::Relaxed);
}
if self.home.narrowed.take().is_some() {
self.home_refresh();
}
return;
};
// Everything under a shorter prefix is everything under this one too.
if self.home.narrowed.as_ref().is_some_and(|n| {
n.dir == dir && (n.prefix == prefix || (!n.truncated && prefix.starts_with(&n.prefix)))
}) {
return;
}
if let Some((d, p, _)) = &self.home_narrowing
&& *d == dir
&& *p == prefix
{
return;
}
if let Some((_, _, cancelled)) = self.home_narrowing.take() {
cancelled.store(true, std::sync::atomic::Ordering::Relaxed);
}
let cancelled = Arc::new(std::sync::atomic::AtomicBool::new(false));
self.home_narrowing = Some((dir.clone(), prefix.clone(), cancelled.clone()));
let tx = self.events.clone();
let owed = self.owed_answer(AppEvent::HomeNarrowed {
dir: dir.clone(),
prefix: prefix.clone(),
listed: None,
});
let cloud = self.app_config.cloud.clone();
let runtime = self.runtime.clone();
std::thread::spawn(move || {
owed.run(|| {
let url = dir.to_string_lossy().into_owned();
let watch = crate::cloud_browse::Watch {
progress: None,
cancelled,
names_from: Some(prefix.clone()),
};
let listed = wait_on_runtime(&runtime, async move {
crate::cloud_browse::list_objects_watched(&url, &cloud, &watch).await
});
let listed = match listed {
Some(Ok(level)) if !level.cancelled => Some((level.rows, level.truncated)),
_ => None,
};
let _ = tx.send(AppEvent::HomeNarrowed {
dir,
prefix,
listed,
});
})
});
}
/// Find the cloud sources this machine and the config describe, and list their
/// buckets when `[cloud] list_on_start` asks. Once per session; Ctrl+R asks again.
#[cfg(feature = "cloud")]
fn spawn_cloud_discovery(&mut self) {
if self.cloud_discovery_started {
return;
}
self.cloud_discovery_started = true;
let list = self.app_config.cloud.list_on_start;
self.list_cloud_sources(None, list);
}
/// List the source being browsed, when it has not been asked this session.
/// Entering a source is the request to list it.
#[cfg(feature = "cloud")]
fn list_browsed_cloud_source(&mut self) {
let Some(id) = self
.home
.browsing
.as_deref()
.and_then(home::cloud_source_id)
else {
return;
};
let Some(source) = self.home.cloud.iter_mut().find(|s| s.id == id) else {
return;
};
if source.asked {
return;
}
source.begin_listing();
self.list_cloud_sources(Some(id), true);
}
/// Send the rows of every source, or list the buckets of the one named.
///
/// The rows go out first, filled from the last run's listing when the source still
/// points at the same place, so the home screen has its counts before any request
/// is made. With `list`, the sources are then listed side by side, a few at a
/// time, and each result is sent the moment it arrives. Without it nothing leaves
/// the machine: no request, and no credential command.
///
/// Runs on the runtime rather than a detached thread. Unlike a probe of a dead
/// `hard` mount, an HTTP request cannot wedge forever: every call here is bounded
/// by a global timeout, so the task is guaranteed to end.
#[cfg(feature = "cloud")]
fn list_cloud_sources(&mut self, only: Option<String>, list: bool) {
let tx = self.events.clone();
let cloud = self.app_config.cloud.clone();
let cache = self.cache.clone();
self.runtime.spawn(async move {
let mut hidden = cache.load_hidden_cloud_sources();
hidden.extend(cloud.hide.iter().cloned());
let cached_for = |source: &crate::cloud_sources::Source| {
cache.cloud_listing(&source.id, &source.fingerprint())
};
let found = {
let env = crate::cloud_browse::Environment::current();
crate::cloud_sources::discover(&cloud, &env)
};
// Shown or not: a bucket under Recent opens with the login that listed it
// whatever `discover` says. Not a hidden source, which may be hidden for a
// login that no longer works; the default login opens its buckets instead.
// Only with the rows, so an old listing never overrides one made since.
if only.is_none() {
for source in found.iter().filter(|s| !hidden.contains(&s.id)) {
if let Some(cached) = cached_for(source) {
crate::cloud_sources::remember_listed(source, &cached.buckets);
}
}
}
let sources: Vec<crate::cloud_sources::Source> =
crate::cloud_sources::on_home(found, &cloud)
.into_iter()
.filter(|s| !hidden.contains(&s.id))
.collect();
match &only {
None => {
let rows = sources
.iter()
.map(|source| home_cloud_source(source, cached_for(source).as_ref(), list))
.collect();
let _ = tx.send(AppEvent::HomeCloudSources { sources: rows });
}
// Gone since its row was drawn: a profile removed, a source hidden
// elsewhere. Said, so the row does not wait on an answer never coming.
Some(id) if !sources.iter().any(|s| &s.id == id) => {
let _ = tx.send(AppEvent::HomeCloudListed {
id: id.clone(),
buckets: Vec::new(),
details: Vec::new(),
failure: Some((
"not found".to_string(),
format!("{id} is gone or hidden. Ctrl+R at the top looks again."),
)),
listed_at: std::time::SystemTime::now(),
});
return;
}
Some(_) => {}
}
if !list {
return;
}
// Enough to keep one slow endpoint from delaying the rest, few enough that a
// long list of sources does not open a connection storm.
const LISTING_AT_ONCE: usize = 4;
let permits = std::sync::Arc::new(tokio::sync::Semaphore::new(LISTING_AT_ONCE));
let mut listings = tokio::task::JoinSet::new();
for source in sources
.into_iter()
.filter(|s| only.as_ref().is_none_or(|id| &s.id == id))
{
let permits = permits.clone();
listings.spawn(async move {
let _permit = permits.acquire_owned().await;
let result = crate::cloud_browse::list_first_level(&source).await;
(source, result)
});
}
while let Some(joined) = listings.join_next().await {
let Ok((source, result)) = joined else {
continue;
};
let listed_at = std::time::SystemTime::now();
// Buckets named in the config are shown whether or not the login can
// list them; that is what naming them is for.
let mut names = source.buckets.clone();
let mut details = Vec::new();
let failure = match result {
Ok(listed) => {
for item in listed {
crate::cloud_sources::remember_bucket(&source, &item.name);
if !item.details.is_empty() {
details.push((item.place.clone(), item.details));
}
let name = item.name;
if !names.contains(&name) {
names.push(name);
}
}
cache.save_cloud_listing(
&source.id,
crate::cache::CloudListing {
fingerprint: source.fingerprint(),
buckets: names.clone(),
listed_at: listed_at
.duration_since(std::time::UNIX_EPOCH)
.map(|d| d.as_secs())
.unwrap_or(0),
},
);
None
}
Err(e) => {
log::warn!(target: "datui::cloud", "listing {} failed: {e}", source.id);
Some(summarize_cloud_failure(&e))
}
};
let _ = tx.send(AppEvent::HomeCloudListed {
id: source.id.clone(),
buckets: names
.iter()
.map(|b| PathBuf::from(source.bucket_url(b)))
.collect(),
details,
failure,
listed_at,
});
}
});
}
/// Ask again for what is on screen, ignoring what is cached: the buckets of the
/// source being browsed, the contents of the bucket or directory being browsed, or
/// every source's buckets from the home listing.
fn home_reload(&mut self) {
#[cfg(feature = "cloud")]
{
let browsing = self.home.browsing.clone();
match browsing.as_deref().and_then(home::cloud_source_id) {
Some(id) => {
if let Some(source) = self.home.cloud.iter_mut().find(|s| s.id == id) {
source.begin_listing();
}
self.list_cloud_sources(Some(id), true);
}
None if browsing.is_none() && !self.home.cloud.is_empty() => {
for source in &mut self.home.cloud {
source.begin_listing();
}
self.list_cloud_sources(None, true);
}
None => {}
}
}
if let Some(dir) = self.home.browsing.clone() {
self.home.probed.remove(&dir);
self.home.unreachable.remove(&dir);
self.home.cut_short.remove(&dir);
}
// A peek that failed is asked again: Ctrl+R is the request to try. So is a web
// file that was not there.
self.home.peek_failed.clear();
for path in std::mem::take(&mut self.home.web_gone).into_keys() {
self.home.sized.remove(&path);
}
self.home.status = None;
self.home_refresh();
}
/// Start the recursive search below the working directory, if it is wanted and
/// not already running.
///
/// Triggered by typing rather than by opening the home screen: typing is the
/// signal that someone is looking for something. Launching datui, pressing Enter
/// on a recent dataset and leaving costs no walk at all.
fn spawn_home_search(&mut self) {
if self.home_search_inflight || self.home.search.done {
return;
}
let config = self.app_config.home.search.clone();
if !config.enabled {
return;
}
let Some(root) =
crate::search::search_root(self.home.browsing.as_ref(), self.home.network_check)
else {
return;
};
self.home.search.reset();
self.home.search.root = Some(root.clone());
self.home.search.running = true;
self.home.search.epoch = next_search_epoch();
self.home.search_limit = config.max_results;
self.home_search_inflight = true;
let generation = self.home_generation;
self.home_search_generation = generation;
let tx = self.events.clone();
let formats = self.formats.clone();
// Ended, with what the batches already found kept.
let owed = self.owed_answer(AppEvent::HomeSearchDone {
generation,
root: root.clone(),
scanned: 0,
limited: Some("partial · failed".to_string()),
});
// A detached thread for the same reason the probes use one: the walk touches
// a filesystem, and nothing that touches a filesystem may run where a stall
// would stop the screen from drawing.
std::thread::spawn(move || {
owed.run(|| {
let walk_root = root.clone();
let batch_tx = tx.clone();
let batch_gen = generation;
let batch_root = root.clone();
let outcome = crate::search::walk_with_specs(
&walk_root,
&config,
&formats,
move |found, outcome| {
// Sent even when empty: it carries the progress count, and it is the
// only place the walk learns that nobody is listening any more.
batch_tx
.send(AppEvent::HomeSearchBatch {
generation: batch_gen,
root: batch_root.clone(),
found,
scanned: outcome.scanned,
})
// A closed channel means the app is gone; stop walking.
.is_ok()
},
);
let _ = tx.send(AppEvent::HomeSearchDone {
generation,
root,
scanned: outcome.scanned,
limited: outcome.note().map(str::to_string),
});
})
});
}
/// Score the filter against the search's files on a worker, when a scoring is owed.
///
/// Asked after every event. Over a tree of tens of thousands of files the scoring
/// is what held each keystroke's echo back, so it runs where a stall cannot hold
/// the screen, one at a time; each answer asks for the next if the filter moved on.
fn home_score_search(&mut self) {
if self.input_mode != InputMode::Home {
return;
}
let Some(job) = self.home.score_job() else {
return;
};
let epoch = job.epoch;
let tx = self.events.clone();
// A worker that dies answers with nothing.
let owed = self.owed_answer(AppEvent::HomeSearchScored {
epoch,
matches: None,
});
self.runtime.spawn_blocking(move || {
owed.run(move || {
let matches =
crate::search::score(&job.results, &job.query, job.base.as_ref(), job.limit);
let _ = tx.send(AppEvent::HomeSearchScored {
epoch,
matches: Some(Box::new(matches)),
});
})
});
}
/// Rebuild the home listing from the filesystem.
fn home_refresh(&mut self) {
// Every way into a source comes through here: Enter, Backspace up from a
// bucket, a jump, and rows arriving while the source is already open.
#[cfg(feature = "cloud")]
self.list_browsed_cloud_source();
// Somewhere else now, a listing of where the user was is pages for nobody.
self.stop_listings_left_behind();
self.home_generation = self.home_generation.wrapping_add(1);
let generation = self.home_generation;
self.move_remembered_places();
self.home.catalogs = home::catalogs(&self.app_config);
// Hidden with Delete on its heading: the catalog that comes with datui only,
// never a user's own `examples.toml`.
if self.cache.examples_hidden() {
self.home
.catalogs
.retain(|c| c.origin != crate::catalog::Origin::Bundled);
}
let mut request = home::ListingRequest {
// Filled in on the worker, from the cache and the desktop's recents: files
// all the same, and the first frame does not wait on a file.
recents: Vec::new(),
desktop_dirs: Vec::new(),
browsing: self.home.browsing.clone(),
probed: self.home.probed.clone(),
unreachable: self.home.unreachable.clone(),
listing_so_far: self.home.listing_so_far.clone(),
cut_short: self.home.cut_short.clone(),
narrowed: self.home.narrowed.clone(),
probe_errors: self.home.probe_errors.clone(),
network_check: self.home.network_check,
cloud: self.home.cloud.clone(),
catalogs: self.home.catalogs.clone(),
known: Default::default(),
formats: self.formats.clone(),
};
let read_folds = std::mem::take(&mut self.home.folds_owed);
let desktop = self.app_config.home.desktop_recents;
let cache = self.cache.clone();
let writes = self.cache_writes.clone();
self.home.listing_in_flight = true;
let tx = self.events.clone();
let owed = self.owed_answer(AppEvent::HomeListingFailed);
self.runtime.spawn_blocking(move || {
owed.run(move || {
// After the dataset just left is in the recents with its shape.
writes.settle();
// Ranked by frecency; the newest is where the cursor lands, so the
// last file is still one Enter away.
let (recents, visits) = cache.load_recents_with_visits();
let newest = recents.first().cloned();
request.recents = crate::cache::by_frecency(recents, &visits);
request.known = cache.load_dataset_facts();
if desktop {
request.desktop_dirs = home::desktop_recent_dirs();
}
let listing = home::build_listing(&request);
let mut visits = visits;
listing.alias_visits(&mut visits);
// A record shown is a record used: the ones eviction keeps.
let shown: Vec<PathBuf> = listing
.sections
.iter()
.flat_map(|s| s.rows.iter().chain(&s.door))
.flat_map(|row| [row.path.clone(), home::index_key(&row.path)])
.filter(|key| request.known.contains_key(key))
.collect();
cache.touch_dataset_facts(shown.iter().map(PathBuf::as_path));
let _ = tx.send(AppEvent::HomeListingReady {
generation,
listing: Box::new(listing),
known: request.known,
visits,
newest,
folds: read_folds.then(|| cache.load_folds()),
});
})
});
}
/// Ask the worker to measure rows that are on screen and not yet known.
///
/// Reading a Parquet footer opens a file. That is the call that blocks on a FIFO,
/// a device node, a wedged mount or a failing disk, so it never happens on the
/// thread that draws.
fn request_home_measurements(&mut self) {
if self.home.measure_in_flight {
return;
}
let wanted = self.home.unmeasured_visible(MEASURE_BATCH);
if wanted.is_empty() {
return;
}
self.home.measure_in_flight = true;
let tx = self.events.clone();
let cache = self.cache.clone();
self.runtime.spawn_blocking(move || {
home::look_into_batch(wanted, &cache, |path, m| {
let _ = tx.send(AppEvent::HomeMeasured {
measured: vec![(path, m)],
done: false,
});
});
let _ = tx.send(AppEvent::HomeMeasured {
measured: Vec::new(),
done: true,
});
});
}
/// Ask for what the frame just drawn needs and did not have: counts for the rows
/// on screen that have none, and kinds for the rows nothing has looked into.
///
/// After the frame, never during it. Scrolling is what brings new rows into view,
/// and the reading is a worker's job — this thread only decides what is worth
/// asking about.
pub fn request_what_the_frame_needs(&mut self) {
if self.input_mode == InputMode::Normal {
self.load_ahead();
self.catch_up_follow();
}
self.inspector_needs();
if self.input_mode != InputMode::Home {
return;
}
if std::mem::take(&mut self.home.pending_enrich) {
self.request_home_measurements();
}
#[cfg(feature = "http")]
self.size_selected_web_file();
if std::mem::take(&mut self.home.pending_classify) {
self.request_home_classifications();
}
#[cfg(feature = "cloud")]
if std::mem::take(&mut self.home.pending_peek) {
self.peek_cloud_directories();
}
}
/// Ask an HTTP(S) server what the file under the cursor weighs, once a session, when
/// nothing has measured it: its row shows a catalog's `~33 MB` until the answer
/// lands, and the answer is kept with what datui measured, for the next listing.
#[cfg(feature = "http")]
fn size_selected_web_file(&mut self) {
if !self.head_web_rows {
return;
}
let Some(entry) = self.home.selected_entry() else {
return;
};
if entry.size.is_some()
|| !matches!(
source::input_source(&entry.path),
source::InputSource::Http(_)
)
|| !self.home.sized.insert(entry.path.clone())
{
return;
}
let tx = self.events.clone();
let cache = self.cache.clone();
self.runtime.spawn_blocking(move || {
let size = match Self::fetch_remote_size_http(&entry.path.to_string_lossy()) {
Ok(Some(size)) => size,
Ok(None) => return,
Err(gone) => {
let _ = tx.send(AppEvent::HomeWebGone {
path: entry.path,
gone,
});
return;
}
};
let key = home::index_key(&entry.path);
let mut facts = cache.dataset_facts(&key).unwrap_or_default();
facts.size = size;
cache.record_dataset_facts(&[(key, facts)]);
let measured = home::Measured {
rows: entry.rows,
cols: entry.cols,
cols_sampled: entry.cols_sampled,
size: Some(size),
columns: entry.columns.clone(),
cost: entry.cost.clone(),
kind: None,
holds: entry.holds.clone(),
};
let _ = tx.send(AppEvent::HomeSized {
path: entry.path,
measured,
});
});
}
/// Ask a worker what the rows on screen are.
///
/// Classifying a row means reading the directory it names, which on a share is a
/// round trip and on a wedged mount never returns — so it happens here rather
/// than while the listing is built, where it was paid for in directory order and
/// bought a label for the first sixty-four rows and a wrong one for the rest.
///
/// A detached thread, not the runtime's blocking pool, for the reason the probes
/// give: a thread stuck on an unreachable `hard` mount never comes back, and the
/// pool is shared with the work that actually loads data. One at a time, so a
/// share that has stopped answering costs one thread and then stops asking.
///
/// This measures remote rows as well as classifying them, which
/// [`HomeState::unmeasured_visible`] deliberately refuses to do — it leaves them to
/// their root's probe, so that a share gets one thread and not two. That reasoning
/// no longer reaches: the probe scans a remote directory before anything has looked
/// into it, so every row it returns is `Unknown` and there is nothing for it to
/// measure. This pass is the only thing left that can, and it makes the same bargain
/// the probe made — one detached thread, on a filesystem it is already reading.
fn request_home_classifications(&mut self) {
if self.home.classify_in_flight {
return;
}
let wanted = self.home.unclassified_visible(CLASSIFY_BATCH);
if wanted.is_empty() {
return;
}
self.home.classify_in_flight = true;
let tx = self.events.clone();
let cache = self.cache.clone();
std::thread::spawn(move || {
home::look_into_batch(wanted, &cache, |path, m| {
let _ = tx.send(AppEvent::HomeClassified {
measured: vec![(path, m)],
done: false,
});
});
let _ = tx.send(AppEvent::HomeClassified {
measured: Vec::new(),
done: true,
});
});
}
/// Enter the home screen, rebuilding it, abandoning any in-flight load.
///
/// Returning home puts the cursor on whatever you currently have open, so the
/// round trip out and back lands where you left rather than at the top.
///
/// Abandoning puts the open in flight down at once ([`loading::Loader::retire`]):
/// its jobs are superseded, so their answers are dropped on arrival and none can
/// install a dataset or take the user off the screen they went to; its stop flag
/// is raised, so a download stops and an in-flight cloud pass stops issuing paid
/// reads within a wave. Work that is not the open's — an export, an analysis, the
/// footer pass of the dataset already on screen — is deliberately left alone, so
/// its progress indicator and its completion modal must survive this.
pub fn abandon_load(&mut self) {
let retired = self.loading.retire();
if let Some(retired) = retired {
self.put_down_load(retired);
}
// A chart being prepared for the dataset we are leaving would otherwise keep
// the throbber up on the home screen, and its result could later land in a
// different dataset with the same column names.
self.reset_chart_state();
// A look that is out belongs to the home screen being left, and the thread it is
// on may never come back — a share that has gone away is the case it exists for.
// Superseded, its answer touches nothing, and the keyboard does not wait for it.
if self.jobs.supersede(|job| matches!(job, Job::Classify(_))) {
self.home.status = None;
}
// And a collect that was waiting behind this load goes with it. Left standing,
// it runs the moment the generation is free — reading the dataset the user
// walked away from, at the home screen, with every key held.
self.jobs.take_owed(Self::owed_rows);
// Only an open's own wait is put down. An export holds keys too, and it keeps
// running. The rows the open's last step is reading still land; nobody waits on
// them.
if retired.is_some() {
self.busy = false;
let quieted = self.jobs.quiet(Self::reading_rows);
if self
.status_message
.as_ref()
.is_some_and(|status| quieted.contains(status))
{
self.status_message = None;
}
}
// Keys typed at the frozen screen were meant for the load, not for home:
// replayed there they could open a dataset nobody asked for.
self.screen_generation = self.screen_generation.wrapping_add(1);
}
pub fn enter_home(&mut self) {
self.pause_indexing();
if self.return_from_quality_evidence(false) {
self.analysis_modal.close();
}
// The view modal keys and renders off its own `active`, not the input
// mode, so left open here it would come back as a zombie over the next
// dataset opened.
self.view_modal.close();
self.inspector_modal.close();
self.stop_find();
self.hex = None;
// A count of the dataset being left is read for nobody.
self.stop_value_count();
self.export_counts = None;
self.abandon_load();
// Nobody is watching the file any more.
if let Some(state) = self.data_table_state.as_mut() {
state.stop_following();
}
self.home.status = None;
// The search that found the dataset comes back, selected: the next character
// typed starts a new one, and `~` opens the path prompt.
self.home.filter_selected = !self.home.filter.is_empty();
self.home.folds_owed = true;
self.home_refresh();
if let Some(open_path) = self.path.clone() {
let target =
crate::canonical::canonicalize(&open_path).unwrap_or_else(|_| open_path.clone());
if let Some(idx) = self.home.visible().iter().position(|row| match row {
home::Row::Entry { entry, .. } => {
crate::canonical::canonicalize(&entry.path)
.unwrap_or_else(|_| entry.path.clone())
== target
}
// Not the door: its path is the directory's, so an open file whose
// directory is being browsed would put the cursor on the row that
// opens the whole directory rather than on the file itself.
home::Row::Header { .. }
| home::Row::Place { .. }
| home::Row::More { .. }
| home::Row::Hidden { .. }
| home::Row::Door { .. } => false,
}) {
self.home.selected = idx;
}
}
self.input_mode = InputMode::Home;
}
/// Esc backs out one layer of context at a time: the filter, then the directory
/// descended into, then back to the data that was open. At the top level it does
/// nothing. It used to quit there, which made a reflexive Esc close the program
/// while the same key one level down merely went up; Ctrl+C quits, from anywhere.
fn home_escape(&mut self) -> Option<AppEvent> {
if !self.home.filter.is_empty() {
self.home.filter.clear();
self.home.sync_search_section();
// On a dataset, as at launch, not on the first section's header.
self.home.select_first_entry();
return None;
}
if self.home.browsing.is_some() {
if self.home.below_browse_start() {
self.home_ascend();
} else {
// Climbing past where the browse began would take Esc somewhere the
// user never was; it returns to the listing they started from instead.
self.home_leave_browsing(None);
}
return None;
}
if self.data_table_state.is_some() {
self.input_mode = InputMode::Normal;
// Said on arrival: Esc pressed once too often to clear the home screen lands
// here, and the keys typed next act on the table (#547 D14).
let name = self
.path
.as_deref()
.and_then(|p| p.file_name())
.map(|n| n.to_string_lossy().into_owned());
if let Some(name) = name {
self.flash_note(format!("Back to {name}"));
}
}
None
}
/// Drop the highlighted dataset from the recents list.
///
/// Only from the Recent section: a row under a directory is a file on disk, and
/// forgetting it there would either do nothing or imply a deletion datui is not
/// going to perform.
fn home_forget_selected(&mut self) {
// A catalog's heading: Delete hides the one that comes with datui, until the
// cache is cleared. A catalog of the user's is hidden by its id in the config.
if let Some(catalog) = self.home.selected_catalog() {
if catalog.origin == crate::catalog::Origin::Bundled {
let message = format!(
"Hide {}? It comes back after datui cache clear.",
catalog.label
);
self.pending_hide_examples = true;
self.confirmation_modal.show_destructive(message, "Hide");
} else {
self.home.status = Some(format!(
"[home] hide = [\"{}\"] in config.toml hides it",
catalog.id
));
}
return;
}
// A place row stands for every recent under it. Forgetting them all is one
// keystroke from forgetting one, so it asks first, the way Shift+Delete does.
if let Some(home::Row::Place { path, held, .. }) = self.home.selected_row() {
self.pending_forget_place = Some(path.clone());
self.confirmation_modal.show(format!(
"Forget {held} recently opened {} under {}?",
if held == 1 { "dataset" } else { "datasets" },
home::display_path(&path)
));
return;
}
// A row of catalog.toml's own section goes from the file, as Ctrl+D on it does.
// The same place under Recent is a recent, and Delete forgets only that.
let in_mine = self
.home
.selected_section()
.and_then(|i| self.home.sections.get(i))
.is_some_and(|s| s.origin == Some("catalog.toml"));
if in_mine
&& let Some((location, _)) = self.home_row_for_catalog()
&& let Some((id, name)) = self.mine_entry_at(&location)
{
self.home_forget_from_catalog(&id, &name);
return;
}
let section_title = self
.home
.selected_section()
.and_then(|i| self.home.sections.get(i))
.map(|s| s.title.clone())
.unwrap_or_default();
if self.home.browsing.is_none()
&& section_title == home::HomeState::CLOUD_SECTION
&& let Some(id) = self
.home
.selected_entry()
.and_then(|e| home::cloud_source_id(&e.path))
{
self.cache.hide_cloud_source(&id);
self.home.cloud.retain(|s| s.id != id);
self.home_refresh();
return;
}
let in_recents = section_title == "Recent";
if !in_recents {
self.home.status = Some("Only recents and catalog.toml rows can be forgotten".into());
return;
}
let Some(entry) = self.home.selected_entry() else {
return;
};
self.cache.forget_recent(&entry.path);
// Nothing to say: the row going is the answer.
self.home.status = None;
self.home_refresh();
}
/// The dataset or directory the highlighted row stands for, as Ctrl+D adds it: its
/// location, and the name its row shows. A heading stands for the directory its
/// section lists. A table inside a file, a cloud source and the rows that are not
/// places have none.
fn home_row_for_catalog(&self) -> Option<(PathBuf, String)> {
match self.home.selected_row()? {
home::Row::Place { path, .. } => {
let name = home::display_path(&path);
Some((path, name))
}
home::Row::Door { entry, .. } => {
// A local door's trailing slash goes; a URL keeps its `//`, which
// `components` would fold into a local path.
let path: PathBuf = if matches!(
source::input_source(&entry.path),
source::InputSource::Local(_)
) {
entry.path.components().collect()
} else {
entry.path.clone()
};
Some((path.clone(), home::display_path(&path)))
}
home::Row::Entry { entry, .. } => (entry.table.is_none()
&& !home::is_cloud_place(&entry.path)
&& crate::members::split(&entry.path).is_none())
.then(|| (entry.path.clone(), entry.name.clone())),
home::Row::Header { section, .. } => {
let root = self.home.sections.get(section)?.root.clone()?;
let name = home::display_path(&root);
Some((root, name))
}
home::Row::More { .. } | home::Row::Hidden { .. } => None,
}
}
/// `catalog.toml`'s path: beside the config file read, else in the config directory.
fn mine_catalog_file(&self) -> Option<PathBuf> {
let dir = match &self.app_config.catalog_dir {
Some(dir) => dir.clone(),
None => config::ConfigManager::new(APP_NAME)
.ok()?
.config_dir()
.to_path_buf(),
};
Some(dir.join(catalog::MINE_FILE))
}
/// The id and name of the `catalog.toml` entry at `location`, when there is one.
fn mine_entry_at(&self, location: &Path) -> Option<(String, String)> {
self.app_config
.read_catalogs
.iter()
.find(|c| c.origin == catalog::Origin::Mine)?
.dataset_at(location)
.map(|d| (d.id.clone(), d.name.clone()))
}
/// Read the catalogs again after `catalog.toml` changed, and list again.
fn reload_catalogs(&mut self) -> Result<(), String> {
let dir = self
.mine_catalog_file()
.and_then(|f| f.parent().map(Path::to_path_buf));
self.app_config
.read_catalog_files(dir.as_deref())
.map_err(|e| e.to_string())?;
// The open dataset's notes and Documentation tab follow the file.
let shown = home::catalogs(&self.app_config);
let path = self.path.clone();
self.codebook = path.as_deref().and_then(|p| home::codebook_for(&shown, p));
self.catalog_entry = path
.as_deref()
.and_then(|p| home::catalog_entry_for(&shown, p));
self.open_info_documentation();
self.home_refresh();
Ok(())
}
/// What Ctrl+D writes for a row at `location` named `name`: a copy of what another
/// catalog says of it, or the place as it is.
fn new_catalog_dataset(&self, location: &Path, name: &str) -> catalog::NewDataset {
if let Some((_, shown)) = self.home.catalog_dataset(location) {
let entry = &shown.entry;
return catalog::NewDataset {
name: entry.name.clone(),
path: entry.local_path().map(|p| home::display_path(&p)),
url: entry.url.clone(),
auth: entry.auth.clone(),
connection: entry.connection.clone(),
description: entry.description.clone(),
size: entry.size,
};
}
let mut new = catalog::NewDataset {
name: name.to_string(),
..Default::default()
};
if matches!(
source::input_source(location),
source::InputSource::Local(_)
) {
let absolute = if location.is_relative() {
std::env::current_dir()
.map(|cwd| cwd.join(location))
.unwrap_or_else(|_| location.to_path_buf())
} else {
location.to_path_buf()
};
new.path = Some(home::display_path(&absolute));
return new;
}
// A store reached through a named source: the source becomes the connection
// when it is one of the config's, and the URL loses it.
let text = location.to_string_lossy();
let (id, plain) = source::split_source_id(&text);
new.url = Some(plain.into_owned());
if let Some(id) = id {
new.connection = Some(id.to_string());
}
new
}
/// Ctrl+D: add the row under the cursor to `catalog.toml`, or forget it from there.
fn home_toggle_catalog(&mut self) {
let Some((location, name)) = self.home_row_for_catalog() else {
self.home.status = Some("Move to a dataset or directory to add it".into());
return;
};
if let Some((id, name)) = self.mine_entry_at(&location) {
self.home_forget_from_catalog(&id, &name);
return;
}
let Some(file) = self.mine_catalog_file() else {
self.home.status = Some("No config directory to keep catalog.toml in".into());
return;
};
let new = self.new_catalog_dataset(&location, &name);
// A source found on the machine, not one of the config's connections, cannot
// be named in a catalog: without it the URL would be read elsewhere.
if let Some(connection) = &new.connection
&& !self
.app_config
.cloud
.connections
.iter()
.any(|c| c.name == *connection)
{
self.home.status = Some(format!(
"Not added: {connection} is not a [[cloud.connections]] entry in the config"
));
return;
}
if let Err(why) = new.check() {
self.home.status = Some(format!("Not added: {why}"));
return;
}
let label = self
.app_config
.read_catalogs
.iter()
.find(|c| c.origin == catalog::Origin::Mine)
.map(|c| c.label.clone())
.unwrap_or_else(|| catalog::MINE_LABEL.to_string());
match catalog::add(&file, &new) {
Ok(_) => match self.reload_catalogs() {
Ok(()) => self.flash_note(format!("Added {} to {label}", new.name)),
Err(e) => self.error_modal.show(e),
},
Err(e) => self.error_modal.show(e.to_string()),
}
}
/// Remove the entry `id` from `catalog.toml`.
fn home_forget_from_catalog(&mut self, id: &str, name: &str) {
let Some(file) = self.mine_catalog_file() else {
return;
};
match catalog::forget(&file, id) {
Ok(()) => match self.reload_catalogs() {
Ok(()) => self.flash_note(format!("Forgot {name}")),
Err(e) => self.error_modal.show(e),
},
Err(e) => self.error_modal.show(e.to_string()),
}
}
/// Before 0.4.0 Ctrl+D kept directories in the cache. Once, they move into
/// `catalog.toml`, where Ctrl+D keeps them now, and the cache's list goes.
fn move_remembered_places(&mut self) {
if std::mem::replace(&mut self.remembered_moved, true) {
return;
}
let places = self.cache.load_remembered_places();
if places.is_empty() {
return;
}
let Some(file) = self.mine_catalog_file() else {
return;
};
// The cache's list goes only once every place is in catalog.toml: a failure
// leaves it for the next run.
match catalog::move_places(&file, &places) {
Ok(_) => self.cache.clear_remembered_places(),
Err(e) => {
log::warn!(target: "datui", "moving remembered places into catalog.toml: {e:#}")
}
}
let dir = file.parent().map(Path::to_path_buf);
if let Err(e) = self.app_config.read_catalog_files(dir.as_deref()) {
log::warn!(target: "datui", "reading catalog.toml: {e:#}");
}
}
/// Info's Documentation tab for the open dataset: the catalog entry it is, or is
/// inside, with what the format spec that read it says; closed when neither says.
fn open_info_documentation(&mut self) {
self.info_documentation.close();
let spec = self.data_table_state.as_ref().and_then(|state| {
state
.format_read()
.map(|read| read.spec.clone())
.or_else(|| state.delimited_read().map(|read| read.spec.clone()))
});
let name = self
.path
.as_deref()
.and_then(Path::file_name)
.map(|n| n.to_string_lossy().into_owned())
.unwrap_or_default();
if let Some(doc) = widgets::documentation::Documented::new(
self.catalog_entry.clone(),
spec.and_then(|s| s.docs()).map(std::sync::Arc::new),
name,
) {
self.info_documentation.open(doc, None);
self.info_documentation.links_open = self.local_desktop;
}
}
/// Ctrl+E: the Documentation view of the row under the cursor: the catalog dataset
/// it is or is inside, and what the format spec that reads it says.
fn home_open_documentation(&mut self) {
let Some((path, doc)) = self.home_documented_row() else {
self.home.status =
Some("Ctrl+E shows what a catalog or a format spec says of a row".into());
return;
};
let measured = self
.home
.selected_entry()
.filter(|e| {
e.path == path
&& doc
.catalog
.as_ref()
.is_some_and(|(_, entry)| entry.location() == path)
})
.and_then(|e| e.size);
self.documentation.open(doc, measured);
self.documentation.links_open = self.local_desktop;
}
/// What Ctrl+E documents for the row under the cursor, with the row's path: the
/// catalog dataset it is, or is inside, and what the format spec that reads a file
/// says of it, when the spec documents anything.
pub(crate) fn home_documented_row(
&self,
) -> Option<(PathBuf, widgets::documentation::Documented)> {
let row = self.home.selected_row();
let (path, file) = match &row {
Some(home::Row::Entry { entry, .. }) | Some(home::Row::Door { entry, .. }) => {
(Some(entry.path.clone()), Some(*entry))
}
Some(home::Row::Place { path, .. }) => (Some(path.clone()), None),
Some(home::Row::Header { section, .. }) => (
self.home
.sections
.get(*section)
.and_then(|s| s.root.clone()),
None,
),
_ => (None, None),
};
let path = path?;
let catalog = home::catalog_entry_for(&self.home.catalogs, &path);
let spec = file
.filter(|e| e.kind == discover::EntryKind::File)
.and_then(|e| e.format_spec.as_deref())
.and_then(|name| self.home.formats.get(name))
.and_then(|spec| spec.docs())
.map(std::sync::Arc::new);
// A record type's row (`day.ord/add`) documents its file.
let name = file
.map(|e| {
let text = e.path.to_string_lossy();
text.strip_suffix(e.name.as_str())
.filter(|_| e.table.is_some())
.map(|file| file.trim_end_matches(std::path::is_separator))
.and_then(|file| Path::new(file).file_name())
.map_or_else(|| e.name.clone(), |n| n.to_string_lossy().into_owned())
})
.unwrap_or_default();
let doc = widgets::documentation::Documented::new(catalog, spec, name)?;
Some((path, doc))
}
/// What Ctrl+D does on the row under the cursor, as the footer names it: add it to
/// `catalog.toml`, or forget it from there; `None` on a row it cannot add.
/// Whether Delete on the selected row hides a catalog: the heading of the one
/// that comes with datui.
pub(crate) fn home_hides_catalog(&self) -> bool {
self.home
.selected_catalog()
.is_some_and(|c| c.origin == crate::catalog::Origin::Bundled)
}
pub(crate) fn home_catalog_action(&self) -> Option<&'static str> {
let (location, _) = self.home_row_for_catalog()?;
Some(if self.mine_entry_at(&location).is_some() {
"Forget"
} else {
"Add"
})
}
/// A key while the Documentation view is open over home.
fn documentation_key(&mut self, event: &KeyEvent) {
let page = self.documentation.view_height.max(1) as isize;
match event.code {
KeyCode::Esc | KeyCode::Char('q') | KeyCode::Left => self.documentation.close(),
KeyCode::Up | KeyCode::Char('k') => self.documentation.move_cursor(-1),
KeyCode::Down | KeyCode::Char('j') => self.documentation.move_cursor(1),
KeyCode::PageUp => self.documentation.move_cursor(-page),
KeyCode::PageDown => self.documentation.move_cursor(page),
KeyCode::Home | KeyCode::Char('g') => self.documentation.move_cursor(isize::MIN / 2),
KeyCode::End | KeyCode::Char('G') => self.documentation.move_cursor(isize::MAX / 2),
KeyCode::Enter | KeyCode::Char(' ') | KeyCode::Right => {
self.documentation.toggle_legend();
}
KeyCode::Char('y') => self.copy_documentation_line(),
KeyCode::Char('o') => {
let link = self.documentation.link();
self.ask_to_open_link(link);
}
// The view takes no text, so ? is help here, as at the table.
KeyCode::Char('?') => self.open_help_overlay(),
_ => {}
}
}
/// `y` in the Documentation view: the line's link or value, whole.
fn copy_documentation_line(&mut self) {
match self.documentation.copy_text() {
Some(text) => self.copy_documentation_text(text),
None => self.flash_note("Nothing to copy on this line".to_string()),
}
}
/// `o` on a Documentation page: ask, with the whole URL, before the browser
/// opens it. Nothing on a line without a link; a status line where no local
/// browser would show it, or the link is not http or https.
pub(crate) fn ask_to_open_link(&mut self, link: Option<String>) {
let Some(link) = link else {
return;
};
if !self.local_desktop {
self.flash_note("o opens links on a local desktop; y copies it".to_string());
return;
}
match link_open::checked_url(&link) {
Ok(url) => {
self.confirmation_modal
.show_choice(format!("Open {url}?"), "Open", "Cancel");
self.pending_link = Some(url);
}
Err(why) => self.flash_note(format!("Not opened: {why}; y copies it")),
}
}
/// Put a line of a Documentation page on the clipboard, whole.
pub(crate) fn copy_documentation_text(&mut self, text: String) {
let shown: String = text.chars().take(60).collect();
let said = if shown.len() < text.len() {
format!("Copied {shown}{}", glyphs::get().ellipsis)
} else {
format!("Copied {shown}")
};
self.finish_copy(clipboard::Payload::text(text), said);
}
/// Collapse or expand the section the cursor is in.
///
/// Collapsing moves the cursor to the header, so the section the user just folded
/// is what stays selected rather than whatever row happens to fall into place.
/// Fold or unfold the section whose header is highlighted.
fn home_toggle_fold(&mut self) {
if let Some(section) = self.home.selected_section() {
self.home.toggle_collapsed(section);
self.home.clamp_selection();
self.cache.save_folds(&self.home.folds);
}
}
fn home_collapse(&mut self, collapse: bool) {
// The listing browsed into is the whole screen. It never folds, and the fold
// must not be remembered for its path either — see `set_collapsed`.
if self.home.browsing.is_some() {
return;
}
let Some(section) = self.home.selected_section() else {
return;
};
if collapse && !self.home.is_collapsed(section) {
self.home.set_collapsed(section, true);
if let Some(idx) = self
.home
.visible()
.iter()
.position(|row| row.section() == section)
{
self.home.selected = idx;
}
} else if !collapse {
self.home.set_collapsed(section, false);
}
self.home.clamp_selection();
self.cache.save_folds(&self.home.folds);
}
/// Step out of a directory that was descended into.
fn home_ascend(&mut self) {
let Some(current) = self.home.browsing.clone() else {
return;
};
let parent = self.home.parent_of(¤t);
self.home_leave_browsing(parent);
}
/// Move the browse up to `to`, or back to the root listing when `None`.
fn home_leave_browsing(&mut self, to: Option<PathBuf>) {
// Whatever the last place said about itself, it said about that place. "these
// are the files under it" is wrong the moment "it" is somewhere else.
self.home.status = None;
let from = std::mem::replace(&mut self.home.browsing, to);
// Backspace can climb above where the browse began; the start follows, so a
// later Esc still has a place to stop.
if !self.home.below_browse_start() {
self.home.browse_start = self.home.browsing.clone();
}
// The filter, search and row the user left here, if they were here; the
// listing lands later, and the cursor goes back to that row when it does.
// Somewhere new, a search of the old place no longer answers the question.
self.home.come_back(from);
self.home.sync_search_section();
self.home.selected = 0;
self.home_refresh();
if !self.home.filter.is_empty() {
self.spawn_home_search();
}
}
/// Whether a peek's answer changes anything a row draws.
///
/// Every answer that tells a row something, not only the ones that change the kind:
/// a prefix of twelve CSV objects is a `Directory` — only Parquet is read in place —
/// and it is still `12 csv`, which is the count the row is labelled from.
///
/// An answer that says neither is replaced by "a directory, and nothing to say
/// about it" rather than dropped. The directory still has to come back — that is what
/// takes it out of `peeking` and holds the one-request-per-directory promise — and
/// once the request has been made, "nothing to say" is a real answer rather than
/// the claim it was when it was being written before the request. What this decides
/// is whether the peek's own words are kept.
///
/// "Says something" is `Holds::is_empty`, not the formats alone. The row is not the
/// only thing an answer reaches: the details pane draws the whole `holds` line, so a
/// prefix of a README and two PDFs has `3 not read` to report, and one of twelve
/// sub-prefixes has `12 directories`. Testing the formats dropped both, and the same
/// directories on disk said both things.
///
/// The cost is real and is the reason the distinction is kept: each batch rebuilds
/// the listing on the thread drawing the frame. `Holds::is_empty` is the line
/// because it is the same question the pane asks before drawing the line at all.
///
/// A `Holds` whose only field is `truncated` is let through too: it is what turns
/// a cloud row's `dir` into `dir+`.
#[cfg(feature = "cloud")]
fn peek_tells_a_row_something(answer: &(discover::EntryKind, discover::Holds)) -> bool {
answer.0 != discover::EntryKind::Directory || !answer.1.is_empty()
}
/// Look inside the cloud directories the cursor is on or near, so the ones that are
/// datasets say `hive` or `multi` and open as one. One small listing request per
/// directory, and each directory is peeked at once per session.
///
/// A directory the listing takes for `multi` costs a little more: up to three ranged
/// reads of a few kilobytes each, to ask the footers whether its files are really
/// one table. Nothing else reads an object, and nothing reads a whole one.
///
/// Driven by the cursor rather than by the listing. It used to take the first
/// forty-eight directories of each listing, once: a bucket of two hundred prefixes
/// had forty-eight labelled and the rest reading `dir` for the session however long
/// you spent on them, and paging straight past those forty-eight spent the requests
/// on rows nobody saw. The budget is the same shape as the local classify pass now —
/// what is on screen, a batch at a time, the highlighted row first.
#[cfg(feature = "cloud")]
fn peek_cloud_directories(&mut self) {
const PEEKS_AT_ONCE: usize = 4;
let directories = self.home.cloud_directories_to_peek(PEEKS_AT_ONCE);
if directories.is_empty() {
return;
}
// Out, not answered. A second pass before these land must not ask again, and an
// answer written here instead would be a claim — `dir` on a row that has a
// count, and "never again this session" staked on a request that may fail.
for directory in &directories {
self.home.peeking.insert(directory.clone());
}
let tx = self.events.clone();
let cloud = self.app_config.cloud.clone();
self.runtime.spawn(async move {
let permits = Arc::new(tokio::sync::Semaphore::new(PEEKS_AT_ONCE));
let mut peeks = tokio::task::JoinSet::new();
// By task, so a peek that panics is still sent back, as failed, and does
// not stay in `peeking` spinning for good.
let mut asked = std::collections::HashMap::new();
for directory in directories {
let (permits, cloud) = (permits.clone(), cloud.clone());
let task_directory = directory.clone();
let task = peeks.spawn(async move {
let directory = task_directory;
let _permit = permits.acquire_owned().await;
let kind =
crate::cloud_browse::peek_kind(&directory.to_string_lossy(), &cloud).await;
(directory, kind)
});
asked.insert(task.id(), directory);
}
// Sent a few at a time: the labels fill in as they are found, without a
// rebuild per directory.
//
// Every directory asked about is sent back, including the ones whose peek
// decided nothing and the ones whose request failed. That is what takes
// them out of `peeking` and what holds the one-request-per-directory promise
// — and an answer of "a directory, and nothing to say about it" is a real
// answer once the request has been made, which is what it was not while it
// was being written before the request.
let mut found = Vec::new();
let mut failed = Vec::new();
while let Some(joined) = peeks.join_next_with_id().await {
match joined {
Ok((_, (directory, Ok(answer)))) => {
let answer = Some(answer)
.filter(Self::peek_tells_a_row_something)
.unwrap_or((discover::EntryKind::Directory, Default::default()));
found.push((directory, answer));
}
Ok((_, (directory, Err(_)))) => failed.push(directory),
Err(error) => failed.extend(asked.remove(&error.id())),
}
if found.len() + failed.len() >= PEEKS_AT_ONCE {
let _ = tx.send(AppEvent::HomeCloudKinds {
kinds: std::mem::take(&mut found),
failed: std::mem::take(&mut failed),
});
}
}
if !found.is_empty() || !failed.is_empty() {
let _ = tx.send(AppEvent::HomeCloudKinds {
kinds: found,
failed,
});
}
});
}
/// Browse into a directory or bucket, local or remote.
fn home_browse_into(&mut self, path: PathBuf) {
self.home.leave_mark();
if self.home.browsing.is_none() {
self.home.browse_start = Some(path.clone());
} else if self.home.browse_start.is_none() {
self.home.browse_start = self.home.browsing.clone();
}
self.home.browsing = Some(path);
// "Below here" now means somewhere else. Whatever the last walk found
// describes a different place, and a fresh one starts on the next
// keystroke. The status line goes with them: "these are the files under it"
// is about wherever "it" was. A caller with something to say about the place
// it is going says it after this returns.
self.home.status = None;
self.home.search.reset();
self.home.filter.clear();
self.home.sync_search_section();
self.home.selected = 0;
self.home_refresh();
}
/// Whether the highlighted row is the `(all files)` row: the one that opens the
/// directory being browsed, and so is already inside it.
///
/// `Enter` on it opens the directory whatever the label says, and → on it would
/// descend into where it already is.
/// Asked of the row's variant rather than of a flag on the entry it carries: the
/// door is a `Row::Door` now, so this is one match instead of a clone.
fn selection_opens_the_whole_directory(&self) -> bool {
self.home.selection_is_the_door()
}
/// The highlighted row, when → goes inside it.
///
/// Every directory, whatever its label. A label describes what is directly inside; it
/// no longer decides what can be reached, so the exception list this used to carry —
/// hive, multi and the three lake markers — is gone, and with it the directories that
/// had no way in because datui did not recognize how they were stored. What is left
/// out is what is not a directory: a file, a section header, and the row that opens
/// the directory you are already in.
///
/// Local or remote. The split this used to carry — remote only — was never about
/// where the directory was: a cloud prefix simply could not be descended into until
/// there was a listing to descend with.
fn selected_directory_to_enter(&self) -> Option<PathBuf> {
// A place under `RECENT` is a directory to go inside, and → is one of its two
// doors. It has no entry to ask about, so it is answered before one is looked for.
if let Some(home::Row::Place { path, .. }) = self.home.selected_row() {
return home::place_is_browsable(&path).then_some(path);
}
let entry = self.home.selected_entry()?;
if self.selection_opens_the_whole_directory() || self.home.missing.contains(&entry.path) {
return None;
}
// A SQLite database lists its tables, however many it has.
if entry.cost.tables.is_some() {
return Some(entry.path);
}
(!matches!(
entry.kind,
discover::EntryKind::File | discover::EntryKind::Other
))
.then_some(entry.path)
}
/// Why a prefix in an object store cannot be read as one table, when it cannot.
///
/// Every cloud path is scanned as Parquet — the directory-format dispatch is local
/// only — so a prefix of anything else comes back "Could not read from S3. Check
/// credentials and URL", which is a false statement about a login that is fine.
/// What the prefix holds is already counted and on screen, so saying so costs no
/// request. #275 phase 4 is where these read.
///
/// `None` for a prefix that may yet be Parquet: one holding Parquet, and one
/// holding no data files at all, whose data may be a level down.
/// Why Enter on a bucket directory's `(all files)` row reads nothing, by the rule
/// Enter itself applies: a hive root or a directory of one table is read through
/// its files, and one with a reader for what it holds is read with that.
#[cfg(feature = "cloud")]
pub(crate) fn why_a_door_reads_nothing(entry: &discover::Entry) -> Option<String> {
if !home::is_object_store_url(&entry.path)
|| matches!(
entry.kind,
discover::EntryKind::Hive | discover::EntryKind::MultiFile
)
|| Self::cloud_prefix_format(&entry.holds).is_some()
{
return None;
}
Self::why_a_cloud_prefix_cannot_be_read(&entry.holds)
}
#[cfg(feature = "cloud")]
fn why_a_cloud_prefix_cannot_be_read(holds: &discover::Holds) -> Option<String> {
let reads_parquet =
|name: &str| crate::FileFormat::from_name(name) == Some(crate::FileFormat::Parquet);
if holds.formats.iter().any(|(name, _)| reads_parquet(name)) {
return None;
}
match holds.formats.as_slice() {
// Data files, none of them Parquet. `label()` says `mixed` for more than
// one format, which is a word rather than a count, so the line is spelled
// out from the formats themselves.
[] => {
// Nothing datui has a reader for. Only a refusal when there is also
// nothing below: a prefix of sub-prefixes may hold Parquet a level
// down, and nothing here has looked.
(holds.not_read > 0 && holds.directories == 0).then(|| {
"this prefix holds nothing datui can read — datui reads a directory in \
an object store as Parquet only."
.to_string()
})
}
formats => {
let held = formats
.iter()
.map(|(name, count)| format!("{count} {name}"))
.collect::<Vec<_>>()
.join(", ");
Some(format!(
"this prefix holds {held} — datui reads a directory in an object store \
as Parquet only. Open one of the files below instead."
))
}
}
}
/// The reader a prefix in an object store calls for, from what its listing counted.
///
/// The commonest format, which is the same rule a directory on disk follows — and
/// `rank_formats` is the same order, so a prefix and the directory it mirrors pick
/// the same reader. `None` when nothing there has a multi-file reader, which is where
/// the refusal that names what is there belongs.
///
/// Parquet included and returned as itself: the cloud branches compare against it
/// and take their own path, which is the one every cloud dataset took before any of
/// this, and the only one with hive partitioning behind it.
#[cfg(feature = "cloud")]
fn cloud_prefix_format(
holds: &discover::Holds,
) -> Option<(FileFormat, Vec<(FileFormat, usize)>)> {
// A saved DatasetDict: its splits are Arrow, read one at a time.
if holds.dataset_dict {
return Some((FileFormat::Arrow, Vec::new()));
}
// A model's weights beside its config and tokenizer JSON: the prefix is the
// model, as a directory on disk is, and the JSON is not data passed over.
if let Some((name, _)) = holds.model_weights() {
return FileFormat::from_name(name).map(|format| (format, Vec::new()));
}
let (name, _) = holds.formats.first()?;
// A GPS log is read whole from disk; a bucket's logs are opened one at a time,
// as its text files are.
let format = FileFormat::from_name(name)
.filter(|f| f.reads_many_files() && !f.reads_into() && !f.is_lines())?;
// And what taking the commonest passes over. The local read reports its own —
// it is the pass that decides — but here Polars does the listing and never sees
// the other formats, so the note has to be written from the listing on screen.
let left_out = holds
.formats
.iter()
.skip(1)
.filter_map(|(name, n)| FileFormat::from_name(name).map(|f| (f, *n)))
.collect();
Some((format, left_out))
}
/// Open the highlighted entry: toggle a section, descend into a directory, or
/// load a dataset.
fn home_open_selected(&mut self) -> Option<AppEvent> {
match self.home.selected_row() {
// Into the directory or prefix the recents under it live in: the way back
// to a place found by hand, now that recents no longer make roots.
Some(home::Row::Place { path, .. }) => {
if home::place_is_browsable(&path) {
self.home_browse_into(path);
} else {
self.home.status = Some(
"An HTTP server has no listing to browse. Open a file under it".into(),
);
}
return None;
}
// The rest of `RECENT`, for the session.
Some(home::Row::More { .. }) => {
self.home.recent_expanded = true;
return None;
}
// What Ctrl+A shows. The cursor goes to the first of them, where the row
// that stood for them was.
Some(home::Row::Hidden { .. }) => {
self.home.hide_unreadable = false;
if let Some(idx) = self.home.visible().iter().position(|row| {
matches!(row, home::Row::Entry { entry, .. }
if entry.hidden_by_default())
}) {
self.home.selected = idx;
}
return None;
}
_ => {}
}
if self.home.selection_is_header() {
self.home_toggle_fold();
return None;
}
let entry = self.home.selected_entry()?;
// A collection's local dataset that is not there: said here, where it was named.
if self.home.missing.contains(&entry.path) {
self.home.status = Some(format!(
"{} does not exist",
home::display_path(&entry.path)
));
return None;
}
// A place a collection suggests is a starting point: Enter opens it as one
// table rather than stepping inside. → still goes in.
if entry.kind != discover::EntryKind::File && self.home.bookmark(&entry.path).is_some() {
#[cfg(feature = "cloud")]
let reader = if home::is_object_store_url(&entry.path)
&& !matches!(
entry.kind,
discover::EntryKind::Hive | discover::EntryKind::MultiFile
) {
Self::cloud_prefix_format(&entry.holds)
} else {
None
};
#[cfg(not(feature = "cloud"))]
let reader = None;
let directory = home::directory_dataset_url(&entry.path);
return Some(self.home_open_directory_as(directory, true, None, reader));
}
// The `(all files)` row opens the directory it names, whatever the directory is
// labelled. That is the whole of what it is for: the label describes, and this
// row is the promise that the description cannot lock you out. Sent straight to
// the open, because `open_what_it_is` would read the label back and send a
// `dir` row inside the directory it is already in.
if self.selection_opens_the_whole_directory() {
// A lake table is not a directory of Parquet files however much it looks like
// one: reading it as one counts tombstoned rows, every rewritten version
// and both sides of a compaction. So the read is labelled rather than
// refused. Refusing it left a directory the user could see and could not read
// at all — this row is the promise that no label locks you out, and a
// refusal here is that promise broken on the one directory that needed it.
// Until datui reads the log, its files are what there is, and what makes
// that honest is that nothing about it is silent: a note in the panel, a
// chip in the control bar, and `Enter` on the row one level up still goes
// inside and says datui does not read the table itself yet.
let lake = entry.kind.lake_name();
// A prefix in an object store used to be scanned as Parquet whatever was
// in it — every cloud path returns before the directory-format dispatch is
// reached — so a prefix of CSV answered "Could not read from S3. Check
// credentials and URL", a false statement about the user's login. What the
// prefix holds was counted by the listing and is on screen, so the reader
// is picked from it, which costs no request. Only a prefix the listing
// already calls a dataset is left alone: a hive root is read through its
// partitions, and one stray `manifest.csv` beside them is not what it
// holds — but it is the only thing in `formats`.
#[cfg(feature = "cloud")]
let reader = if home::is_object_store_url(&entry.path)
&& !matches!(
entry.kind,
discover::EntryKind::Hive | discover::EntryKind::MultiFile
) {
let reader = Self::cloud_prefix_format(&entry.holds);
// Nothing here datui has a reader for. The listing is on screen, so the
// refusal names what is there rather than blaming the connection.
if reader.is_none()
&& let Some(what) = Self::why_a_cloud_prefix_cannot_be_read(&entry.holds)
{
self.home.status = Some(what);
return None;
}
reader
} else {
None
};
#[cfg(not(feature = "cloud"))]
let reader = None;
// `hive: true` says read this as one, which is the whole of what the row
// promises — it is also what carries partition columns through, for a
// directory the dispatch sends down the hive route. The cloud route returns
// before the dispatch is reached.
let directory = home::directory_dataset_url(&entry.path);
return Some(self.home_open_directory_as(directory, true, lake, reader));
}
// A row nothing has looked at is looked at before it is opened, rather than
// opened as whatever it turns out to be. `EntryKind::Unknown` is offered as
// openable, so without this a lake root reached this way is read as one table:
// #237 through the door #249 leaves open.
let mut entry = entry;
if entry.kind == discover::EntryKind::Unknown {
if self.looking_could_block(&entry.path) {
return Some(AppEvent::ClassifyThenOpen {
path: entry.path,
jump: false,
});
}
if entry.path.is_dir() {
entry.kind = discover::classify_directory(&entry.path);
}
}
// A database of several tables lists them rather than opening; one not yet
// measured is opened, and the open lands on its tables the same way.
if entry.enter_lists_tables() {
self.home_browse_into(entry.path);
return None;
}
self.open_what_it_is(entry.path, entry.kind, false)
}
/// Whether finding out what a path is could sit on a mount that never answers.
///
/// Two halves. An object-store or HTTP URL names something no mount is responsible
/// for — what is behind it is the scan's business, and stat'ing it only ever asks the
/// working directory about a file called `s3:` — and an ordinary local path answers at
/// once, so making the user wait a round trip for it would be a delay bought with
/// nothing.
///
/// What is left is a path on a mount the home screen calls a network one, which is
/// the case `is_remote_path` exists to name and the only one worth a worker.
fn looking_could_block(&self, path: &Path) -> bool {
// `cloud://<id>` is a place, not a path: `input_source` calls the unknown scheme
// local and `is_remote_path` calls it remote, so without this a worker would be
// sent to stat it and come back with "No such path".
!home::is_cloud_place(path)
&& matches!(source::input_source(path), source::InputSource::Local(_))
&& (self.home.network_check)(path)
}
/// Do with a path whatever its kind calls for: browse into it, say it is a lake
/// table, or open it.
///
/// `jump` is a path typed at `~` rather than a row already listed, which starts a new
/// browse so Esc comes back from there to the listing.
fn open_what_it_is(
&mut self,
path: PathBuf,
kind: discover::EntryKind,
jump: bool,
) -> Option<AppEvent> {
let go_inside = |app: &mut Self, path: PathBuf| {
if jump {
app.home_jump_into(path);
} else {
app.home_browse_into(path);
}
};
if kind == discover::EntryKind::Directory {
go_inside(self, path);
return None;
}
// No reader: a local file's bytes, in the hex view. A remote one is dimmed and
// its details pane says why.
if kind == discover::EntryKind::Other {
if matches!(source::input_source(&path), source::InputSource::Local(_)) {
self.open_hex(path, crate::hex_view::Origin::Home, true, None);
}
return None;
}
// A lake table's files are not its rows: the ones a delete or an update
// tombstoned are still on disk, every rewritten version is here together, and
// compaction leaves both sides in place. Going inside is what datui can honestly
// do with one, and saying so is better than a silent wrong answer.
if let Some(format) = kind.lake_name() {
self.home.lake_here = Some((path.clone(), format));
go_inside(self, path);
return None;
}
let directory = matches!(
kind,
discover::EntryKind::Hive | discover::EntryKind::MultiFile
);
// A directory typed at `~` is a place to go, as → makes it, whatever it holds:
// its door is one row in, and naming a directory never starts a read of all of it.
if directory && jump {
go_inside(self, path);
return None;
}
// A cloud directory that is a dataset opens as one: its URL as a prefix, which is
// what makes the open a scan of every file under it.
if directory && home::is_object_store_url(&path) {
// A prefix, not a directory: the scan is what walks it.
return Some(self.home_open_path(home::directory_dataset_url(&path), false));
}
// Said here, where the file was named, rather than after a download and a load
// that could only end the same way. A row would be dimmed; a typed path has no
// row, so the line says it.
// A format spec may read it: by its glob, or by magic the open looks for.
let a_spec_may_read = !self.formats.by_glob(&path, false).is_empty()
|| self.formats.specs.iter().any(|f| !f.spec.magic.is_empty());
// A table inside a file of tables (`flight.ulg/sensor_accel.1`) has the file's
// name in front, and a log found by its first bytes (`00000042.BIN`) a name
// that says nothing.
if kind == discover::EntryKind::File
&& discover::unreadable_by_name(&path)
&& !a_spec_may_read
&& crate::members::split(&path).is_none()
&& crate::members::holder(&path).is_none()
&& crate::members::split_variant(&path, &self.formats).is_none()
&& crate::hf_splits::split_place(&path).is_none()
{
self.home.status = Some(discover::NO_READER.to_string());
return None;
}
// The preview read this file's first page through the open's own steps: the
// open installs that dataset rather than reading it again.
let prepared = (!directory)
.then(|| self.home_previews.take_prepared(&path))
.flatten();
// A small file of the built-in catalog is fetched without a question: the row
// already said what it is and what it weighs. A URL the user typed still asks.
let unasked = self
.home
.catalogs
.iter()
.filter(|c| c.origin == catalog::Origin::Bundled)
.flat_map(|c| c.datasets.iter())
.find(|d| d.location == path)
.filter(|_| {
!jump && matches!(source::input_source(&path), source::InputSource::Http(_))
})
.map(|dataset| UnaskedDownload {
limit: UnaskedDownload::LIMIT,
listed: dataset.size,
});
match self.home_open_path(path, directory) {
AppEvent::Open(paths, mut options) => {
options.prepared = prepared.map(|p| Arc::new(Mutex::new(Some(p))));
options.download_unasked = unasked;
Some(AppEvent::Open(paths, options))
}
event => Some(event),
}
}
/// What `datui <path>` does with a directory: the same rule as `Enter` on its row.
///
/// A directory used to be `Unsupported file type` unless `--hive` was passed, while
/// pyarrow, Polars, pandas and Spark all open one. Naming a directory *is* the
/// request to read it, so the three doors onto a path — the highlighted row, the `~`
/// prompt and the command line — now answer the same: a hive root or a directory
/// whose files are one table opens as one table, and a directory that is a place to
/// look inside opens the home screen browsed into it, one keystroke from either file
/// or union.
///
/// The directory is looked into rather than guessed at, because that is what the
/// rule is: [`home::look_into`] is the same call the home screen's background pass
/// makes, footers and all.
///
/// `--hive` is untouched. It names a glob or forces partition columns, and it is
/// still the only way to say "read this as partitioned" about something whose
/// layout does not say so itself.
///
/// Asks the filesystem whether a local path is a directory, so `run` calls it on a
/// worker ([`AppEvent::OpenNamed`]). Returns the event that carries the open on:
/// `LookThenOpenDirectory` or `Open`.
pub fn route_named_paths(paths: Vec<PathBuf>, options: OpenOptions) -> AppEvent {
Self::route_named_paths_with(paths, options, &crate::formats::Registry::default())
}
/// [`Self::route_named_paths`], with the format specs on the search path: a
/// directory a spec reads as column files is opened, not looked at.
pub fn route_named_paths_with(
paths: Vec<PathBuf>,
options: OpenOptions,
formats: &crate::formats::Registry,
) -> AppEvent {
if let Some(event) = Self::route_named_without_looking(&paths, &options) {
return event;
}
// Several paths are a list of files to read together, and `--hive` is an answer
// already given. Neither is a question about what one directory is.
let single = (paths.len() == 1 && !options.hive).then(|| paths[0].clone());
let Some(dir) = single.filter(|p| p.is_dir()) else {
return AppEvent::Open(paths, options);
};
// A format spec named for it, or one whose glob names it, reads it as columns.
if options.spec_file.is_some()
|| options.spec_name.is_some()
|| !formats.by_glob(&dir, true).is_empty()
{
return AppEvent::Open(paths, options);
}
// Looking at a directory reads its footers, or the front of a spread of its
// files. For a directory of large Parquet that is seconds — 4.6 of them on a real
// one — so it goes to a worker, and the answer comes back as an event like every
// other read.
AppEvent::LookThenOpenDirectory(dir, options)
}
/// The part of [`Self::route_named_paths`] that needs no filesystem: a cloud
/// directory is looked at too, by one page of its listing — what is in it picks the
/// reader, as it does for the `(all files)` row. Scanned blind, it was read as
/// Parquet whatever it held. A glob, a file name or `--format` already says what to
/// read.
fn route_named_without_looking(paths: &[PathBuf], options: &OpenOptions) -> Option<AppEvent> {
#[cfg(feature = "cloud")]
if let [dir] = paths
&& !options.hive
&& home::is_object_store_url(dir)
&& options.format.is_none()
&& !dir.to_string_lossy().contains('*')
&& !home::names_a_file(dir)
{
return Some(AppEvent::LookThenOpenDirectory(
dir.clone(),
options.clone(),
));
}
let _ = (paths, options);
None
}
/// The first named local path that is not there. A URL or a glob is left to the
/// open, which says what it found, and standard input is no path.
pub fn missing_named_path(
paths: &[PathBuf],
formats: &crate::formats::Registry,
) -> Option<PathBuf> {
paths
.iter()
.find(|path| {
!source::is_remote_url(path)
&& !crate::stdin::is_stdin(path)
&& !source::expands_as_glob(path)
&& !path.exists()
&& crate::members::split(path).is_none()
&& crate::members::split_variant(path, formats).is_none()
})
.cloned()
}
/// Act on what the look at a directory named on the command line found.
///
/// The other half of [`Self::route_named_paths`], which is
/// where the reasoning for the rule itself is.
fn open_the_directory_looked_at(
&mut self,
dir: PathBuf,
kind: discover::EntryKind,
holds: Option<&discover::Holds>,
mut options: OpenOptions,
) -> Option<AppEvent> {
#[cfg(feature = "cloud")]
if home::is_object_store_url(&dir) {
return self.open_the_cloud_directory_looked_at(dir, kind, holds, options);
}
let _ = holds;
// No override for the user's reader settings here, and none needed: the look
// read every file the way this open will, so `--no-header` and the skips have
// already been accounted for by the rule rather than around it. Overriding
// instead took three goes to get wrong in three different ways — it fired on
// config values, it fired on directories with nothing readable in them, and it
// fired on Parquet, which no CSV setting can affect.
//
// A lake table's files are not its rows, so the home screen is opened on it and
// says why — the same sentence the row gives, because it is the same refusal.
if let Some(format) = kind.lake_name() {
self.enter_home();
self.home.lake_here = Some((dir.clone(), format));
self.home_jump_into(dir);
return None;
}
// One table: read it. `hive` is what puts the open on the directory route, where
// what the directory holds picks the reader.
if matches!(
kind,
discover::EntryKind::Hive | discover::EntryKind::MultiFile
) {
options.hive = true;
self.set_loading_phase("Scanning input", 10);
self.name_what_is_loading(dir.clone());
return Some(AppEvent::Open(vec![dir], options));
}
// A place to look inside. `datui .` is this, and so is a directory of separate
// tables — where the `(all files)` row inside is the one keystroke that unions
// them anyway.
self.enter_home();
self.home_jump_into(dir);
None
}
/// As [`Self::open_the_directory_looked_at`], for a cloud directory: what `Enter`
/// on its `(all files)` row does, or a browse into it when there is no data
/// directly inside to read.
#[cfg(feature = "cloud")]
fn open_the_cloud_directory_looked_at(
&mut self,
dir: PathBuf,
kind: discover::EntryKind,
holds: Option<&discover::Holds>,
options: OpenOptions,
) -> Option<AppEvent> {
let open = |app: &mut Self, path: PathBuf, options: OpenOptions| {
app.set_loading_phase("Scanning input", 10);
app.name_what_is_loading(path.clone());
Some(AppEvent::Open(vec![path], options))
};
// The listing was refused. The open says why, in the words of whatever
// refused it, which is what happened before anything looked.
let Some(holds) = holds else {
return open(self, dir, options);
};
if let Some(format) = kind.lake_name() {
self.enter_home();
self.home.lake_here = Some((dir.clone(), format));
self.home_jump_into(dir);
return None;
}
let directory = home::directory_dataset_url(&dir);
if matches!(
kind,
discover::EntryKind::Hive | discover::EntryKind::MultiFile
) {
let options = OpenOptions {
hive: true,
..options
};
return open(self, directory, options);
}
if let Some((format, left_out)) = Self::cloud_prefix_format(holds) {
let options = OpenOptions {
hive: true,
format: Some(format),
left_out,
..options
};
return open(self, directory, options);
}
// Only directories, or nothing datui reads: somewhere to look inside, with
// the reason when there is one.
self.enter_home();
self.home_jump_into(dir);
self.home.status = Self::why_a_cloud_prefix_cannot_be_read(holds);
None
}
/// Browse into `path` as a jump, from wherever the user was.
///
/// Unlike `home_browse_into`, the browse *starts* here: Esc comes back from here to
/// the listing rather than up through whatever the path happens to sit under.
fn home_jump_into(&mut self, path: PathBuf) {
// A new browse: Esc comes back from here to the listing, so only the listing's
// mark is still a way back.
self.home.trail.retain(|mark| mark.place.is_none());
if self.home.browsing.is_none() {
self.home.leave_mark();
}
self.home.browse_start = Some(path.clone());
self.home.browsing = Some(path);
self.home.status = None;
self.home.search.reset();
self.home.filter.clear();
self.home.sync_search_section();
self.home.selected = 0;
self.home_refresh();
}
/// What an open the home screen starts reads with: the config's read and CSV
/// settings, as an open named on the command line has them under its flags.
fn open_defaults(&self) -> OpenOptions {
match crate::cli::parse_args(["datui"]) {
Ok(args) => OpenOptions::from_args_and_config(&args, &self.app_config),
Err(_) => OpenOptions::default(),
}
}
/// Load a path from the home screen.
///
/// The recent entry is recorded by the `Open` handler, which every open goes
/// through, so this does not record one itself.
fn home_open_path(&mut self, path: PathBuf, hive: bool) -> AppEvent {
self.home_open_directory(path, hive, None)
}
/// As [`Self::home_open_path`], and carrying whether the directory being read is a
/// lake table whose plain files this read is, so the dataset can say so.
fn home_open_directory(
&mut self,
path: PathBuf,
hive: bool,
lake: Option<&'static str>,
) -> AppEvent {
self.home_open_directory_as(path, hive, lake, None)
}
/// As [`Self::home_open_directory`], naming the reader to use.
///
/// For a prefix in an object store, where nothing downstream reads the listing: the
/// cloud branches scan before the directory-format dispatch is reached, so the format
/// the listing counted has to travel with the open or the scan falls back to
/// Parquet, which is what it always did.
fn home_open_directory_as(
&mut self,
path: PathBuf,
hive: bool,
lake: Option<&'static str>,
reader: Option<(FileFormat, Vec<(FileFormat, usize)>)>,
) -> AppEvent {
let (format, left_out) = match reader {
Some((format, left_out)) => (Some(format), left_out),
None => (None, Vec::new()),
};
// A directory of partitions is only meaningful read as one hive dataset. Told
// rather than stat'ed: the caller already knows what this is, and on a share that
// has gone away a `stat` here would freeze the thread reading the keys — the same
// reason the size below is left to the `Open` handler.
let options = OpenOptions {
hive,
read_as_plain_files_of: lake,
format,
left_out,
..self.open_defaults()
};
self.input_mode = InputMode::Normal;
// Chosen here, so a failure is reported here.
self.announce_open(true, "Scanning input".to_string(), 10);
// A frame is drawn between this keypress and the `Open` that carries it out,
// and it is the one the user is looking at when they press Enter — so it says
// which file, not just that something is happening. `Open` fills in the size a
// frame later; stat'ing here would put a possibly-dead mount on this thread.
self.name_what_is_loading(path.clone());
AppEvent::Open(vec![path], options)
}
/// Key handling for the home screen.
fn home_key(&mut self, event: &KeyEvent) -> Option<AppEvent> {
let ctrl = event.modifiers.contains(KeyModifiers::CONTROL);
// The line beside the prompt answers the last key, and this one replaces it: a
// key with something to say sets it again below. Left up, "Forgot laps.parquet"
// stayed until the next time the listing changed.
self.home.status = None;
if self.documentation.is_open() {
self.documentation_key(event);
return None;
}
// The home screen puts every plain character into the filter — `q` has to
// type a `q`, or you could never search for "quarterly". Quitting is Ctrl+C,
// handled before this is reached, and Esc once there is no context left to
// back out of.
if self.home.path_input_active {
match event.code {
KeyCode::Esc => {
self.home.path_input_active = false;
self.home.path_input.clear();
self.home.path_listing = None;
self.home.path_pick = None;
self.home.status = None;
}
// The list under the prompt is the directory being typed: ↑↓ pick a
// name in it, which Enter and Tab then take.
KeyCode::Up | KeyCode::Down => {
let n = self.home.path_candidates().len();
self.home.path_pick = match (event.code, self.home.path_pick) {
_ if n == 0 => None,
(KeyCode::Down, None) => Some(0),
(KeyCode::Down, Some(i)) => Some((i + 1).min(n - 1)),
(KeyCode::Up, Some(0)) | (KeyCode::Up, None) => None,
(KeyCode::Up, Some(i)) => Some(i - 1),
(_, pick) => pick,
};
}
KeyCode::Enter => {
if let Some(picked) = self.home.picked_path() {
self.home.path_input = picked;
self.home.path_pick = None;
}
let raw = self.home.path_input.trim().to_string();
if raw.is_empty() {
self.home.path_input_active = false;
return None;
}
let path = home::expand_user_path(&raw);
// A URL is not stat'ed: `exists` asks the working directory about a
// file called `gs:`. Its name decides, as it does for a recent — a
// file opens, and anything else in a bucket is browsed, where the
// listing says what is there.
if home::is_object_store_url(&path) || home::is_cloud_place(&path) {
self.home.path_input.clear();
self.home.path_input_active = false;
let kind = if home::names_a_file(&path) {
discover::EntryKind::File
} else {
discover::EntryKind::Directory
};
return self.open_what_it_is(path, kind, true);
}
// And an HTTP URL is one file.
if !matches!(source::input_source(&path), source::InputSource::Local(_)) {
self.home.path_input.clear();
self.home.path_input_active = false;
return self.open_what_it_is(path, discover::EntryKind::File, true);
}
// Whether it is there, whether it is a directory and what kind of one
// are three filesystem calls, and a typed path is exactly where a
// dead mount gets named. All three go to a worker when the mount is
// one that might not answer.
if self.looking_could_block(&path) {
self.home.path_input.clear();
self.home.path_input_active = false;
return Some(AppEvent::ClassifyThenOpen { path, jump: true });
}
// Before the prompt closes: a typo is worth fixing where it was
// typed, rather than retyping the whole path.
if !path.exists()
&& crate::members::split(&path).is_none()
&& crate::members::split_variant(&path, &self.formats).is_none()
{
self.home.status = Some(format!("No such path: {}", path.display()));
return None;
}
self.home.path_input.clear();
self.home.path_input_active = false;
let kind = if path.is_dir() {
discover::classify_directory(&path)
} else {
discover::EntryKind::File
};
return self.open_what_it_is(path, kind, true);
}
KeyCode::Backspace => {
self.home.path_input.pop();
self.home.status = None;
}
KeyCode::Char('u') if ctrl => self.home.path_input.clear(),
// What the names listed agree on, as a shell completes, or a name
// picked further down with ↓. Before the listing is in, completion
// reads the directory on a worker.
KeyCode::Tab => {
let completed = match self.home.path_pick {
Some(i) if i > 0 => self.home.picked_path(),
_ => self.home.path_completion(),
};
let listed = self
.home
.path_listing
.as_ref()
.is_some_and(|l| l.dir == home::typed_dir(&self.home.path_input));
match completed {
Some(completed) => self.home.path_input = completed,
None if !listed => self.request_path_completion(),
None => {}
}
}
KeyCode::Char(c) if !ctrl => {
self.home.path_input.push(c);
self.home.status = None;
}
_ => {}
}
// Whatever changed what is typed puts the pick back on the first name
// that matches, and a new directory is listed.
self.list_the_typed_directory();
if !matches!(event.code, KeyCode::Up | KeyCode::Down) {
self.home.pick_first_path();
}
return None;
}
// A filter kept from before is selected: a character, Backspace or Delete
// replaces it, as a selection in any field; any other key keeps it.
if std::mem::take(&mut self.home.filter_selected) {
let replaces = match event.code {
KeyCode::Char(_) => !ctrl,
KeyCode::Backspace => true,
_ => false,
};
if replaces {
self.home.filter.clear();
self.home.sync_search_section();
self.home.select_first_entry();
if event.code == KeyCode::Backspace {
return None;
}
}
}
// Every plain character types into the filter, so no letter or bracket is
// a key here: typing "json" must not move the cursor on the "j". Navigation
// is the arrows and the Ctrl chords, which cannot be part of a name.
match event.code {
KeyCode::Esc => return self.home_escape(),
KeyCode::Enter => return self.home_open_selected(),
// Section to section, past however many rows the current one holds.
KeyCode::Down if ctrl => self.home.jump_section(1),
KeyCode::Up if ctrl => self.home.jump_section(-1),
KeyCode::Up => self.home.move_selection(-1),
KeyCode::Down => self.home.move_selection(1),
KeyCode::Char('n') if ctrl => self.home.move_selection(1),
KeyCode::Char('p') if ctrl => self.home.move_selection(-1),
// Left/right fold the section the cursor is in, wherever in it the cursor
// happens to be — so collapsing does not require first finding the header.
// Tab cycles the sort. Every plain key goes into the filter, so an
// ordinary letter is not available for this.
KeyCode::Tab => {
self.home.sort = self.home.sort.next();
self.home.select_first_entry();
}
KeyCode::Left => self.home_collapse(true),
KeyCode::Right => match self.selected_directory_to_enter() {
// Into a directory that opens as one dataset rather than opening it, to
// reach one partition or one file. This clears the filter, as browsing
// anywhere does.
Some(directory) => {
// The heading Enter leaves, for the same reason: this is the door
// the control bar advertises on a lake row, and arriving inside one
// with no explanation is the silent wrong answer #237 is about.
if let Some(format) = self
.home
.selected_entry()
.and_then(|entry| entry.kind.lake_name())
{
self.home.lake_here = Some((directory.clone(), format));
}
self.home_browse_into(directory);
}
None => self.home_collapse(false),
},
// A screenful, matching the table; the renderer keeps view_height current.
KeyCode::PageUp => {
let page = self.home.view_height.max(1) as isize;
self.home.page_selection(-page);
}
KeyCode::PageDown => {
let page = self.home.view_height.max(1) as isize;
self.home.page_selection(page);
}
KeyCode::Home => self.home.page_selection(isize::MIN),
KeyCode::End => self.home.page_selection(isize::MAX),
KeyCode::Char('u') if ctrl => {
self.home.filter.clear();
self.home.sync_search_section();
self.home.select_first_entry();
#[cfg(feature = "cloud")]
self.narrow_cloud_listing();
}
KeyCode::Char('r') if ctrl => self.home_reload(),
// A browser's bookmark key: add the row to catalog.toml, or forget it.
KeyCode::Char('d') if ctrl => self.home_toggle_catalog(),
KeyCode::Char('e') if ctrl => self.home_open_documentation(),
// Any local file's bytes, whatever datui would read it as.
KeyCode::Char('x') if ctrl => {
let local = |path: &Path| {
matches!(source::input_source(path), source::InputSource::Local(_))
};
match self.home.selected_entry() {
Some(entry)
if !self.home.selection_is_the_door()
&& entry.table.is_none()
&& matches!(
entry.kind,
discover::EntryKind::File
| discover::EntryKind::Other
| discover::EntryKind::Unknown
)
&& local(&entry.path) =>
{
self.open_hex(entry.path, crate::hex_view::Origin::Home, false, None);
}
_ => self.flash_note("Ctrl+X shows a local file's bytes".to_string()),
}
}
KeyCode::Char('a') if ctrl => {
let on = self.home.selected_key();
self.home.hide_unreadable = !self.home.hide_unreadable;
// Inside a database, what is hidden is its own tables.
let tables = self
.home
.sections
.iter()
.any(|section| section.rows.iter().any(|row| row.table.is_some()));
self.flash_note(
match (self.home.hide_unreadable, tables) {
(true, true) => "Hiding internal tables",
(false, true) => "Showing internal tables",
(true, false) => "Hiding files with no reader",
(false, false) => "Showing files with no reader",
}
.to_string(),
);
// The same row where it is still there; the cursor stays put otherwise.
self.home.reselect(on);
}
KeyCode::Backspace => {
if self.home.filter.is_empty() {
self.home_ascend();
} else {
self.home.filter.pop();
self.home.sync_search_section();
self.home.select_first_entry();
#[cfg(feature = "cloud")]
self.narrow_cloud_listing();
}
}
// Forget the highlighted entry. Only meaningful in Recent — elsewhere the
// row is a real directory listing, and datui does not delete files.
// Shift+Delete forgets the lot. It sits next to the key that forgets
// one, so it asks first — an accidental press should not silently throw
// away every place the user has been.
KeyCode::Delete if event.modifiers.contains(KeyModifiers::SHIFT) => {
let count = self.cache.load_recents().len();
if count == 0 {
self.home.status = Some("Nothing to forget".into());
} else {
self.pending_clear_recents = true;
self.confirmation_modal
.show(format!("Forget all {count} recently opened datasets?"));
}
}
KeyCode::Delete => self.home_forget_selected(),
KeyCode::Char('~') if self.home.filter.is_empty() => {
self.home.path_input_active = true;
self.home.status = None;
self.home.path_listing = None;
self.home.path_pick = None;
self.list_the_typed_directory();
self.home.pick_first_path();
}
// The one printable that is a key, and only before typing starts: a
// filter beginning with a literal `?` matches nothing anyway, and this
// is where a new user asks for the keys. F1 opens help mid-filter.
KeyCode::Char('?') if self.home.filter.is_empty() && !ctrl => {
self.open_help_overlay();
}
// Space before typing starts folds a header, as Enter does, and is otherwise
// nothing: a filter of one space is invisible at the prompt and matched every
// name with a space in it, below the working directory too.
KeyCode::Char(' ') if self.home.filter.is_empty() && !ctrl => {
if self.home.selection_is_header() {
self.home_toggle_fold();
}
}
KeyCode::Char(c) if !ctrl => {
self.home.filter.push(c);
// Typing is what asks for the recursive search. Starting it here and
// not on open means the walk is only ever paid for by someone who is
// actually looking for something.
self.spawn_home_search();
self.home.sync_search_section();
self.home.select_first_entry();
#[cfg(feature = "cloud")]
self.narrow_cloud_listing();
}
_ => {}
}
None
}
/// Get a color from the theme by name
fn color(&self, name: &str) -> Color {
self.theme.get(name)
}
/// The export format to offer by default for a dataset opened from `path`.
///
/// The format the open read wins (`format`: what it sniffed, or `--format`), then
/// the extension. A compressed CSV keeps its CSV identity: `sales.csv.gz` has
/// extension `gz`, and the `.csv` that matters is in the stem, so reading the
/// extension alone offered no default at all.
fn export_format_for(path: &Path, format: Option<FileFormat>) -> Option<ExportFormat> {
format
.or_else(|| FileFormat::from_path(path))
.or_else(|| {
CompressionFormat::from_extension(path)
.and(path.file_stem())
.and_then(|stem| FileFormat::from_path(Path::new(stem)))
})
.and_then(crate::readers::export_default)
}
/// `options` for the compressed delimited file `file`, in the dialect of the
/// delimited spec it matches, or as they are when it matches none. The loader sends
/// such a file straight to be decompressed, past the scan that matches the others.
fn with_delimited_spec(
file: &Path,
mut options: OpenOptions,
formats: &crate::formats::Registry,
) -> Result<OpenOptions> {
if options.delimited.is_some() {
return Ok(options);
}
let asked = crate::formats::Asked {
spec_file: options.spec_file.clone(),
spec: options.spec_fetched.clone(),
spec_name: options.spec_name.clone(),
compression: options.compression,
..Default::default()
};
let crate::formats::Route::Delimited(choice) =
crate::formats::route(file, &asked, formats).map_err(|e| color_eyre::eyre::eyre!(e))?
else {
return Ok(options);
};
let Some(delimited) = choice.spec.delimited.clone() else {
return Ok(options);
};
delimited.apply(&mut options);
let chosen =
crate::delimited_spec::DelimitedRead::chosen(choice.spec, choice.by, choice.also);
let read = crate::delimited_spec::read_facts(&chosen, &[file.to_path_buf()], &options)?;
options.delimited = Some(Arc::new(read));
Ok(options)
}
/// Read a compressed CSV, TSV or PSV into a table state, split on its format's
/// separator.
///
/// This is the one input datui cannot scan lazily: the file has to be
/// decompressed and parsed before anything can be shown, which for a large export
/// is minutes. It takes no `&self` so it can run on a background thread.
fn decompressed_delimited_state(
path: &Path,
options: &OpenOptions,
writer: &crate::unfinished::Writer,
) -> Result<DataTableState> {
let separator = options
.format
.and_then(FileFormat::separator)
.unwrap_or(b',');
DataTableState::from_delimited_for_open(path, separator, options, writer)
}
/// Polars' view of one source's S3 settings, for `scan_parquet`.
#[cfg(feature = "cloud")]
fn build_s3_cloud_options(settings: &crate::cloud_sources::S3Settings) -> CloudOptions {
let settings = settings.clone();
let virtual_hosted = (settings.endpoint.is_some() || settings.virtual_hosted.is_some())
.then(|| settings.virtual_hosted_style().to_string());
let configs: Vec<(AmazonS3ConfigKey, String)> = [
(AmazonS3ConfigKey::Endpoint, settings.endpoint),
(AmazonS3ConfigKey::AccessKeyId, settings.access_key_id),
(
AmazonS3ConfigKey::SecretAccessKey,
settings.secret_access_key,
),
(AmazonS3ConfigKey::Token, settings.session_token),
(AmazonS3ConfigKey::Region, settings.region),
(AmazonS3ConfigKey::VirtualHostedStyleRequest, virtual_hosted),
(
AmazonS3ConfigKey::SkipSignature,
settings.skip_signature.then(|| "true".to_string()),
),
]
.into_iter()
.filter_map(|(key, value)| value.map(|v| (key, v)))
.chain([(
AmazonS3ConfigKey::Client(crate::user_agent::CLIENT_KEY),
crate::user_agent::get(),
)])
.collect();
CloudOptions::default().with_aws(configs)
}
/// The bucket and key of an `s3://bucket/key` or `gs://bucket/key` URL. The key
/// is empty for a bucket root.
#[cfg(feature = "cloud")]
fn cloud_bucket_and_key(url: &str) -> Result<(String, String)> {
if let Some((_, container, key)) = source::azure_parts(url) {
return Ok((container, key.trim_matches('/').to_string()));
}
crate::cloud_browse::split_bucket_url(url)
.map(|(_, bucket, key)| (bucket, key))
.ok_or_else(|| {
color_eyre::eyre::eyre!("URL must be s3://bucket/key or gs://bucket/key")
})
}
/// The store Polars itself will scan `url` through, from its cache keyed on the
/// bucket and `options`, so the footer read, the size probe and a download share
/// one credential chain, TLS client and connection pool with the scan instead of
/// each building a store of their own.
#[cfg(feature = "cloud")]
fn polars_object_store(
url: &str,
options: &CloudOptions,
runtime: &tokio::runtime::Handle,
) -> Result<Arc<dyn object_store::ObjectStore>> {
let url = url.to_string();
let options = options.clone();
wait_on_runtime(runtime, async move {
let (_, store) = polars::io::cloud::build_object_store(
PlRefPath::new(url.as_str()),
Some(&options),
false,
)
.await?;
polars::prelude::PolarsResult::Ok(store.to_dyn_object_store().await.into_owned())
})
.ok_or_else(|| color_eyre::eyre::eyre!("cancelled"))?
.map_err(|e| color_eyre::eyre::eyre!("Object store config failed: {}", e))
}
/// Human-readable byte size, for the download confirmation and the load's progress.
fn format_bytes(n: u64) -> String {
const KB: u64 = 1024;
const MB: u64 = KB * 1024;
const GB: u64 = MB * 1024;
const TB: u64 = GB * 1024;
if n >= TB {
format!("{:.2} TB", n as f64 / TB as f64)
} else if n >= GB {
format!("{:.2} GB", n as f64 / GB as f64)
} else if n >= MB {
format!("{:.2} MB", n as f64 / MB as f64)
} else if n >= KB {
format!("{:.2} KB", n as f64 / KB as f64)
} else {
format!("{} bytes", n)
}
}
/// Build an HTTP agent with a total time budget.
///
/// ureq 3 moved timeouts off the request and onto agent configuration, so
/// every request has to come from an agent to be bounded at all. Leaving a
/// request unbounded would mean a remote that accepts a connection and then
/// dribbles bytes forever hangs the whole TUI, and the user's only way out
/// is to kill the process.
///
/// `timeout_global` covers the entire exchange rather than individual
/// socket operations, which is the property that matters here: a server
/// that sends one byte every 29 seconds defeats a per-read timeout but not
/// this one.
#[cfg(feature = "http")]
fn http_agent(total: std::time::Duration) -> ureq::Agent {
crate::user_agent::ureq_config()
.timeout_global(Some(total))
.build()
.into()
}
/// What a HEAD says an HTTP(S) file weighs: `None` when it does not say. An error
/// only when the answer settles that the file cannot be had (a 404, no server); a
/// server that refuses HEAD may still send the file.
#[cfg(feature = "http")]
fn fetch_remote_size_http(
url: &str,
) -> std::result::Result<Option<u64>, crate::error_display::HttpGone> {
let agent = Self::http_agent(std::time::Duration::from_secs(15));
// ureq asks for gzip by default and strips Content-Length from a compressed
// answer, so a server that compresses (GitHub Pages does) reports no size.
// Identity asks for the file's own length, which is what lands on disk.
match agent.head(url).header("Accept-Encoding", "identity").call() {
Ok(r) => Ok(r
.headers()
.get("Content-Length")
.and_then(|v| v.to_str().ok())
.and_then(|s| s.parse::<u64>().ok())),
Err(e) => crate::error_display::http_gone(url, &e).map_or(Ok(None), Err),
}
}
/// The size of one S3 or GCS object, from a HEAD through the shared store.
#[cfg(feature = "cloud")]
fn fetch_remote_size_cloud(
url: &str,
cloud: &crate::config::CloudConfig,
runtime: &tokio::runtime::Handle,
) -> Result<Option<u64>> {
use object_store::ObjectStoreExt;
let (_bucket, key) = Self::cloud_bucket_and_key(url)?;
if key.is_empty() {
return Ok(None);
}
let (_, _, store) = Self::cloud_store_for(Path::new(url), cloud, runtime)?;
let path = crate::cloud_browse::object_path(&key);
let head = wait_on_runtime(runtime, async move { store.head(&path).await });
Ok(head.and_then(|r| r.ok()).map(|meta| meta.size))
}
/// Download `url` to a temporary file. `stop` ends it early, while the server is
/// sending or while it is silent, and any failure removes the file; see
/// [`crate::download::read_to_temp`].
///
/// Past `limit` bytes it stops with a [`crate::download::PastLimit`] error.
#[cfg(feature = "http")]
fn download_http_to_temp(
url: &str,
temp_dir: Option<&Path>,
extension: Option<&str>,
limit: Option<u64>,
writer: &crate::unfinished::Writer,
) -> Result<crate::download::TempDownload> {
use crate::download::StreamError;
let url = url.to_string();
let open = move || {
let agent = Self::http_agent(std::time::Duration::from_secs(300));
// ureq answers a 4xx or 5xx with an error, so every failure is said here.
let response = agent
.get(&url)
.call()
.map_err(|e| crate::error_display::http_message(&url, &e))?;
// No length: ureq hands back a compressed answer decompressed, and the
// Content-Length it came with is the wire's, not the file's.
Ok((response.into_body().into_reader(), None))
};
crate::download::read_to_temp(temp_dir, extension, open, writer, limit).map_err(|error| {
match error {
StreamError::Open(message) => color_eyre::eyre::eyre!(message),
StreamError::Read(e) => {
color_eyre::eyre::eyre!("Download failed partway. Check your connection: {e}")
}
StreamError::Short { expected, got } => color_eyre::eyre::eyre!(
"Download failed partway: it ended after {got} of {expected} bytes."
),
StreamError::Write(report) => report,
StreamError::Cut => color_eyre::eyre::eyre!("Download was cancelled."),
}
})
}
/// Stream one S3, GCS or Azure object to a temporary file, named for the user by
/// its scheme in any error. A few chunks are in memory at a time; see
/// [`crate::download`]. `writer`'s open stopping ends it early, and any failure
/// removes the file.
#[cfg(feature = "cloud")]
fn download_cloud_to_temp(
url: &str,
cloud: &crate::config::CloudConfig,
options: &OpenOptions,
runtime: &tokio::runtime::Handle,
writer: &crate::unfinished::Writer,
) -> Result<crate::download::TempDownload> {
use crate::download::StreamError;
use object_store::ObjectStoreExt;
let (label, example) = match source::input_source(Path::new(url)) {
source::InputSource::Gcs(_) => ("GCS", "gs://bucket/path/file.csv"),
source::InputSource::Azure(_) => (
"Azure",
"abfss://container@account.dfs.core.windows.net/path/file.csv",
),
_ => ("S3", "s3://bucket/path/file.csv"),
};
let ext = source::download_suffix(url);
let (_bucket, key) = Self::cloud_bucket_and_key(url)?;
if key.is_empty() {
return Err(crate::error_display::FileError::new(
Path::new(url),
format!("a {label} URL names an object here, such as {example}"),
)
.into());
}
let (_, _, store) = Self::cloud_store_for(Path::new(url), cloud, runtime)?;
let path = crate::cloud_browse::object_path(&key);
let open = async move {
let got = store
.get(&path)
.await
.map_err(|e| crate::error_display::store_message(&e))?;
let len = got.range.end - got.range.start;
Ok((got.into_stream(), Some(len)))
};
let failed = |what: String| -> color_eyre::Report {
crate::error_display::FileError::new(Path::new(url), what).into()
};
crate::download::stream_to_temp(
runtime,
options.temp_dir.as_deref(),
ext.as_deref(),
open,
writer,
)
.map_err(|error| match error {
StreamError::Open(e) => failed(e),
StreamError::Read(e) => failed(format!("the download stopped: {e}")),
StreamError::Short { expected, got } => {
failed(format!("it ended after {got} of {expected} bytes"))
}
StreamError::Write(report) => report,
StreamError::Cut => failed("the download was cancelled".to_string()),
})
}
/// Run the worker of an open's phase, as `load`'s job: its answer goes to the loader.
///
/// Every phase runs off the event thread. The size probe is a HEAD request: fifteen
/// seconds of timeout for HTTP, unbounded for S3 and GCS, and inline it froze the UI
/// precisely where the user is most likely to want out. Scanning is where the
/// wall-clock time goes — CSV schema inference, and hive directories with many files
/// — and the schema read of a directory reads a footer from each file.
/// The open's scan of `paths`, named `path`: what the frame is, or what has to
/// happen before there is one. Run by the open's `Scan` phase, and by the home
/// screen's preview, which hands what it builds to the open.
pub(crate) fn scan_for_open(
cloud: &crate::config::CloudConfig,
formats: &crate::formats::Registry,
paths: &[PathBuf],
options: OpenOptions,
path: Option<PathBuf>,
) -> std::result::Result<loading::LoadAnswer, String> {
use loading::LoadAnswer;
let bytes_of = |files: &[PathBuf]| -> u64 {
files
.iter()
.filter_map(|f| std::fs::metadata(f).ok())
.map(|m| m.len())
.sum()
};
// What the read passed over rides back with the options it was asked
// for, so the dataset can say what it left out. Seeded with what the
// caller already knows and overwritten by what the read finds: a
// directory on disk is the read's own answer, because it is the pass
// that decides, while for a prefix in an object store Polars does the
// listing and never sees the other formats — there the home screen's
// listing is the only witness.
let mut report = ReadReport {
left_out: options.left_out.clone(),
files_disagree: options.files_disagree,
format: None,
format_read: None,
read_python: Vec::new(),
sqlite: None,
opened: None,
splits: options.splits.clone(),
delimited: None,
table: None,
guessed: false,
read_notes: Vec::new(),
typing: Default::default(),
};
// A followed file reads every row it can and counts the rest: a row
// that does not fit the schema never stops the follow.
let options = OpenOptions {
ignore_errors: options.ignore_errors || options.follow,
..options
};
let named = |e: color_eyre::Report| {
crate::error_display::user_message_from_report(&e, path.as_deref())
};
// An Arrow IPC stream followed is read by a scan of its own, not converted;
// NDJSON followed is scanned rather than read whole, by its reader.
let followed_stream = options.follow
&& crate::follow::followed_stream(
&paths[0],
Some(crate::follow::format_of(&paths[0], options.format)),
&options,
);
let scan = if followed_stream {
crate::follow::stream::scan(&paths[0])
.map(Scan::from)
.map_err(|e| color_eyre::eyre::eyre!(e))
} else {
Self::build_lazyframe_from_paths_with(cloud, paths, &options, &mut report, formats)
}
// Named as the dataset is: a download by its URL, not its temp file.
.map_err(named)?;
let format = scan.format(report.format.or(options.format));
// Bounded to the complete records, and counted for the watcher. A
// recording that cannot be followed is read as it stands, and goes on.
let recording = options
.spool
.as_ref()
.is_some_and(|handle| handle.spool().tee().is_some());
let (scan, tail) = match scan {
Scan::Frame(lf) if options.follow => {
let format = crate::follow::format_of(&paths[0], format);
let refused = (!followed_stream)
.then(|| crate::follow::refusal(Some(format), &options))
.flatten();
match refused {
Some(_) if recording => (Scan::Frame(lf), None),
Some(refusal) => return Err(refusal),
None => {
let (lf, tail) =
crate::follow::bound_to_complete(*lf, &paths[0], format, &options)
.map_err(named)?;
(Scan::Frame(Box::new(lf)), Some(Arc::new(tail)))
}
}
}
_ if options.follow && !recording => {
return Err(crate::follow::refusal(format, &options)
.unwrap_or_else(|| "This file cannot be followed as it grows.".to_string()));
}
scan => (scan, None),
};
let read_mode = scan.read_mode(format, report.format_read.is_some(), &options);
let mut options = OpenOptions {
left_out: report.left_out,
files_disagree: report.files_disagree,
format,
format_read: report.format_read,
sqlite: report.sqlite,
opened: report.opened,
splits: report.splits,
read_python: report.read_python,
read_mode,
tail,
table: report.table.or_else(|| options.table.clone()),
format_guessed: options.format_guessed || report.guessed,
read_notes: report.read_notes,
typing: report.typing,
..options
};
// The spec's dialect stays with the dataset, so a read again (`H`,
// a decompressed copy) reads as this one did.
if let Some(read) = report.delimited {
read.delimited().apply(&mut options);
options.delimited = Some(read);
}
Ok(match scan {
Scan::Frame(lf) => LoadAnswer::Scanned { lf, path, options },
Scan::Decompress { file, .. } => LoadAnswer::Compressed {
file,
path,
options,
},
Scan::Streams(files) => LoadAnswer::Convert {
what: loading::Conversion::Streams,
bytes: bytes_of(&files),
files,
path,
options,
},
Scan::DecompressSpec { file, choice } => LoadAnswer::CompressedRecords {
file,
path,
choice,
options,
},
Scan::ReadInto { files, format } => LoadAnswer::Convert {
what: loading::Conversion::Text(format),
bytes: bytes_of(&files),
files,
path,
options,
},
Scan::Tables { file, tables, .. } => LoadAnswer::Tables { file, tables, path },
Scan::Unpack {
file,
member,
format,
} => LoadAnswer::Convert {
what: loading::Conversion::Text(format),
bytes: bytes_of(std::slice::from_ref(&file)),
files: vec![file],
path,
options: OpenOptions {
table: Some(member),
..options
},
},
Scan::Hex { file, asked } => LoadAnswer::Hex {
file,
asked,
record_size: options.record_size,
},
})
}
/// The open's schema read of the scan's frame: the dataset, built with everything
/// the open `made`. Run by the open's `ReadSchema` phase, and by the home screen's
/// preview.
pub(crate) fn read_schema_for_open(
lf: LazyFrame,
path: Option<PathBuf>,
options: OpenOptions,
cloud: &crate::config::CloudConfig,
runtime: &tokio::runtime::Handle,
report: &crate::measurements::OpenReport,
made: loading::Made,
) -> std::result::Result<loading::LoadAnswer, String> {
use loading::LoadAnswer;
let (state, facts, debug_label) =
Self::build_schema_state(lf, path.as_deref(), &options, cloud, runtime, report)
.map_err(|e| crate::error_display::user_message_from_report(&e, path.as_deref()))?;
// Everything the open found, given to the dataset as it is built.
let loading::Made {
download,
converted,
notes,
other_tables,
detail,
} = made;
let mut open_notes = facts.open_notes;
open_notes.extend(notes);
let mut other_tables_found = facts.other_tables;
other_tables_found.extend(other_tables);
let state = state.with_open(OpenFacts {
fetched: Self::fetched(download.as_ref(), path.as_deref()),
download,
converted,
other_tables: other_tables_found,
open_notes,
detail: detail.or(facts.detail),
..facts
});
Ok(LoadAnswer::SchemaRead {
state: Box::new(state),
path,
options,
debug_label: Some(debug_label),
})
}
fn spawn_load_phase(&mut self, load: loading::LoadId, step: loading::Step) {
use loading::{LoadAnswer, Step};
let job = Job::Load(load);
match step {
#[cfg(any(feature = "http", feature = "cloud"))]
Step::ReadHeaders {
url,
format,
options,
writer,
} => {
let (cloud, runtime) = (self.app_config.cloud.clone(), self.runtime.clone());
self.spawn_job(job, Some("Reading headers..."), move |_| {
let read = crate::remote_model::read(&url, format, &cloud, &runtime, &|| {
writer.stopped()
});
let crate::remote_model::Read { lf, summary, notes } = match read {
Ok(read) => read,
Err(crate::model_files::RangeError::NoRanges) => {
return Ok(Answer::Load(Box::new(LoadAnswer::NoRanges { options })));
}
// The URL in the message may carry a password or a signature.
Err(crate::model_files::RangeError::Failed(message)) => {
return Err(crate::logging::redact(&message, &[]));
}
};
let opened = Arc::new(crate::model_files::opened(&summary));
let options = OpenOptions {
format: Some(format),
opened: Some(opened.clone()),
..options
};
// The table is the headers, in memory: nothing is left to scan.
let state = Self::schema_state_from_full_scan(
lf,
None,
&OpenOptions {
hive: false,
..options.clone()
},
)
.map_err(|e| crate::error_display::user_message_from_report(&e, Some(&url)))?
.with_open(OpenFacts {
detail: opened.detail.clone(),
open_notes: notes,
read_as: Some(format),
..Default::default()
});
Ok(Answer::Load(Box::new(LoadAnswer::SchemaRead {
state: Box::new(state),
path: Some(url),
options,
debug_label: Some("model headers (ranged)".to_string()),
})))
});
}
#[cfg(any(feature = "http", feature = "cloud"))]
Step::Probe(pending) => {
#[cfg(feature = "cloud")]
let (cloud, runtime) = (self.app_config.cloud.clone(), self.runtime.clone());
self.spawn_job(job, Some("Checking size..."), move |_| {
// Arrow in a store: its listing says which objects, and which of
// them are streams to download.
#[cfg(feature = "cloud")]
if let loading::PendingDownload::Arrow { url, .. } = &pending {
let (_, _, options) = pending.parts();
let (objects, options) = crate::cloud_arrow::list(
url, options, &cloud, &runtime,
)
.map_err(|e| crate::error_display::user_message_from_report(&e, None))?;
let size = crate::cloud_arrow::stream_bytes(&objects);
return Ok(Answer::Load(Box::new(LoadAnswer::Sized(
loading::PendingDownload::Arrow {
url: url.clone(),
objects,
size: Some(size),
options,
},
))));
}
let size = match &pending {
#[cfg(feature = "http")]
loading::PendingDownload::Http { url, .. } => {
// A file that is not there, or a host that does not
// answer, ends the open here, not after a question
// about downloading it.
Self::fetch_remote_size_http(url).map_err(|gone| gone.message)?
}
#[cfg(feature = "cloud")]
loading::PendingDownload::S3 { url, .. }
| loading::PendingDownload::Gcs { url, .. }
| loading::PendingDownload::Azure { url, .. } => {
Self::fetch_remote_size_cloud(url, &cloud, &runtime).unwrap_or(None)
}
#[cfg(feature = "cloud")]
loading::PendingDownload::Arrow { size, .. } => *size,
};
Ok(Answer::Load(Box::new(LoadAnswer::Sized(
pending.with_size(size),
))))
});
}
#[cfg(any(feature = "http", feature = "cloud"))]
Step::Download { pending, writer } => {
// The load's stop flag is raised when it is abandoned or another open
// replaces it, and when the app drops: the download stops at the next
// chunk, or while the source is silent, and removes its file. Quitting
// removes it even if the process ends first (`ExitSweep`).
#[cfg(feature = "cloud")]
let (cloud, runtime) = (self.app_config.cloud.clone(), self.runtime.clone());
let status = match &pending {
#[cfg(feature = "http")]
loading::PendingDownload::Http { .. } => "Downloading...",
#[cfg(feature = "cloud")]
loading::PendingDownload::S3 { .. } => "Downloading from S3...",
#[cfg(feature = "cloud")]
loading::PendingDownload::Gcs { .. } => "Downloading from GCS...",
#[cfg(feature = "cloud")]
loading::PendingDownload::Azure { .. } => "Downloading from Azure...",
#[cfg(feature = "cloud")]
loading::PendingDownload::Arrow { url, .. } => {
match source::input_source(Path::new(url)) {
source::InputSource::Gcs(_) => "Downloading from GCS...",
source::InputSource::Azure(_) => "Downloading from Azure...",
_ => "Downloading from S3...",
}
}
};
// How much, when the server said: a download nobody was asked about
// says what it is fetching.
let sized = pending
.parts()
.1
.filter(|_| status == "Downloading...")
.map(|size| format!("Downloading {}...", discover::format_size(size)));
let status = sized.as_deref().unwrap_or(status);
self.spawn_job(job, Some(status), move |_| {
let (url, _, options) = pending.parts();
let fetched = match &pending {
#[cfg(feature = "http")]
loading::PendingDownload::Http { .. } => {
let ext = source::download_suffix(url);
// A download nobody was asked about stops at its limit, when
// the server did not say its size: a size it said bounds the
// transfer, and the bytes counted here are decompressed.
let limit = options
.download_unasked
.filter(|_| pending.parts().1.is_none())
.map(|unasked| unasked.limit);
Self::download_http_to_temp(
url,
options.temp_dir.as_deref(),
ext.as_deref(),
limit,
&writer,
)
.map(|file| (file, options.clone()))
}
#[cfg(feature = "cloud")]
loading::PendingDownload::S3 { .. }
| loading::PendingDownload::Gcs { .. }
| loading::PendingDownload::Azure { .. } => {
Self::download_cloud_to_temp(url, &cloud, options, &runtime, &writer)
.map(|file| (file, options.clone()))
}
// Its streams, converted as they arrive; its IPC files stay put.
#[cfg(feature = "cloud")]
loading::PendingDownload::Arrow { objects, .. } => {
crate::cloud_arrow::download(
objects, options, &cloud, &runtime, &writer,
)
.map(|(file, parts)| {
let options = OpenOptions {
format: Some(FileFormat::Arrow),
hive: false,
arrow_parts: Some(Arc::new(parts)),
..options.clone()
};
(file, options)
})
}
};
let (download, options) = match fetched {
Err(e) if e.downcast_ref::<crate::download::PastLimit>().is_some() => {
return Ok(Answer::Load(Box::new(LoadAnswer::PastLimit(pending))));
}
fetched => fetched.map_err(|e| {
crate::error_display::user_message_from_report(&e, None)
})?,
};
Ok(Answer::Load(Box::new(LoadAnswer::Downloaded {
download,
options,
})))
});
}
Step::Spool {
options,
writer,
read,
} => {
// The read is a thread of its own, so a producer gone quiet does not hold
// up the stop: Ctrl+O and quitting remove the partial file at once.
let piped = self.stdin_reader.take();
let stdout = self.stdout_pass.take();
self.spawn_job(job, Some("Reading stdin..."), move |_| {
let open = move || -> crate::download::Opened<Box<dyn std::io::Read + Send>> {
Ok((piped.unwrap_or_else(|| Box::new(std::io::stdin())), None))
};
// Followed, the copy goes on behind the first rows; recorded, it
// goes to the file the user named.
// And read as it arrives when what it holds can be.
let (download, options) = if options.follow
|| options.tee.is_some()
|| crate::stdin::may_read_as_it_arrives(&options)
{
match crate::follow::spool(open, options, &writer, &read, stdout)? {
(crate::follow::Spooled::Temp(download), options) => {
(download, options)
}
(crate::follow::Spooled::Kept(file), options) => {
return Ok(Answer::Load(Box::new(LoadAnswer::Recorded {
file,
options,
})));
}
}
} else {
crate::stdin::spool(open, options, &writer, &read)?
};
Ok(Answer::Load(Box::new(LoadAnswer::Spooled {
download,
options,
})))
});
}
Step::FetchSpec {
url,
options,
writer,
} => {
#[cfg(any(feature = "http", feature = "cloud"))]
let (cloud, runtime) = (self.app_config.cloud.clone(), self.runtime.clone());
self.spawn_job(job, Some("Reading spec..."), move |_| {
#[cfg(any(feature = "http", feature = "cloud"))]
let fetched = crate::remote_model::fetch_small(
&url,
crate::formats::MAX_SPEC_BYTES,
&cloud,
&runtime,
&|| writer.stopped(),
);
#[cfg(not(any(feature = "http", feature = "cloud")))]
let fetched: std::result::Result<Option<Vec<u8>>, String> = {
let _ = &writer;
Err(crate::error_display::file_message(
&url,
"this build reads no URLs",
))
};
// The URL in the message may carry a password or a signature.
let bytes = fetched
.map_err(|message| crate::logging::redact(&message, &[]))?
.ok_or_else(|| {
crate::logging::redact(
&crate::error_display::file_message(
&url,
&format!(
"a format spec is at most {}",
crate::formats::MAX_SPEC_SAID
),
),
&[],
)
})?;
let spec = crate::formats::Spec::from_bytes(&bytes, &url)
.map_err(|e| crate::logging::redact(&e.to_string(), &[]))?;
Ok(Answer::Load(Box::new(LoadAnswer::SpecFetched {
spec: Arc::new(spec),
options,
})))
});
}
Step::DecompressRecords {
file,
path,
choice,
options,
writer,
} => {
self.spawn_job(job, Some("Decompressing..."), move |_| {
let failed = |e: color_eyre::Report| {
crate::error_display::user_message_from_report(&e, Some(path.as_path()))
};
let compression = options
.compression
.or_else(|| CompressionFormat::from_extension(&file))
.ok_or_else(|| format!("{} is not compressed", path.display()))?;
let temp_dir = options.temp_dir.clone().unwrap_or_else(std::env::temp_dir);
let copy =
DataTableState::decompress_to_copy(&file, compression, &temp_dir, &writer)
.map_err(failed)?;
Ok(Answer::Load(Box::new(LoadAnswer::DecompressedRecords {
copy,
path,
choice,
options,
})))
});
}
Step::ReadRecords {
copy,
path,
choice,
options,
} => {
self.spawn_job(job, Some("Reading records..."), move |_| {
let named = path
.file_name()
.map(|n| n.to_string_lossy().into_owned())
.unwrap_or_default();
let read = crate::formats::read(©, &named, choice)?;
let lf = Arc::clone(&read.records).into_lazy().map_err(|e| {
crate::error_display::user_message_from_report(
&color_eyre::eyre::eyre!(e),
Some(path.as_path()),
)
})?;
Ok(Answer::Load(Box::new(LoadAnswer::Scanned {
lf: Box::new(lf),
path: Some(path),
options: OpenOptions {
format_read: Some(Arc::new(read)),
..options
},
})))
});
}
Step::Decompress {
file,
path,
options,
writer,
download,
} => {
// Only delimited text and lines come this way, the format said by the
// loader or the scan.
let options = OpenOptions {
format: options.format.or(Some(FileFormat::TEXT)),
..options
};
let formats = self.formats.clone();
self.spawn_job(job, Some("Decompressing..."), move |_| {
let failed = |e: color_eyre::Report| {
crate::error_display::user_message_from_report(&e, Some(path.as_path()))
};
let options =
Self::with_delimited_spec(&file, options, &formats).map_err(failed)?;
let lines = options.delimited.is_none()
&& options.format.is_some_and(FileFormat::is_lines);
let (state, opened) = if lines {
let (state, opened) =
DataTableState::from_lines_decompressed(&file, &options, &writer)
.map_err(failed)?;
(state, Some(opened))
} else {
let state = Self::decompressed_delimited_state(&file, &options, &writer)
.map_err(failed)?;
(state, None)
};
let mut open_notes = options
.delimited
.as_ref()
.map(|read| read.notes())
.unwrap_or_default();
open_notes.extend(opened.iter().flat_map(|o| o.notes.iter().cloned()));
let state = state.with_open(OpenFacts {
fetched: Self::fetched(download.as_ref(), Some(&path)),
download,
open_notes,
records: opened.and_then(|o| o.window),
delimited: options.delimited.clone(),
read_as: options.format,
// The loader sends a compressed file here without a scan.
read_mode: options.format.and_then(|f| {
f.read_mode(crate::Stored::Compressed {
in_memory: options.decompress_in_memory,
})
}),
..Default::default()
});
Ok(Answer::Load(Box::new(LoadAnswer::SchemaRead {
state: Box::new(state),
path: Some(path),
options,
debug_label: Some("decompressed delimited".to_string()),
})))
});
}
Step::Convert {
what,
files,
path,
options,
writer,
read,
} => {
// The load's stop flag ends it at the next record batch or chunk,
// removing its files; quitting removes them even if the process ends
// first.
let formats = self.formats.clone();
self.spawn_job(job, Some(what.status()), move |_| {
let named = |e: color_eyre::Report| {
crate::error_display::user_message_from_report(&e, path.as_deref())
};
let converted = match what {
loading::Conversion::Streams => {
let converted = crate::ipc_stream::convert(
&files,
options.temp_dir.as_deref(),
&writer,
&read,
)
.map_err(named)?;
loading::Converted::Streams {
file: converted.file,
parts: converted.parts,
}
}
loading::Conversion::Text(format) => {
let display = path.clone().unwrap_or_else(|| files[0].clone());
let (converted, detail) =
crate::readers::convert(&crate::readers::ConvertIn {
files: &files,
display: &display,
format,
options: &options,
formats: &formats,
writer: &writer,
read: &read,
})
.map_err(named)?;
loading::Converted::Frame {
files: converted.files,
lf: Box::new(converted.lf),
notes: converted.notes,
other_tables: converted.other_tables,
detail,
}
}
};
Ok(Answer::Load(Box::new(LoadAnswer::Converted {
converted,
path,
options,
})))
});
}
Step::Scan {
paths,
options,
display,
status,
} => {
let cloud = self.app_config.cloud.clone();
let formats = self.formats.clone();
// A download is scanned from a temp path the user never typed and would not
// recognise; the URL they did type is what names the dataset.
let path = display.or_else(|| paths.first().cloned());
self.reads.scans += 1;
self.spawn_job(job, Some(status), move |_| {
Self::scan_for_open(&cloud, &formats, &paths, options, path)
.map(|answer| Answer::Load(Box::new(answer)))
});
}
Step::ReadSchema {
lf,
path,
options,
progress,
made,
} => {
self.debug.schema_load = None;
let cloud = self.app_config.cloud.clone();
let runtime = self.runtime.clone();
let report = crate::measurements::OpenReport {
progress,
meter: Arc::new(crate::measurements::Meter::default()),
remembered: Some(self.cache.clone()),
writes: self.cache_writes.clone(),
};
self.spawn_job(job, Some("Reading schema..."), move |_| {
Self::read_schema_for_open(*lf, path, options, &cloud, &runtime, &report, made)
.map(|answer| Answer::Load(Box::new(answer)))
});
}
Step::Nothing
| Step::Crash(_)
| Step::Install(_)
| Step::Failed(_)
| Step::Tables(_)
| Step::Hex(_) => {
unreachable!("not a phase with a worker")
}
#[cfg(any(feature = "http", feature = "cloud"))]
Step::Ask(_) => unreachable!("not a phase with a worker"),
Step::AskRead(_) => unreachable!("not a phase with a worker"),
}
}
/// What the user is asked before files past `[read] memory_warning` are
/// read whole into memory: `big.json: JSON reads 2.1 GB into memory`.
fn in_memory_confirmation_message(read: &loading::InMemory) -> String {
let what = match read.files {
1 => format!(
"{}: {} reads",
read.name
.file_name()
.map(|n| n.to_string_lossy().into_owned())
.unwrap_or_else(|| read.name.display().to_string()),
read.format.title()
),
n => format!("{n} {} files read", read.format.title()),
};
format!(
"{what} {} into memory before the table appears.\n\nRead it?",
Self::format_bytes(read.bytes)
)
}
/// What the user is being asked to agree to before a remote file is downloaded.
#[cfg(any(feature = "http", feature = "cloud"))]
fn download_confirmation_message(
pending: &loading::PendingDownload,
note: Option<&str>,
) -> String {
let (url, size, options) = pending.parts();
let size_str = size
.map(Self::format_bytes)
.unwrap_or_else(|| "unknown".to_string());
let dest_dir = options
.temp_dir
.as_deref()
.map(|p| p.display().to_string())
.unwrap_or_else(|| std::env::temp_dir().display().to_string());
let note = note.map(|note| format!("{note}\n\n")).unwrap_or_default();
// Only a store's Arrow streams are downloaded, and converted as they arrive.
let files = match pending.arrow_files() {
Some((1, 0)) => "Arrow stream: converted as it downloads\n".to_string(),
Some((streams, 0)) => {
format!("Files: {streams} Arrow streams, converted as they download\n")
}
Some((streams, in_place)) => {
let streams = match streams {
1 => "1 Arrow stream, converted as it downloads".to_string(),
n => format!("{n} Arrow streams, converted as they download"),
};
let in_place = match in_place {
1 => "1 IPC file read in place".to_string(),
n => format!("{n} IPC files read in place"),
};
format!("Files: {streams}; {in_place}\n")
}
None => String::new(),
};
format!(
"{note}URL: {url}\n{files}File size: {size_str}\nDestination: {dest_dir} (temporary file)\n\nContinue with download?"
)
}
/// Take the offer on the note the cursor is on: read its column as text.
///
/// Only a note that carries the offer has one, and the offer is taken off a note
/// datui could not act on, so the `Ok(false)` arms here are for a note that has
/// gone stale under the cursor rather than for anything to tell the user about. A
/// failure is the scan's, and is shown the way any other failed read is.
fn read_the_selected_note_s_column_as_text(&mut self) {
let Some(state) = self.data_table_state.as_mut() else {
return;
};
let notes = state.notes();
let Some(column) = notes
.get(self.info_modal.notes_selected_index)
.and_then(|note| note.read_as_text.clone())
else {
return;
};
// Rebuilt here, read off the UI thread. A failure is left showing on the state.
if let Ok(true) = state.deferred(|s| s.read_column_as_text(&column)) {
// The note that offered this is gone and the list is shorter, so the
// cursor would otherwise sit past the end. Kept as near to where the
// user left it as the shorter list allows, rather than thrown to the
// top: one or two notes went, not all of them.
let notes = state.notes().len();
self.info_modal.notes_selected_index = self
.info_modal
.notes_selected_index
.min(notes.saturating_sub(1));
self.info_modal.notes_scroll_offset = 0;
self.spawn_async_collect(Self::LOADING_BUFFER);
}
}
fn hoist_partition_columns(
lf: LazyFrame,
schema: &Schema,
partition_columns: &[String],
drifts: bool,
) -> LazyFrame {
hoist_partition_columns(lf, schema, partition_columns, drifts)
}
}
/// Put hive partition columns first, ahead of the file's own columns. `drifts` keeps
/// the scan's hidden drift column, which the select would otherwise drop.
///
/// A free function rather than a method: rebuilding the scan to read a column as text
/// has to put the columns back the same way, and it happens on the table's state
/// rather than on the app.
pub(crate) fn hoist_partition_columns(
lf: LazyFrame,
schema: &Schema,
partition_columns: &[String],
drifts: bool,
) -> LazyFrame {
if partition_columns.is_empty() {
return lf;
}
let mut exprs: Vec<_> = partition_columns
.iter()
.map(|s| col(s.as_str()))
.chain(
schema
.iter_names()
.map(|s| s.to_string())
.filter(|c| !partition_columns.contains(c))
.map(|s| col(s.as_str())),
)
.collect();
if drifts {
exprs.push(col(crate::schema_union::DRIFT_COLUMN));
}
lf.select(exprs)
}
/// A number for each walk the home search starts, so scorings of one are never taken
/// for another's, even when the two walked the same place.
fn next_search_epoch() -> u64 {
static NEXT: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(1);
NEXT.fetch_add(1, std::sync::atomic::Ordering::Relaxed)
}
/// What a pass behind a staged open reported, and which dataset it was reading for.
/// `None` where the footers are: a pass that could not read them says so, so the
/// dataset stops waiting.
/// Bytes of a text file indexed per step behind its first rows, between which the
/// indexing looks whether it is still wanted.
const INDEX_STEP: usize = 16 << 20;
type FootersReported = Option<(u64, Option<crate::widgets::datatable::FootersFound>)>;
impl App {
/// Schema for a local directory of Parquet files: every column any of them has, from
/// their footers, instead of `collect_schema()` over the whole set or one file's
/// columns standing in for all.
///
/// As a cloud prefix opens: past one wave of footers the two ends open the dataset
/// and the rest are read behind it, joining when they land, and a directory whose
/// listing has not changed since its footers were last all read opens from what
/// they said then. On a network mount each footer is round trips, and a Hive tree
/// is thousands of footers. `None` when the path is not that shape, or when nothing
/// could be read — either way the caller falls back to the general scan, which
/// reports the error properly if there is one.
fn schema_state_from_local_hive(
path: Option<&Path>,
options: &OpenOptions,
report: &crate::measurements::OpenReport,
) -> Option<(DataTableState, OpenFacts)> {
if !options.single_spine_schema {
return None;
}
let p = path.filter(|p| p.is_dir() && options.hive)?;
dataset_files::open(Arc::new(dataset_files::LocalFiles::new(p)), options, report)
}
/// The same one-file trick against an object store. This is the route that used to
/// block the UI thread on a network round trip.
#[cfg(feature = "cloud")]
fn schema_state_from_cloud_hive(
path: Option<&Path>,
options: &OpenOptions,
cloud: &crate::config::CloudConfig,
runtime: &tokio::runtime::Handle,
report: &crate::measurements::OpenReport,
) -> Option<(DataTableState, OpenFacts)> {
// A prefix of Arrow files is read from its download (`cloud_arrow`).
if !options.single_spine_schema || options.format == Some(FileFormat::Arrow) {
return None;
}
// Unlike the local path this does not require --hive: a directory or glob URL
// is already a hive scan by shape.
let p = path.filter(|p| {
let s = p.as_os_str().to_string_lossy();
home::is_object_store_url(p) && (options.hive || source::is_prefix_or_glob(&s))
})?;
let (full, cloud_opts, store) = Self::cloud_store_for(p, cloud, runtime).ok()?;
let (_bucket, key) = Self::cloud_bucket_and_key(&full).ok()?;
Self::schema_state_from_cloud_hive_with(
full, key, store, cloud_opts, options, runtime, report,
)
}
/// The same, against a store already built.
///
/// Split out so a test can hand it an in-memory store and cover the choice between
/// the two routes below — including that each is given the counter it was called
/// with, rather than one of its own.
#[cfg(feature = "cloud")]
fn schema_state_from_cloud_hive_with(
full: String,
key: String,
store: Arc<dyn object_store::ObjectStore>,
cloud_opts: CloudOptions,
options: &OpenOptions,
runtime: &tokio::runtime::Handle,
report: &crate::measurements::OpenReport,
) -> Option<(DataTableState, OpenFacts)> {
// Every file listed once, and the scan, the schema and the count all work from
// that list — for a glob as much as for a prefix. datui expands the glob
// itself: it lists the literal part of the key and matches the rest, so a glob
// is an ordinary list of files by the time anything else sees it, and gets the
// schema union, the row count, the notes and the measurements that a prefix
// gets.
//
// The star cannot be handed to the object store. A listing prefix is a literal
// string, so `data/*.parquet` matches nothing and the open falls through to a
// whole-dataset scan with none of the above — which is what used to happen, for
// every glob, silently (#228).
let pattern = full.contains('*').then(|| {
globset::GlobBuilder::new(&key)
.literal_separator(true)
.build()
.map(|g| g.compile_matcher())
});
let pattern = match pattern {
// A pattern datui cannot read is not one it should guess at.
Some(Err(_)) => return None,
Some(Ok(matcher)) => Some(matcher),
None => None,
};
let listed = cloud_hive::prefix_of_glob(&key).to_string();
Self::schema_state_from_cloud_files(
CloudTarget {
full: &full,
key: listed,
pattern: pattern.as_ref(),
},
store,
cloud_opts,
options,
runtime,
report,
)
}
/// A cloud prefix of Parquet files as one dataset, from a single listing of it.
///
/// The files are scanned by name, so Polars does not list the prefix again, and
/// leniently (see `cloud_hive::lenient_scan`), since files written years apart
/// differ. The state keeps the list, so the count reads footers rather than data
/// and a buffer reads only the files holding its rows (see `RemoteFiles`).
#[cfg(feature = "cloud")]
fn schema_state_from_cloud_files(
target: CloudTarget<'_>,
store: Arc<dyn object_store::ObjectStore>,
cloud_opts: CloudOptions,
options: &OpenOptions,
runtime: &tokio::runtime::Handle,
report: &crate::measurements::OpenReport,
) -> Option<(DataTableState, OpenFacts)> {
let CloudTarget { full, key, pattern } = target;
dataset_files::open(
Arc::new(dataset_files::StoreFiles::new(
full,
key,
pattern.cloned(),
store,
cloud_opts,
runtime,
)),
options,
report,
)
}
/// What the home screen can say about one object opened from a bucket: its rows
/// and columns from the footer the open read, under the URL it resolved to. The
/// object's size is not known here — the footer is read from the tail — so the
/// record carries none, and the row shows none. Its `mtime` is the time of the
/// open: a remote record is never fingerprinted by it, and the index evicts its
/// oldest `mtime` first, so a zero would make these the first to go.
#[cfg(feature = "cloud")]
fn record_cloud_object_facts(
cache: Option<&crate::cache::CacheManager>,
full: &str,
footer: &cloud_hive::FileFooter,
) {
let Some(cache) = cache else {
return;
};
let columns: Vec<String> = footer
.schema
.iter_names()
.map(|name| name.to_string())
.collect();
cache.record_dataset_facts(&[(
PathBuf::from(full),
crate::cache::DatasetFacts {
mtime: std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.map(|d| d.as_secs())
.unwrap_or_default(),
size: 0,
rows: Some(footer.rows()),
cols: Some(columns.len()),
cols_sampled: false,
columns,
kind: Some(discover::EntryKind::File),
classified_by: discover::CLASSIFIER_VERSION,
cost: discover::Cost {
row_groups: Some(footer.row_group_rows.len()),
..Default::default()
},
holds: Default::default(),
},
)]);
}
/// General schema route: ask the frame itself. Slow for a wide hive dataset, which
/// is the reason this whole phase belongs on a background thread.
fn schema_state_from_full_scan(
mut lf: LazyFrame,
path: Option<&Path>,
options: &OpenOptions,
) -> Result<DataTableState> {
let schema = lf
.collect_schema()
.map_err(color_eyre::eyre::Report::from)?;
let partition_columns =
match path.filter(|p| options.hive && (p.is_dir() || source::expands_as_glob(p))) {
Some(p) => DataTableState::discover_hive_partition_columns(p)
.into_iter()
.filter(|c| schema.contains(c.as_str()))
.collect::<Vec<_>>(),
None => Vec::new(),
};
let lf = Self::hoist_partition_columns(lf, &schema, &partition_columns, false);
let part_cols = (!partition_columns.is_empty()).then_some(partition_columns);
DataTableState::from_schema_and_lazyframe(schema, lf, options, part_cols)
}
/// Build the table state for a loaded frame, by the cheapest route that applies.
///
/// Returns the state and a label naming the route it came from, for the debug
/// overlay. Takes its config by value so all of it can run off the UI thread.
fn build_schema_state(
lf: LazyFrame,
path: Option<&Path>,
options: &OpenOptions,
cloud: &crate::config::CloudConfig,
runtime: &tokio::runtime::Handle,
report: &crate::measurements::OpenReport,
) -> Result<(DataTableState, OpenFacts, String)> {
// The facts carry the meter of the route that actually built the dataset, so it
// is installed with the dataset and nothing else can reach it. An open that
// fails never gets here, which is what keeps the dataset still on screen
// showing its own figures.
let (state, mut facts, label) =
Self::schema_state_by_route(lf, path, options, cloud, runtime, report)?;
// What the open did, as against what it found. The one place both are known:
// the scan has reported what it passed over, the caller has said whether this
// is a lake table's plain files, and the state that will carry the notes is in
// hand. See `DataTableState::open_notes` for why they are not the other notes.
// A delimited file read with a header whose names are all numbers: its first
// row of data, most likely, which `H` reads as data instead.
let names_look_like_data = options.format.and_then(FileFormat::separator).is_some()
&& options.has_header != Some(false)
&& !crate::schema_union::names_are_names(
&state
.schema()
.iter_names()
.map(|n| n.to_string())
.collect::<Vec<_>>(),
);
facts.open_notes = crate::notes::from_the_open(
&options.left_out,
options.read_as_plain_files_of,
options.files_disagree,
names_look_like_data,
);
// And the half of it that cannot be missed: the row count on screen is a true
// count of the files and a wrong one of the table.
facts.not_the_table = options.read_as_plain_files_of;
if let Some(splits) = &options.splits {
facts.other_tables = splits.others.clone();
facts
.open_notes
.extend(crate::notes::map_caches(splits.caches));
}
if let Some(read) = &options.format_read {
facts.open_notes.extend(read.notes());
facts.format_read = Some(read.clone());
}
if let Some(opened) = &options.opened {
facts.records = opened.window.clone();
facts.detail = opened.detail.clone();
facts.other_tables = opened.other_tables.clone();
facts.open_notes.extend(opened.notes.iter().cloned());
facts.units = opened.units.clone();
facts.indexing = opened.indexing.clone();
facts.numbering = opened.numbering.clone();
}
if let Some(sqlite) = &options.sqlite {
facts.pushdown = Some(sqlite.pushdown.clone());
facts.hold = sqlite.hold.lock().ok().and_then(|mut hold| hold.take());
facts.other_tables = sqlite.other_tables.clone();
}
if let Some(read) = &options.delimited {
facts.open_notes.extend(read.notes());
facts.delimited = Some(read.clone());
}
facts.open_notes.extend(options.read_notes.iter().cloned());
facts.typing = options.typing.clone();
facts.read_mode = options.read_mode;
facts.read_as = options.format;
// The display path of a downloaded object is its URL too; only a scan that
// really reads the object store in place buffers like one.
// Arrow in a store reads its IPC files in place, and its streams from their
// download (`cloud_arrow`).
facts.remote_source = match &options.arrow_parts {
Some(parts) => parts.iter().any(|part| {
matches!(part, crate::ipc_stream::Part::InPlace(p) if source::is_remote_url(p))
}),
None => path.is_some_and(source::scans_in_place),
};
// The cheap footer-sum row count, for a local Parquet hive directory. Asked
// here because a stat on a mount that has stopped answering hangs its thread.
// A directory read as another format counts its rows by a scan: its footers
// are not Parquet's.
// Not for one read by file: its counter reads only the footers the open did not.
if options.hive
&& facts.remote_files.is_none()
&& options.format.is_none_or(|f| f == FileFormat::Parquet)
&& let Some(dir) = path.filter(|p| !source::is_remote_url(p) && p.is_dir())
{
facts.parquet_count_dir = Some(dir.to_path_buf());
}
Ok((state, facts, label))
}
/// Scan a prefix in an object store with the reader its format calls for.
///
/// Every cloud path went to `scan_parquet` whatever was under it, so a prefix of
/// CSV came back "Could not read from S3. Check credentials and URL" — a false
/// statement about the user's login, made about a directory datui could see the
/// contents of. Polars' other scans take the same `CloudOptions` and do their own
/// listing; nothing was passing them.
///
/// Parquet keeps its own branch at each call site: it is the only one with hive
/// partitioning, which is a Parquet-only capability in this reader, and it is the
/// path every cloud dataset took before this existed.
///
/// `None` when the format is not one of these, which sends the caller back to the
/// Parquet scan it always made.
#[cfg(feature = "cloud")]
fn scan_cloud_prefix(
url: &str,
cloud_opts: CloudOptions,
format: FileFormat,
glob: bool,
options: &OpenOptions,
) -> Option<Result<LazyFrame>> {
// The formats the docs say a prefix reads in place. Parquet takes the caller's
// own scan, and a prefix of model files is read by its headers before this.
if !format.reads_bucket_prefix() {
return None;
}
// A plain prefix is narrowed to the keys with an extension. A console's folder
// marker comes back from the listing as `data` for `data/`, which Polars reads
// as a file of a different kind from the rest and refuses the whole prefix.
let pl_path = if url.ends_with('/') && !url.contains('*') {
PlRefPath::new(format!("{url}**/*.*").as_str())
} else {
PlRefPath::new(url)
};
let scan = crate::readers::of(format).bucket_scan?;
Some(scan(crate::readers::BucketIn {
url,
path: pl_path,
cloud: cloud_opts,
glob,
options,
format,
}))
}
/// The format a prefix or glob in a store is read as, other than Parquet: what
/// the listing said, else what a glob's names end in (`*.arrow`).
#[cfg(feature = "cloud")]
fn cloud_glob_format(url: &str, options: &OpenOptions) -> Option<FileFormat> {
options
.format
.or_else(|| {
url.contains('*')
.then(|| FileFormat::from_path(Path::new(url)))
.flatten()
})
.filter(|f| *f != FileFormat::Parquet)
}
/// The plain URL and Polars options for one object-store path, through the source
/// it names or belongs to (`cloud_sources::resolve`).
#[cfg(feature = "cloud")]
fn resolve_cloud_url(
path: &Path,
cloud: &crate::config::CloudConfig,
) -> Result<(String, CloudOptions)> {
let text = path.to_string_lossy();
let resolved = crate::cloud_sources::resolve_for_open(&text, cloud)
.map_err(|e| color_eyre::eyre::eyre!(e))?;
use object_store::azure::AzureConfigKey;
use polars::io::cloud::GoogleConfigKey;
let gcs_agent = (
GoogleConfigKey::Client(crate::user_agent::CLIENT_KEY),
crate::user_agent::get(),
);
let options = match resolved.kind {
crate::cloud_browse::ProviderKind::S3 => Self::build_s3_cloud_options(&resolved.s3),
crate::cloud_browse::ProviderKind::Gcs
if resolved.signing == crate::cloud_sources::Signing::Unsigned =>
{
CloudOptions::default()
.with_gcp([(GoogleConfigKey::SkipSignature, "true".into()), gcs_agent])
}
crate::cloud_browse::ProviderKind::Gcs => match &resolved.gcloud {
// The token comes from `gcloud` whenever Polars asks, so a long scan
// outlives the one fetched here.
Some((configuration, _)) => CloudOptions::default()
.with_gcp([gcs_agent])
.with_credential_provider(Some(crate::gcloud::polars_provider(configuration))),
None => match &resolved.google_credentials {
Some(file) => CloudOptions::default().with_gcp([
(
GoogleConfigKey::ApplicationCredentials,
file.to_string_lossy().into_owned(),
),
gcs_agent,
]),
None => CloudOptions::default().with_gcp([gcs_agent]),
},
},
crate::cloud_browse::ProviderKind::Azure => {
let (account, _, _) = source::azure_parts(&resolved.url)
.ok_or_else(|| color_eyre::eyre::eyre!("not an Azure URL"))?;
let mut azure = crate::azure::polars_options(&account, &resolved.azure);
azure.push((
AzureConfigKey::Client(crate::user_agent::CLIENT_KEY),
crate::user_agent::get(),
));
CloudOptions::default().with_azure(azure)
}
};
Ok((resolved.url, options))
}
/// The URL, Polars options and store for one object-store path. `cloud` is the
/// effective config the `App` keeps (see `OpenOptions::effective_cloud`).
#[cfg(feature = "cloud")]
fn cloud_store_for(
path: &Path,
cloud: &crate::config::CloudConfig,
runtime: &tokio::runtime::Handle,
) -> Result<(String, CloudOptions, Arc<dyn object_store::ObjectStore>)> {
let (full, cloud_opts) = Self::resolve_cloud_url(path, cloud)?;
let store = Self::polars_object_store(&full, &cloud_opts, runtime)?;
Ok((full, cloud_opts, store))
}
/// One Parquet object read in place: schema and row count from its footer, in one
/// tail read through one store.
///
/// Asking the frame for its schema fetched the footer through Polars, and Polars
/// answers `len()` on a cloud scan by reading the first row group rather than the
/// footer, so the background count that followed an open downloaded row group 0 a
/// second time, alongside the buffer that was showing it. The footer has both
/// answers for one 256 KiB range request; the schema is handed to the scan so
/// Polars does not fetch it again, and the count is known before the first frame.
/// A failure is returned, not swallowed: the caller falls back to asking the frame
/// and puts the reason in the debug label.
#[cfg(feature = "cloud")]
fn schema_state_from_cloud_object(
path: &Path,
options: &OpenOptions,
cloud: &crate::config::CloudConfig,
runtime: &tokio::runtime::Handle,
report: &crate::measurements::OpenReport,
) -> Result<(DataTableState, OpenFacts)> {
let (full, cloud_opts, store) = Self::cloud_store_for(path, cloud, runtime)?;
let (_bucket, key) = Self::cloud_bucket_and_key(&full)?;
if key.is_empty() {
return Err(color_eyre::eyre::eyre!("a bucket, not an object"));
}
let meter = report.meter.clone();
let (footer, etag) = wait_on_runtime(runtime, async move {
cloud_hive::footer_of_cloud_parquet(store, &key, &meter).await
})
.ok_or_else(|| color_eyre::eyre::eyre!("cancelled"))??;
let args = ScanArgsParquet {
schema: Some(footer.schema.clone()),
cloud_options: Some(cloud_opts),
hive_options: polars::io::HiveOptions::default(),
glob: false,
..Default::default()
};
let lf = LazyFrame::scan_parquet(PlRefPath::new(full.as_str()), args)?;
let state =
DataTableState::from_schema_and_lazyframe(footer.schema.clone(), lf, options, None)?;
// The commonest cloud open, and the one the dataset index never heard about:
// the prefix route records what it read, and this one read a footer too.
Self::record_cloud_object_facts(report.remembered.as_ref(), &full, &footer);
let column_bytes = crate::schema_union::column_bytes_per_row(&[Some(footer.clone())]);
let facts = OpenFacts {
remote_objects: vec![crate::local_copy::RemoteObject {
url: full,
size: footer.file_bytes as u64,
etag,
}],
row_groups: vec![footer.row_group_rows],
column_bytes,
..Default::default()
};
Ok((state, facts))
}
/// The schema routes, cheapest first: one local footer, one cloud footer (a hive
/// prefix or a single object), then asking the frame.
fn schema_state_by_route(
lf: LazyFrame,
path: Option<&Path>,
options: &OpenOptions,
cloud: &crate::config::CloudConfig,
runtime: &tokio::runtime::Handle,
report: &crate::measurements::OpenReport,
) -> Result<(DataTableState, OpenFacts, String)> {
#[cfg(not(feature = "cloud"))]
let _ = (cloud, runtime);
// A meter per attempt, and the winner's is the open's. The routes are tried in
// order and the earlier ones measure before they discover they cannot finish —
// the local hive route times its walk and its footer pass, then bails five
// different ways. Sharing one meter would leave those figures on a dataset some
// later route built, which is a row saying no files on a dataset that has them.
//
// Two guards, and the test holds them together rather than either alone: the
// attempts take separate meters, and both full-scan arms hand back an empty
// one. A directory whose only Parquet is a writer's own bookkeeping — a
// `_delta_log` checkpoint — reaches the screen through the second of those, so
// `test_a_route_that_gave_up_leaves_no_figures_on_the_dataset_that_opened`
// fails when both are reverted and passes when either still stands. The
// per-attempt meter alone is defensive: the route guards are mutually
// exclusive enough that nothing reaches a later route through the first.
let attempt = |report: &crate::measurements::OpenReport| crate::measurements::OpenReport {
progress: report.progress.clone(),
meter: Arc::new(crate::measurements::Meter::default()),
remembered: report.remembered.clone(),
writes: report.writes.clone(),
};
let local = attempt(report);
if let Some((state, facts)) = Self::schema_state_from_local_hive(path, options, &local) {
let facts = OpenFacts {
measurements: local.meter,
..facts
};
return Ok((state, facts, "one-file (local)".to_string()));
}
// An open abandoned mid-listing is not one for the routes below to scan whole.
if report.progress.is_cancelled() {
return Err(color_eyre::eyre::eyre!("cancelled"));
}
#[cfg(feature = "cloud")]
let cloud_hive_attempt = attempt(report);
#[cfg(feature = "cloud")]
if let Some((state, facts)) =
Self::schema_state_from_cloud_hive(path, options, cloud, runtime, &cloud_hive_attempt)
{
let facts = OpenFacts {
measurements: cloud_hive_attempt.meter,
..facts
};
return Ok((state, facts, "one-file (cloud)".to_string()));
}
#[cfg(feature = "cloud")]
if let Some(p) = path.filter(|p| {
source::scans_in_place(p)
&& !options.hive
&& !source::is_prefix_or_glob(&p.to_string_lossy())
}) {
let object = attempt(report);
match Self::schema_state_from_cloud_object(p, options, cloud, runtime, &object) {
Ok((state, facts)) => {
let facts = OpenFacts {
measurements: object.meter,
..facts
};
return Ok((state, facts, "footer (cloud)".to_string()));
}
// Visible in the debug overlay, because the fallback costs a row group
// for the count and that should not pass for the intended path.
Err(e) => {
// A fresh meter, not the failed footer read's: the full scan
// measures nothing, and showing the attempt that did not work
// would describe a route the dataset did not come by.
return Self::schema_state_from_full_scan(lf, path, options).map(|state| {
(
state,
OpenFacts::default(),
format!("full scan (cloud footer: {e})"),
)
});
}
}
}
Self::schema_state_from_full_scan(lf, path, options)
.map(|state| (state, OpenFacts::default(), "full scan".to_string()))
}
/// The files of one split, when `dir` is a Hugging Face `datasets` cache: its
/// `dataset_info.json` or `state.json` beside Arrow files. What was chosen and left
/// out goes in `report`. Any other directory reads every file.
fn hugging_face_split(
dir: &Path,
format: FileFormat,
files: Vec<PathBuf>,
options: &OpenOptions,
report: &mut ReadReport,
) -> Result<Vec<PathBuf>> {
let metadata = || {
["dataset_info.json", "state.json"]
.iter()
.any(|name| dir.join(name).is_file())
};
if format != FileFormat::Arrow || !metadata() {
return Ok(files);
}
let names: Vec<&str> = files
.iter()
.map(|f| f.file_name().and_then(|n| n.to_str()).unwrap_or_default())
.collect();
let (chosen, splits) = crate::hf_splits::choose(&names, options.table.as_deref())
.map_err(|e| color_eyre::eyre::eyre!("{}: {e}", dir.display()))?;
report.splits = Some(Arc::new(splits));
Ok(chosen.into_iter().map(|i| files[i].clone()).collect())
}
/// Why `--table` was refused for `path`, a file of `format`, which holds one table.
fn one_table(path: Option<&Path>, format: Option<FileFormat>) -> color_eyre::Report {
match path {
Some(path) => crate::error_display::FileError::new(path, cli::one_table(format)).into(),
None => color_eyre::eyre::eyre!(cli::one_table(format)),
}
}
/// A scan of `url` in an object store that Polars refused, with what to check.
#[cfg(feature = "cloud")]
fn cloud_scan_failed(url: &str, e: &polars::prelude::PolarsError) -> color_eyre::Report {
let said = crate::error_display::user_message_from_polars(e);
let (first, rest) = said.split_once('\n').unwrap_or((&said, ""));
let first = first.trim_end().trim_end_matches('.');
let what = format!("could not read it: {first}. Check the credentials and the URL.");
let what = match rest {
"" => what,
rest => format!("{what}\n{rest}"),
};
crate::error_display::FileError::new(Path::new(url), what).into()
}
/// The inputs of an Arrow read as one table, in order: each IPC file scanned where
/// it is, in a bucket or on disk, and each run of streams as its rows of
/// `converted`, the IPC file they were converted to. Stacked as the files of a
/// directory are ([`DataTableState::union_of_files`]).
fn scan_arrow_parts(
cloud: &crate::config::CloudConfig,
converted: Option<&PathBuf>,
parts: &[crate::ipc_stream::Part],
) -> Result<LazyFrame> {
use crate::ipc_stream::Part;
#[cfg(not(feature = "cloud"))]
let _ = cloud;
let scan = |path: &Path| -> Result<LazyFrame> {
#[cfg(feature = "cloud")]
if source::is_remote_url(path) {
let (url, cloud_options) = Self::resolve_cloud_url(path, cloud)?;
let args = polars::prelude::UnifiedScanArgs {
cloud_options: Some(cloud_options),
..Default::default()
};
return Ok(LazyFrame::scan_ipc(
PlRefPath::new(url.as_str()),
Default::default(),
args,
)?);
}
// A converted stream sits in a temp directory the user names, `[` and all,
// and a file read in place may be called `d[1].arrow` (#632).
let args = polars::prelude::UnifiedScanArgs {
glob: source::expands_as_glob(path),
..Default::default()
};
Ok(LazyFrame::scan_ipc(
polars::prelude::PlRefPath::try_from_path(path)?,
Default::default(),
args,
)?)
};
let streams = |offset: u64, rows: u64| -> Result<LazyFrame> {
let file = converted
.ok_or_else(|| color_eyre::eyre::eyre!("No converted Arrow file to read."))?;
let lf = scan(file)?;
// The whole file needs no slice, which would hide its row count.
let whole = offset == 0
&& parts
.iter()
.all(|part| matches!(part, Part::Converted { .. }));
Ok(if whole {
lf
} else {
lf.slice(offset as i64, rows as polars::prelude::IdxSize)
})
};
let mut frames = Vec::new();
let mut run: Option<(u64, u64)> = None;
for part in parts {
match part {
Part::Converted { offset, rows, .. } => {
run = Some(match run {
Some((start, n)) if start + n == *offset => (start, n + rows),
Some((start, n)) => {
frames.push(streams(start, n)?);
(*offset, *rows)
}
None => (*offset, *rows),
});
}
Part::InPlace(path) => {
if let Some((start, n)) = run.take() {
frames.push(streams(start, n)?);
}
frames.push(scan(path)?);
}
}
}
if let Some((start, n)) = run {
frames.push(streams(start, n)?);
}
match frames.len() {
0 => Err(color_eyre::eyre::eyre!("No Arrow files to read.")),
1 => Ok(frames.remove(0)),
_ => Ok(polars::prelude::concat(
frames.as_slice(),
DataTableState::union_of_files(),
)?),
}
}
/// One split of a `save_to_disk` DatasetDict, `dir`, whose `dataset_dict.json` names
/// `splits`: the subdirectory `--table` names, else the first offered, read as any
/// directory is. The others are listed, as a cache directory's are.
fn dataset_dict_split(
dir: &Path,
splits: &[String],
options: &OpenOptions,
report: &mut ReadReport,
formats: &crate::formats::Registry,
) -> Result<Scan> {
let listed: Vec<&str> = splits.iter().map(String::as_str).collect();
let mut picked = crate::hf_splits::pick(&listed, options.table.as_deref())
.map_err(|e| color_eyre::eyre::eyre!("{}: {e}", dir.display()))?;
let split = dir.join(picked.split.as_deref().unwrap_or_default());
let inner = OpenOptions {
table: None,
splits: None,
..options.clone()
};
let scan = Self::build_local_lazyframe(&[split], &inner, report, formats)?;
// The split's own directory names no splits; its `map()` files are still counted.
picked.caches = report.splits.as_ref().map_or(0, |inner| inner.caches);
report.splits = Some(Arc::new(picked));
Ok(scan)
}
/// Build the LazyFrame for `paths`.
///
/// Takes the cloud config by reference rather than reading `self`, so the same
/// code can run on a background thread — scanning is where the wall-clock time
/// goes for CSV (schema inference) and for hive directories with many files.
/// Whether the files about to be read as one table do not all carry the same
/// columns, for the note that says so.
///
/// Only for the formats with no footer. A Parquet dataset's footers are read anyway
/// and produce the exact version of this — which columns, in how many files, and
/// where — so a second, vaguer note above those would be noise.
///
/// A spread of the files rather than all of them, the same three
/// [`crate::schema_union::sample_files`] reads for the label, and for the same
/// reason: this runs on the way into a read the user is waiting for.
fn files_disagree(
files: &[PathBuf],
options: &OpenOptions,
found: FileFormat,
) -> crate::schema_union::Disagreement {
// The format the read will use, not the one the names suggested: an explicit
// `--format` outranks both, and judging a directory with a reader the open will
// not use is a note about a read that never happened.
let format = options.format.unwrap_or(found);
if format == FileFormat::Parquet {
return Default::default();
}
// Null values are the one setting the sample cannot mirror: `--null`
// takes `COL=VAL` forms the reader resolves against the file it is opening, and
// a sample that guessed would report a widening the table never did. They are
// unset unless the user names them, so this stands down where it must and runs
// everywhere else.
if options.null_values.is_some() {
return Default::default();
}
crate::schema_union::sample_files(files, format, &Self::read_as(options)).disagreement()
}
/// The reader settings a sample has to copy to describe what the open will do.
///
/// Taken from the options the open is actually being made with, not guessed at and
/// then bailed out of: `from_args_and_config` fills in `infer_schema_length` and
/// `parse_strings` on every run with no flags at all, so a predicate over "did the
/// user set anything" is true every time. That shipped once, and the notes about
/// how a directory had been stacked never appeared outside the tests.
fn read_as(options: &OpenOptions) -> crate::schema_union::ReadAs {
crate::schema_union::ReadAs {
delimiter: options.delimiter,
has_header: options.has_header,
skip_rows: options.skip_rows,
skip_lines: options.skip_lines,
infer_schema_length: options.infer_schema_length,
ignore_errors: options.ignore_errors,
try_parse_dates: options.csv_try_parse_dates(),
comment_char: options.comment_char.clone(),
header_rows: options.header_rows.clone(),
header_join: options.header_join.clone(),
}
}
/// A format spec reads a local file, or the downloaded copy of one remote object
/// (`loading::remote_download`). What reaches here remote is a prefix or a glob,
/// which would otherwise be scanned in place without the spec and say nothing.
fn refuse_spec_in_place(path: &Path, options: &OpenOptions) -> Result<()> {
if source::is_remote_url(path)
&& (options.spec_file.is_some() || options.spec_name.is_some())
{
return Err(crate::error_display::FileError::new(
path,
"a format spec reads one remote object at a time, not a prefix or a glob; name the object",
)
.into());
}
Ok(())
}
/// `found` is what the read has to say about itself, for the caller to put in the
/// dataset's notes: which data files it passed over, and whether the files it did
/// read carry the same columns. Written here rather than worked out by the caller
/// because this is the pass that decides, and a second opinion formed from a second
/// directory read is a second answer waiting to disagree.
fn build_lazyframe_from_paths_with(
cloud: &crate::config::CloudConfig,
paths: &[PathBuf],
options: &OpenOptions,
report: &mut ReadReport,
formats: &crate::formats::Registry,
) -> Result<Scan> {
// Arrow streams converted, or a bucket's Arrow listed: the load says where
// each input's rows are.
if let Some(parts) = &options.arrow_parts {
if options.table.is_some() && options.splits.is_none() {
// Named by an input the user knows, never the converted copy.
let named = parts.first().map(|part| match part {
crate::ipc_stream::Part::InPlace(path) => path.as_path(),
crate::ipc_stream::Part::Converted { source, .. } => source.as_path(),
});
return Err(Self::one_table(named, Some(FileFormat::Arrow)));
}
return Self::scan_arrow_parts(cloud, paths.first(), parts).map(Scan::from);
}
// Only the cloud readers below take the settings.
#[cfg(not(feature = "cloud"))]
let _ = cloud;
let path = &paths[0];
Self::refuse_spec_in_place(path, options)?;
match source::input_source(path) {
source::InputSource::Http(_url) => {
#[cfg(feature = "http")]
{
return Err(color_eyre::eyre::eyre!(
"HTTP/HTTPS load is handled in the event loop; this path should not be reached."
));
}
#[cfg(not(feature = "http"))]
{
return Err(color_eyre::eyre::eyre!(
"HTTP/HTTPS URLs are not supported in this build. Rebuild with default features."
));
}
}
source::InputSource::S3(url) => {
#[cfg(feature = "cloud")]
{
let (full, cloud_opts) =
Self::resolve_cloud_url(Path::new(&format!("s3://{url}")), cloud)?;
let is_glob = source::is_prefix_or_glob(&full);
// The reader the prefix's own format calls for, when the listing
// said what that is. Only Parquet falls through to the scan below.
if let Some(format) = Self::cloud_glob_format(&full, options)
&& let Some(lf) = Self::scan_cloud_prefix(
&full,
cloud_opts.clone(),
format,
is_glob,
options,
)
{
return lf.map(Scan::from);
}
let pl_path = PlRefPath::new(full.as_str());
let hive_options = if is_glob {
polars::io::HiveOptions::new_enabled()
} else {
polars::io::HiveOptions::default()
};
let args = ScanArgsParquet {
cloud_options: Some(cloud_opts),
hive_options,
glob: is_glob,
..Default::default()
};
let lf = LazyFrame::scan_parquet(pl_path, args)
.map_err(|e| Self::cloud_scan_failed(&full, &e))?;
// The frame alone. Building a state here would ask Polars for the
// schema, which lists every file under a prefix, and the schema
// phase that follows lists them once more for itself.
return Ok(lf.into());
}
#[cfg(not(feature = "cloud"))]
{
let _ = url;
return Err(color_eyre::eyre::eyre!(
"S3 is not supported in this build. Rebuild with default features and set AWS credentials (e.g. AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, AWS_REGION)."
));
}
}
source::InputSource::Gcs(url) => {
#[cfg(feature = "cloud")]
{
let (full, cloud_opts) =
Self::resolve_cloud_url(Path::new(&format!("gs://{url}")), cloud)?;
let is_glob = source::is_prefix_or_glob(&full);
// The reader the prefix's own format calls for, when the listing
// said what that is. Only Parquet falls through to the scan below.
if let Some(format) = Self::cloud_glob_format(&full, options)
&& let Some(lf) = Self::scan_cloud_prefix(
&full,
cloud_opts.clone(),
format,
is_glob,
options,
)
{
return lf.map(Scan::from);
}
let pl_path = PlRefPath::new(full.as_str());
let hive_options = if is_glob {
polars::io::HiveOptions::new_enabled()
} else {
polars::io::HiveOptions::default()
};
let args = ScanArgsParquet {
cloud_options: Some(cloud_opts),
hive_options,
glob: is_glob,
..Default::default()
};
let lf = LazyFrame::scan_parquet(pl_path, args)
.map_err(|e| Self::cloud_scan_failed(&full, &e))?;
return Ok(lf.into());
}
#[cfg(not(feature = "cloud"))]
{
let _ = url;
return Err(color_eyre::eyre::eyre!(
"GCS (gs://) is not supported in this build. Rebuild with default features."
));
}
}
source::InputSource::Azure(url) => {
#[cfg(feature = "cloud")]
{
let (full, cloud_opts) = Self::resolve_cloud_url(Path::new(&url), cloud)?;
let is_glob = source::is_prefix_or_glob(&full);
// The reader the prefix's own format calls for, when the listing
// said what that is. Only Parquet falls through to the scan below.
if let Some(format) = Self::cloud_glob_format(&full, options)
&& let Some(lf) = Self::scan_cloud_prefix(
&full,
cloud_opts.clone(),
format,
is_glob,
options,
)
{
return lf.map(Scan::from);
}
let args = ScanArgsParquet {
cloud_options: Some(cloud_opts),
hive_options: if is_glob {
polars::io::HiveOptions::new_enabled()
} else {
polars::io::HiveOptions::default()
},
glob: is_glob,
..Default::default()
};
let lf = LazyFrame::scan_parquet(PlRefPath::new(full.as_str()), args)
.map_err(|e| Self::cloud_scan_failed(&full, &e))?;
return Ok(lf.into());
}
#[cfg(not(feature = "cloud"))]
{
let _ = url;
return Err(color_eyre::eyre::eyre!(
"Azure is not supported in this build. Rebuild with default features."
));
}
}
source::InputSource::Local(_) => {}
}
Self::build_local_lazyframe(paths, options, report, formats)
}
/// The files a directory holds, read as `found`: through the delimited spec the
/// first of them matches, when one does, else as the format says.
fn read_directory_files(
files: &[PathBuf],
options: &OpenOptions,
found: FileFormat,
report: &mut ReadReport,
formats: &crate::formats::Registry,
) -> Result<Scan> {
if options.delimited.is_none()
&& options.format.is_none()
&& found.separator().is_some()
&& let Some(first) = files.iter().find(|f| !crate::nul_tail::holds_nothing(f))
&& let Some(choice) = Self::delimited_spec_of(first, options, formats)?
{
let nested = OpenOptions {
hive: false,
format: Some(found),
splits: report.splits.clone(),
..options.clone()
};
return Self::read_with_delimited_spec(files, &nested, report, formats, choice);
}
// A spec's read says how its files differ itself, from their own header lines.
report.files_disagree = Self::files_disagree(files, options, found);
let nested = OpenOptions {
hive: false,
format: Some(options.format.unwrap_or(found)),
splits: report.splits.clone(),
..options.clone()
};
Self::build_local_lazyframe(files, &nested, report, formats)
}
/// The delimited spec whose glob or magic `file` matches, if one does.
fn delimited_spec_of(
file: &Path,
options: &OpenOptions,
formats: &crate::formats::Registry,
) -> Result<Option<crate::formats::Choice>> {
let asked = crate::formats::Asked {
compression: options.compression,
text_only: true,
..Default::default()
};
match crate::formats::route(file, &asked, formats)
.map_err(|e| color_eyre::eyre::eyre!(e))?
{
crate::formats::Route::Delimited(choice) => Ok(Some(choice)),
_ => Ok(None),
}
}
/// `paths` read with the CSV reader in the dialect of the delimited spec `choice`
/// holds.
fn read_with_delimited_spec(
paths: &[PathBuf],
options: &OpenOptions,
report: &mut ReadReport,
formats: &crate::formats::Registry,
choice: crate::formats::Choice,
) -> Result<Scan> {
let mut nested = options.clone();
let Some(delimited) = choice.spec.delimited.clone() else {
return Err(color_eyre::eyre::eyre!(
"{} is not a delimited spec",
choice.spec.name
));
};
delimited.apply(&mut nested);
nested.delimited = Some(Arc::new(crate::delimited_spec::DelimitedRead::chosen(
choice.spec,
choice.by,
choice.also,
)));
Self::build_local_lazyframe(paths, &nested, report, formats)
}
/// The local half of `build_lazyframe_from_paths_with`. A directory resolves to
/// local files, so the recursion stays here and needs no cloud settings.
fn build_local_lazyframe(
paths: &[PathBuf],
options: &OpenOptions,
report: &mut ReadReport,
formats: &crate::formats::Registry,
) -> Result<Scan> {
let path = &paths[0];
// `--hex`: the file's bytes, whatever it holds.
if options.hex
&& let [one] = paths
&& one.is_file()
{
return Ok(Scan::Hex {
file: one.clone(),
asked: true,
});
}
// A glob of local files the first of which a delimited spec reads: read through
// the spec, file by file, as a directory of them is. Polars' own scan of the
// glob would read the spec's header lines as data.
if let [pattern] = paths
&& !options.hive
&& options.delimited.is_none()
&& options.format.is_none()
&& source::expands_as_glob(pattern)
{
let files = crate::local_glob::expand(pattern);
if let Some(first) = files.iter().find(|f| !crate::nul_tail::holds_nothing(f))
&& let Some(choice) = Self::delimited_spec_of(first, options, formats)?
{
let format = FileFormat::from_path(first).filter(|f| f.separator().is_some());
let nested = OpenOptions {
format: format.or(Some(FileFormat::Csv)),
..options.clone()
};
return Self::read_with_delimited_spec(&files, &nested, report, formats, choice);
}
}
// A format spec: one asked for, or one whose glob or magic the path matches. A
// path whose name or bytes already say what it is opens as it always has, but
// for a delimited spec's text. Several files are matched by the first, and
// only to a delimited spec.
if !options.hive && options.delimited.is_none() {
let asked = crate::formats::Asked {
spec_file: options.spec_file.clone(),
spec_name: options.spec_name.clone(),
variant: options.table.clone(),
spec: options.spec_fetched.clone(),
builtin: options.format.is_some(),
compression: options.compression,
text_only: paths.len() > 1,
};
match crate::formats::route(path, &asked, formats)
.map_err(|e| color_eyre::eyre::eyre!(e))?
{
crate::formats::Route::Elsewhere => {}
crate::formats::Route::Delimited(choice) => {
return Self::read_with_delimited_spec(paths, options, report, formats, choice);
}
crate::formats::Route::Read(read) => {
let lf = Arc::clone(&read.records).into_lazy()?;
report.format_read = Some(Arc::new(*read));
return Ok(lf.into());
}
crate::formats::Route::Decompress(choice) => {
return Ok(Scan::DecompressSpec {
file: path.clone(),
choice,
});
}
}
} else if options.hive && (options.spec_file.is_some() || options.spec_name.is_some()) {
return Err(color_eyre::eyre::eyre!(
"a format spec reads one file, or one directory of column files"
));
}
// The header lines of a delimited spec's first file: its units and metadata.
if let Some(read) = &options.delimited
&& report.delimited.is_none()
&& path.is_file()
{
report.delimited = Some(Arc::new(crate::delimited_spec::read_facts(
read, paths, options,
)?));
}
// One path that is a directory, whether or not `--hive` said so: naming a
// directory is the request to read it, and the dispatch below is what picks the
// reader for what it holds. Behind `options.hive` alone, every route that
// reached here with a directory and without the flag fell through to the
// Parquet scan and answered `Unsupported file type`.
if paths.len() == 1 && (options.hive || path.is_dir()) {
// A file is a file whatever its name holds: `a*b.parquet` is not a glob.
let is_single_file = path.is_file();
if !is_single_file {
// What the directory holds picks the reader. A directory used to go
// straight to the Parquet scan whatever was in it, so a directory of
// `.json.gz` was opened by seeking each file's last four bytes for a
// `PAR1` that was never going to be there — the files were fine, the
// reader was never asked to be the right one.
if path.is_dir()
&& let Some(splits) = crate::hf_splits::dataset_dict(path)
{
return Self::dataset_dict_split(path, &splits, options, report, formats);
}
if path.is_dir() {
match crate::discover::directory_format(path) {
// Flat and Parquet: the scan below is already right for it.
crate::discover::DirectoryFormat::One(FileFormat::Parquet, _) => {}
// Partitions, or an empty directory. The files are a level down
// under `key=value` and only the hive scan walks a tree — but
// hive partitioning is a Parquet-only capability in the reader
// datui uses (`HiveOptions::new_disabled()` is hard-coded for
// CSV and NDJSON), so partitions of anything else cannot be
// read as one table here. Saying which files they are beats
// Parquet's complaint that they do not end with `PAR1`.
crate::discover::DirectoryFormat::Deeper => {
if let crate::discover::DirectoryFormat::One(found, files) =
crate::discover::hive_leaf_format(path)
&& found != FileFormat::Parquet
{
// The extension rather than the format's own name: it
// is what is on the files the user can see.
let named = files
.first()
.and_then(|f| crate::discover::data_extension(f))
.unwrap_or_else(|| format!("{found:?}").to_lowercase());
return Err(color_eyre::eyre::eyre!(
"{} is partitioned into key=value directories of .{} \
files. datui reads hive partitioning for Parquet \
only — open one partition instead.",
path.display(),
named
));
}
}
crate::discover::DirectoryFormat::One(found, files) => {
// Read as the files themselves, through the same readers a
// list of files typed on the command line goes through. An
// explicit `--format` is the user's own answer and outranks
// what the names say.
let format = options.format.unwrap_or(found);
let files =
Self::hugging_face_split(path, format, files, options, report)?;
return Self::read_directory_files(
&files, options, found, report, formats,
);
}
crate::discover::DirectoryFormat::Mixed {
format: found,
files,
passed_over,
} => {
// The commonest format is the table. A directory of a
// thousand CSVs and one stray JSON is a directory of CSVs,
// and refusing the whole of it over the stray was datui
// deciding that a directory it could read was not worth
// reading.
let format = options.format.unwrap_or(found);
let files =
Self::hugging_face_split(path, format, files, options, report)?;
let lf = Self::read_directory_files(
&files, options, found, report, formats,
)?;
// After the call, which reads a flat directory of one format
// and leaves nothing out of its own. A model's config and
// tokenizer JSON are not data the read passed over, and the
// weights are not the commonest format there, so neither is
// said.
if !matches!(found, FileFormat::Safetensors | FileFormat::Gguf) {
report.left_out = passed_over;
}
return Ok(lf);
}
}
}
let use_parquet_hive =
path.is_dir() || path.as_os_str().to_string_lossy().contains(".parquet");
if use_parquet_hive {
// Only build the LazyFrame here; schema and partition discovery are the
// schema phase's ("Reading schema").
return DataTableState::scan_parquet_hive(path).map(Scan::from);
}
return Err(color_eyre::eyre::eyre!(
"With --hive use a directory or a glob pattern for Parquet (e.g. path/to/dir or path/**/*.parquet)"
));
}
}
// A file with no extension may still be Parquet: a part file in a directory named
// `.parquet`. A regular file is only read when nothing else settled it. A name
// that says text (`.log`, `.txt`) is read as lines unless its bytes say a format:
// candump writes `.log`.
let compressed = options
.compression
.or_else(|| CompressionFormat::from_extension(path))
.is_some();
// Under a compression suffix, the name before it says delimited text or lines
// (`x.tsv.gz`, `app.log.gz`).
let named = FileFormat::from_path(path).or_else(|| {
compressed
.then(|| FileFormat::from_path(Path::new(path.file_stem()?)))
.flatten()
.filter(|f| f.decompressed_once())
});
let mut effective_format = options
.format
// A name that says a format another refines is asked its bytes for it:
// journal JSON in a `.json` file.
.or_else(|| {
named.filter(|f| !f.is_lines()).map(|f| {
(!compressed)
.then(|| crate::readers::refined(path, f))
.flatten()
.unwrap_or(f)
})
})
.or_else(|| {
(path.extension().is_none()
&& crate::discover::is_parquet_key(&path.to_string_lossy()))
.then_some(FileFormat::Parquet)
})
// Any other file whose name says no format, by its first bytes: each
// format's signature says where it is believed (`crate::readers`).
.or_else(|| crate::readers::sniff_open(path, options.compression))
.or(named);
// Text no signature claims: JSON, CSV or TSV on evidence, lines otherwise. Bytes
// that are not text are shown as they are.
if effective_format.is_none()
&& let [file] = paths
&& file.is_file()
{
effective_format = crate::lines::guess_file(file, options.compression)
.map(|f| crate::lines::as_asked(f, options));
report.guessed = effective_format.is_some();
}
report.format = effective_format;
// Refused rather than ignored: a file of one table opened with `--table` would
// otherwise look like the table asked for.
if options.table.is_some()
&& !effective_format.is_some_and(FileFormat::takes_table)
&& options.splits.is_none()
{
return Err(Self::one_table(Some(path), effective_format));
}
// One compressed CSV, TSV, PSV or text file, as a directory of one resolves to:
// the load decompresses it (`Step::Decompress`) into a copy the dataset holds.
// Read here, the copy went with the state dropped below and the frame scanned
// nothing.
if let [file] = paths
&& compressed
&& let Some(format) = effective_format.filter(|f| f.decompressed_once())
{
return Ok(Scan::Decompress {
file: file.clone(),
format,
});
}
let Some(format) = effective_format else {
if !path.exists() {
return Err(std::io::Error::new(
std::io::ErrorKind::NotFound,
format!("File not found: {}", path.display()),
)
.into());
}
// A local file nothing reads is shown as its bytes (#588).
if paths.len() == 1 && path.is_file() {
return Ok(Scan::Hex {
file: path.clone(),
asked: false,
});
}
return Err(color_eyre::eyre::eyre!(match paths.len() {
1 => UNSUPPORTED.to_string(),
_ => crate::readers::many_files_refused(),
}));
};
// The home screen asks `reads_many_files` before it offers a directory as one
// dataset, and this is the same question, so it cannot offer one this refuses.
if paths.len() > 1 && !format.reads_many_files() {
if !path.exists() {
return Err(std::io::Error::new(
std::io::ErrorKind::NotFound,
format!("File not found: {}", path.display()),
)
.into());
}
return Err(color_eyre::eyre::eyre!(crate::readers::many_files_refused()));
}
let guessed;
let options = if report.guessed {
guessed = OpenOptions {
format_guessed: true,
..options.clone()
};
&guessed
} else {
options
};
crate::readers::scan(crate::readers::ScanIn {
format,
paths,
options,
report,
formats,
})
}
/// Whether the help overlay is on screen.
pub fn help_visible(&self) -> bool {
self.help.is_open()
}
/// The screen the help overlay shows the keys of, while it is up.
pub fn help_context(&self) -> Option<datui_cli::keys::Context> {
self.help.context()
}
/// Open the views list for the dataset on screen, scored against it.
fn open_view_list(&mut self) {
if self.view_dataset().is_none() {
return;
}
self.view_modal.table_state.select(Some(0));
self.refresh_view_list();
self.view_modal.active = true;
self.view_modal.mode = ViewModalMode::List;
}
/// Rebuild the list's rows from the store, scored and annotated against
/// the open dataset; the selection stays near where it was.
fn refresh_view_list(&mut self) {
let (Some(state), Some(dataset)) = (&self.data_table_state, self.view_dataset()) else {
return;
};
let rows: Vec<ViewRow> = self
.view_manager
.find_relevant_views(dataset, state.source_schema())
.into_iter()
.map(|(view, score)| {
let reason = view::match_reason(&view, dataset, state.source_schema());
ViewRow {
view,
score,
reason,
}
})
.collect();
self.view_modal.broken_views = self.view_manager.broken_views.clone();
let selected = self.view_modal.table_state.selected().unwrap_or(0);
self.view_modal.table_state.select(if rows.is_empty() {
None
} else {
Some(selected.min(rows.len() - 1))
});
self.view_modal.rows = rows;
}
/// Open the save-view form prefilled from the open dataset: a name the
/// user will recognize, this file's paths and patterns as criteria, and
/// schema match on — the criterion that carries the view to the next
/// table shaped like this one.
fn open_save_view_form(&mut self) {
self.view_modal
.enter_create_mode(self.history_limit, &self.theme);
let query = self.data_table_state.as_ref().and_then(|state| {
let (query, sql_query, fuzzy_query) = active_query_settings(
state.get_active_query(),
state.get_active_sql_query(),
state.get_active_fuzzy_query(),
);
sql_query.or(fuzzy_query).or(query)
});
self.view_modal.name_input.suggest(
self.view_manager
.suggest_name(self.path.as_deref(), query.as_deref()),
);
// Data piped in has no file to pin; its columns are what match it.
if let Some(path) = self.path.as_ref().filter(|_| !self.reads_stdin()) {
// Pin this file: its absolute path or URL, its path relative to the
// working directory when it is local and under it, and glob suggestions.
let absolute_path = view::exact_location(path);
self.view_modal
.exact_path_input
.suggest(absolute_path.to_string_lossy());
if let Some(relative) = view::relative_location(path) {
self.view_modal.relative_path_input.suggest(relative);
}
// Suggest a path pattern from the absolute path: the parent of a
// bare relative name is "", and ""/*.parquet is a pattern that
// matches every parquet file anywhere, forever. The separator is the
// path's own, or a Windows path never fits its pattern.
if let Some(parent) = absolute_path.parent()
&& let Some(parent_str) = parent.to_str()
&& !parent_str.is_empty()
&& let Some(ext) = absolute_path.extension()
{
let separator = if crate::source::is_remote_url(path) {
'/'
} else {
std::path::MAIN_SEPARATOR
};
self.view_modal.path_pattern_input.suggest(format!(
"{}{separator}*.{}",
parent_str.trim_end_matches(separator),
ext.to_string_lossy()
));
}
// Suggest a filename pattern with digit runs wildcarded, so
// sales_2024.csv offers itself to sales_2025.csv.
if let Some(filename) = path.file_name()
&& let Some(filename_str) = filename.to_str()
{
use regex::Regex;
let pattern = match Regex::new(r"\d+") {
Ok(re) => re.replace_all(filename_str, "*").to_string(),
Err(_) => filename_str.to_string(),
};
self.view_modal.filename_pattern_input.suggest(pattern);
}
}
self.view_modal.table = self.view_table().map(str::to_string);
// Schema match starts on: "apply this to a similar table" is the
// reason views exist, and the columns are the only criterion that
// says similar.
if let Some(ref state) = self.data_table_state
&& !state.source_schema().is_empty()
{
self.view_modal.schema_match_enabled = true;
}
}
/// Validate and persist the form: a new view, or the edited one. The
/// settings are rebuilt from the table's applied state either way. A
/// failed save keeps the form open.
fn save_view_form(&mut self) {
self.view_modal.name_error = None;
let name = self.view_modal.name_input.value().trim().to_string();
if name.is_empty() {
self.view_modal.name_error = Some("name is required".to_string());
self.view_modal.form_focus = FormFocus::Name;
return;
}
let renaming_to_taken = match &self.view_modal.editing_view_id {
None => self.view_manager.view_exists(&name),
Some(id) => self
.view_manager
.get_view_by_name(&name)
.is_some_and(|other| other.id != *id),
};
if renaming_to_taken {
self.view_modal.name_error = Some("name already exists".to_string());
self.view_modal.form_focus = FormFocus::Name;
return;
}
let non_empty = |input: &widgets::text_input::TextInput| {
let value = input.value().trim();
(!value.is_empty()).then(|| value.to_string())
};
let match_criteria = view::MatchCriteria {
exact_path: non_empty(&self.view_modal.exact_path_input).map(std::path::PathBuf::from),
relative_path: non_empty(&self.view_modal.relative_path_input),
path_pattern: non_empty(&self.view_modal.path_pattern_input),
filename_pattern: non_empty(&self.view_modal.filename_pattern_input),
// The columns the view's settings run on, not the query's output: the
// next file is matched as loaded.
schema_columns: if self.view_modal.schema_match_enabled {
self.data_table_state.as_ref().map(|state| {
state
.source_schema()
.iter_names()
.map(|s| s.to_string())
.collect()
})
} else {
None
},
schema_types: None,
table: self.view_modal.table.clone(),
};
let description = {
let value = self.view_modal.description_input.value();
(!value.is_empty()).then(|| value.to_string())
};
let saved = if let Some(editing_id) = self.view_modal.editing_view_id.clone() {
let Some(mut view) = self.view_manager.get_view_by_id(&editing_id).cloned() else {
return;
};
view.name = name;
view.description = description;
let stored_schema = view.match_criteria.schema_columns.take();
view.match_criteria = match_criteria;
let editing_the_active_view =
self.active_view_id.as_deref() == Some(editing_id.as_str());
// The same principle as the settings below: editing an unapplied
// view must not swap the columns it matches on for the columns of
// whatever table happens to be open. The toggle still works — off
// drops the criterion — and the active view follows its table.
if !editing_the_active_view
&& self.view_modal.schema_match_enabled
&& stored_schema.is_some()
{
view.match_criteria.schema_columns = stored_schema;
}
// The settings follow the table only while this view is the one
// dressing it. Editing an unapplied view changes its name,
// description and matching alone — it must not overwrite what
// the view carries with whatever the table happens to show.
if editing_the_active_view && let Some(state) = &self.data_table_state {
view.settings = view_settings_of(state);
view.settings.chart = self.saved_chart();
}
match self.view_manager.update_view(&view) {
Ok(()) => true,
Err(e) => {
// Deleted elsewhere, it has left the list too; otherwise the form
// stays, edits and all, to try again.
if self.view_manager.get_view_by_id(&editing_id).is_none() {
self.refresh_view_list();
self.view_modal.exit_form();
}
self.error_modal.show(format!("Error saving view: {e}"));
return;
}
}
} else {
self.create_view_from_current_state(name, description, match_criteria)
.is_ok()
};
if saved {
self.refresh_view_list();
self.view_modal.exit_form();
}
}
/// The selected view's score breakdown, for the list's `i` popup.
fn view_score_details(&self) -> Option<(String, String)> {
let state = self.data_table_state.as_ref()?;
let path = self.view_dataset()?;
let idx = self.view_modal.table_state.selected()?;
let row = self.view_modal.rows.get(idx)?;
let view = &row.view;
let exact_path_match = view::exact_path_matches(&view.match_criteria, path);
let relative_path_match = view::relative_path_matches(&view.match_criteria, path);
let file_cols: std::collections::HashSet<&str> = state
.source_schema()
.iter_names()
.map(|s| s.as_str())
.collect();
let exact_schema_match =
view.match_criteria
.schema_columns
.as_ref()
.is_some_and(|required| {
let required: std::collections::HashSet<&str> =
required.iter().map(|s| s.as_str()).collect();
required.is_subset(&file_cols) && file_cols.len() == required.len()
});
let mut details = format!("Total score: {:.1}\n\n", row.score);
if exact_path_match && exact_schema_match {
details.push_str("Exact path + exact schema: 2000.0\n");
} else if exact_path_match {
details.push_str("Exact path: 1000.0\n");
} else if relative_path_match && exact_schema_match {
details.push_str("Relative path + exact schema: 1950.0\n");
} else if relative_path_match {
details.push_str("Relative path: 950.0\n");
} else if exact_schema_match {
details.push_str("Exact schema: 900.0\n");
} else {
if view::path_pattern_matches(&view.match_criteria, path) {
details.push_str("Path pattern match: 50.0+\n");
}
if view::filename_pattern_matches(&view.match_criteria, path) {
details.push_str("Filename pattern match: 30.0+\n");
}
if let Some(required_cols) = &view.match_criteria.schema_columns {
let matching_count = required_cols
.iter()
.filter(|col| file_cols.contains(col.as_str()))
.count();
if matching_count > 0 {
details.push_str(&format!(
"Partial schema match: {:.1} ({} columns)\n",
matching_count as f64 * 2.0,
matching_count
));
}
}
}
if view.usage_count > 0 {
details.push_str(&format!(
"Usage count: {:.1}\n",
(view.usage_count.min(10) as f64) * 1.0
));
}
if let Some(last_used) = view.last_used
&& let Ok(duration) = std::time::SystemTime::now().duration_since(last_used)
{
let days_since = duration.as_secs() / 86400;
if days_since <= 7 {
details.push_str("Recent usage: 5.0\n");
} else if days_since <= 30 {
details.push_str("Recent usage: 2.0\n");
}
}
Some((format!("Score: {}", view.name), details))
}
/// Open the help overlay on the keys of the screen it is opened at. No-op if it is
/// already up.
pub(crate) fn open_help_overlay(&mut self) {
// A question or an error under the help would take its keys unseen.
if self.help.is_open() || self.confirmation_modal.active || self.error_modal.active {
return;
}
let context = self.keys_context();
// The home filter types too once something is typed into it.
let typing = self.text_field_focused()
|| (self.input_mode == InputMode::Home
&& !self.documentation.is_open()
&& (!self.home.filter.is_empty() || self.home.path_input_active));
self.help.open(context, typing);
}
/// Close the help when the screen under it changed on its own (a query that
/// finished, a load that failed): its keys are for a screen that is gone, and
/// Enter would press one of them on another. A question or an error that arrived
/// under it takes the keys, so it closes for those too.
fn close_help_left_behind(&mut self) {
let left = self
.help
.context()
.is_some_and(|shown| shown != self.keys_context());
if left || self.confirmation_modal.active || self.error_modal.active {
self.help.close();
}
}
/// The screen the keys typed now go to, as the key registry names it.
pub fn keys_context(&self) -> datui_cli::keys::Context {
use crate::analysis_modal::{AnalysisTool, AnalysisView};
use datui_cli::keys::Context;
if self.analysis_modal.active {
return match self.analysis_modal.view {
AnalysisView::DistributionDetail => Context::DistributionDetail,
AnalysisView::CorrelationDetail => Context::CorrelationDetail,
AnalysisView::Main => match self.analysis_modal.selected_tool {
Some(AnalysisTool::DistributionAnalysis) => Context::Distribution,
Some(AnalysisTool::CorrelationMatrix) => Context::Correlation,
Some(AnalysisTool::DataQuality) => Context::DataQuality,
Some(AnalysisTool::Describe) | None => Context::Describe,
},
};
}
if self.view_modal.active {
return Context::Views;
}
match self.input_mode {
InputMode::Normal => Context::Table,
InputMode::Editing => match self.input_type {
Some(InputType::Find) => Context::Find,
_ => Context::Query,
},
InputMode::SortFilter => Context::SortFilter,
InputMode::PivotMelt => Context::PivotMelt,
InputMode::Export => Context::Export,
InputMode::Copy => Context::Copy,
InputMode::Inspect => Context::Inspector,
InputMode::GoToColumn => Context::GoToColumn,
InputMode::PickFormat => Context::FormatPicker,
InputMode::Retype => Context::Retype,
InputMode::Combine => Context::Combine,
InputMode::PickTable => Context::TablePicker,
InputMode::Sample => Context::Sample,
InputMode::Info => Context::Info,
InputMode::Chart => Context::Chart,
InputMode::Home if self.documentation.is_open() => Context::Documentation,
InputMode::Home => Context::Home,
InputMode::Hex => Context::Hex,
InputMode::ValueCounts => Context::ValueCounts,
}
}
/// True while the confirmation modal is asking whether to download a remote file,
/// or to read a large one whole into memory.
///
/// Those are the confirmations the user has to be able to walk away from: the
/// size probe behind a download can take fifteen seconds, and the answer to
/// "actually, never mind" is the home screen, not the exit.
pub fn awaiting_open_confirmation(&self) -> bool {
self.confirmation_modal.active && self.loading.asking()
}
fn key(&mut self, event: &KeyEvent) -> Option<AppEvent> {
self.debug.on_key(event);
// A completion flash lives until the next key: whatever this key does,
// the bar's line about the last action is stale now.
self.flash = None;
// A key puts back a header being carried, so a release later moves nothing.
self.cancel_drag();
let ctrl = event.modifiers.contains(KeyModifiers::CONTROL);
// Ctrl-Q quits from anywhere, before any mode gets a say — including a mode
// with no CONTROL arm of its own (the chart view) that would otherwise swallow
// it while busy.
if ctrl && event.code == KeyCode::Char('q') {
return Some(AppEvent::Exit);
}
// Ctrl-C too, a text field included: a terminal user's reflex for leaving, and
// the field copies with Alt+W instead.
if ctrl && event.code == KeyCode::Char('c') {
return Some(AppEvent::Exit);
}
// The context menu takes its keys first. A line chosen closes it and presses
// its key, offered as typed (as Enter on a help line is); any other key closes
// it and then acts as it would have. A menu something else has covered since
// (an error, a load's screen) is gone.
if !self.menu_showing() {
self.context_menu = None;
}
if let Some(menu) = self.context_menu.as_mut() {
match menu.key(event) {
context_menu::MenuKey::Moved => return None,
context_menu::MenuKey::Close => {
self.context_menu = None;
return None;
}
context_menu::MenuKey::Run(key) => {
self.context_menu = None;
return Some(AppEvent::Press(key));
}
context_menu::MenuKey::Do(action) => {
self.context_menu = None;
self.menu_action(action);
return None;
}
context_menu::MenuKey::Other => self.context_menu = None,
}
}
// Acts at once (see `hard_escape_while_busy`), ahead of the keys held behind
// the view.
if event.code == KeyCode::Esc && self.view_applying() {
self.cancel_view();
return None;
}
// The same for a find that is reading.
if event.code == KeyCode::Esc && self.finding() {
self.cancel_find();
return None;
}
// And for a count of footers, at the table its progress line is on.
if event.code == KeyCode::Esc
&& self.input_mode == InputMode::Normal
&& self.in_normal_table_view()
&& self.footers_counted().is_some()
{
self.stop_count();
return None;
}
if event.code == KeyCode::Esc
&& self.input_mode == InputMode::Normal
&& !self.analysis_modal.active
&& !self.error_modal.active
&& !self.confirmation_modal.active
&& self.return_from_quality_evidence(true)
{
return None;
}
// F1 opens help first so no other branch (e.g. Editing) can consume it; again,
// it closes it.
if event.code == KeyCode::F(1) {
if self.help.is_open() {
self.help.close();
} else {
self.open_help_overlay();
}
return None;
}
// Home owns the whole screen and every key while it is up — except under a
// modal or the help overlay. Both render over home unconditionally, so if
// home also ate their keys they would be undismissable, and Esc would try
// to leave home instead.
if self.input_mode == InputMode::Home
&& !self.confirmation_modal.active
&& !self.error_modal.active
&& !self.help.is_open()
{
return self.home_key(event);
}
// Ctrl+O goes home from anywhere, including mid-load. That is what makes
// browsing cheap: opening the wrong 300 MB file costs one keystroke to leave,
// not a wait for it to finish.
if event.code == KeyCode::Char('o')
&& event.modifiers.contains(KeyModifiers::CONTROL)
&& (!self.confirmation_modal.active || self.awaiting_open_confirmation())
{
self.help.close();
self.enter_home();
return None;
}
// Handle modals first - they have highest priority
// Confirmation modal (for overwrite)
if self.confirmation_modal.active {
match event.code {
KeyCode::Left | KeyCode::Char('h') => {
self.confirmation_modal.focus_yes = true;
}
KeyCode::Right | KeyCode::Char('l') => {
self.confirmation_modal.focus_yes = false;
}
KeyCode::Tab => {
// Toggle between Yes and No
self.confirmation_modal.focus_yes = !self.confirmation_modal.focus_yes;
}
// ←→ carry the choice, so ↑↓ (k/j) scroll a long question; the
// render clamps the offset.
KeyCode::Up | KeyCode::Char('k') => {
self.confirmation_modal.scroll =
self.confirmation_modal.scroll.saturating_sub(1);
}
KeyCode::Down | KeyCode::Char('j') => {
self.confirmation_modal.scroll =
self.confirmation_modal.scroll.saturating_add(1);
}
KeyCode::Enter if self.pending_leave.is_some() => {
let stop = self.confirmation_modal.focus_yes;
self.confirmation_modal.hide();
return self.leave_recording(stop);
}
KeyCode::Enter => {
if self.confirmation_modal.focus_yes {
// The confirmations that are not about overwriting a file come
// first: reading every row, and forgetting recents.
if std::mem::take(&mut self.pending_read_all) {
self.confirmation_modal.hide();
// Every row is a sample method like the others: it shows in
// the strip, and `s` changes it back.
let sample = sampling::Sample {
method: sampling::SampleMethod::EveryRow,
..self.analysis_modal.sample.clone()
};
return self.apply_sample(sample);
}
if let Some(url) = self.pending_link.take() {
self.confirmation_modal.hide();
return Some(AppEvent::OpenLink(url));
}
if self.pending_clear_recents {
self.pending_clear_recents = false;
self.confirmation_modal.hide();
self.cache.clear_recents();
self.home_refresh();
self.home.status = None;
return None;
}
// A full scan agreed to: Setup runs, past the question.
if self.analysis_modal.data_quality_confirm_run {
self.confirmation_modal.hide();
let event = self.run_quality_setup();
// Asked once: a run that waits or is refused asks again.
self.analysis_modal.data_quality_confirm_run = false;
return event;
}
if std::mem::take(&mut self.pending_hide_examples) {
self.confirmation_modal.hide();
self.cache.hide_examples();
self.home_refresh();
self.home.select_first_entry();
return None;
}
if let Some(id) = self.pending_delete_view.take() {
self.confirmation_modal.hide();
if self.view_manager.delete_view(&id).is_ok() {
self.refresh_view_list();
}
return None;
}
if let Some(place) = self.pending_forget_place.take() {
self.confirmation_modal.hide();
let paths = self.home.recents_in(&place);
self.cache.forget_recents(&paths);
self.home_refresh();
self.home.status = None;
return None;
}
// The overwrite was agreed to: each export may now replace the
// file it asked about, and only through that answer.
if let Some((path, format)) = self.pending_quality_export.take() {
self.confirmation_modal.hide();
return Some(AppEvent::QualityReportExport(
path,
format,
Overwrite::Replace,
));
}
if let Some(request) = self.pending_chart_export.take() {
self.confirmation_modal.hide();
return Some(AppEvent::ChartExport(ChartExportRequest {
overwrite: Overwrite::Replace,
..request
}));
}
if let Some(request) = self.pending_export.take() {
self.confirmation_modal.hide();
return Some(AppEvent::Export(ExportRequest {
overwrite: Overwrite::Replace,
..request
}));
}
if let Some((format, header)) = self.pending_copy.take() {
self.confirmation_modal.hide();
return Some(AppEvent::CopyTable { format, header });
}
if self.loading.asking() {
self.confirmation_modal.hide();
// The loader lets go of its hold on the generation as the
// download or the read starts, and its job takes it before
// anything else can look.
let step = self.loading.confirmed();
return self.run_load_step(step);
}
} else {
self.pending_clear_recents = false;
self.pending_link = None;
self.pending_read_all = false;
self.pending_forget_place = None;
self.pending_delete_view = None;
self.pending_hide_examples = false;
// Declining the full read leaves the draft staged, and the
// sample and report as they were.
self.analysis_modal.data_quality_confirm_run = false;
// Declining an overwrite returns to the filled form:
// the typed path, format and options survive the No.
if self.pending_chart_export.take().is_some() {
self.chart_export_modal.resume();
}
// The report's dialog stays open behind the question.
self.pending_quality_export = None;
if self.pending_export.take().is_some() {
self.export_modal.resume();
self.input_mode = InputMode::Export;
}
self.pending_copy = None;
if self.loading.asking() {
self.enter_home();
return None;
}
self.confirmation_modal.hide();
}
}
KeyCode::Esc => {
// Disarmed on every exit from the modal, so a declined confirmation
// cannot fire against whatever the *next* one is asking about.
self.pending_clear_recents = false;
self.pending_link = None;
self.pending_read_all = false;
self.pending_forget_place = None;
self.pending_delete_view = None;
self.pending_hide_examples = false;
self.analysis_modal.data_quality_confirm_run = false;
// Staying: the recording goes on, and so does the view.
self.pending_leave = None;
// Declining an overwrite returns to the filled form: the
// typed path, format and options survive the Esc.
if self.pending_chart_export.take().is_some() {
self.chart_export_modal.resume();
}
self.pending_quality_export = None;
if self.pending_export.take().is_some() {
self.export_modal.resume();
self.input_mode = InputMode::Export;
}
self.pending_copy = None;
if self.loading.asking() {
// Declining a download used to quit datui outright, which made
// a remote open the one thing in the app you could not back out
// of. `enter_home` puts the open down and hides this.
self.enter_home();
return None;
}
self.confirmation_modal.hide();
}
_ => {}
}
return None;
}
// Error modal
if self.error_modal.active {
match event.code {
// A long diagnostic scrolls; the render clamps the offset.
KeyCode::Up | KeyCode::Char('k') => {
self.error_modal.scroll = self.error_modal.scroll.saturating_sub(1);
return None;
}
KeyCode::Down | KeyCode::Char('j') => {
self.error_modal.scroll = self.error_modal.scroll.saturating_add(1);
return None;
}
KeyCode::PageUp => {
self.error_modal.scroll = self.error_modal.scroll.saturating_sub(8);
return None;
}
KeyCode::PageDown => {
self.error_modal.scroll = self.error_modal.scroll.saturating_add(8);
return None;
}
KeyCode::Esc | KeyCode::Enter => {
self.error_modal.hide();
// With nothing loaded, dismissing the error would otherwise leave
// an empty table and no indication of what to do. Go back to the
// list the dataset was chosen from, carrying the reason, so the
// next choice is one keystroke away.
if self.data_table_state.is_none() {
let reason = self.last_load_error.take();
self.enter_home();
self.home.status = reason;
}
}
_ => {}
}
return None;
}
// Main table: the column cursor keys (before help/mode blocks so they always work
// in Normal). No is_press()/is_release() check: some terminals do not report key
// kind correctly. Exclude view/analysis modals so they can handle Left/Right
// themselves.
let in_main_table = !(self.input_mode != InputMode::Normal
|| self.help.is_open()
|| self.view_modal.active
|| self.analysis_modal.active);
// The footer offers the column's keys once the column cursor moves, until a
// key that is not about the column.
if in_main_table && event.is_press() {
self.column_hints = Self::column_cursor_key(event).is_some()
|| (self.column_hints
&& matches!(
event.code,
KeyCode::Char(
'+' | '-' | 'F' | '[' | ']' | 'H' | 'L' | '<' | '>' | '=' | 'w'
)
));
}
if in_main_table
&& let Some(mv) = Self::column_cursor_key(event)
&& let Some(state) = self.data_table_state.as_mut()
{
state.move_cursor(mv);
if self.debug.enabled {
self.debug.last_action = format!("move_cursor({mv:?})");
}
return None;
}
// The help owns the keys while it is up. Enter on a line closes it and presses
// that line's key: handed back as this key's follow-up, it reaches the screen
// under the help the way a typed key does, held while the app is busy.
self.close_help_left_behind();
if self.help.is_open() {
return match self.help.key(event) {
help::HelpKey::Press(key) => Some(AppEvent::Press(key)),
help::HelpKey::Stay | help::HelpKey::Closed => None,
};
}
if event.code == KeyCode::Char('?') {
let ctrl_help = event.modifiers.contains(KeyModifiers::CONTROL);
// The home screen always accepts characters, into its filter or path input.
let in_text_input = self.text_field_focused() || self.input_mode == InputMode::Home;
// Ctrl-? always opens help; bare ? only when not in a text field
if ctrl_help || !in_text_input {
self.open_help_overlay();
return None;
}
}
if self.input_mode == InputMode::SortFilter {
return self.sort_filter_key(event);
}
if self.input_mode == InputMode::Export {
return self.export_key(event);
}
if self.input_mode == InputMode::Sample {
return self.table_sample_form_key(event);
}
if self.input_mode == InputMode::Inspect {
return self.inspector_key(event);
}
if self.input_mode == InputMode::ValueCounts {
return self.value_counts_key(event);
}
if self.input_mode == InputMode::Hex {
return self.hex_key(event);
}
if self.input_mode == InputMode::GoToColumn {
self.go_to_column_key(event);
return None;
}
if self.input_mode == InputMode::PickFormat {
return self.format_picker_key(event);
}
if self.input_mode == InputMode::Retype {
return self.retype_key(event);
}
if self.input_mode == InputMode::Combine {
return self.combine_key(event);
}
if self.input_mode == InputMode::PickTable {
return self.table_picker_key(event);
}
if self.input_mode == InputMode::Copy {
return self.copy_key(event);
}
if self.input_mode == InputMode::PivotMelt {
return self.pivot_melt_key(event);
}
if self.input_mode == InputMode::Info {
return self.info_key(event);
}
if self.input_mode == InputMode::Chart {
return self.chart_key(event);
}
if self.analysis_modal.active {
return self.analysis_key(event);
}
if self.view_modal.active {
return self.view_key(event);
}
if self.input_mode == InputMode::Editing {
return self.editing_key(event);
}
const RIGHT_KEYS: [KeyCode; 2] = [KeyCode::Right, KeyCode::Char('l')];
const LEFT_KEYS: [KeyCode; 2] = [KeyCode::Left, KeyCode::Char('h')];
const DOWN_KEYS: [KeyCode; 2] = [KeyCode::Down, KeyCode::Char('j')];
const UP_KEYS: [KeyCode; 2] = [KeyCode::Up, KeyCode::Char('k')];
// The letter arms below are unmodified keys. Without this guard the
// bare-`Char` matches also fired with Ctrl or Alt held, so Ctrl+E
// opened Export and Ctrl+R reversed — bindings nobody declared.
// Paging (Ctrl+F/B/D/U) is the only modified set this match owns;
// the global escapes were handled before reaching here.
if event
.modifiers
.intersects(KeyModifiers::CONTROL | KeyModifiers::ALT)
&& !matches!(event.code, KeyCode::Char('f' | 'b' | 'd' | 'u'))
{
return None;
}
match event.code {
// q pops the context: opened from the home screen, it returns
// there; launched straight onto a file, it quits as it always
// has. Q and Ctrl+Q stay unconditional.
KeyCode::Char('q') => {
if self.opened_from_home {
self.enter_home();
None
} else {
Some(AppEvent::Exit)
}
}
KeyCode::Char('Q') => Some(AppEvent::Exit),
KeyCode::Char('R') => Some(AppEvent::Reset),
KeyCode::Char('H' | 'L') if event.is_press() => {
self.move_cursor_column(event.code == KeyCode::Char('L'))
}
KeyCode::Char('+' | '-') if event.is_press() => {
self.quick_filter(event.code == KeyCode::Char('+'))
}
KeyCode::Char('#') => {
let renumbered = self
.data_table_state
.as_mut()
.is_some_and(|state| state.deferred(|s| s.toggle_row_numbers()));
if renumbered {
self.spawn_async_collect(Self::LOADING_BUFFER);
}
None
}
// The column cursor's width, applied as typed so its effect shows (#647).
KeyCode::Char('<' | '>' | '=' | 'w')
if event.is_press() && !event.modifiers.contains(KeyModifiers::CONTROL) =>
{
if let Some(state) = self.data_table_state.as_mut()
&& let Some(name) = state.current_column().map(str::to_string)
{
// From the width on screen: `>` on a column filling the right edge
// widens what is seen.
let (choice, shown) = (state.width_choice(&name), state.on_screen_width(&name));
let width = match event.code {
KeyCode::Char('<') => choice.narrower(shown),
KeyCode::Char('>') => choice.wider(shown),
KeyCode::Char('=') => WidthChoice::Fit,
_ => WidthChoice::Auto,
};
state.set_width_choices([(name, width)]);
}
None
}
// `/` finds, as in less and vim; `f` too. Ctrl+F pages down, below.
KeyCode::Char('/' | 'f')
if event.is_press() && !event.modifiers.contains(KeyModifiers::CONTROL) =>
{
self.open_find();
None
}
KeyCode::Char('[' | ']') if event.is_press() => {
self.sort_by_cursor_column(event.code == KeyCode::Char(']'))
}
KeyCode::Char('n') if event.is_press() => {
self.find_again(find::Direction::Next);
None
}
KeyCode::Char('N') if event.is_press() => {
self.find_again(find::Direction::Previous);
None
}
KeyCode::Char('D') => {
// The type row is drawn from the schema the table already has, so
// this is a render-time flip like `,`. Session-only.
self.dtype_row = !self.dtype_row;
if self.debug.enabled {
self.debug.last_action = format!(
"toggle_dtype_row({})",
if self.dtype_row { "on" } else { "off" }
);
}
None
}
KeyCode::Char('F') => {
self.open_value_counts();
None
}
KeyCode::Char(',') => {
// Formatting is applied at render time, so this takes effect on
// the next frame with no re-collect. Session-only: the config
// file stays the source of truth at launch.
self.number_format.enabled = !self.number_format.enabled;
if self.debug.enabled {
self.debug.last_action = format!(
"toggle_number_format({})",
if self.number_format.enabled {
"on"
} else {
"off"
}
);
}
None
}
KeyCode::Esc => {
// The find is the nearest layer: its mark goes first, then a drill.
if self.find_shown() {
self.find.active = None;
return None;
}
// A sample being drawn stops, keeping the rows so far.
if self.sample_drawing() {
self.stop_sample_draw();
return None;
}
let mut from_counts = false;
let drilled_up = if let Some(ref mut state) = self.data_table_state {
if state.is_drilled_down() {
from_counts = state.drilled_into_value();
let _ = state.deferred(|s| s.drill_up());
true
} else {
false
}
} else {
false
};
if drilled_up {
self.sync_sort_filter_modal();
}
// Out of a drill from Value Counts, back to the counts it came from:
// the view is the one they were read of.
if from_counts
&& std::mem::take(&mut self.value_counts.drill_return)
&& let Some(state) = self.data_table_state.as_ref()
{
self.value_counts.rebase(state.len_generation());
self.input_mode = InputMode::ValueCounts;
}
if drilled_up {
self.spawn_async_collect(Self::LOADING_BUFFER);
return None;
}
// Out of a follow: the rows read so far stay.
if let Some(state) = self.data_table_state.as_mut()
&& state.follow().is_some()
{
state.stop_following();
self.flash_note("Stopped following".to_string());
}
// Escape no longer exits - use 'q' or Ctrl-C to exit
// (Info modal handles Esc in its own block)
None
}
KeyCode::Char('t') if event.is_press() => self.toggle_follow(),
code if RIGHT_KEYS.contains(&code) || LEFT_KEYS.contains(&code) => {
if let Some(ref mut state) = self.data_table_state {
state.move_cursor(if RIGHT_KEYS.contains(&code) {
crate::widgets::column_paging::CursorMove::Right
} else {
crate::widgets::column_paging::CursorMove::Left
});
}
None
}
code if event.is_press() && DOWN_KEYS.contains(&code) => {
let would_collect = self
.data_table_state
.as_ref()
.map(|s| s.scroll_would_trigger_collect(1))
.unwrap_or(false);
if would_collect {
self.busy = true;
Some(AppEvent::DoScrollNext)
} else {
if let Some(ref mut s) = self.data_table_state {
s.select_next();
}
None
}
}
code if event.is_press() && UP_KEYS.contains(&code) => {
let would_collect = self
.data_table_state
.as_ref()
.map(|s| s.scroll_would_trigger_collect(-1))
.unwrap_or(false);
if would_collect {
self.busy = true;
Some(AppEvent::DoScrollPrev)
} else {
if let Some(ref mut s) = self.data_table_state {
s.select_previous();
}
None
}
}
KeyCode::PageDown if event.is_press() => {
let would_collect = self
.data_table_state
.as_ref()
.map(|s| s.scroll_would_trigger_collect(s.visible_rows as i64))
.unwrap_or(false);
if would_collect {
self.busy = true;
Some(AppEvent::DoScrollDown)
} else {
if let Some(ref mut s) = self.data_table_state {
s.page_down();
}
None
}
}
KeyCode::Home if event.is_press() => self.jump_key(AppEvent::DoScrollHome),
KeyCode::End | KeyCode::Char('G') if event.is_press() => {
self.jump_key(AppEvent::DoScrollEnd)
}
KeyCode::Char('f')
if event.modifiers.contains(KeyModifiers::CONTROL) && event.is_press() =>
{
let would_collect = self
.data_table_state
.as_ref()
.map(|s| s.scroll_would_trigger_collect(s.visible_rows as i64))
.unwrap_or(false);
if would_collect {
self.busy = true;
Some(AppEvent::DoScrollDown)
} else {
if let Some(ref mut s) = self.data_table_state {
s.page_down();
}
None
}
}
KeyCode::Char('b')
if event.modifiers.contains(KeyModifiers::CONTROL) && event.is_press() =>
{
let would_collect = self
.data_table_state
.as_ref()
.map(|s| s.scroll_would_trigger_collect(-(s.visible_rows as i64)))
.unwrap_or(false);
if would_collect {
self.busy = true;
Some(AppEvent::DoScrollUp)
} else {
if let Some(ref mut s) = self.data_table_state {
s.page_up();
}
None
}
}
KeyCode::Char('d')
if event.modifiers.contains(KeyModifiers::CONTROL) && event.is_press() =>
{
let half = self
.data_table_state
.as_ref()
.map(|s| (s.visible_rows / 2).max(1) as i64)
.unwrap_or(1);
let would_collect = self
.data_table_state
.as_ref()
.map(|s| s.scroll_would_trigger_collect(half))
.unwrap_or(false);
if would_collect {
self.busy = true;
Some(AppEvent::DoScrollHalfDown)
} else {
if let Some(ref mut s) = self.data_table_state {
s.half_page_down();
}
None
}
}
KeyCode::Char('u')
if event.modifiers.contains(KeyModifiers::CONTROL) && event.is_press() =>
{
let half = self
.data_table_state
.as_ref()
.map(|s| (s.visible_rows / 2).max(1) as i64)
.unwrap_or(1);
let would_collect = self
.data_table_state
.as_ref()
.map(|s| s.scroll_would_trigger_collect(-half))
.unwrap_or(false);
if would_collect {
self.busy = true;
Some(AppEvent::DoScrollHalfUp)
} else {
if let Some(ref mut s) = self.data_table_state {
s.half_page_up();
}
None
}
}
KeyCode::PageUp if event.is_press() => {
let would_collect = self
.data_table_state
.as_ref()
.map(|s| s.scroll_would_trigger_collect(-(s.visible_rows as i64)))
.unwrap_or(false);
if would_collect {
self.busy = true;
Some(AppEvent::DoScrollUp)
} else {
if let Some(ref mut s) = self.data_table_state {
s.page_up();
}
None
}
}
KeyCode::Enter if event.is_press() => {
if self.input_mode != InputMode::Normal {
return None;
}
// With no group to drill into, Enter is Space: the row inspector.
if self.enter_inspects() {
self.open_inspector();
return None;
}
self.drill_selected_row();
None
}
KeyCode::Char('i') if event.is_press() => {
if let Some(state) = self.data_table_state.as_mut() {
// Unread notes put the panel's Notes tab in front — that is
// what the accented `i` chip was promising. Read before the
// mark, which is what retires the accent.
let unseen = state.notes_unseen();
state.mark_notes_seen();
if unseen {
self.info_modal
.open_on(crate::widgets::info::InfoTab::Notes);
} else if state.format_detail().is_some_and(|d| d.first) {
// A table whose columns are the same for every file (a model's
// tensors, an audio file's frames, a VCD dump's changes): what
// is particular to it is its own tab.
self.info_modal
.open_on(crate::widgets::info::InfoTab::Format);
} else {
self.info_modal.open();
}
// A list of the file's tables starts its cursor on the one open.
if let Some(detail) = state.format_detail()
&& let Some(at) = detail.list.iter().position(|(key, _)| {
detail.table.as_ref() == Some(key) && detail.tables.contains(key)
})
{
self.info_modal.detail_selected = at;
}
self.input_mode = InputMode::Info;
self.read_file_facts();
self.count_unfit();
}
None
}
KeyCode::Char(':') if event.is_press() => {
self.open_command_line();
None
}
KeyCode::Char('V') => {
// Apply the best view whose criteria match this dataset. When none
// does, the answer is not silence and not the best-scored stranger: the
// list opens, so the user sees what exists and picks — or saves one.
if let Some(ref state) = self.data_table_state
&& let Some(dataset) = self.view_dataset()
{
match self
.view_manager
.get_most_relevant(dataset, state.source_schema())
{
Some((view, why)) => {
if let Err(e) = self.apply_matched_view(&view, why) {
self.error_modal.show(format!("Error applying view: {}", e));
}
}
None => self.open_view_list(),
}
}
None
}
KeyCode::Char('v') => {
self.open_view_list();
None
}
KeyCode::Char('S') => {
if self.input_mode == InputMode::Normal {
self.open_table_sample_form();
}
None
}
KeyCode::Char('s') => {
if self.data_table_state.is_some() {
// Rebuilt from the table's applied state, never from what the modal
// held last time: an edit staged and then canceled must not arrive
// pre-staged, one Apply away from committing silently.
self.sync_sort_filter_modal();
let current = self
.data_table_state
.as_ref()
.and_then(|state| state.current_column())
.map(str::to_string);
self.sort_filter_modal.open(
self.history_limit,
&self.theme,
current.as_deref(),
);
self.input_mode = InputMode::SortFilter;
}
None
}
KeyCode::Char('r') => {
if let Some(state) = &mut self.data_table_state {
state.deferred(DataTableState::reverse);
self.spawn_async_collect("Sorting...");
}
None
}
KeyCode::Char('a') => {
// Open analysis modal; no computation until user selects a tool from the sidebar (Enter)
if self.data_table_state.is_some()
&& self.input_mode == InputMode::Normal
&& self.quality_evidence_return.is_none()
{
// The results a close put down come back on the view they are of.
let view = self.data_table_state.as_ref().map(|s| s.len_generation());
self.analysis_modal.open(view);
// The sample outlives a close, but its scope names this
// dataset's rows: another dataset starts from its current view.
if self.analysis_modal.sample_dataset != Some(self.dataset_generation) {
self.analysis_modal.sample.scope = data_quality::QualityScope::CurrentView;
self.analysis_modal.sample_dataset = Some(self.dataset_generation);
}
// A view with a sample: every tool reads it, whole.
let sampled = self
.data_table_state
.as_ref()
.is_some_and(|state| state.sampled().is_some());
self.analysis_modal.follow_view_sample(sampled);
self.sync_quality_plan();
}
None
}
KeyCode::Char('c') => {
if let Some(state) = &self.data_table_state
&& self.input_mode == InputMode::Normal
{
let numeric_columns: Vec<String> = state
.schema()
.iter()
.filter(|(_, dtype)| dtype.is_numeric())
.map(|(name, _)| name.to_string())
.collect();
let datetime_columns: Vec<String> = state
.schema()
.iter()
.filter(|(_, dtype)| {
matches!(
dtype,
DataType::Datetime(_, _) | DataType::Date | DataType::Time
)
})
.map(|(name, _)| name.to_string())
.collect();
let category_columns: Vec<String> = state
.schema()
.iter()
.filter(|(_, dtype)| chart_data::is_category_dtype(dtype))
.map(|(name, _)| name.to_string())
.collect();
// Show Me: the chart starts from the cursor column's type.
let cursor = state.current_column().and_then(|name| {
let dtype = state.schema().get(name)?.clone();
Some((name.to_string(), dtype))
});
// Dates and datetimes take a time bucket; a time of day does not.
let bucketable_columns: Vec<String> = state
.schema()
.iter()
.filter(|(_, dtype)| {
matches!(dtype, DataType::Datetime(_, _) | DataType::Date)
})
.map(|(name, _)| name.to_string())
.collect();
self.chart_modal.series_cap = Some(self.theme.series_colors().len());
self.chart_modal.row_order = self.view_state().sort;
let sampled = state.sampled().is_some();
self.chart_modal.open(
ChartColumns {
numeric: &numeric_columns,
datetime: &datetime_columns,
bucketable: &bucketable_columns,
category: &category_columns,
},
cursor.as_ref().map(|(name, dtype)| (name.as_str(), dtype)),
Some(self.app_config.analysis.chart_rows),
self.app_config.analysis.chart_grid,
self.dataset_generation,
);
// A view's sample is read whole: the chart has no sample of its own.
if sampled {
self.chart_modal.row_limit = None;
} else if self.chart_modal.view_sampled {
self.chart_modal.row_limit = Some(self.chart_modal.sample_rows);
}
self.chart_modal.view_sampled = sampled;
self.chart_cache.clear();
self.input_mode = InputMode::Chart;
}
None
}
KeyCode::Char('p') => {
if self.data_table_state.is_some() && self.input_mode == InputMode::Normal {
self.open_pivot_builder();
}
None
}
KeyCode::Char('e') => {
if self.data_table_state.is_some() && self.input_mode == InputMode::Normal {
self.export_counts = None;
self.export_modal.open(
self.original_file_format,
self.history_limit,
&self.theme,
self.original_file_delimiter,
);
// A name to start from, beside the source's rather than on it.
let stem = self.dataset_stem();
self.export_modal.suggest_path(&format!("{stem}-export"));
if let Some(state) = self.data_table_state.as_ref() {
self.export_modal.offer_source_file = state.can_name_source_files();
self.export_modal.nested_columns = state
.get_column_order()
.iter()
.filter_map(|name| state.schema().get(name))
.any(crate::nested_json::is_nested);
self.export_modal.avro_renames =
state.get_column_order().iter().any(|name| {
state
.schema()
.get(name)
.is_some_and(|dtype| crate::avro_types::renames(name, dtype))
});
}
self.input_mode = InputMode::Export;
}
None
}
KeyCode::Char(' ') if event.is_press() => {
if self.input_mode == InputMode::Normal {
self.open_inspector();
}
None
}
KeyCode::Char('g') if event.is_press() => {
if self.input_mode == InputMode::Normal {
self.open_go_to_column();
}
None
}
KeyCode::Char('b') if event.is_press() => {
if self.input_mode == InputMode::Normal {
self.open_format_picker();
}
None
}
KeyCode::Char('T') if event.is_press() => {
if self.input_mode == InputMode::Normal {
self.open_table_picker();
}
None
}
KeyCode::Char('y') => {
if self.input_mode == InputMode::Normal
&& let Some(state) = self.data_table_state.as_ref()
{
let columns = state.get_column_order().to_vec();
let context = copy_modal::CopyContext {
row_number: state.selected_display_row().unwrap_or(0),
view_rows: state.copy_view_df().map(|d| d.height()).unwrap_or(0),
view_cols: columns.len(),
total_rows: state.num_rows_if_valid(),
};
let current = state.current_column().map(str::to_string);
self.copy_modal.open(columns, current.as_deref(), context);
self.input_mode = InputMode::Copy;
}
None
}
_ => None,
}
}
/// Handle one event. A key that arrives while the app is busy is not acted on and
/// not dropped either: it comes back as `Err(key)` for the caller to hold until the
/// app is idle. The main loop ([`event_pump::EventPump`]) does exactly that;
/// [`App::event`] is the same call for callers that have nowhere to hold a key.
pub fn handle(&mut self, event: &AppEvent) -> EventOutcome {
// Without the pump to offer it as typed, a pressed key is a key.
if let AppEvent::Press(key) = event {
return self.handle(&AppEvent::Key(*key));
}
if let AppEvent::Key(key) = event
&& self.is_busy()
&& !self.key_acts_while_busy(key)
{
return Err(*key);
}
let out = self.dispatch_event(event);
// Not while this handler is returning a continuation. A follow-up is the rest of
// the event just handled — the analysis sets `computing` and returns
// `AnalysisChunk`, and the phase that chunk will spawn has not spawned — so
// nothing holds the generation yet, and the errands would advance it out from
// under the errand that is halfway through. They run after every event and are
// built to wait; one more event is nothing to them.
if out.is_none() {
self.let_waiting_errands_in();
}
self.ensure_chart_data();
self.home_score_search();
// New rows on hand under an open find prompt: light up their matches.
self.refresh_stale_live_matches();
Ok(out)
}
/// The errands that wait for the generation to be free, given their turn: after
/// every event, and when a continuation's hold is let go.
pub(crate) fn let_waiting_errands_in(&mut self) {
// Columns a dataset's footers found while the user was inside a query are held
// rather than dropped; this is where they get in, on the first event after the
// view comes back to the data.
if self.join_held_footers() {
self.reread_after_the_footers_joined();
}
self.join_followed_fields();
self.describe_ended_journal();
// And the same turn for a re-read owed to a dataset whose footers could not be
// read: it waits on the same work, and gets in the same way.
self.reread_when_the_work_allows();
self.collect_when_the_work_allows();
}
pub fn event(&mut self, event: &AppEvent) -> Option<AppEvent> {
self.handle(event).unwrap_or(None)
}
/// True while chart data for the current view is being prepared off-thread — either
/// its worker is running, or it is waiting its turn behind an orphaned worker that
/// cannot be cancelled (see `ChartInflight::stale`). Either way the user is waiting
/// on a computation and the throbber should say so.
pub fn chart_preparing(&self) -> bool {
match self.chart_inflight.as_ref() {
Some(inflight) if !inflight.stale => true,
Some(_) => self.chart_request_pending(),
None => self.chart_settling(),
}
}
/// Whether the selection on screen is a step through the aggregates still waiting
/// for the next step.
fn chart_settling(&self) -> bool {
self.chart_asked
.as_ref()
.and_then(|(_, until)| *until)
.is_some_and(|until| std::time::Instant::now() < until)
}
/// Whether the chart view wants data it does not have and cannot be told it will
/// never get.
fn chart_request_pending(&self) -> bool {
if self.input_mode != InputMode::Chart || !self.chart_modal.active {
return false;
}
ChartRequest::from_modal(&self.chart_modal)
.is_some_and(|request| self.chart_cache.get(&request).is_none())
}
/// Forget everything chart-related that belongs to the view or dataset on its way
/// out: the cache, the handed-over slot, an export parked on data that is now never
/// coming, and an export write still running (its file may still appear, but its
/// result is ignored and `busy` is released). The preparation in flight is marked
/// stale rather than forgotten: it cannot be cancelled, so it is waited for and its
/// result discarded on arrival. Called when the chart view closes and whenever the
/// dataset changes or is left for the home screen.
fn reset_chart_state(&mut self) {
self.chart_cache.clear();
self.chart_asked = None;
if let Some(inflight) = self.chart_inflight.as_mut() {
inflight.stale = true;
inflight
.cancel
.store(true, std::sync::atomic::Ordering::Relaxed);
}
// A failed export reopens its modal; it must not follow the user to the next
// dataset.
self.chart_export_modal.close();
*self
.pending_chart_result
.lock()
.unwrap_or_else(|e| e.into_inner()) = None;
let writing = self
.jobs
.supersede(|job| matches!(job, Job::ChartExport { .. }));
let waiting = self.chart_export_waiting.take().is_some();
if writing || waiting {
self.export_progress = None;
self.status_message = None;
self.busy = false;
}
}
/// What the chart being prepared is doing: an aggregate groups every row of the
/// view, and says how many where the table knows.
pub(crate) fn chart_status(&self) -> String {
let aggregating = self
.chart_inflight
.as_ref()
.filter(|i| !i.stale)
.map(|i| i.request.aggregates())
.or_else(|| ChartRequest::from_modal(&self.chart_modal).map(|r| r.aggregates()))
.unwrap_or(false);
if !aggregating {
return "Preparing chart...".to_string();
}
match self
.data_table_state
.as_ref()
.and_then(|s| s.num_rows_if_valid())
{
Some(rows) => format!("Grouping {} rows...", crate::discover::format_rows(rows)),
None => "Grouping every row...".to_string(),
}
}
/// The series of the line or scatter chart on screen, by name, once prepared:
/// its Y columns or its color groups.
pub fn chart_names(&self) -> Option<Vec<String>> {
let request = ChartRequest::from_modal(&self.chart_modal)?;
match self.chart_cache.prepared(&request)? {
ChartPrepared::XY(xy) => Some(xy.names.clone()),
_ => None,
}
}
/// True when the chart cache holds the data for the modal's current selection.
pub fn chart_data_ready(&self) -> bool {
ChartRequest::from_modal(&self.chart_modal).is_some_and(|r| self.chart_cache.satisfies(&r))
}
/// Start preparing the chart the modal currently asks for, unless the cache already
/// has it, it is known to fail, or another preparation is still running (the newest
/// selection is picked up when that one lands). Runs after every event, so a change
/// of column or option is noticed as soon as it is made and render only ever draws.
fn ensure_chart_data(&mut self) {
const CHART_AGGREGATE_SETTLE: std::time::Duration = std::time::Duration::from_millis(150);
if self.input_mode != InputMode::Chart || !self.chart_modal.active {
return;
}
// What Every row costs, as the table counted it.
self.chart_modal.view_rows = self
.data_table_state
.as_ref()
.and_then(|state| state.num_rows_if_valid());
let request = ChartRequest::from_modal(&self.chart_modal);
if let Some(inflight) = self.chart_inflight.as_ref()
&& !request
.as_ref()
.is_some_and(|r| r.reads_as(&inflight.request))
{
// A count streaming a large view for a selection the cursor has moved
// past would hold up the next chart for as long as it reads.
inflight
.cancel
.store(true, std::sync::atomic::Ordering::Relaxed);
}
let Some(request) = request else {
return;
};
// Stepping none, count, distinct, sum, mean grouped every row at each step, and
// drew each: a step waits a moment for the next, and only where it stops is
// prepared. A Wake when the wait ends prepares it.
let settle = match self.chart_asked.take() {
Some((asked, until)) if asked == request => until,
Some((asked, _)) if request.steps_aggregate_from(&asked) => {
let tx = self.events.clone();
std::thread::spawn(move || {
std::thread::sleep(CHART_AGGREGATE_SETTLE);
let _ = tx.send(AppEvent::Wake);
});
Some(std::time::Instant::now() + CHART_AGGREGATE_SETTLE)
}
_ => None,
};
self.chart_asked = Some((request.clone(), settle));
if self.chart_cache.get(&request).is_some() {
self.chart_cache.touch(&request, self.chart_modal.log_scale);
// A cached chart's colors were counted with it.
if let Some(colors) = request
.spec
.encoding
.color
.field
.as_deref()
.and_then(|c| self.chart_cache.colors(c))
{
self.chart_modal.color_counts = Some(colors.clone());
}
return;
}
if self.chart_inflight.is_some() || self.chart_settling() {
return;
}
let Some(state) = self.data_table_state.as_ref() else {
return;
};
// Unsorted: the rows a chart draws do not depend on the table's order, a line
// is drawn in X order anyway, and a sort would make a sampled read read it all.
// First and last are the order's: they read the view as sorted.
let lf = if request.sorted {
state.lf().clone()
} else {
state.analysis_lf()
};
let schema = state.schema().clone();
let dataset = Some(state.len_generation());
let sampling = chart_data::ChartSampling {
// None for an aggregate: it reads every row.
limit: request.row_limit,
known_total: state.num_rows_if_valid(),
seed: self.analysis_modal.sample.seed,
streaming: self.app_config.performance.streaming,
full_passes: !state.is_remote_source(),
held: self.chart_cache.held_rows(dataset),
cancel: Arc::default(),
};
self.chart_inflight = Some(ChartInflight {
dataset,
request: request.clone(),
stale: false,
cancel: Arc::clone(&sampling.cancel),
});
let slot = self.pending_chart_result.clone();
let tx = self.events.clone();
self.runtime.spawn_blocking(move || {
// A panic in the preparation must still report back: without the event the
// in-flight record would stand for the rest of the session and every later
// selection would be refused.
let result = logging::catch_panic(|| request.prepare(&lf, &schema, &sampling))
.unwrap_or_else(|_| Err(color_eyre::eyre::eyre!("Chart preparation panicked")))
.map_err(|e| crate::error_display::user_message_from_report(&e, None));
*slot.lock().unwrap_or_else(|e| e.into_inner()) = Some(result);
let _ = tx.send(AppEvent::BackgroundChartReady);
});
}
fn dispatch_event(&mut self, event: &AppEvent) -> Option<AppEvent> {
self.debug.num_events += 1;
match event {
AppEvent::Key(key) => {
// Leaving while standard input is still being recorded asks first.
if let Some(leaving) = self.leaves(key)
&& !self.confirmation_modal.active
&& self.recording().is_some_and(|spool| spool.live())
{
self.ask_about_recording(leaving);
return None;
}
self.key(key)
}
AppEvent::Open(paths, options) => {
if paths.is_empty() {
return Some(AppEvent::Crash("No paths provided".to_string()));
}
// Home is now in the stack, so q pops back to it. Never unset:
// a reread from the table (H) is not a new place.
if self.input_mode == InputMode::Home {
self.opened_from_home = true;
}
// `az://container/path` and its kin name no account; where they were
// typed, or the config, does.
#[cfg(feature = "cloud")]
let expanded = match paths
.iter()
.map(|p| {
crate::cloud_sources::expand_azure_short_url(
p,
&self.app_config.cloud,
self.home.browsing.as_deref(),
)
})
.collect::<std::result::Result<Vec<_>, _>>()
{
Ok(expanded) => expanded,
Err(message) => return Some(AppEvent::Crash(message)),
};
#[cfg(feature = "cloud")]
if &expanded != paths {
return Some(AppEvent::Open(expanded, options.clone()));
}
// Asks the filesystem for the size the loading screen shows, and whether
// the path is there to be a recent.
let mut request =
loading::OpenRequest::named(paths.clone(), options.clone(), &self.formats);
request.warn_in_memory_above = self.app_config.read.memory_warning();
self.begin_new_dataset();
let step = self.loading.open(request);
self.run_load_step(step)
}
AppEvent::OpenLazyFrame(lf, options) => {
self.begin_new_dataset();
let step = self.loading.open_frame((**lf).clone(), options.clone());
self.run_load_step(step)
}
AppEvent::HomeListingReady {
generation,
listing,
known,
visits,
newest,
folds,
} => {
// Only the current listing's answer clears the flag: a stale one landing
// first said nothing was in flight while the listing for where the user
// is still ran. Every refresh asks again, so the newest always answers.
if *generation == self.home_generation {
self.home.listing_in_flight = false;
}
// Read fresh from the cache, so true whichever listing carried them:
// the facts fill in rows the recursive search finds the same way, and
// only the first listing after entering home carries the folds.
self.home.known = known.clone();
self.home.visits = visits.clone();
self.home.newest_recent = newest.clone();
if let Some(folds) = folds {
self.home.folds = folds.clone();
}
// A listing from a superseded request describes somewhere the user has
// already left.
if *generation != self.home_generation {
return None;
}
self.home.apply_listing((**listing).clone());
// Probes are chosen from the sections, so they can only be started
// once those exist — asking before the listing lands finds nothing.
self.spawn_home_probes();
#[cfg(feature = "cloud")]
self.spawn_cloud_discovery();
self.request_home_measurements();
self.request_home_classifications();
None
}
AppEvent::HomeListingFailed => {
// The rows already listed stay. The panic is flashed as a raw worker's.
self.home.listing_in_flight = false;
None
}
AppEvent::HomeMeasured { measured, done } => {
for (path, m) in measured {
self.home.enriched.insert(path.clone(), m.clone());
}
self.home.apply_measurements();
if *done {
self.home.measure_in_flight = false;
self.request_home_measurements();
}
None
}
AppEvent::HomeSized { path, measured } => {
// Only the size: a measurement that landed meanwhile keeps the rest.
match self.home.enriched.get_mut(path) {
Some(known) => known.size = measured.size,
None => {
self.home.enriched.insert(path.clone(), measured.clone());
}
}
self.home.apply_measurements();
None
}
AppEvent::HomeWebGone { path, gone } => {
self.home.web_gone.insert(path.clone(), gone.clone());
None
}
AppEvent::HomeClassified { measured, done } => {
// Kept even when the listing has been rebuilt since it was asked for. A
// probe or a cloud peek landing rebuilds it, and a Recent section full of
// buckets lands several in a row: dropping the answer each time left a
// share's rows unlabeled for as long as the cloud kept answering.
for (path, m) in measured {
self.home.enriched.insert(path.clone(), m.clone());
}
// Nothing re-sorts. `apply_measurements` writes the kind into the row
// where it already is, which is the whole reason a kind is allowed to
// arrive after the row was drawn: a listing that reshuffled itself
// under the cursor while it filled in would be worse than a late
// label.
self.home.apply_measurements();
// The next batch is chosen from the viewport as it is now, so a page
// that scrolled past four hundred rows while this one was out asks
// about the forty it landed on, not the four hundred it left behind.
if *done {
self.home.classify_in_flight = false;
self.request_home_classifications();
}
None
}
AppEvent::HomePathListed { listing } => {
// Kept only for the directory still being typed: a listing for one the
// user has typed past would offer names from somewhere else.
if self.home.path_input_active
&& home::typed_dir(&self.home.path_input) == listing.dir
{
self.home.path_listing = Some((**listing).clone());
if self.home.path_pick.is_none() {
self.home.pick_first_path();
}
}
None
}
AppEvent::HomePathCompleted {
generation,
typed,
completed,
candidates,
} => {
// Discard if the user has typed since asking: completing onto a
// different string would scramble what they are in the middle of.
if *generation != self.home_generation || &self.home.path_input != typed {
return None;
}
if *candidates == 0 {
self.home.status = Some("No such path".to_string());
} else {
self.home.status = None;
if *candidates > 1 {
self.flash_note(format!("{candidates} matches"));
}
self.home.path_input = completed.clone();
self.home.pick_first_path();
}
None
}
AppEvent::HomePreviewReady {
path,
stamp,
read_at,
rows,
prepared,
} => {
let prepared = prepared.lock().ok().and_then(|mut p| p.take());
// The columns came with the rows: the pane lists them, for a CSV too.
if let Some(prepared) = &prepared {
let schema = prepared
.state
.schema()
.iter()
.map(|(name, dtype)| (name.to_string(), dtype.clone()))
.collect();
self.home_schema_cache.insert(path.clone(), Some(schema));
}
let prepared = prepared.filter(|_| read_at.is_some());
self.home_previews.landed(
path.clone(),
*stamp,
read_at.unwrap_or(*stamp),
rows.clone(),
prepared,
);
None
}
AppEvent::HomeSchemaReady {
generation,
path,
preview,
} => {
self.home_schema_inflight.retain(|p| p != path);
// A preview's columns are not taken back by a metadata read that had none.
let known = self
.home_schema_cache
.get(path)
.is_some_and(Option::is_some);
if *generation == self.home_generation && (preview.is_some() || !known) {
self.home_schema_cache.insert(path.clone(), preview.clone());
}
None
}
AppEvent::HomeSearchBatch {
generation,
root,
found,
scanned,
} => {
// Results from a walk that a later navigation superseded describe a
// place the user has left. The walk is abandoned, not cancelled, so
// late batches are expected rather than exceptional. A refresh of the
// same place supersedes nothing: its end dropped kept it running.
if *generation == self.home_search_generation {
self.home.search_batch(root, found.clone(), *scanned);
}
None
}
AppEvent::HomeSearchScored { epoch, matches } => {
// A scoring that died is not asked again: the next would die the same
// way, and the matches already listed stand.
if let Some(matches) = matches {
self.home.search_scored(*epoch, (**matches).clone());
}
None
}
AppEvent::HomeSearchDone {
generation,
root,
scanned,
limited,
} => {
if *generation == self.home_search_generation {
self.home.search_finished(root, *scanned, limited.clone());
}
self.home_search_inflight = false;
// A walk abandoned by a browse held up the one the filter now asks for.
if !self.home.filter.is_empty() && self.home.search.root.is_none() {
self.spawn_home_search();
}
None
}
#[cfg(feature = "cloud")]
AppEvent::HomeCloudSources { sources } => {
self.home.cloud = sources.clone();
self.home_refresh();
None
}
#[cfg(feature = "cloud")]
AppEvent::HomeCloudListed {
id,
buckets,
details,
failure,
listed_at,
} => {
if let Some(source) = self.home.cloud.iter_mut().find(|s| &s.id == id) {
source.refreshing = false;
for (place, lines) in details {
source.place_details.insert(place.clone(), lines.clone());
}
match failure {
// A refresh that failed keeps what the last one found: stale
// buckets are more use than none, and the row says it failed.
Some((short, detail)) => {
for bucket in buckets {
if !source.buckets.contains(bucket) {
source.buckets.push(bucket.clone());
}
}
source.status = home::CloudStatus::Failed {
short: short.clone(),
detail: detail.clone(),
};
}
None => {
source.buckets = buckets.clone();
source.status = home::CloudStatus::Listed;
source.listed_at = Some(*listed_at);
}
}
}
self.home_refresh();
None
}
AppEvent::HomeNarrowed {
dir,
prefix,
listed,
} => {
// Only the request out now: one replaced by a later key may still
// answer, after the later one, and put back the shorter prefix.
let asked = self
.home_narrowing
.as_ref()
.is_some_and(|(d, p, _)| d == dir && p == prefix);
if asked {
self.home_narrowing = None;
}
// Only while it is still where the user is and what the filter asks.
let wanted = asked
&& self.home.browsing.as_ref() == Some(dir)
&& !self.home.filter.is_empty();
if let (Some((rows, truncated)), true) = (listed, wanted) {
self.home.narrowed = Some(home::Narrowed {
dir: dir.clone(),
prefix: prefix.clone(),
rows: rows.clone(),
truncated: *truncated,
});
self.home_refresh();
}
None
}
AppEvent::HomeProbeCancelled { root } => {
self.home_probes_inflight.retain(|p| p != root);
self.home_listing_cancels.remove(root);
self.home.listing_so_far.remove(root);
// Come back to after it had stopped: listed afresh.
if self.home.browsing.as_ref() == Some(root) {
self.home_refresh();
}
None
}
AppEvent::HomeProbeFailed { root, message } => {
self.home_probes_inflight.retain(|p| p != root);
self.home_listing_cancels.remove(root);
self.home.probe_failed(root.clone());
self.home.probe_errors.insert(root.clone(), message.clone());
self.home_refresh();
None
}
AppEvent::HomeProbeProgress { root, rows } => {
// Only while that listing is still out: a late batch must not paint
// over the whole answer.
if self.home_probes_inflight.contains(root) && !self.home.probed.contains_key(root)
{
self.home.listing_so_far.insert(root.clone(), rows.clone());
self.home_refresh();
}
None
}
AppEvent::HomeProbeReady {
root,
rows,
cut_short,
} => {
// Give the slot back. The cap exists to bound threads wedged on a dead
// `hard` mount, which never send this event and so keep their slot for
// good — a probe that answered is not one of those. Without this the
// list only grows, and after MAX_CONCURRENT_PROBES roots no further
// root is ever probed for the rest of the session.
self.home_probes_inflight.retain(|p| p != root);
self.home_listing_cancels.remove(root);
let landed = rows.is_some();
match rows {
Some(rows) => self.home.probe_ready(root.clone(), rows.clone()),
None => self.home.probe_failed(root.clone()),
}
if *cut_short {
self.home.cut_short.insert(root.clone());
}
// A filter typed while it was listing asks the server too.
#[cfg(feature = "cloud")]
if *cut_short && !self.home.filter.is_empty() {
self.narrow_cloud_listing();
}
// An account read with its keys because the sign-in has no data role
// says so beside the account.
#[cfg(feature = "cloud")]
if let Some((account, _, _)) = source::azure_parts(&root.to_string_lossy())
&& crate::azure::remembered_key(&account).is_some()
{
for source in &mut self.home.cloud {
let place = source
.buckets
.iter()
.find(|b| home::cloud_account(b).is_some_and(|(_, a)| a == account))
.cloned();
if let Some(place) = place {
let lines = source.place_details.entry(place).or_default();
if !lines.iter().any(|(k, _)| k == "access") {
lines.push(("access".to_string(), "access key".to_string()));
}
}
}
}
// Rebuild so the listing picks the result up; the probe is the only
// thing that ever reads a remote root.
self.home_refresh();
// And then ask about the rows it brought. After the rebuild, never
// before: the picker reads `visible()`, which is written by the
// rebuild, so a peek asked between `probe_ready` and here looks at the
// previous listing and finds nothing in it to ask about.
//
// Asked here at all because a listing that lands while the cursor is
// already where it will stay may draw no further frame, and the frame
// is what otherwise notices.
#[cfg(feature = "cloud")]
if landed {
self.peek_cloud_directories();
}
#[cfg(not(feature = "cloud"))]
let _ = landed;
None
}
AppEvent::HomeCloudKinds { kinds, failed } => {
for directory in failed {
self.home.peeking.remove(directory);
self.home.peek_failed.insert(directory.clone());
}
let roots: Vec<PathBuf> = self.home.probed.keys().cloned().collect();
for (directory, kind) in kinds {
// Answered: out of the in-flight set and into the one the rows are
// labelled from. Every directory asked about comes back, so nothing
// stays in `peeking` and nothing is asked twice.
self.home.peeking.remove(directory);
self.home
.cloud_kinds
.insert(directory.clone(), kind.clone());
}
for root in roots {
self.home.apply_cloud_kinds(&root);
}
self.home_refresh();
None
}
AppEvent::Resize(_cols, _rows) => {
// No work here: the next render sets visible_rows and flips needs_recollect,
// which the main loop turns into an async collect against the correct size.
None
}
AppEvent::Collect => {
self.spawn_async_collect(Self::LOADING_BUFFER);
None
}
AppEvent::DoScrollDown => self.handle_scroll(|s| s.page_down()),
AppEvent::DoScrollUp => self.handle_scroll(|s| s.page_up()),
AppEvent::DoScrollNext => self.handle_scroll(|s| s.select_next()),
AppEvent::DoScrollPrev => self.handle_scroll(|s| s.select_previous()),
AppEvent::DoScrollEnd => self.handle_scroll(|s| s.scroll_to_end()),
AppEvent::DoScrollHome => self.handle_scroll(|s| s.scroll_to_start()),
AppEvent::DoScrollHalfDown => self.handle_scroll(|s| s.half_page_down()),
AppEvent::DoScrollHalfUp => self.handle_scroll(|s| s.half_page_up()),
AppEvent::GoToLine(n) => {
let n = *n;
// Past the lines indexed so far: gone to once they all are.
if let Some(state) = self.data_table_state.as_ref()
&& state.indexing().is_some()
&& (n >= state.num_rows()
|| state.changes_rows()
|| !state.view_sort_columns().is_empty()
|| !state.view_sort_ascending())
{
self.goto_when_indexed = Some((self.dataset_generation, n));
self.status_message = Some(Self::INDEXING_FOR_ROW.to_string());
self.busy = false;
return None;
}
self.handle_scroll(|s| s.scroll_to_row_centered(n))
}
AppEvent::AnalysisChunk => {
// Binary columns are stubbed by the source: their blobs are never read
// for analysis (multi-GB blobs across partitions can exhaust memory).
let (source, known_total) = match &self.data_table_state {
Some(state) => self.sample_source(state),
None => {
self.analysis_computation = None;
self.analysis_modal.computing = None;
self.busy = false;
return None;
}
};
let comp = self.analysis_computation.take()?;
if comp.df.is_none() {
let sample = self.analysis_modal.sample.clone();
let streaming = self.app_config.performance.streaming;
self.spawn_job(
Job::Analysis(jobs::AnalysisRun::default()),
Some("Running analysis..."),
move |_| {
let results = source
.cut(&sample.scope)
.and_then(|lf| {
crate::statistics::compute_describe_from_lazy(
&lf,
known_total,
&sample,
streaming,
)
})
.map_err(|e| format!("{e}"))?;
Ok(Answer::Described(results))
},
);
}
None
}
AppEvent::AnalysisDistributionCompute => {
if let Some(state) = &self.data_table_state {
let (source, known_total) = self.sample_source(state);
let sample = self.analysis_modal.sample.clone();
let streaming = self.app_config.performance.streaming;
self.spawn_job(
Job::Analysis(jobs::AnalysisRun::default()),
Some("Analyzing distributions..."),
move |_| {
let options = crate::statistics::ComputeOptions {
include_distribution_info: true,
include_distribution_analyses: true,
include_correlation_matrix: false,
include_skewness_kurtosis_outliers: true,
polars_streaming: streaming,
};
let results = source
.cut(&sample.scope)
.and_then(|lf| {
crate::statistics::compute_statistics_for_sample(
&lf,
&sample,
known_total,
options,
)
})
.map_err(|e| format!("{e}"))?;
Ok(Answer::Distributions(results))
},
);
} else {
self.analysis_modal.computing = None;
self.busy = false;
}
None
}
AppEvent::AnalysisCorrelationCompute => {
if let Some(state) = &self.data_table_state {
let (source, known_total) = self.sample_source(state);
let streaming = state.polars_streaming();
let sample = self.analysis_modal.sample.clone();
let seed = sample.seed;
self.spawn_job(
Job::Analysis(jobs::AnalysisRun::default()),
Some("Computing correlation matrix..."),
move |_| {
// Only the numeric columns: nothing else is correlated, and on a
// wide table the rest is most of what a full read would hold.
let result = source
.cut(&sample.scope)
.and_then(|lf| {
let schema = lf.clone().collect_schema()?;
let numeric: Vec<polars::prelude::Expr> = schema
.iter()
.filter(|(_, dtype)| dtype.is_numeric())
.map(|(name, _)| col(name.clone()))
.collect();
crate::sampling::read(
&lf.select(numeric),
&sample,
known_total,
streaming,
)
})
.map(|rows| {
let matrix =
crate::statistics::compute_correlation_matrix(&rows.df)
.ok();
crate::statistics::AnalysisResults {
column_statistics: vec![],
total_rows: rows.total_rows,
sample_size: rows.sample_size,
per_value: rows.per_value.map(|per_value| per_value.kept),
sample_seed: seed,
correlation_matrix: matrix,
distribution_analyses: vec![],
}
});
let results = result.map_err(|e| format!("{e}"))?;
Ok(Answer::Correlations(results))
},
);
} else {
self.analysis_modal.computing = None;
self.busy = false;
}
None
}
AppEvent::AnalysisDataQualityCompute => {
// The plan Run committed; Setup's Run is the only way here.
if let Some(state) = &self.data_table_state {
let plan = self.analysis_modal.data_quality_plan.clone();
let source_scope = plan.scope.uses_source();
let (lf, source, cached_rows) = if source_scope {
let (lf, source) = state.data_quality_source_scan();
(lf, source, None)
} else {
let ordered = matches!(
plan.scope,
data_quality::QualityScope::FirstRows(_)
| data_quality::QualityScope::ViewRows { .. }
);
let (lf, source) = state.data_quality_scan(ordered);
let rows = state.num_rows_if_valid().map(|rows| match &plan.scope {
data_quality::QualityScope::CurrentView => rows,
data_quality::QualityScope::FirstRows(limit) => rows.min(*limit),
data_quality::QualityScope::ViewRows { start, end } => {
rows.min(*end).saturating_sub(start.saturating_sub(1))
}
_ => unreachable!(),
});
(lf, source, rows)
};
let streaming = state.polars_streaming();
// An audio file's signal checks read its samples whole: a full run's.
let audio = (plan.compute == data_quality::QualityCompute::Full)
.then(|| state.window_for_quality(&plan.scope))
.flatten()
.and_then(crate::audio::recording);
let view_generation = state.len_generation();
let dataset_generation = self.dataset_generation;
let kept_entry = self.kept_quality_entry(&plan.sample());
let kept = kept_entry.map(|kept| kept.rows.clone());
// A sampled run on rows already read is labeled as their read was:
// the file may have changed since, and these rows did not.
let kept_source = kept_entry
.filter(|_| plan.compute == data_quality::QualityCompute::Sample)
.map(|kept| kept.source.clone());
let mut identity = self.quality_source_identity(state, &plan.scope);
let copy_job = match self.quality_copy_plan(&plan) {
data_quality::CopyPlan::Kept { .. } => self
.quality_copy_kept()
.cloned()
.map_or(QualityCopyJob::Source, QualityCopyJob::Kept),
data_quality::CopyPlan::Fetch { .. } => match state.remote_objects() {
Some(objects) => QualityCopyJob::Fetch {
objects,
root: self.quality_copies_root(),
},
None => QualityCopyJob::Source,
},
_ => QualityCopyJob::Source,
};
#[cfg(feature = "cloud")]
let (cloud, runtime) = (self.app_config.cloud.clone(), self.runtime.clone());
// Only a confirmed full scan pays to read the values a type
// conflict hides, and only its access plan promised the read.
let mut source = source;
if plan.compute == data_quality::QualityCompute::Full
&& let Some(source) = source.as_mut()
{
source.conflict_scan = state.quality_conflict_scan();
}
// Each stage the worker enters comes back as the job's progress, so a
// cancelled run's stages are dropped. The watch is the job's: Esc
// stops the run through its record.
let started = self.start_job(
Job::Analysis(jobs::AnalysisRun::default()),
Some("Profiling data quality..."),
);
let ticket = started.ticket();
let phases = self.events.clone();
let watch = data_quality::QualityWatch::new(move |phase| {
let _ = phases.send(AppEvent::JobProgress {
ticket,
progress: Progress::QualityPhase(phase),
});
});
if let Some(progress) = self.analysis_modal.computing.as_mut() {
progress.read = Some(watch.read().clone());
}
if let Some(Job::Analysis(run)) = self.jobs.job_mut(ticket) {
run.watch = Some(watch.clone());
}
started.run(&self.runtime, move |worker| {
// A stat of a local file as the run begins, not a read.
match kept_source {
Some(source) => identity = source,
None => identity.stat(),
}
let lf = if source_scope {
data_quality::prepare_source_quality_scan(lf, source.as_ref())
.map_err(|error| format!("{error}"))?
} else {
lf
};
let lf =
data_quality::apply_quality_scope(lf, &plan.scope, source.as_ref())
.map_err(|error| format!("{error}"))?;
// Held to the end of the run: the copy stays on disk while its
// passes read it, released or not.
let fetch = |objects: &[crate::local_copy::RemoteObject], root: &Path| {
#[cfg(feature = "cloud")]
{
Self::fetch_quality_copy(
objects,
root,
&cloud,
&runtime,
watch.read(),
)
}
#[cfg(not(feature = "cloud"))]
{
let _ = (objects, root);
Err(color_eyre::eyre::eyre!("Built without cloud support"))
}
};
let kept_copy = |copy: Option<Arc<crate::local_copy::LocalCopy>>| {
worker.send(AppEvent::BackgroundQualityCopyKept {
dataset_generation,
copy,
});
};
let (lf, held) =
Self::quality_scope_on_copy(lf, copy_job, &watch, fetch, kept_copy)
.map_err(|error| format!("{error}"))?;
let (results, rows) = crate::data_quality::compute_data_quality_watched(
&lf,
cached_rows,
&plan,
source.as_ref(),
streaming,
kept.as_deref(),
&watch,
);
let results = match (results, audio) {
(Ok(mut results), Some(audio)) => {
crate::data_quality::add_signal_observations(
&mut results,
&audio,
&watch,
)
.map(|()| results)
}
(results, _) => results,
};
// Let go before the answer goes out: a `d` handled as soon
// as it lands must find the app's handle the last one.
drop(held);
let kept = rows.map(|rows| KeptQualitySample {
dataset_generation,
view_generation,
sample: plan.sample(),
rows: std::sync::Arc::new(rows),
source: identity.clone(),
});
match results {
Ok(mut results) => {
results.source = Some(Box::new(identity));
Ok(Answer::DataQuality {
results: Box::new(results),
kept,
plan: Box::new(plan),
})
}
Err(error) => {
// Stopped after the sample was read: the read is kept.
if let Some(kept) = kept {
worker.send(AppEvent::BackgroundQualitySampleKept { kept });
}
Err(format!("{error}"))
}
}
});
} else {
self.analysis_modal.computing = None;
self.busy = false;
}
None
}
AppEvent::BackgroundLenReady {
len_generation,
num_rows,
file_row_groups,
} => {
if self.len_count_inflight == Some(*len_generation) {
self.len_count_inflight = None;
}
if self.len_count_failed == Some(*len_generation) {
self.len_count_failed = None;
}
// A count of the view a running query replaced goes back with it.
if let Some(run) = self.query_running.as_mut() {
run.rollback.count_landed(
*len_generation,
*num_rows,
file_row_groups.as_deref(),
);
if run.len_count_inflight == Some(*len_generation) {
run.len_count_inflight = None;
}
}
// Apply the exact total only if the data hasn't changed since the count
// was spawned. This runs independently of the buffer paint (which has
// usually already rendered), so it just corrects the scrollbar/total —
// no busy state, no re-collect.
if let Some(state) = self.data_table_state.as_mut()
&& state.count_landed(*len_generation, *num_rows, file_row_groups.as_deref())
{
// End was pressed before there was an end to go to.
if self.end_after_count == Some(*len_generation) {
self.end_after_count = None;
self.status_message = None;
return self.jump_key(AppEvent::DoScrollEnd);
}
} else if self.end_after_count == Some(*len_generation) {
// This is the count End was waiting on, and it answers a frame that
// is gone — a join landed underneath it and took a fresh
// `len_generation` past it. Left here the flag is stranded on a
// generation nothing will ever match: the next count to fail for any
// reason would speak in its name. So it is retired, and the status
// it put up comes down with it.
//
// Retired, not asked again of the frame that is here. That frame can
// belong to a dataset the user opened since — `end_after_count` names
// a `len_generation`, which says nothing about which dataset — and
// re-asking made the *new* dataset scroll itself to the end on the
// strength of a key pressed in the old one. A jump the frame change
// swallowed is a jump the user can make again; a jump that arrives on
// its own, in a directory they did not press it in, is not.
self.retire_the_end_that_was_waiting();
}
self.remember_a_downloads_shape();
None
}
AppEvent::FramePainted => {
self.frame_painted();
None
}
AppEvent::BackgroundLenFailed { len_generation } => {
if self.len_count_inflight == Some(*len_generation) {
self.len_count_inflight = None;
}
if self.count_after_stop.take() == Some(*len_generation) {
self.count_exactly();
return None;
}
if let Some(run) = self.query_running.as_mut()
&& run.len_count_inflight == Some(*len_generation)
{
run.len_count_inflight = None;
run.len_count_failed = Some(*len_generation);
}
// Mark this generation's count as failed so the row count renders as "?"
// instead of a misleading provisional total. Before the End handling
// below: this is about the count, not about who was waiting on it.
//
// Only for the frame on screen, because the slot holds one generation.
// Counts for two frames run at once — a join, a query, a filter or a
// sort takes a fresh `len_generation` without stopping the count already
// running — so a failure arriving is not necessarily this frame's.
// Written unconditionally, an orphan's failure overwrote a live frame's,
// `count_unknown` went false, and the bar fell through from "?" to the
// number the buffer happened to reach: a confident partial on a dataset
// whose count failed. The orphan's own failure is worth nothing to
// anybody — nothing will ever render against a generation that is gone.
if self
.data_table_state
.as_ref()
.is_some_and(|state| state.len_generation() == *len_generation)
{
self.len_count_failed = Some(*len_generation);
}
// Only for the count End was actually waiting on. Taken unconditionally,
// a count that failed for one frame answered for an End pressed on
// another — printing "Could not count the rows to find the end" about a
// key the user pressed somewhere else entirely, and long since.
if self.end_after_count == Some(*len_generation) {
self.end_after_count = None;
if self
.data_table_state
.as_ref()
.is_some_and(|state| state.len_generation() == *len_generation)
{
self.status_message =
Some("Could not count the rows to find the end".to_string());
} else {
// The frame it was counting is gone, so its failure says nothing
// about the one on screen, and the End it belonged to cannot be
// answered by it. Retired quietly, as above.
self.take_down_the_counting_status();
}
}
None
}
AppEvent::LinesIndexed { generation, rows } => {
self.lines_indexed(*generation, *rows);
None
}
AppEvent::BackgroundFootersJoined { .. } => {
// Taken whoever the event belongs to, and judged by what is *in* the
// slot rather than by the event that woke us. Two passes can be running
// at once, and the newer one may have overwritten the slot before the
// older one's event is handled: judging by the event would throw the
// newer answer away and leave the dataset on screen waiting for one
// that has already been and gone. An entry is also worth draining
// either way — it is a dataset's worth of schema and every file name.
let taken = self
.pending_footers_result
.lock()
.unwrap_or_else(|e| e.into_inner())
.take();
// Whether this is still the dataset on screen. Not whether an open is in
// flight: going home leaves the dataset up and puts any open down, and
// coming straight back to it must not find it stranded on two footers
// for the rest of the session.
if let Some((slot_generation, found)) = taken
&& slot_generation == self.dataset_generation
{
let Some(found) = found else {
// The pass could not read them. The dataset stays as it opened
// and stops waiting, so it can go and count itself the ordinary
// way rather than never at all — which is what the collect
// below sets going, since it is the counting the dataset was
// declining while it waited.
if let Some(state) = self.data_table_state.as_mut() {
state.give_up_on_pending_footers();
}
// The pass is not bringing a count after all, so the jump goes
// back to waiting on the ordinary one the collect starts. Owed
// rather than run: the collect bumps `task_generation`, and an
// export or an analysis may be waiting on the one it would bump
// past. `reread_when_the_work_allows` runs it the moment that
// work is done.
self.reread_owed = Some(slot_generation);
self.reread_when_the_work_allows();
return None;
};
self.footers_held = Some((slot_generation, found));
if self.join_held_footers() {
self.reread_after_the_footers_joined();
}
}
None
}
AppEvent::BackgroundQualitySampleKept { kept } => {
self.retain_quality_sample(kept);
None
}
AppEvent::BackgroundQualityCopyKept {
dataset_generation,
copy,
} => {
self.retain_quality_copy(*dataset_generation, copy.clone());
None
}
AppEvent::OpenNamed(paths, options) => {
if let Some(event) = Self::route_named_without_looking(paths, options) {
return Some(event);
}
let (paths, options) = (paths.clone(), options.clone());
let formats = self.formats.clone();
// The open's first phase. Unleased, as the look is: an answer for an open
// the user has left (Ctrl+O) is thrown away by the loader, not waited for.
self.make_way_for_an_open();
let load = self.loading.look_at_paths();
self.spawn_job(Job::OpenNamed(load), Some("Scanning input..."), move |_| {
if let Some(missing) = Self::missing_named_path(&paths, &formats) {
return Ok(Answer::NamedPathMissing(missing));
}
let (paths, options, directory) =
match Self::route_named_paths_with(paths, options, &formats) {
AppEvent::LookThenOpenDirectory(dir, options) => {
(Vec::new(), options, Some(dir))
}
AppEvent::Open(paths, options) => (paths, options, None),
_ => unreachable!("a named path is opened or looked at"),
};
Ok(Answer::NamedPaths {
paths,
options: Box::new(options),
directory,
})
});
None
}
AppEvent::LookThenOpenDirectory(dir, options) => {
// The name on the wait, so the first frame says which directory is being
// looked at rather than sitting blank. `spawn_job` puts the throbber up
// and the keys that survive it — Ctrl+C, Ctrl+O — keep working, which
// is the whole of what doing this on the event thread cost.
let looking = dir.clone();
let options = options.clone();
self.make_way_for_an_open();
let load = self.loading.look_at_directory(looking.clone());
// A newer look replaces an older one.
self.jobs
.supersede(|job| matches!(job, Job::LookAtDirectory { .. }));
// The same words the loading screen shows, so the control bar and the
// screen above it do not name the wait two different ways.
// Unleased. A lease exists to make a bump wait for an answer that
// would otherwise be stranded — and this answer is *meant* to be
// thrown away when the user moves on, which is the whole of the guard
// below. Leased, it made everything else wait instead: Ctrl+O out of a
// seventeen-second look and open a small CSV, and its buffer collect
// was owed until the abandoned look finally returned.
// Advertising Ctrl+O as the way out of the wait and then holding the
// next dataset behind it is the wait again, wearing a different hat.
#[cfg(feature = "cloud")]
let (cloud, runtime) = (self.app_config.cloud.clone(), self.runtime.clone());
let job = Job::LookAtDirectory {
load,
path: looking.clone(),
};
self.spawn_job(job, Some(Self::LOOKING_AT_A_DIRECTORY), move |_| {
#[cfg(feature = "cloud")]
if home::is_object_store_url(&looking) {
let url = looking.to_string_lossy().into_owned();
let peeked = wait_on_runtime(&runtime, async move {
crate::cloud_browse::peek_kind(&url, &cloud).await
})
.and_then(Result::ok);
let (kind, holds) = match peeked {
Some((kind, holds)) => (kind, Some(Box::new(holds))),
None => (discover::EntryKind::Unknown, None),
};
return Ok(Answer::LookedAt {
kind,
holds,
options: Box::new(options),
});
}
// A panic here used to unwind through `run()` and report a crash,
// because the look was made on the way to the first frame. On a
// worker it is swallowed with the dropped handle instead, and nothing
// would ever be sent: the spinner would stay up and the directory
// unopened for as long as the user waited. Caught, so the answer is
// "a directory" and the home screen opens on it. Read the way this
// open will read them, so the rule judges the directory the user is
// about to see rather than one nobody will open.
let as_read = Self::read_as(&options);
let looked = logging::catch_panic(|| {
let mut entry = discover::Entry::directory(&looking);
entry.kind = discover::EntryKind::Unknown;
home::look_into_as(&entry, &as_read)
});
let kind = match looked {
Ok(entry) => entry.kind,
Err(_) => discover::EntryKind::Directory,
};
Ok(Answer::LookedAt {
kind,
holds: None,
options: Box::new(options),
})
});
None
}
AppEvent::ClassifyThenOpen { path, jump } => {
// A second Enter replaces the first rather than being refused. Every key
// acts on the home screen even while `busy`, so a second one is
// reachable, and the newer look is the one the user is waiting for — and
// refusing meant a look at a share that never answers killed the feature
// for the rest of the session, silently.
let looking = path.clone();
self.jobs.supersede(|job| matches!(job, Job::Classify(_)));
let look = Job::Classify(jobs::Classify {
path: looking.clone(),
browsing: self.home.browsing.clone(),
jump: *jump,
});
let name = looking
.file_name()
.map(|n| n.to_string_lossy().into_owned())
.unwrap_or_else(|| looking.display().to_string());
// The home screen's own line, because the control bar's is the table's.
self.home.status = Some(format!("Looking at {name}..."));
self.spawn_job(look, Some(Self::LOOKING), move |_| {
// Every one of these can sit forever on a share that has gone away,
// which is the whole reason they are here and not where keys are read.
let found = if !looking.exists() {
None
} else if looking.is_dir() {
Some(crate::discover::classify_directory(&looking))
} else {
Some(crate::discover::EntryKind::File)
};
Ok(Answer::Kind(found))
});
None
}
AppEvent::JobEnded(ticket) => self.job_ended(*ticket),
AppEvent::JobProgress { ticket, progress } => {
self.job_progress(*ticket, progress);
None
}
AppEvent::QQuery(query) => {
self.run_query(QueryMode::Q, query, "Applying query...");
None
}
AppEvent::SqlQuery(sql) => {
self.run_query(QueryMode::Sql, sql, "Applying SQL query...");
None
}
AppEvent::Filter(statements) => {
if let Some(state) = &mut self.data_table_state {
state.deferred(|s| s.filter(statements.clone()));
}
self.spawn_async_collect("Filtering...");
None
}
AppEvent::Sort(columns, descending) => {
if let Some(state) = &mut self.data_table_state {
state.deferred(|s| s.sort_by(columns.clone(), descending.clone()));
}
self.spawn_async_collect("Sorting...");
None
}
AppEvent::Reset => {
// The sample is a step of the view: a reset takes it away too.
if self
.data_table_state
.as_ref()
.is_some_and(|state| state.sampled().is_some())
{
self.put_down_sample_draw();
if let Some(state) = self.data_table_state.take() {
self.data_table_state = Some(state.into_unsampled());
}
self.sample_changed();
}
if let Some(state) = &mut self.data_table_state {
state.deferred(|s| s.reset());
}
self.spawn_async_collect(Self::LOADING_BUFFER);
// Clear active view when resetting
self.active_view_id = None;
None
}
AppEvent::ColumnOrder(order, locked_count) => {
if let Some(state) = &mut self.data_table_state {
state.deferred(|s| {
s.set_column_order(order.clone());
s.set_locked_columns(*locked_count);
});
self.spawn_async_collect(Self::LOADING_BUFFER);
}
None
}
AppEvent::Pivot(spec) => {
// The modal stays up until the result is in, so a pivot that fails
// leaves the spec there to fix.
let job = self.data_table_state.as_ref()?.plan_pivot(spec);
let spec = spec.clone();
self.spawn_job(Job::Pivot, Some(Self::COMPUTING_PIVOT), move |_| {
let pivoted = job
.run()
.map_err(|e| crate::error_display::user_message_from_report(&e, None))?;
Ok(Answer::Pivoted { spec, pivoted })
});
None
}
AppEvent::Melt(spec) => {
self.busy = true;
if let Some(state) = &mut self.data_table_state {
let result = state.deferred(|s| s.melt(spec));
match result {
Ok(()) => {
self.pivot_melt_modal.close();
self.input_mode = InputMode::Normal;
self.spawn_async_collect("Computing melt...");
None
}
Err(e) => {
self.busy = false;
self.error_modal
.show(crate::error_display::user_message_from_report(&e, None));
None
}
}
} else {
self.busy = false;
None
}
}
AppEvent::QualityReportExport(path, format, overwrite) => {
// The report on screen and the plan it was measured with, cloned into
// the writer: the file is built from memory and nothing is read.
let results = self.analysis_modal.data_quality_results.clone()?;
let plan = self.analysis_modal.quality_result_plan().clone();
let (path, format, overwrite) = (path.clone(), *format, *overwrite);
self.spawn_job(
Job::QualityReport,
Some("Writing the report..."),
move |_| {
crate::quality_export::write(&path, &results, &plan, format, overwrite)
.map_err(|error| Self::format_export_error(&error))?;
Ok(Answer::QualityReportWritten(path))
},
);
None
}
AppEvent::ChartExport(request) => {
self.busy = true;
self.export_progress = Some(ExportProgress {
file_path: request.path.clone(),
current_phase: "Exporting chart".to_string(),
written: None,
});
Some(AppEvent::DoChartExport(request.clone()))
}
AppEvent::DoChartExport(request) => {
// `ChartExport` arms `busy` and defers here so the phase can be drawn
// first. A Ctrl-O in that window has already left the chart view, and
// there is nothing to export any more: release the app rather than park
// an export that no view would ever prepare.
if self.input_mode != InputMode::Chart || !self.chart_modal.active {
self.export_progress = None;
self.status_message = None;
self.busy = false;
return None;
}
self.start_chart_export(request.clone());
None
}
AppEvent::BackgroundChartReady => {
// The result belongs to the one preparation in flight. It is installed
// only while that record is current (a reset marks it stale when its
// view or dataset goes) and only into the dataset it was computed from.
// Taking the record is what lets the next request start; the slot is
// emptied either way so a discarded series is not kept around.
let inflight = self.chart_inflight.take()?;
let outcome = self
.pending_chart_result
.lock()
.unwrap_or_else(|e| e.into_inner())
.take()
.unwrap_or_else(|| Err("Chart preparation produced no result".to_string()));
if inflight.stale {
return None;
}
// A count stopped part way is no answer, and must not be remembered as
// a failure; the selection is prepared again when it comes back.
if outcome.is_err() && inflight.cancel.load(std::sync::atomic::Ordering::Relaxed) {
return None;
}
let dataset = self.data_table_state.as_ref().map(|s| s.len_generation());
if dataset != inflight.dataset {
return None;
}
let outcome = outcome.map(|(prepared, colors)| {
if let Some(colors) = colors {
self.chart_cache.hold_colors(colors.clone());
self.chart_modal.color_counts = Some(colors);
}
prepared
});
self.chart_cache.insert(inflight.request, outcome);
// An export parked on chart data resumes against the *current*
// selection, whatever just landed: it is written if that selection is
// now prepared, fails with the reason if that is the one that failed,
// and otherwise waits for the next result (which `ensure_chart_data`
// starts once this handler returns).
if let Some(request) = self.chart_export_waiting.take() {
self.start_chart_export(request);
}
None
}
AppEvent::Export(request) => {
if self.data_table_state.is_some() {
self.busy = true;
self.export_progress = Some(ExportProgress {
file_path: request.path.clone(),
current_phase: "Preparing export".to_string(),
written: None,
});
// Drawn before the export starts.
Some(AppEvent::DoExport(request.clone()))
} else {
None
}
}
AppEvent::Followed(news) => {
self.followed(news);
None
}
AppEvent::FollowedDetail {
dataset_generation,
detail,
} => {
if *dataset_generation == self.dataset_generation
&& let Some(state) = self.data_table_state.as_mut()
{
state.set_format_detail((**detail).clone());
}
None
}
AppEvent::DoExport(request) => {
let Some(state) = &self.data_table_state else {
self.export_progress = None;
self.busy = false;
return None;
};
// Cloned, not taken: a failed write reopens the dialog on the same counts.
let frame = match self.export_counts.clone() {
Some(counts) => crate::widgets::datatable::ExportFrame::of(
polars::prelude::IntoLazy::lazy(counts),
),
None => state.export_frame(request.options.source_file),
};
let streaming = state.polars_streaming();
// One job from plan to commit: it holds the generation throughout,
// and the rows it collects, if it collects, die with it.
let phase = match request.route(streaming) {
crate::export::Route::Streamed => Self::export_write_phase(request),
crate::export::Route::Collected => "Collecting data",
};
self.export_progress = Some(ExportProgress {
file_path: request.path.clone(),
current_phase: phase.to_string(),
written: None,
});
let writing = Self::export_write_phase(request);
let request = request.clone();
self.spawn_job(Job::Export, Some("Exporting..."), move |worker| {
let report = worker.reporter();
let written = move |bytes| {
report(Progress::ExportWriting {
phase: writing,
bytes,
})
};
frame
.into_lazy()
.map_err(color_eyre::eyre::Report::from)
.and_then(|lf| crate::export::run(lf, &request, streaming, written))
.map_err(|e| Self::format_export_error(&e))?;
// Success is reported only once the file is committed.
Ok(Answer::Exported(request.path))
});
None
}
AppEvent::OpenLink(url) => {
// Started, not waited on; a browser that will not start is a line,
// not an error to acknowledge.
if link_open::open(url).is_err() {
self.flash_note("Couldn't open the link; y copies it".to_string());
}
None
}
AppEvent::CopyTable { format, header } => {
let accepts = match self.copy_destination() {
Ok(destination) => destination.accepts(),
Err(e) => {
self.busy = false;
self.error_modal.show(e);
return None;
}
};
if let Some(state) = &self.data_table_state {
let lf = state.visible_lf();
let streaming = state.polars_streaming();
let (format, header) = (*format, *header);
self.spawn_job(Job::Copy, Some("Collecting data for copy..."), move |_| {
// A capped destination's copy is read in batches and given
// up at the cap; any other is collected and built whole.
let (payload, rows) = match accepts.base64_limit {
Some(limit) => crate::clipboard::bounded_table_text(
lf, format, header, limit,
)
.map(|(text, rows)| (crate::clipboard::Payload::text(text), rows)),
None => crate::statistics::collect_lazy(lf, streaming)
.map_err(|e| crate::error_display::user_message_from_polars(&e))
.and_then(|df| {
crate::clipboard::tabular_payload(
&df,
format,
header,
accepts.html,
)
.map(|payload| (payload, df.height()))
}),
}
.map_err(|message| format!("Copy failed: {message}"))?;
// Handed on whole, never copied.
Ok(Answer::Copied {
payload,
message: format!(
"Copied {} rows as {}",
copy_modal::thousands(rows),
format.as_str()
),
})
});
} else {
self.busy = false;
}
None
}
AppEvent::TerminalBackground(mode) => {
self.terminal_answered(*mode);
None
}
AppEvent::TerminalFocused => {
self.background_query |= self.app_config.theme.follow;
None
}
_ => None,
}
}
/// What `column` holds, for an export's axis ticks.
fn axis_numbers(&self, column: &str) -> chart_data::AxisNumbers {
let schema = self.data_table_state.as_ref().map(|s| s.schema().as_ref());
chart_data::AxisNumbers::column(&self.number_format, schema, column)
}
/// What `columns` hold on one axis.
fn axes_numbers(&self, columns: &[String]) -> chart_data::AxisNumbers {
let schema = self.data_table_state.as_ref().map(|s| s.schema().as_ref());
chart_data::AxisNumbers::columns(&self.number_format, schema, columns)
}
/// The figure to export from the prepared chart for the current spec. `Ok(None)`
/// means that chart is still being prepared and the caller should wait for it.
fn build_chart_figure(&self) -> Result<Option<chart_export::Figure>> {
use chart_export::{Axis, Figure, Plot, Series};
if self.data_table_state.is_none() {
return Err(color_eyre::eyre::eyre!("No data loaded"));
}
let modal = &self.chart_modal;
let Some(request) = ChartRequest::from_modal(modal).filter(|r| !r.x_only) else {
return Err(color_eyre::eyre::eyre!(
"Pick the columns the chart needs first"
));
};
let prepared = match self.chart_cache.get(&request) {
Some(Ok(prepared)) => prepared,
// A selection known not to chart is never retried, so waiting for its data
// would wait forever: fail the export now with the reason.
Some(Err(message)) => return Err(color_eyre::eyre::eyre!("{}", message)),
None => return Ok(None),
};
let no_points = || color_eyre::eyre::eyre!("No valid data points to export");
let spec = &request.spec;
let x_name = spec.encoding.x.field.clone().unwrap_or_default();
let ys = &spec.encoding.y.field;
let title = |column: &str| modal.axis_title(column);
let numbers_of = |column: &str| self.axis_numbers(column);
let y_axis = || {
use chart_modal::Aggregate;
let aggregate = spec.encoding.y.aggregate;
let numbers = match aggregate {
Aggregate::Count | Aggregate::Distinct => {
chart_data::AxisNumbers::count(&self.number_format)
}
a if a.is_fractional() => self.axes_numbers(ys).fractional(),
_ => self.axes_numbers(ys),
};
let names = if aggregate == Aggregate::Count {
"count".to_string()
} else {
ys.iter().map(|y| title(y)).collect::<Vec<_>>().join(", ")
};
let title = match aggregate {
Aggregate::None | Aggregate::Count => names,
_ => format!("{} {names}", spec.encoding.y.aggregate_name()),
};
Axis {
title,
numbers,
log: modal.log_scale,
..Default::default()
}
};
let plot = match prepared {
ChartPrepared::XY(cache) => {
let points = if modal.log_scale {
cache
.series_log
.clone()
.unwrap_or_else(|| log_series(&cache.series))
} else {
cache.series.clone()
};
let last = cache.names.len().saturating_sub(1);
let series: Vec<Series> = points
.into_iter()
.zip(&cache.names)
.zip(&cache.breaks)
.enumerate()
.filter(|(_, ((points, _), _))| !points.is_empty())
.map(|(i, ((points, name), breaks))| Series {
name: name.clone(),
points,
breaks: breaks.clone(),
other: cache.other && i == last,
})
.collect();
if series.is_empty() {
return Err(no_points());
}
Plot::Lines {
series,
scatter: spec.mark == chart_modal::Mark::Scatter,
x: Axis {
title: title(&cache.x_column),
numbers: numbers_of(&cache.x_column),
kind: cache.x_axis_kind,
log: false,
},
y: y_axis(),
y_from_zero: modal.y_starts_at_zero,
}
}
ChartPrepared::Histogram(data) => {
if data.bins.is_empty() {
return Err(no_points());
}
Plot::Histogram {
data: data.clone(),
x: Axis {
title: title(&data.column),
numbers: numbers_of(&data.column),
..Default::default()
},
y: Axis {
title: if data.share { "share" } else { "count" }.to_string(),
numbers: if data.share {
chart_data::AxisNumbers::measure(&self.number_format, "Share")
} else {
chart_data::AxisNumbers::count(&self.number_format)
},
..Default::default()
},
}
}
ChartPrepared::BoxPlot(data) => {
if data.stats.is_empty() {
return Err(no_points());
}
Plot::Box {
data: data.clone(),
x_title: x_name.clone(),
y: Axis {
title: ys.first().map(|y| title(y)).unwrap_or_default(),
numbers: self.axes_numbers(ys),
..Default::default()
},
}
}
ChartPrepared::Kde(data) => {
if data.series.is_empty() {
return Err(no_points());
}
Plot::Kde {
data: data.clone(),
x: Axis {
title: title(&x_name),
numbers: numbers_of(&x_name).fractional(),
..Default::default()
},
y: Axis {
title: "density".to_string(),
numbers: chart_data::AxisNumbers::measure(&self.number_format, "Density"),
..Default::default()
},
}
}
ChartPrepared::Heatmap(data) => {
if data.counts.is_empty() || data.max_count <= 0.0 {
return Err(no_points());
}
Plot::Heatmap {
data: data.clone(),
x: Axis {
title: title(&data.x_column),
numbers: numbers_of(&data.x_column),
..Default::default()
},
y: Axis {
title: title(&data.y_column),
numbers: numbers_of(&data.y_column),
..Default::default()
},
}
}
ChartPrepared::Bar(data) => {
if data.bars.is_empty() {
return Err(no_points());
}
Plot::Bars {
value: Axis {
title: data.value_column.clone(),
numbers: chart_data::AxisNumbers {
format: data.value_format(&self.number_format),
whole: data.value_dtype.is_integer(),
},
..Default::default()
},
data: data.clone(),
}
}
// Left out above: a single X column has nothing to export.
ChartPrepared::XRange(_) => return Err(no_points()),
};
Ok(Some(Figure {
plot,
// The file always has the middle dot; the terminal may be ASCII.
chart_notes: self.chart_notes_of(prepared, "·"),
grid: modal.grid,
}))
}
/// Write the chart from the prepared data off-thread, or park the export until that
/// data is ready. `busy` was set by `ChartExport` and stays set until the export ends.
fn start_chart_export(&mut self, mut request: ChartExportRequest) {
// How the chart was made, from the view and chart as they are now; none
// when the dialog says Omit.
request.options.recipe = if request.recipe {
self.chart_recipe()
} else {
None
};
match self.build_chart_figure() {
Ok(Some(figure)) => {
self.chart_export_waiting = None;
let write = Job::ChartExport {
path: request.path.clone(),
format: request.format,
};
self.spawn_job(write, Some("Exporting chart..."), move |_| {
let ChartExportRequest {
path,
format,
options,
overwrite,
..
} = request;
ChartExportJob { figure, options }
.write(&path, format, overwrite)
.map_err(|e| Self::format_export_error(&e))?;
Ok(Answer::ChartExported)
});
}
// Still being prepared; `BackgroundChartReady` comes back here.
Ok(None) => self.chart_export_waiting = Some(request),
Err(e) => {
let message = Self::format_export_error(&e);
self.finish_chart_export(&request.path, request.format, Err(message));
}
}
}
fn finish_chart_export(
&mut self,
path: &Path,
format: ChartExportFormat,
result: Result<(), String>,
) {
self.chart_export_waiting = None;
self.export_progress = None;
self.status_message = None;
self.busy = false;
match result {
Ok(()) => {
self.flash_path("Chart exported to ", path);
self.chart_export_modal.close();
}
// The form comes back as it was, the reason on its status line.
Err(message) => {
self.chart_export_modal.reopen_with_path(path, format);
self.chart_export_modal.error = Some(message);
}
}
}
/// Why one of the sidebar's filters cannot apply: its value does not read as its
/// column's type. The first such, said for the user.
fn filter_problem(&self) -> Option<String> {
let schema = self.data_table_state.as_ref()?.schema();
self.sort_filter_modal
.filter
.statements
.iter()
.find_map(|f| crate::python_script::SidebarFilter::problem(f, schema.get(&f.column)))
}
/// Whether the dataset on screen is delimited text, whose first row `H` on the
/// Info panel's Schema tab reads the other way.
pub fn header_toggle_offered(&self) -> bool {
self.opened
.as_ref()
.and_then(|(_, options)| options.format)
.and_then(FileFormat::separator)
.is_some()
}
/// Read the dataset again with its first row the other way: as column names, or
/// as data under `column_1`, `column_2`, …. Only delimited text has a header to
/// turn off; anything else carries its own names, and this does nothing there.
pub(crate) fn toggle_header(&mut self) -> Option<AppEvent> {
if !self.header_toggle_offered() {
return None;
}
let (paths, options) = self.opened.clone()?;
let options = OpenOptions {
has_header: Some(!options.has_header.unwrap_or(true)),
..options
};
self.set_loading_phase("Scanning input", 10);
self.name_what_is_loading(paths[0].clone());
Some(AppEvent::Open(paths, options))
}
/// `H` / `L`: the column cursor's column one place left or right in the column
/// order the sidebar's `+` / `-` set, the cursor with it. A frozen column moves
/// among the frozen ones and a scrolling one among the scrolling ones; at an end,
/// nothing moves.
fn move_cursor_column(&mut self, right: bool) -> Option<AppEvent> {
let state = self.data_table_state.as_ref()?;
let at = state.current_column_index()?;
let mut order = state.headers();
let locked = state.locked_columns_count().min(order.len());
let to = if right { at + 1 } else { at.checked_sub(1)? };
if to >= order.len() || (at < locked) != (to < locked) {
return None;
}
// Held by name, so it lands on the column where the move puts it.
let moving = order[at].clone();
self.data_table_state.as_mut()?.set_current_column(&moving);
order.swap(at, to);
// The sidebar places hidden columns by the order it last applied; the two
// trade places there too, so that order still agrees with the table.
let applied = &mut self.sort_filter_modal.sort.applied_order;
if let (Some(i), Some(j)) = (
applied.iter().position(|c| *c == order[at]),
applied.iter().position(|c| *c == order[to]),
) {
applied.swap(i, j);
}
Some(AppEvent::ColumnOrder(order, locked))
}
/// `+` / `-`: a filter on the cursor's cell, added to the sidebar's Filters list
/// and applied, so it shows there, joins the others with "and", and `R` clears
/// it. `+` keeps the rows with the cell's value and `-` drops them; a null cell
/// is "is null" or "not null". The value is the cell's exactly as stored.
/// `[` / `]` at the table: sort by the cursor's column, ascending or descending,
/// in place of the sort in effect. The same key again on a view sorted that way
/// by that column alone takes the sort away.
fn sort_by_cursor_column(&mut self, descending: bool) -> Option<AppEvent> {
let state = self.data_table_state.as_ref()?;
let column = state.current_column()?.to_string();
let already = state.view_sort_columns() == std::slice::from_ref(&column)
&& state.view_sort_descending() == [descending];
if already {
// Back to the natural order: `sort` with no columns resets the direction
// `]` left behind, which would otherwise read as a reversal.
if let Some(state) = self.data_table_state.as_mut() {
state.deferred(|s| s.sort(Vec::new(), true));
}
self.spawn_async_collect("Sorting...");
return None;
}
Some(AppEvent::Sort(vec![column], vec![descending]))
}
fn quick_filter(&mut self, keep: bool) -> Option<AppEvent> {
let state = self.data_table_state.as_ref()?;
let column = state.current_column()?.to_string();
let row = state.copy_row_df()?;
let series = row.column(&column).ok()?.as_materialized_series().clone();
let value = series.get(0).ok()?;
// The schema's type, not the buffer's: binary is buffered as a stub.
let dtype = state.schema().get(&column)?.clone();
let (operator, text) = if value.is_null() {
let operator = if keep {
FilterOperator::IsNull
} else {
FilterOperator::IsNotNull
};
(operator, String::new())
} else {
let operator = if keep {
FilterOperator::Eq
} else {
FilterOperator::NotEq
};
// Text that reads back to this very value: a float exactly as stored,
// a date and time to its last digit, in its zone.
let text = crate::typed_value::text_of(&value, &dtype);
let Some(text) = text else {
let kind = match dtype {
DataType::List(_) => "lists",
DataType::Array(..) => "arrays",
DataType::Struct(_) => "structs",
DataType::Binary | DataType::BinaryOffset => "binary",
_ => "this type",
};
self.flash_note(format!("+ and - filter on plain values, not {kind}"));
return None;
};
(operator, text)
};
let statement = FilterStatement {
columns: Vec::new(),
column,
operator,
value: text,
logical_op: LogicalOperator::And,
};
let mut statements = state.view_filters().to_vec();
if statements.contains(&statement) {
return None;
}
statements.push(statement);
Some(AppEvent::Filter(statements))
}
/// Bring the Sort & Filter sidebar in line with the state actually applied to the
/// frame on screen: the real column order and hidden set, the applied sort, the
/// active filters. Called on open, so an edit staged in the modal and then
/// canceled dies with it rather than arriving pre-staged next time — and after a
/// drill-down swap, where a sidebar still showing the grouped view's filters
/// would re-send one against a List column.
fn sync_sort_filter_modal(&mut self) {
let Some(state) = self.data_table_state.as_ref() else {
return;
};
let filters = state.view_filters().to_vec();
let sort_columns = state.view_sort_columns().to_vec();
let sort_descending = state.view_sort_descending().to_vec();
let headers: Vec<String> = state.schema().iter_names().map(|s| s.to_string()).collect();
let schema = state.schema().clone();
let order = state.headers();
let locked = state.locked_columns_count();
let modal = &mut self.sort_filter_modal;
modal.filter.applied = filters.clone();
modal.filter.statements = filters;
modal.filter.operands = order
.iter()
.map(|name| {
schema
.get(name)
.map(crate::filter_modal::Operand::of)
.unwrap_or_default()
})
.collect();
modal.filter.available_columns = order.clone();
// The cursor starts on the add row; the editor never survives a resync.
modal.filter.cursor = modal.filter.statements.len();
modal.filter.editor = None;
// A schema column the applied order leaves out is hidden; it is listed where
// it stood when hidden, so showing it again puts it back there. The order the
// sidebar last applied says where only while the table still shows it; once a
// view, query or reshape has set the order, the schema places them.
let shown: std::collections::HashSet<&str> = order.iter().map(String::as_str).collect();
let applied = &modal.sort.applied_order;
let current = applied
.iter()
.filter(|name| shown.contains(name.as_str()))
.eq(order.iter());
let reference: &[String] = if current { applied } else { &[] };
let full = order_with_hidden(&order, &headers, reference);
let places: HashMap<&str, usize> = full
.iter()
.enumerate()
.map(|(i, name)| (name.as_str(), i))
.collect();
let place = |name: &String| places.get(name.as_str()).copied();
// Everything up to the last frozen column stays frozen, hidden ones included.
// A hidden column that ended the frozen span is known only to the applied order.
let last_locked = applied
.get(..modal.sort.applied_locked)
.filter(|span| {
current && span.iter().filter(|n| shown.contains(n.as_str())).count() == locked
})
.and_then(|span| span.iter().rev().find_map(place))
.or_else(|| {
locked
.checked_sub(1)
.and_then(|i| order.get(i))
.and_then(place)
});
modal.sort.columns = headers
.iter()
.map(|name| {
let display_order = places[name.as_str()];
SortColumn {
name: name.clone(),
// 1-based: what toggling a column in the modal assigns and what
// the sidebar prints.
sort_order: sort_columns.iter().position(|c| c == name).map(|o| o + 1),
sort_descending: sort_columns
.iter()
.position(|c| c == name)
.and_then(|i| sort_descending.get(i).copied())
.unwrap_or(false),
display_order,
is_locked: last_locked.is_some_and(|l| display_order <= l),
is_to_be_locked: false,
is_visible: shown.contains(name.as_str()),
width: state.width_choice(name),
shown_width: state.on_screen_width(name),
}
})
.collect();
modal.sort.has_unapplied_changes = false;
}
/// Apply everything the sidebar stages — column order and locks, the sort with
/// its per-column directions, the filters — and close it. Enter and Ctrl+Enter,
/// from anywhere in the sidebar.
fn apply_sort_filter(&mut self) -> Option<AppEvent> {
// A row still under edit is committed, never silently dropped.
if self.sort_filter_modal.filter.editor.is_some() {
self.sort_filter_modal.filter.commit_editor();
}
// A value its column cannot compare with stays in the sidebar, which says why.
if let Some(why) = self.filter_problem() {
self.sort_filter_modal.sort.status = Some(why);
return None;
}
let (columns, descending) = self.sort_filter_modal.sort.sorted_columns_and_directions();
let column_order = self.sort_filter_modal.sort.get_column_order();
let locked_count = self.sort_filter_modal.sort.get_locked_columns_count();
self.sort_filter_modal.sort.applied_order =
self.sort_filter_modal.sort.get_full_column_order();
self.sort_filter_modal.sort.applied_locked = self.sort_filter_modal.sort.get_locked_span();
let statements = self.sort_filter_modal.filter.statements.clone();
// Widths read nothing, so they apply here; a fit measures the rows on screen
// when the table is next drawn. With nothing else changed the view stays
// where it is, on the page the fit was asked for: applying the order, filters
// and sort again would read the rows afresh from the top.
let view_unchanged = self.data_table_state.as_mut().is_some_and(|state| {
state.set_width_choices(self.sort_filter_modal.sort.width_choices());
state.headers() == column_order
&& state.locked_columns_count() == locked_count
&& state.view_filters() == statements.as_slice()
&& state.view_sort_columns() == columns.as_slice()
&& state.view_sort_descending() == descending.as_slice()
});
for col in &mut self.sort_filter_modal.sort.columns {
col.is_to_be_locked = false;
}
self.sort_filter_modal.sort.has_unapplied_changes = false;
self.sort_filter_modal.close();
self.input_mode = InputMode::Normal;
if view_unchanged {
return None;
}
let _ = self.send_event(AppEvent::ColumnOrder(column_order, locked_count));
let _ = self.send_event(AppEvent::Filter(statements));
Some(AppEvent::Sort(columns, descending))
}
/// Which of the Info panel's optional tabs the current dataset offers.
fn info_tabs_on_offer(&self) -> crate::widgets::info::TabsOffered {
let facts_tab = self.info_facts_tab();
self.data_table_state
.as_ref()
.map(|state| crate::widgets::info::TabsOffered {
documentation: self.info_documentation.is_open(),
..crate::widgets::info::TabsOffered::of(state, facts_tab)
})
.unwrap_or_default()
}
/// The format of the dataset on screen, as the open read it.
pub(crate) fn opened_format(&self) -> Option<FileFormat> {
self.opened
.as_ref()
.and_then(|(_, options)| options.format)
.or_else(|| self.path.as_deref().and_then(FileFormat::from_path))
}
/// The format's tab of the Info panel that the file facts fill: for one local file,
/// not a hive directory, whose reader has a facts read. See
/// [`crate::widgets::info::InfoContext::facts_tab`].
pub(crate) fn info_facts_tab(&self) -> Option<&'static str> {
self.info_facts()
.and_then(|(format, _)| format.summary_tab())
}
/// The format whose facts read the Info panel's worker makes for the dataset on
/// screen, and that read, once the panel has asked for the file's facts.
pub(crate) fn info_facts(&self) -> Option<(FileFormat, crate::readers::Facts)> {
match self.file_facts()? {
// A directory, which has no footer of its own.
FileFacts::Read {
size: None,
detail: None,
..
} => None,
_ => self.facts_of_open(),
}
}
/// The facts read for the dataset on screen, if its file has one: one file, stored
/// as its format says (a stream or a compressed copy has no footer).
fn facts_of_open(&self) -> Option<(FileFormat, crate::readers::Facts)> {
let hive = self
.opened
.as_ref()
.is_some_and(|(_, options)| options.hive);
let format = self.opened_format()?;
let facts = crate::readers::of(format).facts?;
let state = self.data_table_state.as_ref()?;
let plain = state
.read_mode()
.is_none_or(|mode| Some(mode) == format.read_mode(crate::Stored::Plain));
// Several files, whose footers the Notes and Schema tabs already sum up.
let one_file = state.dataset_schema().is_none();
(!hive && plain && one_file).then_some((format, facts))
}
/// Start applying `view`. Its steps are planned here, which reads nothing; a
/// step that cannot be planned fails here and changes nothing. The reads — a pivot,
/// then the view's first rows — run in the background, and the view is installed
/// when they are in. One that fails there puts the view before it back (#400).
fn apply_view(&mut self, view: &SavedView) -> Result<()> {
self.apply_view_with(view, None)
}
/// [`Self::apply_view`], for a view applied because its criteria fit as `why`
/// says: once its rows are in, a flash names it and the reason.
fn apply_matched_view(&mut self, view: &SavedView, why: view::MatchReason) -> Result<()> {
self.apply_view_with(view, Some(why))
}
fn apply_view_with(&mut self, view: &SavedView, why: Option<view::MatchReason>) -> Result<()> {
self.jobs.supersede(|job| matches!(job, Job::ViewPivot(_)));
if let Some(saved) = &view.settings.sample {
return self.apply_sampled_view(view, saved, why);
}
let Some(state) = self.data_table_state.as_mut() else {
return Ok(());
};
match state.try_transition(|s| Self::replay_view(s, &view.settings, None))? {
(Replayed::Planned, rollback) => {
self.view_planned(view, rollback, why);
Ok(())
}
(Replayed::Pivot(job), rollback) => {
// The table stays as it is while the pivot is read.
state.roll_back(rollback);
// Past any load-ahead for the view on screen, whose rows must not land
// in the one that replaces it.
self.jobs.try_advance();
let pivot_view = Job::ViewPivot(Box::new((view.clone(), why)));
self.spawn_job(pivot_view, Some(Self::APPLYING_VIEW), move |_| {
let pivoted = job
.run()
.map_err(|e| crate::error_display::user_message_from_report(&e, None))?;
Ok(Answer::ViewPivoted(pivoted))
});
Ok(())
}
}
}
/// The view's steps are planned over `rollback`, the view it replaces: mark it
/// applied and read its first rows. Until they are in, a failure puts `rollback`
/// back and the view marked applied before it.
fn view_planned(
&mut self,
view: &SavedView,
rollback: crate::widgets::datatable::ViewRollback,
why: Option<view::MatchReason>,
) {
if let Some(path) = &self.path {
use crate::logging::LogFailure;
self.view_manager
.record_use(&view.id, path)
.or_log("record a view's use");
}
let previous = self.active_view_id.replace(view.id.clone());
self.restore_view_chart(view.settings.chart.as_ref());
let Some(state) = self.data_table_state.as_ref() else {
return;
};
self.query_running = Some(QueryRun {
origin: RunOrigin::View {
previous,
matched: why.map(|why| (view.name.clone(), why)),
},
frame: state.len_generation(),
rollback,
len_count_inflight: self.len_count_inflight,
count_after_paint: self.count_after_paint,
len_count_failed: self.len_count_failed,
rows: None,
});
if !self.spawn_async_collect(Self::APPLYING_VIEW) {
// Nothing to read: the view has no rows. Applied on open, it was the
// open's last step.
if let Some(why) = why {
self.flash_view_applied(&view.name, why);
}
self.query_running = None;
self.busy = false;
self.status_message = None;
self.first_rows_settled();
}
}
/// Whether a view is being applied at the table: its pivot or its first rows are
/// being read.
pub(crate) fn view_applying(&self) -> bool {
if !self.is_busy() || !self.in_normal_table_view() {
return false;
}
let pivot = self
.jobs
.current(|job| matches!(job, Job::ViewPivot(_)))
.is_some();
let rows = self.query_running.as_ref().is_some_and(|run| {
matches!(run.origin, RunOrigin::View { .. })
&& self
.data_table_state
.as_ref()
.is_some_and(|state| state.len_generation() == run.frame)
});
pivot || rows
}
/// What the control bar says while a query's first rows are read over the frame on
/// screen, from the read's job record. The rows drawn meanwhile are the view it
/// replaces, under columns it may have changed.
pub(crate) fn query_reading(&self) -> Option<&str> {
let run = self.query_running.as_ref()?;
let frame = self.data_table_state.as_ref()?.len_generation();
if !matches!(run.origin, RunOrigin::Query(_)) || run.frame != frame {
return None;
}
self.jobs
.waiting_status(|job| Self::reading_rows(job) || Self::owed_rows(job))
}
/// Stop applying a view and keep the one before it. As with a pivot, a worker runs
/// to the end and the bump drops its answer.
fn cancel_view(&mut self) {
self.jobs.advance();
self.screen_generation = self.screen_generation.wrapping_add(1);
if let Some(run) = self.take_query_run() {
self.roll_back_query_run(run);
}
// A collect for the view, queued behind a worker, would read it after all.
self.forget_the_rows_read();
self.read_after_view_rollback();
self.flash_note("View cancelled".to_string());
}
/// A job's outcome is in: take it, and the job's record with it, from [`Jobs`], and
/// act on it. The record goes in this step, so the job holds the generation and
/// the keys until its answer is handled and not after: whatever the answer starts
/// next holds them before anything else can look.
///
/// What the job held is put down here, for every job alike: a job the user waited
/// on gives the keys back, and its line on the control bar goes with it, unless the
/// answer goes on to a continuation, which keeps the wait up across the gap.
fn job_ended(&mut self, ticket: Ticket) -> Option<AppEvent> {
let jobs::Ended {
job,
current,
keys,
outcome,
..
} = self.jobs.end(ticket)?;
let cancelled_analysis = !current && Self::reads_for_analysis(&job);
let waited = keys.is_some();
let out = match outcome {
Outcome::Answered(answer) => self.answered(job, current, waited, *answer),
Outcome::Failed { message, panicked } => {
self.background_failed(&job, current, waited, &message, panicked);
None
}
};
if let Some(status) = keys {
if out.is_some() {
self.busy = true;
} else {
self.busy = false;
// Unless a job that is still running says the same: the read of a
// view's rows that its pivot's answer started.
if self.status_message.as_deref() == Some(status.as_str())
&& !self.jobs.shows(&status)
{
self.status_message = None;
}
}
}
// A cancelled analysis's worker has exited: its read is over, and once no other
// is still going, Run can run again and Setup no longer says it waits.
if cancelled_analysis
&& self.cancelled_analysis().is_none()
&& self.analysis_modal.data_quality_setup_note.as_deref() == Some(QUALITY_RUN_WAITS)
{
self.analysis_modal.data_quality_setup_note = None;
}
out
}
/// A report from a job still running, taken while the job is current.
fn job_progress(&mut self, ticket: Ticket, progress: &Progress) {
if !self.jobs.is_current(ticket) {
return;
}
match progress {
Progress::ExportWriting { phase, bytes } => {
if let Some(export) = self.export_progress.as_mut() {
export.current_phase = phase.to_string();
export.written = Some(*bytes);
}
}
Progress::QualityPhase(phase) => {
if let Some(progress) = self.analysis_modal.computing.as_mut() {
progress.phase = phase.stage.label().to_string();
progress.reads_source = Some(phase.reads_source);
progress.interruptible = Some(phase.interruptible);
}
}
Progress::Finding { rows } => self.find_progress(*rows),
Progress::HexFinding { read, total } => self.hex_find_progress(*read, *total),
Progress::SampleBegun(schema) => self.sample_begun(schema),
Progress::SampleGrew => self.sample_grew(),
}
}
/// `job` answered. `current` says whether its answer is still the one waited for:
/// a stale one changes nothing on screen, and whatever it carries is dropped here.
/// `waited` says the user was waiting on it.
fn answered(
&mut self,
job: Job,
current: bool,
waited: bool,
answer: Answer,
) -> Option<AppEvent> {
match answer {
Answer::Load(answer) => {
// The open's to judge, by its own identity rather than the generation: an
// answer for an open given up or replaced, or for a phase it has left,
// changes nothing on screen, and what it carries — a download's file, a
// dataset — is dropped with it.
let Job::Load(load) = job else {
return None;
};
let step = self.loading.answered(
load,
*answer,
#[cfg(any(feature = "http", feature = "cloud"))]
&self.jobs,
);
self.run_load_step(step)
}
Answer::NamedPaths {
paths,
options,
directory,
} => {
// The user left the open while its paths were looked at, or another took
// its place.
let Job::OpenNamed(load) = job else {
return None;
};
if !self.loading.looking_at_paths(load) {
return None;
}
// Either carries the same open on: it is still starting.
Some(match directory {
Some(dir) => AppEvent::LookThenOpenDirectory(dir, *options),
None => AppEvent::Open(paths, *options),
})
}
Answer::NamedPathMissing(path) => {
let Job::OpenNamed(load) = job else {
return None;
};
if !self.loading.looking_at_paths(load) {
return None;
}
// The session ends saying so; nothing is opened.
if let Some(retired) = self.loading.retire() {
self.put_down_load(retired);
}
Some(AppEvent::NamedPathMissing(path))
}
Answer::LookedAt {
kind,
holds,
options,
} => {
// The user pressed Ctrl+O and went to the home screen, a newer look
// replaced this one, or another open took its place while this was
// reading. Their choice is the one on screen, and this is the answer to a
// question nobody is waiting for.
let Job::LookAtDirectory { load, path } = job else {
return None;
};
if !self.loading.looking_at_directory(load) {
return None;
}
// An `Open` that follows carries the same open on.
self.open_the_directory_looked_at(path, kind, holds.as_deref(), *options)
}
Answer::Kind(found) => {
// Superseded: a newer look, a trip away from home, or something that took
// the screen over owns the wait, so this one touches nothing.
let Job::Classify(asked) = job else {
return None;
};
if !current {
return None;
}
self.home.status = None;
// A key pressed on the home screen answers on the home screen. If they
// went back to the data, opening now would arrive from nowhere; if the
// browse has moved, the answer is about somewhere they navigated away
// from, and acting on it would take them back into it.
if self.input_mode != InputMode::Home || self.home.browsing != asked.browsing {
return None;
}
let path = asked.path;
let Some(kind) = found else {
self.home.status = Some(format!("No such path: {}", path.display()));
if asked.jump {
// A typo typed at `~` is worth another go without retyping it.
self.home.path_input = path.display().to_string();
self.home.path_input_active = true;
self.list_the_typed_directory();
}
return None;
};
self.open_what_it_is(path, kind, asked.jump)
}
Answer::Rows(result) => {
// A stale page is dropped; the wait belongs to whatever replaced it.
let Job::Rows(inflight) = job else {
return None;
};
if !current {
return None;
}
// Timed to here rather than to the next paint: this is the moment the
// rows exist to be drawn, and the frame that draws them costs the same
// whatever the page cost to fetch.
if let Some(state) = self.data_table_state.as_ref() {
let took = inflight.began.elapsed();
log::debug!(
target: "datui",
"rows {}..{} of {}: read in {took:.1?}",
inflight.start,
inflight.end,
inflight.dataset
);
state.measurements().read_page(took, inflight.files);
}
if let Some(state) = &mut self.data_table_state {
state.apply_async_collect(result);
}
self.retire_a_count_the_rows_answered();
self.remember_a_downloads_shape();
// Rows a follow counted while these were read are shown next.
self.catch_up_follow();
// The query's first rows are in: it stands.
let ran = self.take_query_run();
// A load-ahead's end is nobody's wait ending: whatever else is under
// way meanwhile keeps its spinner and its message.
if waited {
self.first_rows_settled();
match ran.map(|run| run.origin) {
Some(RunOrigin::Query(mode)) if self.query_prompt_mode() == Some(mode) => {
self.leave_query_prompt_after_run();
}
// Shown once the wait is over, or the spinner's message hides it.
Some(RunOrigin::View {
matched: Some((name, why)),
..
}) => self.flash_view_applied(&name, why),
_ => {}
}
}
None
}
Answer::RowsFailed {
message,
conversion,
} => {
self.rows_failed(current, waited, &message, conversion.as_deref());
None
}
Answer::Described(results) => {
if current {
self.analysis_modal.describe_results = Some(results);
self.analysis_modal.computing = None;
}
None
}
Answer::Distributions(results) => {
if current {
self.analysis_modal.distribution_results = Some(results);
self.analysis_modal.computing = None;
}
None
}
Answer::Correlations(results) => {
if current {
self.analysis_modal.install_correlations(results);
self.analysis_modal.computing = None;
}
None
}
Answer::DataQuality {
results,
kept,
plan,
} => {
// Kept whatever became of the run's results: the rows are the rows the
// key names, and a read is not to be thrown away.
if let Some(kept) = kept {
self.retain_quality_sample(&kept);
}
if current
&& self.analysis_modal.active
&& self.analysis_modal.selected_tool
== Some(analysis_modal::AnalysisTool::DataQuality)
{
// Labeled with the plan it was dispatched with, whatever has been
// staged since.
self.cache_quality_result(&results, (*plan).clone());
self.analysis_modal.data_quality_last_plan = Some(*plan);
self.analysis_modal.data_quality_results = Some(*results);
self.analysis_modal.data_quality_from_cache = false;
self.analysis_modal
.set_quality_page(crate::data_quality::QualityPage::Overview);
self.analysis_modal.computing = None;
}
None
}
Answer::SampleDrawn(drawn) => self.sample_drawn(job, current, drawn),
Answer::Sample { df, label } => {
if current {
self.analysis_modal.computing = None;
self.show_sample_view(df, label);
}
None
}
Answer::Pivoted { spec, pivoted } => {
// Superseded means something replaced the view, which owns the wait.
if !current {
return None;
}
let installed = self.data_table_state.as_mut().map(|state| {
state
.deferred(|s| s.install_pivot(&spec, pivoted))
.map_err(|e| crate::error_display::user_message_from_report(&e, None))
});
match installed {
Some(Ok(())) => {
self.pivot_melt_modal.close();
// Only from the modal: a trip home meanwhile stays home.
if self.input_mode == InputMode::PivotMelt {
self.input_mode = InputMode::Normal;
}
// The wait passes to the read of its rows.
self.spawn_async_collect(Self::LOADING_BUFFER);
}
Some(Err(message)) => self.error_modal.show(message),
None => {}
}
None
}
Answer::ReshapePreviewed { input, result } => {
if let Job::ReshapePreview { epoch, token } = job {
self.reshape_preview_ended(epoch, token, input, result);
}
None
}
Answer::ViewPivoted(pivoted) => {
// Superseded means the view was cancelled or something replaced it, which
// owns the wait.
let Job::ViewPivot(pivot) = job else {
return None;
};
let (view, why) = *pivot;
if !current {
return None;
}
let planned = self.data_table_state.as_mut().map(|state| {
// Nothing changed while the pivot was read, so the steps before
// it plan as they did; this time the pivot is in hand.
state
.try_transition(|s| Self::replay_view(s, &view.settings, Some(pivoted)))
.map(|(_, rollback)| rollback)
.map_err(|e| e.to_string())
});
match planned {
// The wait passes to the read of its rows.
Some(Ok(rollback)) => self.view_planned(&view, rollback, why),
Some(Err(message)) => self.view_pivot_failed(&message),
None => {}
}
None
}
Answer::DrillRow { group_index, row } => {
// Superseded means something replaced the view, which owns the wait.
if current {
self.drill_into(group_index, &row);
}
None
}
Answer::FieldsRead(values) => {
// Superseded means something replaced the view, which owns the wait.
let Job::InspectRow { frame, row } = job else {
return None;
};
if !current {
return None;
}
let asked = self
.inspector_modal
.read
.as_ref()
.is_some_and(|read| read.key() == (frame, row));
if self.inspector_modal.active && asked {
self.inspector_modal.read =
Some(inspector_modal::FieldRead::Read { frame, row, values });
}
None
}
Answer::JsonParsed(root) => {
// Superseded means something replaced the view, which owns the wait.
let Job::InspectJson { token } = job else {
return None;
};
if !current || !self.inspector_modal.active {
return None;
}
let modal = &mut self.inspector_modal;
if let Some(wait) = modal.json_wait.take_if(|w| w.token == token) {
let node = inspector_drill::Node::Json {
root,
path: Vec::new(),
};
modal.drill_in(wait.frame, wait.row, wait.label, node);
}
None
}
Answer::Indented(text) => {
let Job::InspectPretty { token } = job else {
return None;
};
let modal = &mut self.inspector_modal;
if current
&& let Some(inspector_modal::Pretty::Pending { token: t, place }) =
modal.pretty.as_ref()
&& *t == token
{
modal.pretty = Some(inspector_modal::Pretty::Ready {
place: place.clone(),
text,
});
}
None
}
Answer::Unpacked(decoded) => {
let Job::InspectUnpack { token } = job else {
return None;
};
let modal = &mut self.inspector_modal;
if current
&& let Some(inspector_modal::Unpack::Pending { token: t, place }) =
modal.unpack.as_ref()
&& *t == token
{
modal.unpack = Some(inspector_modal::Unpack::Ready {
place: place.clone(),
text: std::sync::Arc::new(decoded),
});
}
None
}
Answer::ValueWritten(open) => {
if current && self.inspector_modal.active {
self.external_open = Some(open);
}
None
}
Answer::Exported(path) => {
// Written: the dialog held for a failure is done with.
self.export_modal.close();
self.export_counts = None;
if current {
self.export_progress = None;
self.flash_path("Exported to ", &path);
}
None
}
Answer::Copied { payload, message } => {
if current {
self.export_progress = None;
self.finish_copy(payload, message);
}
None
}
Answer::QualityReportWritten(path) => {
self.analysis_modal.data_quality_export = None;
if current {
self.flash_path("Report written to ", &path);
}
None
}
Answer::ChartExported => {
// Leaving the chart's dataset supersedes the write: one that finishes
// after Ctrl-O must not reopen its modal over the home screen.
if let Job::ChartExport { path, format } = job
&& current
{
self.finish_chart_export(&path, format, Ok(()));
}
None
}
Answer::FileFacts(facts) => {
if let Job::FileFacts { dataset } = job {
self.file_facts_landed(dataset, facts);
}
None
}
Answer::UnfitCounted(unfit) => {
// Every value fitting says nothing in the Notes; the log says it ran.
let columns: Vec<&str> = unfit.iter().map(|u| u.column.as_str()).collect();
let said = if columns.is_empty() {
"none".to_string()
} else {
columns.join(", ")
};
log::debug!(target: "datui", "values column types made null, by column: {said}");
if let Job::UnfitCount { dataset, version } = job
&& dataset == self.dataset_generation
&& let Some(state) = self.data_table_state.as_mut()
{
match version {
None => state.unfit_counted(&unfit),
Some(version) => state.changes_unfit_counted(version, &unfit),
}
}
None
}
Answer::Found(found) => {
if let Job::Find(run) = job {
self.find_answered(run, current, found);
}
None
}
Answer::HexOpened(source) => {
self.hex_opened(job, current, *source);
None
}
Answer::HexFound(hit) => {
self.hex_found(job, current, hit);
None
}
Answer::ValueCounts(counts) => {
// Superseded means the screen moved on: another column, a cancel, a
// trip away.
if current {
self.value_counts.computing = None;
self.value_counts.hold(*counts);
}
None
}
// What a test's answer carries goes with it.
#[cfg(test)]
Answer::Probe(held) => {
drop(held);
None
}
}
}
/// Put down what a failed background operation started, and say why.
///
/// The keys and the line it held are put down by [`Self::job_ended`]. Each arm
/// clears only what the job itself started, and only when the job is current: a
/// load-ahead that dies leaves the analysis beside it running, and an older look at
/// a path leaves the newer one waiting. One that is not current is dropped.
fn background_failed(
&mut self,
job: &Job,
current: bool,
waited: bool,
message: &str,
panicked: bool,
) {
match job {
// Judged by the open, as its answers are: one put down or replaced is not the
// open the user is waiting on.
Job::Load(load) | Job::OpenNamed(load) | Job::LookAtDirectory { load, .. } => {
if let loading::Step::Failed(failed) = self.loading.failed(*load, message) {
self.load_failed(failed);
}
}
Job::Classify(_) => {
if current {
self.home.status = None;
self.error_modal.show(message.to_string());
}
}
Job::Rows(_) | Job::OwedRows { .. } => self.rows_failed(current, waited, message, None),
Job::Analysis(_) | Job::SampleRows => {
if current {
self.analysis_modal.computing = None;
self.error_modal.show(message.to_string());
}
}
Job::SampleDraw(_) => self.sample_draw_failed(job, current, message),
// The form stays up with its spec, to be fixed.
Job::Pivot | Job::Copy => {
if current {
self.error_modal.show(message.to_string());
}
}
// The dialog is still up, the reason on its status line under the path.
Job::QualityReport => {
if current {
match self.analysis_modal.data_quality_export.as_mut() {
Some(form) => form.error = Some(message.to_string()),
None => self.error_modal.show(message.to_string()),
}
}
}
Job::ViewPivot(_) => {
if current {
self.view_pivot_failed(message);
}
}
// The preview says why in its own pane; the log has a panic's details.
Job::ReshapePreview { epoch, token } => {
let message = if panicked {
"Could not preview; see the log".to_string()
} else {
message.to_string()
};
self.reshape_preview_ended(*epoch, *token, None, Err(message));
}
Job::DrillRow => {
// The grouped view stays as it was. A flash has one line, and a panic's
// message is an internal error with the log's path under it: the log
// has the details.
if current {
self.flash_note(if panicked {
"Could not drill in; see the log".to_string()
} else {
format!("Could not drill in: {message}")
});
}
}
Job::InspectJson { token } => {
let modal = &mut self.inspector_modal;
if current && let Some(wait) = modal.json_wait.take_if(|w| w.token == *token) {
modal.not_json = Some((wait.frame, wait.row, wait.path));
self.flash_note(if panicked {
"Could not read the JSON; see the log".to_string()
} else {
sentence(message)
});
}
}
Job::InspectPretty { token } => {
let modal = &mut self.inspector_modal;
if let Some(inspector_modal::Pretty::Pending { token: t, place }) =
modal.pretty.as_ref()
&& t == token
{
modal.pretty = Some(inspector_modal::Pretty::Failed {
place: place.clone(),
});
}
}
Job::InspectUnpack { token } => {
let modal = &mut self.inspector_modal;
if let Some(inspector_modal::Unpack::Pending { token: t, place }) =
modal.unpack.as_ref()
&& t == token
{
modal.unpack = Some(inspector_modal::Unpack::Failed {
place: place.clone(),
});
}
}
Job::OpenValue => {
if current {
self.flash_note(if panicked {
"Could not open the value; see the log".to_string()
} else {
format!("Could not open the value: {message}")
});
}
}
Job::InspectRow { frame, row } => {
if current {
let asked = self
.inspector_modal
.read
.as_ref()
.is_some_and(|read| read.key() == (*frame, *row));
if asked {
// The pane has room for the reason; a panic's is the log's.
let message = if panicked {
"Could not read the field; see the log".to_string()
} else {
format!("Could not read the field: {message}")
};
self.inspector_modal.read = Some(inspector_modal::FieldRead::Failed {
frame: *frame,
row: *row,
message,
});
}
}
}
// The form comes back as it was, the reason on its status line, to fix
// the path and press Enter again.
Job::Export => {
if current {
self.export_progress = None;
self.export_modal.resume();
self.export_modal.path_error = Some(message.to_string());
self.input_mode = InputMode::Export;
} else {
self.export_modal.close();
self.export_counts = None;
}
}
Job::ChartExport { path, format } => {
if current {
self.finish_chart_export(path, *format, Err(message.to_string()));
}
}
Job::Find(_) => self.find_failed(current, message),
Job::HexOpen { .. } => {
if current {
self.error_modal.show(message.to_string());
}
}
Job::HexFind(_) => {
if current {
self.status_message = None;
self.flash_note(message.to_string());
}
}
Job::ValueCounts => {
// Said on the screen, in place of the counts.
if current && let Some(computing) = self.value_counts.computing.take() {
let why = if panicked {
"could not count; see the log".to_string()
} else {
message.to_string()
};
self.value_counts.failed = Some((computing.column, why));
}
}
// Judged by the dataset, as its answer is.
Job::FileFacts { dataset } => {
// The panel has one line for it, and a panic's message is an internal
// error with the log's path under it.
let why = if panicked {
"could not read; see the log".to_string()
} else {
message.to_string()
};
self.file_facts_landed(*dataset, FileFacts::Failed(why));
}
// The note is left unsaid; the log has why.
Job::UnfitCount { .. } => {
log::warn!(target: "datui", "counting values that did not fit their type failed: {message}");
}
}
}
/// Ask a worker for the open file's size and footer, unless this dataset has
/// already asked. A source with no file on this machine has none to ask for.
///
/// No lease and no busy state. The answer is judged by `dataset_generation`, which
/// a bump does not change, so a bump cannot strand it; leased, a slow stat would
/// hold the next buffer collect behind it. And busy would hold the keys typed at
/// the panel, Esc included, behind a read the panel already says it is waiting on.
fn read_file_facts(&mut self) {
let dataset = self.dataset_generation;
if self.data_table_state.is_none()
|| self
.file_facts
.as_ref()
.is_some_and(|(read, _)| *read == dataset)
|| self.file_facts_reading()
{
return;
}
// One file on this machine, or nothing: a glob is no file to stat, and several
// files are not the first one's size.
let several = self
.opened
.as_ref()
.is_some_and(|(paths, _)| paths.len() > 1);
let piped = self.reads_stdin();
let Some(path) = self.path.clone().filter(|path| {
!several
&& !piped
&& !source::is_remote_url(path)
&& !source::is_prefix_or_glob(&path.to_string_lossy())
}) else {
return;
};
let facts = self.facts_of_open().map(|(_, facts)| facts);
#[cfg(test)]
let read: FileFactsReader = self
.file_facts_reader
.clone()
.unwrap_or_else(|| Arc::new(FileFacts::read));
#[cfg(not(test))]
let read = FileFacts::read;
self.spawn_job(Job::FileFacts { dataset }, None, move |_| {
Ok(Answer::FileFacts(read(&path, facts)?))
});
}
/// Count, behind the Info panel, the values the read's column types made null, for
/// the Notes: one pass over the frame before the types, the first time the panel
/// opens on a dataset with typed columns.
fn count_unfit(&mut self) {
let dataset = self.dataset_generation;
let Some(state) = self.data_table_state.as_ref() else {
return;
};
let read = state
.unfit_to_count()
.map(|(source, typed)| (source, typed, None));
let view = state
.changes_unfit_to_count()
.map(|(source, typed, version)| (source, typed, Some(version)));
let streaming = self.app_config.performance.streaming;
for (source, typed, version) in [read, view].into_iter().flatten() {
let running = self
.jobs
.current(|job| {
matches!(job, Job::UnfitCount { dataset: d, version: v }
if *d == dataset && *v == version)
})
.is_some();
if running {
continue;
}
self.spawn_job(Job::UnfitCount { dataset, version }, None, move |_| {
let counted = crate::statistics::collect_lazy(
crate::column_types::unfit_frame(source, &typed),
streaming,
)
.map_err(|e| crate::error_display::user_message_from_polars(&e))?;
Ok(Answer::UnfitCounted(crate::column_types::unfit_counts(
&counted, &typed,
)))
});
}
}
/// Whether the values the read's column types made null are being counted.
pub fn unfit_count_pending(&self) -> bool {
self.jobs
.current(|job| matches!(job, Job::UnfitCount { .. }))
.is_some()
}
/// Whether the open dataset's file facts are being read.
pub(crate) fn file_facts_reading(&self) -> bool {
let dataset = self.dataset_generation;
self.jobs
.current(|job| matches!(job, Job::FileFacts { dataset: asked } if *asked == dataset))
.is_some()
}
/// The file facts read for `dataset`, kept if that is still the dataset on screen.
/// An answer for one replaced since is about a file no longer there.
fn file_facts_landed(&mut self, dataset: u64, facts: FileFacts) {
if dataset == self.dataset_generation {
self.file_facts = Some((dataset, facts));
}
}
/// What the Info panel knows about the open file: `None` until it is asked, and
/// for a source with no file on this machine. Installing a dataset clears it, so
/// what is here is the open dataset's.
pub fn file_facts(&self) -> Option<&FileFacts> {
Self::facts_shown(
&self.file_facts,
self.dataset_generation,
self.file_facts_reading(),
)
}
/// [`Self::file_facts`], from the fields it reads, for a caller holding the rest
/// of the app.
pub(crate) fn facts_shown(
read: &Option<(u64, FileFacts)>,
dataset: u64,
reading: bool,
) -> Option<&FileFacts> {
static READING: FileFacts = FileFacts::Reading;
match read {
Some((read_for, facts)) if *read_for == dataset => Some(facts),
_ => reading.then_some(&READING),
}
}
/// An open found a database of several tables: the home screen lists them, as it
/// lists a directory of separate tables.
fn land_on_tables(&mut self, tables: loading::Tables) {
let loading::Tables {
database,
from_home,
} = tables;
self.status_message = None;
self.busy = false;
self.enter_home();
if from_home {
self.home_browse_into(database);
} else {
self.home_jump_into(database);
}
}
/// An open failed before its first rows; the loader has put it down. The dataset
/// already up is the current one again, or the home screen is, when that is where
/// the open was chosen.
fn load_failed(&mut self, failed: loading::Failed) {
let loading::Failed { message, from_home } = failed;
self.status_message = None;
self.busy = false;
// Kept so the home screen can say why, if dismissing the error lands the user
// there from a command line that named the file. Chosen at home, the dialog
// has said it, and the prompt's line saying it again was the same failure
// reported twice (#547 D8).
if from_home {
self.last_load_error = None;
self.enter_home();
} else {
self.last_load_error = Some(message.clone());
}
self.error_modal.show(message);
}
/// The table's rows could not be read. A query or view waiting on them is not
/// applied (#400, #432); a page the table waited on ends the wait with the reason;
/// a load-ahead's failure is left for the page that needs those rows.
fn rows_failed(
&mut self,
current: bool,
waited: bool,
message: &str,
conversion: Option<&crate::error_display::ConversionFailure>,
) {
if !current {
return;
}
let message = &self.named_by_source(message);
if let Some(run) = self.take_query_run() {
self.fail_query_run(run, message, conversion);
return;
}
if !waited {
return;
}
// A count waiting for this page to paint would read the frame that just
// failed to: it fails with it, the way a count riding in the collect does.
if let Some(generation) = self.count_after_paint.take() {
if self.len_count_inflight == Some(generation) {
self.len_count_inflight = None;
}
if self
.data_table_state
.as_ref()
.is_some_and(|state| state.len_generation() == generation)
{
self.len_count_failed = Some(generation);
}
}
self.first_rows_settled();
self.error_modal.show(message.to_string());
}
/// `message` with the temporary files the dataset on screen reads (a download, a
/// decompressed copy) called by what the user opened.
fn named_by_source(&self, message: &str) -> String {
let (Some(state), Some(source)) = (self.data_table_state.as_ref(), self.path.as_deref())
else {
return message.to_string();
};
state
.temp_files()
.into_iter()
.fold(message.to_string(), |message, file| {
crate::error_display::named_by_source(&message, file, source)
})
}
/// A view's pivot could not be read or planned: the view before it stays.
fn view_pivot_failed(&mut self, message: &str) {
self.error_modal
.show(format!("Error applying view: {message}"));
self.read_after_view_rollback();
}
/// A query or view whose first rows could not be read is not applied: put back
/// what it replaced and say why where its origin says to.
fn fail_query_run(
&mut self,
run: QueryRun,
message: &str,
conversion: Option<&crate::error_display::ConversionFailure>,
) {
let rows = run.rows;
let origin = self.roll_back_query_run(run);
self.first_rows_settled();
self.status_message = None;
self.busy = false;
// Run from the prompt, the reason goes under the query, which stays open to
// be fixed. Sent any other way — a view applied — there is nothing to edit,
// and the error modal says why.
let mode = match origin {
RunOrigin::View { .. } => {
self.error_modal
.show(format!("Error applying view: {message}"));
self.read_after_view_rollback();
return;
}
RunOrigin::Query(mode) if self.query_prompt_mode() == Some(mode) => mode,
RunOrigin::Query(_) => {
self.error_modal.show(message.to_string());
return;
}
};
let sql = mode == QueryMode::Sql;
self.query_run_error = Some(match conversion {
Some(failure) if sql => failure.sql_message(rows),
_ => message.to_string(),
});
self.inline_failures = self.inline_failures.wrapping_add(1);
}
/// Put back the view a running query or view replaced, with its row count, and
/// return where the query came from.
fn roll_back_query_run(&mut self, run: QueryRun) -> RunOrigin {
if let Some(state) = self.data_table_state.as_mut() {
state.roll_back(run.rollback);
}
self.len_count_inflight = run.len_count_inflight;
self.count_after_paint = run.count_after_paint;
self.len_count_failed = run.len_count_failed;
if let RunOrigin::View { previous, .. } = &run.origin {
self.active_view_id = previous.clone();
}
run.origin
}
/// The view before a failed or cancelled one is back: read its rows if it has none
/// on hand, as when the view was applied on open, else stop being busy.
fn read_after_view_rollback(&mut self) {
self.busy = false;
self.status_message = None;
if !self.spawn_async_collect(Self::LOADING_BUFFER) {
self.first_rows_settled();
}
}
/// Run a view's steps on `state` in the order they were built. With a pivot or melt:
/// the query, filters and sort it ran over, the reshape, then the query, filters and
/// sort on its result. Without one: the query, filters and sort. Column order last.
/// Stops at the first step that fails, and at a pivot unless `pivoted` holds it.
fn replay_view(
state: &mut DataTableState,
settings: &view::ViewSettings,
pivoted: Option<DataFrame>,
) -> Result<Replayed> {
if settings.pivot.is_some() || settings.melt.is_some() {
if let Some(source) = &settings.reshape_source {
Self::replay_query(
state,
source.sql_query.as_deref(),
source.query.as_deref(),
source.fuzzy_query.as_deref(),
)?;
Self::replay_filters_and_sort(
state,
&source.filters,
&source.sort_columns,
source.sort_directions(),
)?;
}
let reshaped = match (&settings.pivot, &settings.melt, pivoted) {
(Some(spec), _, Some(pivoted)) => state.install_pivot(spec, pivoted),
(Some(spec), _, None) => {
Self::check_plan(state)?;
return Ok(Replayed::Pivot(Box::new(state.plan_pivot(spec))));
}
(None, Some(spec), _) => state.melt(spec),
(None, None, _) => Ok(()),
};
reshaped.map_err(|e| {
color_eyre::eyre::eyre!(
"{}",
crate::error_display::user_message_from_report(&e, None)
)
})?;
}
Self::replay_query(
state,
settings.sql_query.as_deref(),
settings.query.as_deref(),
settings.fuzzy_query.as_deref(),
)?;
// Before the filters, which may compare in the types it gives.
if !settings.columns.is_empty() {
state.set_column_changes(&settings.columns);
}
Self::replay_filters_and_sort(
state,
&settings.filters,
&settings.sort_columns,
settings.sort_directions(),
)?;
if !settings.column_order.is_empty() {
state.set_column_order(settings.column_order.clone());
state.set_locked_columns(settings.locked_columns_count);
}
Self::check_plan(state)?;
Ok(Replayed::Planned)
}
/// Whether the frame the steps so far built can be read, by its plan alone.
fn check_plan(state: &DataTableState) -> Result<()> {
state.check_plan().map_err(|e| {
color_eyre::eyre::eyre!("{}", crate::error_display::user_message_from_polars(&e))
})
}
/// A view's query: SQL or q (at most one is stored), then a Text query.
fn replay_query(
state: &mut DataTableState,
sql: Option<&str>,
dsl: Option<&str>,
fuzzy: Option<&str>,
) -> Result<()> {
let stated = |q: Option<&str>| q.filter(|q| !q.trim().is_empty()).map(str::to_string);
if let Some(sql) = stated(sql) {
state.sql_query(sql);
} else if let Some(query) = stated(dsl) {
state.query(query);
}
if state.error().is_none()
&& let Some(fuzzy) = stated(fuzzy)
{
state.fuzzy_search(fuzzy);
}
match state.error().cloned() {
Some(error) => Err(color_eyre::eyre::eyre!(
"{}",
crate::error_display::user_message_from_polars(&error)
)),
None => Ok(()),
}
}
/// A view's sidebar filters, then its sort.
fn replay_filters_and_sort(
state: &mut DataTableState,
filters: &[FilterStatement],
sort_columns: &[String],
descending: Vec<bool>,
) -> Result<()> {
if !filters.is_empty() {
state.filter(filters.to_vec());
if let Some(error) = state.error().cloned() {
return Err(color_eyre::eyre::eyre!("{}", error));
}
}
if !sort_columns.is_empty() {
state.sort_by(sort_columns.to_vec(), descending);
if let Some(error) = state.error().cloned() {
return Err(color_eyre::eyre::eyre!("{}", error));
}
}
Ok(())
}
/// What the status line says while an export writes its file.
fn export_write_phase(request: &ExportRequest) -> &'static str {
if request.options.compression(request.format).is_some() {
"Writing and compressing file"
} else {
"Writing file"
}
}
/// What the error modal says when writing an export, report or chart fails.
/// Why an export did not write, for the dialog's status line, which sits under
/// the path it is about.
fn format_export_error(error: &color_eyre::eyre::Report) -> String {
use std::io::{self, ErrorKind};
for cause in error.chain() {
if let Some(io_err) = cause.downcast_ref::<io::Error>() {
// Matched by type, not kind: an encoder's own errors share
// kinds such as InvalidInput with the destination checks.
let msg = match (crate::output_file::Refused::of(io_err), io_err.kind()) {
(Some(refused), _) => format!("{refused}."),
// A CSV open in a spreadsheet app, on Windows.
(None, _) if crate::error_display::held_by_another_program(io_err) => {
"it is open in another program; close it there and try again.".to_string()
}
(None, ErrorKind::PermissionDenied) => "permission denied.".to_string(),
(None, ErrorKind::IsADirectory) => "it is a directory.".to_string(),
(None, _) => crate::error_display::user_message_from_io(io_err, None),
};
return format!("Cannot write: {msg}");
}
if let Some(pe) = cause.downcast_ref::<polars::prelude::PolarsError>() {
let msg = crate::error_display::user_message_from_polars(pe);
return format!("Export failed: {}", msg);
}
}
let error_str = error.to_string();
let first_line = error_str.lines().next().unwrap_or("Unknown error").trim();
format!("Export failed: {}", first_line)
}
/// Above this estimated size a table copy asks first: most paste targets
/// choke long before it, and the clipboard holds the whole thing at once.
const COPY_CONFIRM_BYTES: usize = 10 * 1024 * 1024;
/// Above this a table copy is refused outright; a file is the medium for
/// data this size, and export writes one without holding it all in text.
const COPY_REFUSE_BYTES: usize = 200 * 1024 * 1024;
/// Enter in the copy dialog: the synchronous scopes copy from the buffer
/// and flash; the table scope guards on size, then collects off-thread.
fn perform_copy(&mut self) -> Option<AppEvent> {
use copy_modal::{CopyScope, thousands};
/// What Enter decided, worked out under the table borrow and acted on
/// after it: writing to the clipboard needs the whole app back.
enum Planned {
Copy(clipboard::Payload, String),
Collect,
/// None: the size is not known (the row count is still coming, or a
/// binary column's width is known to no footer).
Confirm(Option<usize>),
}
let format = self.copy_modal.format;
let header = self.copy_modal.header();
let scope = self.copy_modal.scope;
// What the destination takes decides what is built: no HTML flavor for one
// that cannot offer it, and no copy past its cap.
let accepts = match self.copy_destination() {
Ok(destination) => destination.accepts(),
Err(e) => {
self.copy_modal.close();
self.input_mode = InputMode::Normal;
self.error_modal.show(e);
return None;
}
};
let planned: Result<Planned, String> = match self.data_table_state.as_ref() {
None => Err("Nothing to copy: no table is open".to_string()),
Some(state) => match scope {
CopyScope::Cell => {
let column = self.copy_modal.column.clone().unwrap_or_default();
match state.copy_cell_value(&column) {
Some(value) => {
let row = state.selected_display_row().unwrap_or(0);
Ok(Planned::Copy(
clipboard::Payload::text(value),
format!("Copied cell {column} of row {}", thousands(row)),
))
}
None => Err("Nothing to copy: the current row is not buffered".to_string()),
}
}
CopyScope::Row => match state.copy_row_df() {
Some(df) => clipboard::tabular_payload(&df, format, header, accepts.html).map(
|payload| {
let row = state.selected_display_row().unwrap_or(0);
Planned::Copy(
payload,
format!("Copied row {} as {}", thousands(row), format.as_str()),
)
},
),
None => Err("Nothing to copy: the current row is not buffered".to_string()),
},
CopyScope::View => match state.copy_view_df() {
Some(df) => clipboard::tabular_payload(&df, format, header, accepts.html).map(
|payload| {
Planned::Copy(
payload,
format!(
"Copied {} rows as {}",
thousands(df.height()),
format.as_str()
),
)
},
),
None => Err("Nothing to copy: no rows are on screen".to_string()),
},
CopyScope::Python => Ok(Planned::Copy(
clipboard::Payload::text(self.python_script(state)),
"Copied the view as Python".to_string(),
)),
CopyScope::Table => {
// A capped destination's copy is read only as far as its cap, so
// what could be held is the smaller of the two.
let cap = accepts
.base64_limit
.map_or(usize::MAX, |limit| limit / 4 * 3);
match state.estimated_copy_bytes() {
Some(bytes) if bytes > Self::COPY_REFUSE_BYTES => Err(format!(
"The table is about {} — too much to hold on a clipboard. \
Export it to a file instead (e).",
Self::format_bytes(bytes as u64)
)),
Some(bytes) if bytes.min(cap) > Self::COPY_CONFIRM_BYTES => {
Ok(Planned::Confirm(Some(bytes)))
}
Some(_) => Ok(Planned::Collect),
None if cap <= Self::COPY_CONFIRM_BYTES => Ok(Planned::Collect),
// The row count has not landed yet, or a binary column's width
// is unknown, so the size is anyone's guess: ask before
// collecting an unknown amount.
None => Ok(Planned::Confirm(None)),
}
}
},
};
self.copy_modal.close();
self.input_mode = InputMode::Normal;
match planned {
Ok(Planned::Copy(payload, message)) => {
self.finish_copy(payload, message);
None
}
Ok(Planned::Collect) => Some(AppEvent::CopyTable { format, header }),
Ok(Planned::Confirm(bytes)) => {
self.pending_copy = Some((format, header));
let counting = self
.data_table_state
.as_ref()
.is_some_and(|state| state.num_rows_if_valid().is_none());
self.confirmation_modal.show(match bytes {
Some(bytes) => format!(
"This copies about {} to the clipboard.\n\nCopy the whole table?",
Self::format_bytes(bytes as u64)
),
None if counting => "The table's size is not known yet — the row count \
is still being read.\n\nCopy the whole table anyway?"
.to_string(),
None => "The size of the table's binary columns is not known.\n\n\
Copy the whole table anyway?"
.to_string(),
});
None
}
Err(message) => {
self.error_modal.show(message);
None
}
}
}
/// The view on screen as a Python Polars script: the open's reader, then every
/// step that made the view. See [`python_script`].
pub fn python_script(&self, state: &DataTableState) -> String {
let (paths, options) = match &self.opened {
Some((paths, options)) => (Some(paths.as_slice()), options.clone()),
None => (None, OpenOptions::default()),
};
let cloud = options.effective_cloud(&self.app_config.cloud);
let mut options = options;
if options.read_python.is_empty() {
// A decompressed file is read into its dataset directly, not through a scan.
options.read_python = state.read_python().to_vec();
}
// The source an `s3://<id>@bucket` URL names has its own endpoint and region.
let remote = paths
.and_then(|paths| paths.first())
.map(|p| p.to_string_lossy().into_owned())
.filter(|p| source::is_remote_url(Path::new(p)));
let source_of = remote
.as_deref()
.and_then(|url| source::split_source_id(url).0)
.and_then(|id| cloud.connections.iter().find(|c| c.name == id));
// What this session learned of the place: read unsigned, it is public.
#[cfg(feature = "cloud")]
let unsigned = remote
.as_deref()
.and_then(crate::cloud_sources::known_access)
.unwrap_or(false);
#[cfg(not(feature = "cloud"))]
let unsigned = false;
let record = python_script::OpenRecord {
paths,
options: &options,
format: state.read_as().or(options.format),
read_mode: state.read_mode(),
schema: state.source_schema(),
remote_objects: state
.remote_objects()
.unwrap_or_default()
.into_iter()
.map(|object| object.url)
.collect(),
s3_endpoint: source_of
.and_then(|c| c.endpoint_url.clone())
.or(cloud.s3_endpoint_url.clone())
.filter(|s| !s.trim().is_empty()),
s3_region: source_of
.and_then(|c| c.region.clone())
.or(cloud.s3_region.clone())
.filter(|s| !s.trim().is_empty()),
unsigned,
read_as_text: state.read_as_text().iter().map(|c| c.to_string()).collect(),
spec: state.format_read().map(|read| read.spec.name.clone()),
};
python_script::Script {
source: python_script::source(&record),
steps: state.python_steps(),
}
.render()
}
/// Hand a payload to the clipboard destination, building the destination
/// at the first copy, and flash or raise the error modal — a copy that
/// silently did nothing would be worse than one that failed out loud.
fn finish_copy(&mut self, payload: clipboard::Payload, message: String) {
let written = self
.copy_destination()
.and_then(|destination| destination.write(payload));
match written {
Ok(()) => self.flash_note(message),
Err(e) => self.error_modal.show(e),
}
}
/// The clipboard destination, built at the first copy.
fn copy_destination(&mut self) -> Result<&mut dyn clipboard::Destination, String> {
if self.clipboard.is_none() {
let choice = clipboard::BackendChoice::parse(&self.app_config.clipboard.backend)
.unwrap_or_default();
let limit = usize::try_from(self.app_config.clipboard.osc52_limit.bytes())
.unwrap_or(usize::MAX);
self.clipboard = Some(clipboard::destination(choice, limit)?);
}
Ok(self
.clipboard
.as_deref_mut()
.expect("destination just built"))
}
/// Whether Enter at the table opens the inspector, as Space does: there is a table,
/// and it is not one whose rows drill into groups (a `by` view, a SQL GROUP BY).
/// Inside a drill-down there is nothing further to drill into either.
pub fn enter_inspects(&self) -> bool {
self.input_mode == InputMode::Normal
&& self
.data_table_state
.as_ref()
.is_some_and(|state| !state.can_drill_down())
}
/// Space at the table, and Enter where there is nothing to drill into: the
/// inspector over the selected row.
fn open_inspector(&mut self) {
let Some(state) = self.data_table_state.as_ref() else {
return;
};
if state.inspect_row().is_none() {
self.flash_note("No row to inspect".to_string());
return;
}
self.inspector_modal
.open(state.inspect_fields(), state.current_column());
self.input_mode = InputMode::Inspect;
}
/// `F` at the table: Value Counts for the column cursor's column.
fn open_value_counts(&mut self) {
let Some(state) = self.data_table_state.as_ref() else {
return;
};
let names = state.get_column_order().to_vec();
let Some(at) = state
.current_column()
.and_then(|current| names.iter().position(|n| n == current))
else {
return;
};
self.value_counts.open(names, at, state.len_generation());
self.input_mode = InputMode::ValueCounts;
self.count_values(false);
}
/// Whether the Value Counts screen is up: on its own, or under the export
/// dialog writing its counts.
pub(crate) fn value_counts_shown(&self) -> bool {
self.input_mode == InputMode::ValueCounts
|| (self.input_mode == InputMode::Export && self.export_counts.is_some())
}
/// Whether a count for the Value Counts screen is being read while it is up.
pub(crate) fn value_counts_computing(&self) -> bool {
self.input_mode == InputMode::ValueCounts && self.value_counts.computing.is_some()
}
/// Where the export dialog goes back to: Value Counts when it is writing them.
fn export_returns_to(&self) -> InputMode {
if self.export_counts.is_some() {
InputMode::ValueCounts
} else {
InputMode::Normal
}
}
/// Count the column on the Value Counts screen, unless its counts are already
/// held: quickly, or every row when `exact`. Off the UI thread, without holding
/// the keys, so stepping to another column or Esc stops it.
fn count_values(&mut self, exact: bool) {
let Some(column) = self.value_counts.column().map(str::to_string) else {
return;
};
let held_sample = self.value_counts.current().map(|c| c.is_sample());
if held_sample == Some(false) || (held_sample == Some(true) && !exact) {
return;
}
if self
.value_counts
.computing
.as_ref()
.is_some_and(|c| c.column == column && (c.exact || !exact))
{
return;
}
self.stop_value_count();
let Some(state) = self.data_table_state.as_ref() else {
return;
};
let read = if exact {
value_counts::Read::Exact
} else {
value_counts::Read::Quick {
sample_rows: self.app_config.analysis.sample_rows,
seed: self.analysis_modal.sample.seed,
remote: state.is_remote_source(),
}
};
let plan = value_counts::Plan {
lf: state.analysis_lf(),
column: column.clone(),
read,
known_total: state.num_rows_if_valid(),
streaming: state.polars_streaming(),
};
let watch = crate::sampling::ReadWatch::default();
self.value_counts.failed = None;
self.value_counts.computing = Some(value_counts_modal::Computing {
column,
exact,
watch: watch.clone(),
file_starts: state.file_row_starts().map(Arc::new),
});
self.spawn_job(Job::ValueCounts, None, move |_| {
plan.run(&watch)
.map(|counts| Answer::ValueCounts(Box::new(counts)))
.map_err(|e| crate::error_display::user_message_from_report(&e, None))
});
}
/// Stop the count in flight, if one is: its read stops at its next batch and its
/// answer is dropped.
fn stop_value_count(&mut self) {
if let Some(computing) = self.value_counts.computing.take() {
computing.watch.stop();
self.jobs.cancel(|job| matches!(job, Job::ValueCounts));
}
}
/// Keys on the Value Counts screen.
fn value_counts_key(&mut self, event: &KeyEvent) -> Option<AppEvent> {
if !event.is_press() {
return None;
}
let page = self.value_counts_page() as isize;
// The histogram has no lines to move through or drill into.
let listing = !self.value_counts.shows_histogram();
match event.code {
// A count still reading stops; with nothing to show for the column, Esc
// goes on back to the table.
KeyCode::Esc => {
let counting = self.value_counts.counting();
self.stop_value_count();
if counting && self.value_counts.current().is_some() {
self.flash_note("Count stopped".to_string());
} else {
self.input_mode = InputMode::Normal;
}
}
KeyCode::Down | KeyCode::Char('j') if listing => self.value_counts.move_by(1),
KeyCode::Up | KeyCode::Char('k') if listing => self.value_counts.move_by(-1),
KeyCode::PageDown if listing => self.value_counts.move_by(page),
KeyCode::PageUp if listing => self.value_counts.move_by(-page),
KeyCode::Home if listing => self.value_counts.move_to_start(),
KeyCode::End | KeyCode::Char('G') if listing => self.value_counts.move_to_end(),
KeyCode::Left | KeyCode::Right | KeyCode::Char('h') | KeyCode::Char('l') => {
let by = if matches!(event.code, KeyCode::Left | KeyCode::Char('h')) {
-1
} else {
1
};
if self.value_counts.step(by) {
// The table follows, so Esc lands on the column last counted.
if let (Some(state), Some(column)) =
(self.data_table_state.as_mut(), self.value_counts.column())
{
state.set_current_column(column);
}
self.count_values(false);
}
}
KeyCode::Char('s') if listing => self.value_counts.toggle_order(),
KeyCode::Char('c') => self.value_counts.toggle_view(),
KeyCode::Char('a') => {
if self.value_counts.current().is_some_and(|c| c.is_sample()) {
self.count_values(true);
}
}
KeyCode::Enter if listing => self.drill_into_counted_value(),
KeyCode::Char('y') => self.copy_value_counts(),
KeyCode::Char('e') => self.export_value_counts(),
// The counts keep the rows they were read of; `t` counts the new ones too.
KeyCode::Char('t') if self.follow_rows_waiting() => {
self.stop_value_count();
self.take_follow_rows(false);
if let Some(state) = self.data_table_state.as_ref() {
let names = self.value_counts.columns.clone();
let at = self.value_counts.at;
self.value_counts.open(names, at, state.len_generation());
}
self.count_values(false);
}
_ => {}
}
None
}
/// Lines a page of the Value Counts listing moves: as many as the last frame drew.
fn value_counts_page(&self) -> usize {
self.value_counts.page.max(1)
}
/// Enter on Value Counts: the rows holding the value under the cursor, as a drill.
fn drill_into_counted_value(&mut self) {
let Some(kind) = self.value_counts.selected_kind() else {
return;
};
let (Some(counts), Some(column)) = (
self.value_counts.current().cloned(),
self.value_counts.column().map(str::to_string),
) else {
return;
};
let value = match kind {
value_counts::LineKind::Value(at) => match counts.value(at) {
Ok(value) => value,
Err(_) => return,
},
value_counts::LineKind::Null => polars::prelude::AnyValue::Null,
value_counts::LineKind::Other(_) => {
self.flash_note("Other is many values: pick one to see its rows".to_string());
return;
}
};
let Some(state) = self.data_table_state.as_mut() else {
return;
};
let nested = state.is_drilled_down();
match state.deferred(|s| s.drill_into_value(&column, value)) {
Ok(()) => {
self.stop_value_count();
// Inside a group already, Esc goes back past this view to the one
// the group came from, so there are no counts to come back to.
self.value_counts.drill_return = !nested;
self.input_mode = InputMode::Normal;
self.sync_sort_filter_modal();
self.spawn_async_collect(Self::LOADING_BUFFER);
}
Err(e) => self.flash_note(format!(
"Could not drill in: {}",
crate::error_display::user_message_from_report(&e, None)
)),
}
}
/// `y` on Value Counts: every value with its count and percentages, as TSV.
fn copy_value_counts(&mut self) {
let Some(counts) = self.value_counts.current().cloned() else {
return;
};
let order = self.value_counts.order;
let html = match self.copy_destination() {
Ok(destination) => destination.accepts().html,
Err(e) => {
self.error_modal.show(e);
return;
}
};
self.spawn_job(Job::Copy, Some("Copying..."), move |_| {
let table = counts
.table(order)
.map_err(|e| format!("Copy failed: {e}"))?;
let payload =
crate::clipboard::tabular_payload(&table, clipboard::CopyFormat::Tsv, true, html)
.map_err(|e| format!("Copy failed: {e}"))?;
Ok(Answer::Copied {
payload,
message: format!(
"Copied {} values as TSV",
copy_modal::thousands(table.height())
),
})
});
}
/// `e` on Value Counts: the export dialog, writing the counts.
fn export_value_counts(&mut self) {
let Some(counts) = self.value_counts.current() else {
return;
};
let table = match counts.table(self.value_counts.order) {
Ok(table) => table,
Err(e) => {
self.error_modal
.show(format!("Cannot export the counts: {e}"));
return;
}
};
self.export_modal.open(
self.original_file_format,
self.history_limit,
&self.theme,
self.original_file_delimiter,
);
let stem = self.dataset_stem();
self.export_modal.suggest_path(&format!("{stem}-counts"));
self.export_modal.offer_source_file = false;
self.export_modal.nested_columns = false;
self.export_modal.avro_renames = table
.columns()
.iter()
.any(|c| crate::avro_types::renames(c.name(), c.dtype()));
self.export_counts = Some(table);
self.input_mode = InputMode::Export;
}
/// `g` at the table: pick a shown column by name, bring it on screen and put the
/// column cursor on it. Starts on the cursor's column, so ↑↓ move from there.
fn open_go_to_column(&mut self) {
let Some(state) = self.data_table_state.as_ref() else {
return;
};
let names = state.get_column_order().to_vec();
if names.is_empty() {
return;
}
let at = state
.current_column()
.and_then(|current| names.iter().position(|n| n == current))
.unwrap_or(0);
self.go_to_column = crate::widgets::ui::PickerState::new(names);
self.go_to_column.select_original(at);
self.input_mode = InputMode::GoToColumn;
}
/// The column picker owns the keys: type to narrow, ↑↓ move, Enter goes, Esc
/// closes without moving.
fn go_to_column_key(&mut self, event: &KeyEvent) {
match event.code {
KeyCode::Esc => self.input_mode = InputMode::Normal,
KeyCode::Enter => {
let Some(index) = self.go_to_column.selected_original() else {
// Nothing matches; the picker says so and stays.
return;
};
// By name: the order may have changed under the picker since it opened.
let name = self.go_to_column.items()[index].clone();
if let Some(state) = self.data_table_state.as_mut() {
state.go_to_column(&name);
}
self.input_mode = InputMode::Normal;
}
KeyCode::Up => self.go_to_column.move_up(),
KeyCode::Down => self.go_to_column.move_down(),
KeyCode::Backspace => self.go_to_column.backspace(),
KeyCode::Char(c) => self.go_to_column.filter_key(c, event.modifiers),
_ => {}
}
}
/// `b` at a table read through a format spec: the specs that could read it, the
/// ones that matched first, to read it again with another.
fn open_format_picker(&mut self) {
let Some(read) = self
.data_table_state
.as_ref()
.and_then(|s| s.format_read())
.cloned()
else {
// Only a file read through a spec has a format to pick.
return;
};
let mut names = vec![read.spec.name.clone()];
names.extend(read.also.iter().cloned());
for found in &self.formats.specs {
if found.spec.layout == read.spec.layout
&& !found.spec.is_delimited()
&& !names.contains(&found.spec.name)
{
names.push(found.spec.name.clone());
}
}
self.format_picker = crate::widgets::ui::PickerState::new(names);
self.input_mode = InputMode::PickFormat;
}
/// The format picker owns the keys: type to narrow, ↑↓ move, Enter reads the file
/// again with the spec chosen, Esc closes.
fn format_picker_key(&mut self, event: &KeyEvent) -> Option<AppEvent> {
match event.code {
KeyCode::Esc => self.input_mode = InputMode::Normal,
KeyCode::Enter => {
let index = self.format_picker.selected_original()?;
let name = self.format_picker.items()[index].clone();
self.input_mode = InputMode::Normal;
let current = self
.data_table_state
.as_ref()
.and_then(|s| s.format_read())
.map(|read| read.spec.name.clone());
if current.as_deref() == Some(name.as_str()) {
return None;
}
let (paths, options) = self.opened.clone()?;
let options = OpenOptions {
spec_name: Some(name),
spec_file: None,
spec_fetched: None,
table: None,
format_read: None,
sqlite: None,
format: None,
..options
};
self.set_loading_phase("Scanning input", 10);
self.name_what_is_loading(paths[0].clone());
return Some(AppEvent::Open(paths, options));
}
KeyCode::Up => self.format_picker.move_up(),
KeyCode::Down => self.format_picker.move_down(),
KeyCode::Backspace => self.format_picker.backspace(),
KeyCode::Char(c) => self.format_picker.filter_key(c, event.modifiers),
_ => {}
}
None
}
/// The tables of the source on screen, from what its open holds; `None` for a
/// source of one.
pub fn sibling_tables(&self) -> Option<table_switch::Tables> {
let state = self.data_table_state.as_ref()?;
let (paths, options) = self.opened.as_ref()?;
table_switch::of(state, paths, options)
}
/// Whether the source on screen has another table for `T` to open.
pub fn offers_other_tables(&self) -> bool {
match (self.data_table_state.as_ref(), self.opened.as_ref()) {
(Some(state), Some((paths, options))) => table_switch::several(state, paths, options),
_ => false,
}
}
/// `T` at the table: the source's tables, the one on screen marked, to open
/// another. A source of one says so.
fn open_table_picker(&mut self) {
let Some(tables) = self.sibling_tables().filter(table_switch::Tables::several) else {
self.flash_note("Only one table here".to_string());
return;
};
let labels = tables.tables.iter().map(|t| t.label.clone()).collect();
self.table_picker = crate::widgets::ui::PickerState::new(labels);
if let Some(at) = tables.current {
self.table_picker.select_original(at);
}
self.table_choices = Some(tables);
self.input_mode = InputMode::PickTable;
}
/// The table picker owns the keys: type to narrow, ↑↓ move, Enter opens the table
/// chosen, Esc closes.
fn table_picker_key(&mut self, event: &KeyEvent) -> Option<AppEvent> {
match event.code {
KeyCode::Esc => {
self.input_mode = InputMode::Normal;
self.table_choices = None;
}
KeyCode::Enter => {
let index = self.table_picker.selected_original()?;
let tables = self.table_choices.take()?;
self.input_mode = InputMode::Normal;
if tables.current == Some(index) {
return None;
}
let table = tables.tables.get(index)?.table.clone();
return self.switch_table(table);
}
KeyCode::Up => self.table_picker.move_up(),
KeyCode::Down => self.table_picker.move_down(),
KeyCode::Backspace => self.table_picker.backspace(),
KeyCode::Char(c) => self.table_picker.filter_key(c, event.modifiers),
_ => {}
}
None
}
/// Open `table` of the file on screen in its place (`None`: the whole file), as
/// `--table` or home's row for it would: the query, filters and sort go with the
/// table they were on, recents record it, and a view for it applies.
pub(crate) fn switch_table(&mut self, table: Option<String>) -> Option<AppEvent> {
let (paths, options) = self.opened.clone()?;
let shown = match &table {
Some(name) => crate::members::place(&paths[0], name),
None => paths[0].clone(),
};
let options = OpenOptions {
table,
// `--view` was for the first open; a view for this table applies as on
// any open.
view: None,
prepared: None,
..options
};
self.set_loading_phase("Scanning input", 10);
self.name_what_is_loading(shown);
Some(AppEvent::Open(paths, options))
}
fn close_inspector(&mut self) {
self.inspector_modal.close();
self.input_mode = InputMode::Normal;
}
/// Enter on a row of a grouped view: its group's rows, read off this thread
/// when the buffer does not hold the row.
fn drill_selected_row(&mut self) {
let Some(state) = self.data_table_state.as_ref() else {
return;
};
// An empty result has no row selected, and says so like any other.
let drill = state
.table_state
.selected()
.map(|selected| state.start_row() + selected)
.and_then(|index| Some((index, state.drill_row(index)?)));
match drill {
None => self.flash_note("No group to drill down into".to_string()),
Some((group_index, DrillRow::Buffered(row))) => self.drill_into(group_index, &row),
Some((group_index, DrillRow::Read(lf))) => {
let streaming = state.polars_streaming();
self.spawn_job(Job::DrillRow, Some(Self::READING_GROUP), move |_| {
let row = crate::statistics::collect_lazy(*lf, streaming)
.map_err(|e| crate::error_display::user_message_from_polars(&e))?;
Ok(Answer::DrillRow { group_index, row })
});
}
}
}
/// The inspector's list as the row shown has it: Filled, Compare and the find
/// text depend on the row's values, which a key may have moved.
fn refresh_inspector_list(&mut self) {
if let Some(state) = self.data_table_state.as_ref() {
let visible = crate::widgets::inspector::visible_fields(&self.inspector_modal, state);
self.inspector_modal.set_visible(visible);
}
}
/// The pane for the focused value: as last drawn while that is still the
/// focused value, else built for the key (without the table's preview, which
/// only a frame knows).
fn inspector_pane(&self) -> Option<crate::widgets::inspector::Pane> {
let modal = &self.inspector_modal;
if modal.drill.is_some() {
return modal.pane.as_ref().map(|(_, pane)| pane.clone());
}
let state = self.data_table_state.as_ref()?;
let row = state.inspect_row()?;
let field = modal.focused()?;
if let Some(pane) = modal.pane_for(row.frame, row.row, &field.name) {
return Some(pane.clone());
}
let shown = crate::widgets::inspector::shown(field, &row, modal.read.as_ref(), state);
Some(crate::widgets::inspector::pane(
&field.dtype,
&shown,
&crate::widgets::inspector::PaneAsk {
choice: modal.view,
width: modal
.pane
.as_ref()
.map_or(80, |(key, _)| key.width as usize),
table: None,
indented: crate::widgets::inspector::Indented::None,
not_json: modal.known_not_json(row.frame, row.row, &field.name),
unpacked: modal.unpacked(&(row.frame, row.row, field.name.clone())),
read_key: "Enter",
},
))
}
/// The inspector's keys. Moving between rows moves the table's cursor, so the
/// table is where the inspector left it on close.
fn inspector_key(&mut self, event: &KeyEvent) -> Option<AppEvent> {
if !event.is_press() {
return None;
}
let modal = &mut self.inspector_modal;
if modal.finding {
match event.code {
KeyCode::Esc => modal.clear_find(),
KeyCode::Enter | KeyCode::Tab | KeyCode::Down => modal.finding = false,
KeyCode::Up => {
modal.finding = false;
self.refresh_inspector_list();
self.inspector_modal.prev_field();
return None;
}
KeyCode::Backspace => modal.find_backspace(),
KeyCode::Char(c) => modal.find_key(c, event.modifiers),
_ => {}
}
// The focus follows the narrowing now, not at the next frame: a key
// replayed before it acts on the field the find left focused.
self.refresh_inspector_list();
return None;
}
if modal.value_find.as_ref().is_some_and(|f| f.editing) {
self.value_find_key(event);
return None;
}
if event
.modifiers
.intersects(KeyModifiers::CONTROL | KeyModifiers::ALT)
{
return None;
}
if modal.focus == inspector_modal::Focus::Value {
return self.inspector_value_key(event);
}
if modal.drill.is_some() {
return self.drill_key(event);
}
self.refresh_inspector_list();
let modal = &mut self.inspector_modal;
match event.code {
// Esc backs out one level at a time: the find, then Compare, then the
// inspector.
KeyCode::Esc if !modal.filter.is_empty() => modal.clear_find(),
KeyCode::Esc if modal.compare => {
modal.compare = false;
modal.filled_only = false;
}
KeyCode::Esc | KeyCode::Char(' ') => self.close_inspector(),
KeyCode::Down | KeyCode::Char('j') => modal.next_field(),
KeyCode::Up | KeyCode::Char('k') => modal.prev_field(),
KeyCode::Home => modal.first_field(),
KeyCode::End => modal.last_field(),
KeyCode::PageDown => modal.page_fields(1),
KeyCode::PageUp => modal.page_fields(-1),
// Two panes: Tab and Shift+Tab both cross to the value.
KeyCode::Tab | KeyCode::BackTab => {
if modal.focused().is_some() {
modal.focus = inspector_modal::Focus::Value;
}
}
KeyCode::Char('/') => modal.finding = true,
KeyCode::Char('e') => self.inspector_view(),
KeyCode::Char('w') => self.inspector_wrap(),
KeyCode::Char('f') => {
modal.filled_only = !modal.filled_only;
modal.list_offset = 0;
}
KeyCode::Char('s') => {
modal.order = modal.order.next();
modal.list_offset = 0;
}
KeyCode::Char('c') => {
modal.compare = !modal.compare;
if !modal.compare {
modal.filled_only = false;
}
}
KeyCode::Char('m') => self.toggle_inspector_pin(),
KeyCode::Right | KeyCode::Char('l') => return self.step_row(1),
KeyCode::Left | KeyCode::Char('h') => return self.step_row(-1),
KeyCode::Char('y') => self.copy_inspected_field(),
KeyCode::Char('Y') => self.copy_inspected_row(),
KeyCode::Char('o') => self.open_inspected_value(),
KeyCode::Char('r') => self.read_focused_field(),
KeyCode::Enter => return self.inspector_enter(),
_ => {}
}
None
}
/// The keys with the focus in the value: scroll it, search it, change its view.
fn inspector_value_key(&mut self, event: &KeyEvent) -> Option<AppEvent> {
let Some(pane) = self.inspector_pane() else {
self.inspector_modal.focus = inspector_modal::Focus::List;
return None;
};
let modal = &mut self.inspector_modal;
let h = modal.page.max(1);
let content = &pane.content;
let page = h.saturating_sub(1).max(1) as isize;
match event.code {
KeyCode::Esc
if modal
.value_find
.as_ref()
.is_some_and(|f| !f.text.is_empty()) =>
{
modal.value_find = None;
}
KeyCode::Esc | KeyCode::Tab | KeyCode::BackTab => {
modal.focus = inspector_modal::Focus::List;
}
KeyCode::Char(' ') => self.close_inspector(),
KeyCode::Down | KeyCode::Char('j') => modal.reader.scroll(content, h, 1),
KeyCode::Up | KeyCode::Char('k') => modal.reader.scroll(content, h, -1),
KeyCode::PageDown => modal.reader.scroll(content, h, page),
KeyCode::PageUp => modal.reader.scroll(content, h, -page),
KeyCode::Home => modal.reader.home(),
KeyCode::End => modal.reader.end(content, h),
KeyCode::Char('/') => {
modal.value_find = Some(inspector_modal::ValueFind {
editing: true,
pane: pane.id,
..Default::default()
});
}
KeyCode::Char('n') => self.next_value_hit(1),
KeyCode::Char('N') => self.next_value_hit(-1),
KeyCode::Char('e') => self.inspector_view(),
KeyCode::Char('w') => self.inspector_wrap(),
KeyCode::Char('y') => {
if modal.drill.is_some() {
self.copy_drilled_item();
} else {
self.copy_inspected_field();
}
}
KeyCode::Char('o') if modal.drill.is_none() => self.open_inspected_value(),
KeyCode::Right | KeyCode::Char('l') if modal.drill.is_none() => {
return self.step_row(1);
}
KeyCode::Left | KeyCode::Char('h') if modal.drill.is_none() => {
return self.step_row(-1);
}
_ => {}
}
None
}
/// A key typed into the value's find line. Enter finds every place and goes
/// to the first at or after the pane's top.
fn value_find_key(&mut self, event: &KeyEvent) {
let pane = self.inspector_pane();
let modal = &mut self.inspector_modal;
let Some(find) = modal.value_find.as_mut() else {
return;
};
match event.code {
KeyCode::Esc => modal.value_find = None,
KeyCode::Enter => {
find.editing = false;
let Some(pane) = pane else {
return;
};
find.hits = inspector_reader::find_hits(&pane.content, &find.text);
find.pane = pane.id;
let h = modal.page.max(1);
let from = modal.reader.window(&pane.content, h).from;
let at = find.hits.partition_point(|&p| p < from);
find.current = (!find.hits.is_empty()).then(|| at % find.hits.len());
if let Some(at) = find.current {
let pos = find.hits[at];
modal.reader.jump(&pane.content, h, pos);
}
}
KeyCode::Backspace => {
find.text.pop();
}
KeyCode::Char(c) => inspector_modal::edit_find(&mut find.text, c, event.modifiers),
_ => {}
}
}
/// `n` and `N` in the value: the next or the last place found, round the ends.
fn next_value_hit(&mut self, step: isize) {
let Some(pane) = self.inspector_pane() else {
return;
};
let modal = &mut self.inspector_modal;
let Some(find) = modal.value_find.as_mut() else {
return;
};
if find.pane != pane.id && !find.text.is_empty() {
find.hits = inspector_reader::find_hits(&pane.content, &find.text);
find.pane = pane.id;
find.current = None;
}
let n = find.hits.len();
if n == 0 {
return;
}
let at = match find.current {
Some(at) => (at as isize + step).rem_euclid(n as isize) as usize,
None if step > 0 => 0,
None => n - 1,
};
find.current = Some(at);
let pos = find.hits[at];
let h = modal.page.max(1);
modal.reader.jump(&pane.content, h, pos);
}
/// The inspector's keys inside a level drilled into: the same moves as at the
/// row, but `→` and Enter open the focused item and `←` and Esc step back up.
fn drill_key(&mut self, event: &KeyEvent) -> Option<AppEvent> {
let modal = &mut self.inspector_modal;
match event.code {
KeyCode::Esc | KeyCode::Left | KeyCode::Char('h') => {
modal.drill_out();
}
KeyCode::Char(' ') => self.close_inspector(),
KeyCode::Down | KeyCode::Char('j') => modal.next_field(),
KeyCode::Up | KeyCode::Char('k') => modal.prev_field(),
KeyCode::Home => modal.first_field(),
KeyCode::End => modal.last_field(),
KeyCode::PageDown => modal.page_fields(1),
KeyCode::PageUp => modal.page_fields(-1),
// A level with nothing in it has no value to cross to.
KeyCode::Tab | KeyCode::BackTab
if modal
.drill
.as_ref()
.is_some_and(|drill| drill.level().focused().is_some()) =>
{
modal.focus = inspector_modal::Focus::Value;
}
KeyCode::Char('e') => self.inspector_view(),
KeyCode::Char('w') => self.inspector_wrap(),
KeyCode::Char('y') => self.copy_drilled_item(),
KeyCode::Enter | KeyCode::Right | KeyCode::Char('l') => {
let drill = modal.drill.as_ref()?;
let (frame, row) = (drill.frame, drill.row);
let (label, node) = drill.level().focused()?;
let path = drill.item_key(&label);
if node.opens() && !modal.known_not_json(frame, row, &path) {
self.inspector_open(frame, row, label, path, node);
}
}
_ => {}
}
None
}
/// `e` in the inspector: the focused value's next view, where it has more than
/// one. A number never has, so it never changes how a text field is then shown.
fn inspector_view(&mut self) {
if let Some(view) = self.inspector_pane().and_then(|pane| pane.next_view()) {
self.inspector_modal.choose_view(view);
}
}
/// `w`: word wrap or hard wrap, for every value until it is pressed again.
fn inspector_wrap(&mut self) {
let modal = &mut self.inspector_modal;
modal.wrap = match modal.wrap {
inspector_reader::Wrap::Word => inspector_reader::Wrap::Hard,
inspector_reader::Wrap::Hard => inspector_reader::Wrap::Word,
};
}
/// `m`: pin this row for Compare, or let the pin go when it is this row.
fn toggle_inspector_pin(&mut self) {
let Some(row) = self.data_table_state.as_ref().and_then(|s| s.inspect_row()) else {
return;
};
let modal = &mut self.inspector_modal;
let here = modal
.pinned
.as_ref()
.is_some_and(|p| (p.frame, p.row) == (row.frame, row.row));
if here {
modal.pinned = None;
self.flash_note("Unpinned".to_string());
} else {
let n = row.display_row;
modal.pinned = Some(row);
modal.compare = true;
self.flash_note(format!(
"Pinned row {}; Compare shows it",
copy_modal::thousands(n)
));
}
}
/// Enter in the inspector: on a group's row, its rows, as at the table; else
/// open a nested value, or read the row's fields the buffer does not hold.
fn inspector_enter(&mut self) -> Option<AppEvent> {
let state = self.data_table_state.as_ref()?;
if state.can_drill_down() {
self.close_inspector();
self.drill_selected_row();
return None;
}
let row = state.inspect_row()?;
let field = self.inspector_modal.focused().cloned()?;
let shown = crate::widgets::inspector::shown(
&field,
&row,
self.inspector_modal.read.as_ref(),
state,
);
use crate::widgets::inspector::Shown;
match shown {
// A failed read is asked again: the pane said why, and Enter is the retry.
Shown::Unread | Shown::Failed(_) => self.read_focused_field(),
Shown::Value(ref v)
if crate::widgets::inspector::value_opens(v)
&& !self
.inspector_modal
.known_not_json(row.frame, row.row, &field.name) =>
{
let column = if field.buffered() {
row.values.column(&field.name).ok()
} else {
self.inspector_modal
.read_values(row.frame, row.row)
.and_then(|values| values.column(&field.name).ok())
};
if let Some(column) = column {
let node =
inspector_drill::Node::Native(column.as_materialized_series().clone());
let path = inspector_drill::path_key([field.name.as_str()]);
self.inspector_open(row.frame, row.row, field.name.clone(), path, node);
}
}
_ => {}
}
None
}
/// Read the focused row's hidden and binary fields, waited on; from then on the
/// rows moved to are read too while the focus stays on this field.
fn read_focused_field(&mut self) {
let Some(state) = self.data_table_state.as_ref() else {
return;
};
let Some(row) = state.inspect_row() else {
return;
};
let Some(field) = self.inspector_modal.focused().cloned() else {
return;
};
let shown = crate::widgets::inspector::shown(
&field,
&row,
self.inspector_modal.read.as_ref(),
state,
);
if matches!(
shown,
crate::widgets::inspector::Shown::Unread | crate::widgets::inspector::Shown::Failed(_)
) {
self.inspector_modal.follow = Some(field.name.clone());
self.read_inspected_fields(&row, true);
}
}
/// What the inspector needs after a pass: the row moved to read while a read
/// follows the rows, long JSON indented for its JSON view, and compressed
/// bytes decompressed for their Text view. None holds the keys: moving on
/// drops what is no longer wanted.
fn inspector_needs(&mut self) {
if self.input_mode != InputMode::Inspect || !self.inspector_modal.active {
return;
}
let Some(state) = self.data_table_state.as_ref() else {
return;
};
let Some(row) = state.inspect_row() else {
return;
};
let modal = &self.inspector_modal;
if modal.drill.is_some() {
return;
}
let Some(field) = modal.focused().cloned() else {
return;
};
let read_here = modal
.read
.as_ref()
.is_some_and(|r| r.key() == (row.frame, row.row));
if modal.follow.as_deref() == Some(field.name.as_str()) && !field.buffered() && !read_here {
self.read_inspected_fields(&row, false);
return;
}
// Long JSON text asked for the JSON view and not yet indented, or
// compressed bytes asked for the Text view and not yet decompressed.
let pane = modal.pane_for(row.frame, row.row, &field.name);
let place = (row.frame, row.row, field.name.clone());
let indent = pane.is_some_and(|pane| pane.indent)
&& !modal.pretty.as_ref().is_some_and(|p| *p.place() == place);
let unpack = pane.is_some_and(|pane| pane.unpack)
&& !modal.unpack.as_ref().is_some_and(|u| *u.place() == place);
if !indent && !unpack {
return;
}
let column = if field.buffered() {
row.values.column(&field.name).ok().cloned()
} else {
modal
.read_values(row.frame, row.row)
.and_then(|values| values.column(&field.name).ok().cloned())
};
let Some(column) = column else {
return;
};
let modal = &mut self.inspector_modal;
if unpack {
modal.unpack_token += 1;
let token = modal.unpack_token;
modal.unpack = Some(inspector_modal::Unpack::Pending { token, place });
self.spawn_job(Job::InspectUnpack { token }, None, move |_| {
let value = column.get(0).map_err(|e| e.to_string())?;
let bytes = match &value {
polars::prelude::AnyValue::Binary(b) => *b,
polars::prelude::AnyValue::BinaryOwned(b) => b.as_slice(),
_ => return Err("not bytes".to_string()),
};
inspector_bytes::decode_text(bytes, inspector_bytes::sniff(bytes))
.map(Answer::Unpacked)
.ok_or_else(|| "not text".to_string())
});
return;
}
modal.pretty_token += 1;
let token = modal.pretty_token;
modal.pretty = Some(inspector_modal::Pretty::Pending { token, place });
self.spawn_job(Job::InspectPretty { token }, None, move |_| {
let value = column.get(0).map_err(|e| e.to_string())?;
let text = match &value {
polars::prelude::AnyValue::String(s) => *s,
polars::prelude::AnyValue::StringOwned(s) => s.as_str(),
_ => return Err("not text".to_string()),
};
let json = inspector_drill::parse_json(text)?;
let (pretty, _) = inspector_drill::json_text(&json, true, usize::MAX);
Ok(Answer::Indented(std::sync::Arc::from(pretty)))
});
}
/// Under `theme.mode = "auto"`, switch to the theme for the terminal's background
/// (`theme.dark` or `theme.light`), keeping the configured `theme.colors` over it as at startup. An
/// explicit mode ignores the terminal.
pub fn follow_terminal_background(&mut self, mode: ThemeMode) {
let theme = &self.app_config.theme;
if !theme.follow || theme.mode == Some(mode) {
return;
}
let mut next = theme.clone();
let built = theme.palette_for(mode).and_then(|colors| {
next.colors = colors;
next.mode = Some(mode);
Theme::from_config(&next)
});
match built {
Ok(built) => {
self.theme = built;
self.chart_modal.series_cap = Some(self.theme.series_colors().len());
self.app_config.theme = next;
// The prompts live as long as the app and keep the colors they were
// given; a dialog's fields take the theme each time it opens.
for input in [
&mut self.query_input,
&mut self.sql_input,
&mut self.find.input,
] {
*input = std::mem::take(input).with_theme(&self.theme);
}
}
// The configured colors parsed at startup, so this is not expected; the
// palette in use stays.
Err(e) => log::warn!("cannot switch to the {mode:?} palette: {e}"),
}
}
/// The terminal said what its background is: follow it under `auto`, and remember
/// it for the next start's first frame.
fn terminal_answered(&mut self, mode: ThemeMode) {
if self.app_config.theme.follow {
self.cache
.remember_terminal_mode(&terminal_color::terminal_key(), mode);
}
self.follow_terminal_background(mode);
}
/// Settle the palette of the first frame under `auto`, without waiting for the
/// terminal: its answer when `answered` has it, else what this terminal answered
/// last time. An answer that comes later switches palettes if it differs.
pub fn settle_first_palette(&mut self, answered: Option<ThemeMode>) {
if let Some(mode) = answered {
self.terminal_answered(mode);
} else if self.app_config.theme.follow
&& let Some(mode) = self.cache.terminal_mode(&terminal_color::terminal_key())
{
self.follow_terminal_background(mode);
}
}
/// Whether the run loop should ask the terminal for its background, once. Asked by
/// [`AppEvent::TerminalFocused`] under `auto`.
pub fn take_background_query(&mut self) -> bool {
std::mem::take(&mut self.background_query)
}
/// The colors the next frame is drawn with.
pub fn theme(&self) -> &Theme {
&self.theme
}
/// Whether the palette follows the terminal (`theme.mode = "auto"`).
pub fn follows_terminal(&self) -> bool {
self.app_config.theme.follow
}
/// The value the inspector wrote for another program, for the run loop.
pub fn take_external_open(&mut self) -> Option<external_open::ExternalOpen> {
self.external_open.take()
}
/// Whether the session reports the mouse, to take it again after a program
/// had the terminal.
pub fn mouse_enabled(&self) -> bool {
self.app_config.display.mouse
}
/// The run loop opened `open`: a program that waited is done with its file;
/// a failure is said on the bar.
pub fn external_opened(&mut self, open: &external_open::ExternalOpen, failed: Option<String>) {
let program = external_open::program_for(open.document, |name| std::env::var(name).ok());
if matches!(program, external_open::Program::Wait(_)) {
let _ = std::fs::remove_file(&open.path);
}
match failed {
Some(e) => self.flash_note(format!("Could not open the value: {e}")),
None if matches!(program, external_open::Program::Opener(_)) => {
self.flash_note("Opened in the system viewer".to_string())
}
None => {}
}
}
/// `y` in the inspector: the focused value as its view shows it — the stored
/// value exact, indented JSON in the JSON view, the text bytes hold in their
/// Text view, bytes otherwise as base64 — through the same clipboard path as
/// the copy dialog. One over a capped destination's limit is refused
/// unformatted; a large one is written off this thread.
fn copy_inspected_field(&mut self) {
use copy_modal::thousands;
let Some(state) = self.data_table_state.as_ref() else {
return;
};
let (Some(row), Some(field)) = (state.inspect_row(), self.inspector_modal.focused()) else {
return;
};
let field = field.clone();
let column = if field.buffered() {
row.values.column(&field.name).ok().cloned()
} else {
self.inspector_modal
.read_values(row.frame, row.row)
.and_then(|values| values.column(&field.name).ok().cloned())
};
let Some(column) = column else {
self.flash_note(format!("{} is not read yet; Enter reads it", field.name));
return;
};
let message = format!(
"Copied {} of row {}",
field.name,
thousands(row.display_row)
);
use crate::widgets::inspector::CopyAs;
match self.inspector_pane().map(|pane| pane.copy) {
Some(CopyAs::Text(text)) => self.copy_string(text.to_string(), message),
Some(CopyAs::Escaped) => {
let text = column
.get(0)
.map(|v| crate::exact::escaped(&crate::exact::value_text(&v)))
.unwrap_or_default();
self.copy_string(text, message);
}
_ => self.copy_value(column, message),
}
}
/// Copy `text`, refused when it is over a capped destination's limit.
fn copy_string(&mut self, text: String, message: String) {
let limit = match self.copy_destination() {
Ok(destination) => destination.accepts().base64_limit,
Err(e) => {
self.error_modal.show(e);
return;
}
};
if let Some(limit) = limit
&& text.len() > limit / 4 * 3
{
self.error_modal
.show(clipboard::over_osc52_limit(None, limit));
return;
}
self.finish_copy(clipboard::Payload::text(text), message);
}
/// `Y` in the inspector: the whole row as one JSON object, exact, without
/// leaving. Fields not read are left out, and the flash counts them.
fn copy_inspected_row(&mut self) {
use copy_modal::thousands;
let Some(state) = self.data_table_state.as_ref() else {
return;
};
let Some(row) = state.inspect_row() else {
return;
};
let fields = self.inspector_modal.fields.clone();
let read = self.inspector_modal.read.clone();
let display = row.display_row;
let size = row.values.estimated_size()
+ self
.inspector_modal
.read_values(row.frame, row.row)
.map_or(0, |v| v.estimated_size());
let build = move || {
let (json, kept, unread) =
crate::widgets::inspector::row_json(&fields, &row, read.as_ref());
let message = if unread > 0 {
format!(
"Copied row {}: {} fields, {} not read",
thousands(display),
thousands(kept),
thousands(unread)
)
} else {
format!("Copied row {} as JSON", thousands(display))
};
(json, message)
};
if size <= Self::FIELD_COPY_INLINE_BYTES {
let (json, message) = build();
self.copy_string(json, message);
return;
}
let limit = match self.copy_destination() {
Ok(destination) => destination.accepts().base64_limit,
Err(e) => {
self.error_modal.show(e);
return;
}
};
self.spawn_job(Job::Copy, Some("Copying..."), move |_| {
let (json, message) = build();
if let Some(limit) = limit
&& json.len() > limit / 4 * 3
{
return Err(clipboard::over_osc52_limit(None, limit));
}
Ok(Answer::Copied {
payload: clipboard::Payload::text(json),
message,
})
});
}
/// `o` in the inspector: the value written to a file of its own, in the view
/// it is shown in, for another program to open; see [`external_open`].
fn open_inspected_value(&mut self) {
let Some(state) = self.data_table_state.as_ref() else {
return;
};
let Some(row) = state.inspect_row() else {
return;
};
let Some(field) = self.inspector_modal.focused().cloned() else {
return;
};
let column = if field.buffered() {
row.values.column(&field.name).ok().cloned()
} else {
self.inspector_modal
.read_values(row.frame, row.row)
.and_then(|values| values.column(&field.name).ok().cloned())
};
let Some(column) = column else {
self.flash_note(format!("{} is not read yet; Enter reads it", field.name));
return;
};
if self.open_dir.is_none() {
match tempfile::Builder::new().prefix("datui-values-").tempdir() {
Ok(dir) => self.open_dir = Some(dir),
Err(e) => {
self.flash_note(format!("Could not open the value: {e}"));
return;
}
}
}
let dir = self
.open_dir
.as_ref()
.map(|d| d.path().to_path_buf())
.unwrap_or_default();
let shown_as = self.inspector_pane().map(|pane| pane.copy);
let name = field.name.clone();
let display = row.display_row;
self.spawn_job(Job::OpenValue, Some("Writing the value..."), move |_| {
use crate::widgets::inspector::CopyAs;
let value = column.get(0).map_err(|e| e.to_string())?;
let (bytes, extension, document): (Vec<u8>, &str, bool) = match (&shown_as, &value) {
(Some(CopyAs::Text(text)), _) => {
let ext = if inspector_drill::looks_like_json(text) {
"json"
} else {
"txt"
};
(text.as_bytes().to_vec(), ext, false)
}
(_, polars::prelude::AnyValue::Binary(b)) => {
let kind = inspector_bytes::sniff(b);
(
b.to_vec(),
kind.map_or("bin", |k| k.extension()),
kind.is_some_and(|k| k.is_document()),
)
}
(_, polars::prelude::AnyValue::BinaryOwned(b)) => {
let kind = inspector_bytes::sniff(b);
(
b.clone(),
kind.map_or("bin", |k| k.extension()),
kind.is_some_and(|k| k.is_document()),
)
}
(_, v) if crate::exact::is_nested_value(v) => {
let text = crate::exact::copy_text(&column).map_err(|e| e.to_string())?;
(text.into_bytes(), "json", false)
}
(_, v) => {
let text = crate::exact::value_text(v);
let trimmed = text.trim_start();
let ext = if inspector_drill::looks_like_json(&text) {
"json"
} else if trimmed.starts_with('<') {
"xml"
} else {
"txt"
};
(text.into_bytes(), ext, false)
}
};
let file = external_open::file_name(&name, display, extension);
let path =
external_open::write_value(&dir, &file, &bytes).map_err(|e| e.to_string())?;
Ok(Answer::ValueWritten(external_open::ExternalOpen {
path,
document,
}))
});
}
/// Move the table's cursor `delta` rows, reading the next page in the
/// background when the buffer does not hold the row: the table's own ↑↓.
fn step_row(&mut self, delta: i64) -> Option<AppEvent> {
let state = self.data_table_state.as_mut()?;
if state.scroll_would_trigger_collect(delta) {
self.busy = true;
return Some(if delta > 0 {
AppEvent::DoScrollNext
} else {
AppEvent::DoScrollPrev
});
}
if delta > 0 {
state.select_next();
} else {
state.select_previous();
}
None
}
/// The inspector's keys. Moving between rows moves the table's cursor, so the
/// table is where the inspector left it on close.
/// Open `node` as a level under the one shown: a list or struct at once, text as
/// the JSON it holds, parsed here when short and on a worker when long. `path`
/// is the text's place, remembered when it does not parse.
fn inspector_open(
&mut self,
frame: u64,
row: usize,
label: String,
path: String,
node: inspector_drill::Node,
) {
use inspector_drill::{JSON_INLINE_BYTES, Node, Shape};
if node.shape() != Shape::Leaf {
self.inspector_modal.drill_in(frame, row, label, node);
return;
}
let Some(len) = node
.with_text(|s| inspector_drill::opens_as_json(s).then_some(s.len()))
.flatten()
else {
return;
};
// Short text is parsed on this key.
if len <= JSON_INLINE_BYTES {
match node.with_text(inspector_drill::parse_json) {
Some(Ok(value)) => {
let node = Node::Json {
root: std::sync::Arc::new(value),
path: Vec::new(),
};
self.inspector_modal.drill_in(frame, row, label, node);
}
Some(Err(e)) => {
self.inspector_modal.not_json = Some((frame, row, path));
self.flash_note(sentence(&e));
}
None => {}
}
return;
}
let token = self.inspector_modal.wait_for_json(frame, row, label, path);
// The worker reads the text where it is: the node is a one-row slice or a
// shared document, so nothing up to the 4 MiB cap is copied to hand it over.
self.spawn_job(
Job::InspectJson { token },
Some(Self::READING_JSON),
move |_| match node.with_text(inspector_drill::parse_json) {
Some(parsed) => parsed.map(|value| Answer::JsonParsed(std::sync::Arc::new(value))),
None => Err("not JSON: not text".to_string()),
},
);
}
/// `y` inside a drill: the focused item's whole value, exact, as `y` copies a
/// field; a JSON object or array as indented JSON.
fn copy_drilled_item(&mut self) {
use inspector_drill::Node;
let Some(drill) = self.inspector_modal.drill.as_ref() else {
return;
};
let Some((label, node)) = drill.level().focused() else {
return;
};
let g = crate::glyphs::get();
let mut path: Vec<&str> = drill.levels.iter().map(|l| l.label.as_str()).collect();
path.push(&label);
let display_row = self
.data_table_state
.as_ref()
.and_then(|s| s.inspect_row())
.map_or(drill.row + 1, |r| r.display_row);
let message = format!(
"Copied {} of row {}",
path.join(&format!(" {} ", g.trail)),
copy_modal::thousands(display_row)
);
match node {
Node::Native(series) => self.copy_value(polars::prelude::Column::from(series), message),
Node::Json { .. } => {
let limit = match self.copy_destination() {
Ok(destination) => destination.accepts().base64_limit,
Err(e) => {
self.error_modal.show(e);
return;
}
};
// Formatted here up to a size that is quick; past it, on a worker.
let cap = limit.map_or(Self::JSON_COPY_MAX_BYTES, |limit| limit / 4 * 3);
let quick = cap.min(Self::FIELD_COPY_INLINE_BYTES);
let null = serde_json::Value::Null;
let value = node.json().unwrap_or(&null);
if let Some(text) = inspector_drill::json_copy_text(value, quick) {
self.finish_copy(clipboard::Payload::text(text), message);
return;
}
if let Some(limit) = limit.filter(|_| cap <= Self::FIELD_COPY_INLINE_BYTES) {
self.error_modal
.show(clipboard::over_osc52_limit(None, limit));
return;
}
// The worker resolves the path itself: the document is shared, not copied.
self.spawn_job(Job::Copy, Some("Copying..."), move |_| {
let value = node.json().unwrap_or(&serde_json::Value::Null);
let text =
inspector_drill::json_copy_text(value, cap).ok_or_else(|| match limit {
Some(limit) => clipboard::over_osc52_limit(None, limit),
None => "Copy failed: the value is too large to copy".to_string(),
})?;
Ok(Answer::Copied {
payload: clipboard::Payload::text(text),
message,
})
});
}
}
}
/// Read, off this thread, the fields of `row` the buffer does not hold — the
/// hidden columns and the binary ones — with the shown columns beside them, so a
/// sort that orders ties differently on a second read cannot pass another row's
/// fields off as this one's. `wait`: the user waits on it, as on Enter; a read
/// that follows the rows does not hold the keys.
fn read_inspected_fields(&mut self, row: &crate::widgets::datatable::InspectRow, wait: bool) {
let Some(state) = self.data_table_state.as_ref() else {
return;
};
let fields = state.inspect_fields();
let wanted: Vec<String> = fields
.iter()
.filter(|f| !f.buffered())
.map(|f| f.name.clone())
.collect();
let check: Vec<String> = fields
.iter()
.filter(|f| f.buffered())
.map(|f| f.name.clone())
.collect();
let columns: Vec<String> = wanted.iter().chain(check.iter()).cloned().collect();
let lf = match state.inspect_read_lf(row.row, &columns) {
Ok(lf) => lf,
Err(e) => {
self.inspector_modal.read = Some(inspector_modal::FieldRead::Failed {
frame: row.frame,
row: row.row,
message: crate::error_display::user_message_from_polars(&e),
});
return;
}
};
let expected = row.values.select(check.iter().map(String::as_str)).ok();
let streaming = state.polars_streaming();
let (frame, index) = (row.frame, row.row);
self.inspector_modal.read = Some(inspector_modal::FieldRead::Reading { frame, row: index });
self.spawn_job(
Job::InspectRow { frame, row: index },
wait.then_some(Self::READING_FIELDS),
move |_| {
let read = crate::statistics::collect_lazy(lf, streaming)
.map_err(|e| crate::error_display::user_message_from_polars(&e))?;
if read.height() != 1 {
return Err("the row is no longer in the view".to_string());
}
let same = expected.is_some_and(|expected| {
read.select(check.iter().map(String::as_str))
.is_ok_and(|again| again.equals_missing(&expected))
});
if !same {
return Err("the view's order of equal rows changed on reading again; \
sort by a column that tells the rows apart"
.to_string());
}
let values = read
.select(wanted.iter().map(String::as_str))
.map_err(|e| e.to_string())?;
Ok(Answer::FieldsRead(values))
},
);
}
/// Copy the one value of `column`, exact, and flash `message`.
fn copy_value(&mut self, column: polars::prelude::Column, message: String) {
// Destination first, as the copy dialog's: a value over the terminal's cap
// is refused before it is formatted, here or on a worker.
let limit = match self.copy_destination() {
Ok(destination) => destination.accepts().base64_limit,
Err(e) => {
self.error_modal.show(e);
return;
}
};
if let Some(limit) = limit {
let fits = limit / 4 * 3;
let over = column
.get(0)
.is_ok_and(|value| crate::exact::copy_len_floor(&value, fits) > fits);
if over {
self.error_modal
.show(clipboard::over_osc52_limit(None, limit));
return;
}
}
if column.as_materialized_series().estimated_size() <= Self::FIELD_COPY_INLINE_BYTES {
match crate::exact::copy_text(&column) {
Ok(text) => self.finish_copy(clipboard::Payload::text(text), message),
Err(e) => self.error_modal.show(format!("Copy failed: {e}")),
}
return;
}
self.spawn_job(Job::Copy, Some("Copying..."), move |_| {
let text = crate::exact::copy_text(&column).map_err(|e| format!("Copy failed: {e}"))?;
Ok(Answer::Copied {
payload: clipboard::Payload::text(text),
message,
})
});
}
/// Replace the clipboard destination, so tests can watch what a copy sends
/// without a display server or a terminal in the loop.
pub fn set_clipboard_destination(&mut self, destination: Box<dyn clipboard::Destination>) {
self.clipboard = Some(destination);
}
pub fn create_view_from_current_state(
&mut self,
name: String,
description: Option<String>,
match_criteria: view::MatchCriteria,
) -> Result<view::SavedView> {
let settings = match &self.data_table_state {
Some(state) => view::ViewSettings {
chart: self.saved_chart(),
..view_settings_of(state)
},
None => view::ViewSettings {
chart: None,
sample: None,
query: None,
sql_query: None,
fuzzy_query: None,
filters: Vec::new(),
sort_columns: Vec::new(),
sort_descending: Vec::new(),
sort_ascending: true,
column_order: Vec::new(),
locked_columns_count: 0,
pivot: None,
melt: None,
reshape_source: None,
columns: Vec::new(),
},
};
self.view_manager
.create_view(name, description, match_criteria, settings)
}
/// The command line's language while it is open.
pub fn query_prompt_mode(&self) -> Option<QueryMode> {
(self.input_mode == InputMode::Editing && self.input_type == Some(InputType::Query))
.then_some(self.query_mode)
}
/// `:` at the table: the command line, holding the query in effect, selected,
/// so typing states a new one and the arrows edit it.
pub(crate) fn open_command_line(&mut self) {
let Some(state) = self.data_table_state.as_mut() else {
return;
};
self.input_mode = InputMode::Editing;
self.input_type = Some(InputType::Query);
self.query_run_error = None;
self.sql_completion = None;
self.query_input.set_value(state.get_active_query());
self.sql_input.set_value(state.get_active_sql_query());
self.query_input.select_all();
self.sql_input.select_all();
state.suppress_error_display = true;
self.sql_columns = state.sql_table_columns();
self.query_mode = self.opening_query_mode();
self.query_text_restored = !self.query_input_shown().is_empty();
self.sync_query_focus();
}
/// The language `:` opens in: the query in effect's own, so editing never
/// reinterprets it; else the one Ctrl+T last chose; else the configured default.
fn opening_query_mode(&self) -> QueryMode {
let active = self.data_table_state.as_ref().and_then(|state| {
if !state.get_active_sql_query().trim().is_empty() {
Some(QueryMode::Sql)
} else if !state.get_active_query().trim().is_empty() {
Some(QueryMode::Q)
} else {
None
}
});
active
.or(self.query_mode_chosen)
.unwrap_or(self.app_config.query.default_mode)
.resolve()
}
/// The command line's input for its current language.
fn query_input_mut(&mut self) -> &mut TextInput {
match self.query_mode {
QueryMode::Sql => &mut self.sql_input,
QueryMode::Q => &mut self.query_input,
}
}
/// The command line's input for its current language.
pub(crate) fn query_input_shown(&self) -> &TextInput {
match self.query_mode {
QueryMode::Sql => &self.sql_input,
QueryMode::Q => &self.query_input,
}
}
/// Switch the prompt's mode. Each mode keeps its own text; an error from the
/// last run belongs to the mode that ran it.
fn set_query_mode(&mut self, mode: QueryMode) {
self.query_mode = mode.resolve();
if let Some(state) = &mut self.data_table_state {
state.dismiss_error();
}
self.query_run_error = None;
self.sync_query_focus();
}
/// Tab in the command line: complete the column name (or, in SQL, the table
/// name) being typed, and on further presses step through the other names that
/// match.
fn complete_column_name(&mut self) {
let sql = self.query_mode == QueryMode::Sql;
let columns = std::mem::take(&mut self.sql_columns);
let mut cycle = self.sql_completion.take();
let input = self.query_input_mut();
let line = input
.line_at(input.cursor_line())
.unwrap_or_default()
.to_string();
let value = input.value().to_string();
let complete = if sql {
sql_assist::tab
} else {
sql_assist::q_tab
};
if let Some(step) = complete(
&columns,
&line,
input.cursor_col(),
&value,
input.cursor(),
&mut cycle,
) {
input.replace_before_cursor(step.span, &step.insert);
sql_assist::landed(&mut cycle, input.value(), input.cursor());
}
self.sql_completion = cycle;
self.sql_columns = columns;
}
/// The columns of `df` the word at the command line's cursor could name, for
/// the list under the input: every column while nothing is being typed.
pub(crate) fn sql_column_matches(&self) -> Vec<&(String, DataType)> {
let input = self.query_input_shown();
let line = input.line_at(input.cursor_line()).unwrap_or_default();
let word = match self.query_mode {
QueryMode::Sql => sql_assist::word_before(line, input.cursor_col()),
QueryMode::Q => sql_assist::q_word_before(line, input.cursor_col()),
}
.map(|w| w.text)
.unwrap_or_default();
sql_assist::matching(&self.sql_columns, &word)
}
/// The command line's text, while it is open.
pub fn query_prompt_text(&self) -> Option<&str> {
self.query_prompt_mode()?;
Some(self.query_input_shown().value())
}
/// Why the last run failed, for the line under the input: a statement that
/// failed while running, else one that could not be planned.
pub fn query_prompt_error(&self) -> Option<String> {
if let Some(error) = &self.query_run_error {
return Some(error.clone());
}
let state = self.data_table_state.as_ref()?;
let error = state.error()?;
Some(if self.query_mode == QueryMode::Sql {
crate::error_display::sql_error_message(error, state.sql_table_rows())
} else {
crate::error_display::user_message_from_polars(error)
})
}
/// Bumped each time a running statement's failure is put in the prompt.
pub fn inline_failures(&self) -> u64 {
self.inline_failures
}
/// Plan a query in `mode` and read its first rows in the background. A query that
/// cannot be planned leaves its error on the state, where the prompt shows it. One
/// that plans stays pending — the prompt open, when it came from there — until its
/// rows are in; if they fail, the view it replaced comes back.
fn run_query(&mut self, mode: QueryMode, text: &str, status: &str) {
self.query_run_error = None;
let Some(state) = self.data_table_state.as_mut() else {
return;
};
let rollback = state.rollback_point();
let rows = state.sql_table_rows();
// A query that cannot be planned changes nothing and leaves its error showing.
state.deferred(|s| match mode {
QueryMode::Sql => s.sql_query(text.to_string()),
QueryMode::Q => s.query(text.to_string()),
});
if state.error().is_some() {
return;
}
self.query_running = Some(QueryRun {
origin: RunOrigin::Query(mode),
frame: state.len_generation(),
rollback,
len_count_inflight: self.len_count_inflight,
count_after_paint: self.count_after_paint,
len_count_failed: self.len_count_failed,
rows,
});
if !self.spawn_async_collect(status) {
// Nothing to read: the rows on hand already show it.
self.query_running = None;
if self.query_prompt_mode() == Some(mode) {
self.leave_query_prompt_after_run();
}
}
}
/// The query still running over the frame on screen, taken. One whose frame has
/// since been replaced is dropped: its rollback would undo what replaced it.
fn take_query_run(&mut self) -> Option<QueryRun> {
let run = self.query_running.take()?;
let frame = self.data_table_state.as_ref()?.len_generation();
(run.frame == frame).then_some(run)
}
/// A query ran and its rows are in: the prompt closes on them.
fn leave_query_prompt_after_run(&mut self) {
self.sql_completion = None;
self.input_mode = InputMode::Normal;
self.input_type = None;
self.sql_input.set_focused(false);
self.query_input.set_focused(false);
if let Some(state) = &mut self.data_table_state {
state.suppress_error_display = false;
}
}
/// Only the current language's input carries the cursor.
fn sync_query_focus(&mut self) {
let mode = self.query_mode;
self.sql_input.set_focused(mode == QueryMode::Sql);
self.query_input.set_focused(mode == QueryMode::Q);
}
/// Esc from anywhere in the prompt: nothing runs and nothing typed survives.
fn close_query_prompt(&mut self) {
self.query_run_error = None;
self.sql_completion = None;
self.query_input.clear();
self.sql_input.clear();
self.query_input.set_focused(false);
self.sql_input.set_focused(false);
self.input_mode = InputMode::Normal;
self.input_type = None;
if let Some(state) = &mut self.data_table_state {
state.dismiss_error();
state.suppress_error_display = false;
}
}
}
impl Widget for &mut App {
fn render(self, area: Rect, buf: &mut Buffer) {
self.begin_frame();
self.debug.num_frames += 1;
if self.debug.enabled {
self.debug.show_help_at_render = self.help.is_open();
}
use crate::render::context::RenderContext;
use crate::render::layout::app_layout;
use crate::render::main_view::MainViewContent;
let ctx = RenderContext::from_theme_and_config(
&self.theme,
self.table_cell_padding,
self.column_colors,
self.number_format.clone(),
)
.with_dtype_row(self.dtype_row);
let main_view_content = MainViewContent::current(self);
Clear.render(area, buf);
let background_color = self.color("background");
Block::default()
.style(Style::default().bg(background_color))
.render(area, buf);
// The footer grows, taking rows from the bottom of the view, only for a prompt
// being typed or a job with progress.
let progress = self.footer_progress_line(main_view_content);
let prompt_room = crate::render::footer::MAX_LINES - 1;
let prompt_rows = if main_view_content == MainViewContent::Datatable {
crate::render::input_strip::rows(self, area.width, prompt_room)
} else {
0
};
let progress_rows = u16::from(progress.is_some() && prompt_rows < prompt_room);
let footer_lines = 1 + prompt_rows + progress_rows;
// The inspector is framed; its border sets it off from the footer.
let rule = self.input_mode != InputMode::Inspect;
let app_layout = app_layout(area, self.debug.enabled, footer_lines, rule);
// A terminal too short for all of it keeps the status line first, then the
// prompt, then the progress.
let room = app_layout.footer.height.saturating_sub(1);
let prompt_rows = prompt_rows.min(room);
let progress_rows = progress_rows.min(room - prompt_rows);
let main_area = app_layout.main_view;
Clear.render(main_area, buf);
crate::render::main_view_render::render_main_view(area, main_area, buf, self, &ctx);
if self.in_normal_table_view() {
self.render_drop_mark(buf, &ctx);
}
if self.menu_showing()
&& let Some(menu) = self.context_menu.clone()
{
menu.render(main_area, buf, &ctx);
}
// Status messages are shown inline in the control bar (no overlay popups).
if self.confirmation_modal.active {
crate::render::overlays::render_confirmation_modal(
area,
buf,
&mut self.confirmation_modal,
&ctx,
);
}
if self.error_modal.active {
crate::render::overlays::render_error_modal(area, buf, &mut self.error_modal, &ctx);
}
self.close_help_left_behind();
if self.help.is_open() {
// Over the view, never the footer: its rule, and the lines it grows by
// for a prompt or progress, are drawn after and would cut the frame.
crate::render::help::render_help(app_layout.main_view, buf, &mut self.help, &ctx);
}
let footer = self.footer(main_view_content, progress_rows > 0);
crate::render::footer::render_rule(app_layout.rule, buf, &ctx);
let line = Rect {
height: 1,
..app_layout.footer
};
let drawn = footer.render_line(line, buf, &ctx);
if prompt_rows > 0 {
crate::render::input_strip::render(
Rect {
y: line.y + 1,
height: prompt_rows,
..line
},
buf,
self,
&ctx,
);
}
if let Some(progress) = progress.filter(|_| progress_rows > 0) {
crate::render::footer::render_progress(
&progress,
Rect {
y: line.y + 1 + prompt_rows,
height: 1,
..line
},
buf,
&ctx,
);
}
self.pointer.chips_drawn(
drawn
.iter()
.map(|(rect, key)| (*rect, key.as_str()))
.collect(),
);
if let Some(debug_area) = app_layout.debug {
self.debug.render(debug_area, buf);
}
self.pointer.drawn();
// Last line of defence, and deliberately the last statement here.
//
// Everything above draws untrusted text: cell values, column names,
// filenames, parser messages. ratatui strips control characters in
// `Buffer::set_stringn` but not in `Span`/`Line` rendering, which is
// what these widgets use, and the crossterm backend then prints each
// cell symbol unfiltered. Without this sweep a cell containing
// `\x1b]52;c;...\x07` writes to the user's clipboard.
//
// Doing it here rather than at each of the ~200 `Span` construction
// sites means a new widget cannot forget to. See `crate::sanitize`.
crate::sanitize::sanitize_buffer(buf);
}
}
impl App {
/// The view a caller that asked for one gets back when the app exits
/// (`datui.view(..., capture=True)`): the active table's committed frame with
/// datui's internal columns dropped. `None` when no dataset is open. Text still
/// sitting in an editor was never applied, so it is not here either.
///
/// Refused when the frame would scan a temporary file, because those are removed
/// on exit and a plan over deleted paths fails later and worse: a remote download,
/// a decompressed archive, or a converted stream or GPS log. The in-TUI export (`e`) writes
/// real rows and is the way out for those datasets.
pub fn capture_view(&self) -> Result<Option<LazyFrame>> {
let Some(state) = &self.data_table_state else {
return Ok(None);
};
if state.scans_a_download() {
return Err(color_eyre::eyre::eyre!(
"cannot return this view: the data was downloaded to a temporary file \
that is removed when datui exits. Export it from inside datui (press \
e) instead."
));
}
if state.scans_a_temp_file() {
return Err(color_eyre::eyre::eyre!(
"cannot return this view: the data was decompressed or converted into a \
temporary file that is removed when datui exits. Export it from \
inside datui (press e) instead."
));
}
Ok(Some(state.visible_lf()))
}
}
impl Drop for App {
fn drop(&mut self) {
// The dataset's footer pass stops issuing reads. The open in flight is the
// loader's, which stops it as it drops: a download still running stops at its
// next chunk and removes its partial file, and a finished one is removed as
// whatever holds it drops. Drop rather than the end of `run`, because it covers
// every exit: a normal quit, an error return, an unwind from a panic, and the
// Python binding calling `run` again in the same process.
self.footer_progress.cancel();
// The indexing stops, and the reads waiting on it give up, so nothing holds
// the file once the app is gone (the Python binding runs on in the process).
self.indexing_stop
.store(true, std::sync::atomic::Ordering::Relaxed);
if let Some(lines) = self.indexing_lines.take() {
lines.stop_indexing();
}
}
}
/// A source as a home-screen row, with the last run's buckets when they still apply.
#[cfg(feature = "cloud")]
fn home_cloud_source(
source: &crate::cloud_sources::Source,
cached: Option<&crate::cache::CloudListing>,
listing: bool,
) -> home::CloudSource {
let mut details: Vec<(String, String)> = vec![
("source".to_string(), source.id.clone()),
(
"api".to_string(),
match source.kind {
crate::cloud_browse::ProviderKind::S3 => "s3",
crate::cloud_browse::ProviderKind::Gcs => "gcs",
crate::cloud_browse::ProviderKind::Azure => "azure",
}
.to_string(),
),
];
if let Some(endpoint) = &source.s3.endpoint {
details.push(("endpoint".to_string(), endpoint.clone()));
}
if let Some(region) = &source.s3.region {
details.push(("region".to_string(), region.clone()));
}
if let Some(project) = &source.project {
details.push(("project".to_string(), project.clone()));
}
if let Some(profile) = &source.profile {
details.push(("profile".to_string(), profile.clone()));
}
if let Some(configuration) = &source.gcloud {
details.push(("configuration".to_string(), configuration.clone()));
}
if source.s3.virtual_hosted.is_some() {
let style = if source.s3.virtual_hosted_style() {
"virtual-hosted"
} else {
"path-style"
};
details.push(("addressing".to_string(), style.to_string()));
}
details.push(("login".to_string(), source.origin.clone()));
let short = source.problem.as_deref().map(|problem| {
if problem.starts_with("not signed in") {
"not signed in"
} else if problem.starts_with("unsupported login") {
"unsupported login"
} else {
"not configured"
}
});
// A source that cannot list says what to do about it where the row has room: the
// count already carries the short problem, and the login it would use is moot
// (#547 D5).
let note = match (&source.problem, short) {
(Some(problem), Some(short)) => problem
.strip_prefix(short)
.map(|rest| rest.trim_start_matches([':', ' ']))
.filter(|rest| !rest.is_empty())
.unwrap_or(problem)
.to_string(),
_ => [source.detail(), Some(source.origin.clone())]
.into_iter()
.flatten()
.filter(|n| !n.is_empty())
.collect::<Vec<_>>()
.join(&format!(" {} ", crate::glyphs::get().middot)),
};
let mut names: Vec<String> = cached.map(|c| c.buckets.clone()).unwrap_or_default();
for bucket in &source.buckets {
if !names.contains(bucket) {
names.push(bucket.clone());
}
}
let status = match (&source.problem, short) {
(Some(problem), Some(short)) => home::CloudStatus::Failed {
short: short.to_string(),
detail: problem.clone(),
},
_ if cached.is_some() => home::CloudStatus::Listed,
_ if listing => home::CloudStatus::Listing,
_ => home::CloudStatus::Unlisted,
};
home::CloudSource {
id: source.id.clone(),
label: source.label.clone(),
api: match source.kind {
crate::cloud_browse::ProviderKind::S3 => "s3",
crate::cloud_browse::ProviderKind::Gcs => "gcs",
crate::cloud_browse::ProviderKind::Azure => "azure",
}
.to_string(),
note,
buckets: names
.iter()
.map(|b| PathBuf::from(source.bucket_url(b)))
.collect(),
refreshing: listing && cached.is_some() && source.problem.is_none(),
// A source that failed before any request has nothing to ask.
asked: listing || source.problem.is_some(),
listed_at: cached
.map(|c| std::time::UNIX_EPOCH + std::time::Duration::from_secs(c.listed_at)),
status,
details,
place_details: Default::default(),
}
}
/// A listing error as a word for the row and the full message for the details pane.
#[cfg(feature = "cloud")]
fn summarize_cloud_failure(error: &str) -> (String, String) {
let lower = error.to_lowercase();
// A missing tool is already as short as it gets: `needs the AWS CLI`.
if let Some(start) = lower.find("needs ") {
return (error[start..].to_string(), error.to_string());
}
let short = if lower.contains("403")
|| lower.contains("forbidden")
|| lower.contains("accessdenied")
|| lower.contains("access denied")
{
"403"
} else if lower.contains("401")
|| lower.contains("unauthorized")
|| lower.contains("credential")
|| lower.contains("invalidaccesskeyid")
|| lower.contains("expired")
|| lower.contains("sso")
|| lower.contains("az login")
{
"not logged in"
} else if lower.contains("unsupported login") {
"unsupported login"
} else if lower.contains("no gcp project") {
"no project"
} else if lower.contains("is not set") {
"not configured"
} else if lower.contains("timed out")
|| lower.contains("timeout")
|| lower.contains("connection")
|| lower.contains("dns")
|| lower.contains("resolve")
{
"unavailable"
} else {
"error"
};
(short.to_string(), error.to_string())
}
/// Run a future on the app's runtime from a thread outside it, and wait for the answer.
///
/// Every background thread that needs the network goes through this rather than
/// `Handle::block_on`. That polls the future on the calling thread, and quitting shuts
/// the runtime down without waiting for those threads: the next timer or socket an
/// in-flight request touches then panics with "A Tokio 1.x context was found, but it is
/// being shutdown", across the terminal the user just got back. A task spawned onto the
/// runtime is dropped by the shutdown instead of polled, so the wait ends with `None`
/// and the abandoned request goes quietly.
#[cfg(feature = "cloud")]
pub(crate) fn wait_on_runtime<F>(runtime: &tokio::runtime::Handle, future: F) -> Option<F::Output>
where
F: std::future::Future + Send + 'static,
F::Output: Send + 'static,
{
let (tx, rx) = std::sync::mpsc::sync_channel(1);
runtime.spawn(async move {
let _ = tx.send(future.await);
});
rx.recv().ok()
}
/// Restore the terminal, then turn how the loop ended into what `run_impl` returns.
/// The reader stops first, so nothing typed after the screen is handed back is read
/// here. The capture is taken after the screen is handed back, so a refused capture
/// still leaves the terminal usable.
fn conclude(
end: event_pump::Ended,
app: &App,
capture: bool,
reader: &mut terminal_input::TerminalInput,
screen: &mut TakenTerminal,
) -> Result<Option<LazyFrame>> {
reader.stop();
screen.restore();
match end {
event_pump::Ended::Quit if capture => app.capture_view(),
event_pump::Ended::Quit => Ok(None),
event_pump::Ended::Crash(msg) => Err(color_eyre::eyre::eyre!(msg)),
event_pump::Ended::NotFound(path) => Err(std::io::Error::new(
std::io::ErrorKind::NotFound,
format!("File not found: {}", path.display()),
)
.into()),
}
}
/// The exit status the session's ending signal calls for, once one has ended it;
/// 0 until then. Set once.
static ENDED_BY_SIGNAL: std::sync::atomic::AtomicI32 = std::sync::atomic::AtomicI32::new(0);
/// The exit status for the binary when a signal ended the session [`run`] returned
/// from: `128 + n` for SIGTERM, SIGHUP or SIGINT, as if it had not been caught (the
/// statuses of `datui_cli::exit`), and on
/// Windows the status a console process closed by its window ends with.
pub fn ended_by_signal() -> Option<i32> {
let status = ENDED_BY_SIGNAL.load(std::sync::atomic::Ordering::SeqCst);
(status != 0).then_some(status)
}
/// A signal that ends the session arrived: quit as `q` does, so the screen is handed
/// back and an open's temp files are removed (#510). A second one, or a session still
/// running a few seconds after the first, ends the process at once, as the signal
/// would have: a stuck event loop cannot make datui unkillable. Called on the runtime.
fn end_session(status: i32, tx: &std::sync::mpsc::Sender<AppEvent>) {
use std::sync::atomic::Ordering;
// Longer than the exit sweep's grace, which is part of a normal quit, and short
// of the five seconds Windows allows a console process it is closing.
const STRAGGLE: std::time::Duration = std::time::Duration::from_secs(3);
fn end_now(status: i32) -> ! {
restore_terminal();
std::process::exit(status)
}
if ENDED_BY_SIGNAL
.compare_exchange(0, status, Ordering::SeqCst, Ordering::SeqCst)
.is_err()
{
end_now(ENDED_BY_SIGNAL.load(Ordering::SeqCst));
}
let _ = tx.send(AppEvent::Exit);
tokio::spawn(async move {
tokio::time::sleep(STRAGGLE).await;
end_now(status);
});
}
/// End the session on SIGTERM, SIGHUP (the terminal closing) or SIGINT (`kill -INT`;
/// Ctrl+C at the terminal is a key, not this signal).
#[cfg(unix)]
fn quit_on_signals(runtime: &tokio::runtime::Handle, tx: &std::sync::mpsc::Sender<AppEvent>) {
use tokio::signal::unix::{SignalKind, signal};
// `signal` registers with the runtime it is called in.
let _runtime = runtime.enter();
for kind in [
SignalKind::terminate(),
SignalKind::hangup(),
SignalKind::interrupt(),
] {
let Ok(mut arrivals) = signal(kind) else {
continue;
};
let tx = tx.clone();
runtime.spawn(async move {
while arrivals.recv().await.is_some() {
end_session(128 + kind.as_raw_value(), &tx);
}
});
}
}
/// End the session when its console window is closed, or the user logs off or the
/// machine shuts down. Tokio holds the control handler until the process exits, so
/// the quit runs before Windows ends it.
#[cfg(windows)]
fn quit_on_signals(runtime: &tokio::runtime::Handle, tx: &std::sync::mpsc::Sender<AppEvent>) {
use tokio::signal::windows::{ctrl_close, ctrl_logoff, ctrl_shutdown};
/// STATUS_CONTROL_C_EXIT, what a console process ends with when closed.
const CLOSED: i32 = 0xC000_013A_u32 as i32;
// Each registers with the runtime it is called in.
let _runtime = runtime.enter();
// Three listener types with one shape and no trait in common.
macro_rules! quit_on {
($listen:expr) => {
if let Ok(mut arrivals) = $listen {
let tx = tx.clone();
runtime.spawn(async move {
while arrivals.recv().await.is_some() {
end_session(CLOSED, &tx);
}
});
}
};
}
quit_on!(ctrl_close());
quit_on!(ctrl_logoff());
quit_on!(ctrl_shutdown());
}
/// Run the TUI with either file paths or an existing LazyFrame. Single event loop
/// used by the CLI and the Python binding.
pub fn run(input: RunInput, config: Option<AppConfig>) -> Result<()> {
run_impl(input, config, false).map(|_| ())
}
/// As `run`, but a normal quit hands back the active table's final view for the
/// caller to keep working with (the Python binding's `capture=True`). `None` when no
/// dataset was open at quit. See `App::capture_view` for what is refused and why.
pub fn run_captured(input: RunInput, config: Option<AppConfig>) -> Result<Option<LazyFrame>> {
run_impl(input, config, true)
}
fn run_impl(
input: RunInput,
config: Option<AppConfig>,
capture: bool,
) -> Result<Option<LazyFrame>> {
use event_pump::EventPump;
use std::io::Write;
// First, so a missing file is named as the home directory has it.
let input = startup::expand_home(input);
use std::sync::{Mutex, Once, mpsc};
// The saved views are read on a worker from here; the first thing that needs them
// waits for the rest of the read, if any.
let views = Views::read_in_background();
// Install color_eyre at most once per process (e.g. first datui.view() in Python).
// Subsequent run() calls skip install and reuse the result; no error-message detection.
static COLOR_EYRE_INIT: Once = Once::new();
static INSTALL_RESULT: Mutex<Option<Result<(), color_eyre::Report>>> = Mutex::new(None);
COLOR_EYRE_INIT.call_once(|| {
*INSTALL_RESULT.lock().unwrap_or_else(|e| e.into_inner()) = Some(color_eyre::install());
});
if let Some(Err(e)) = INSTALL_RESULT
.lock()
.unwrap_or_else(|e| e.into_inner())
.as_ref()
{
return Err(color_eyre::eyre::eyre!(e.to_string()));
}
let rt = tokio::runtime::Builder::new_multi_thread()
.worker_threads(2)
.enable_all()
.build()
.map_err(|e| color_eyre::eyre::eyre!("Failed to create tokio runtime: {}", e))?;
// Background work (e.g. the row-count `len()` over a huge or remote dataset) runs on
// the runtime's blocking pool. Dropping the runtime normally *joins* those threads, so
// quitting would hang until an in-flight count finished — minutes for a 474 GB hive
// set. Shut the runtime down in the background instead: exit is immediate and the
// abandoned read-only task dies with the process. This guard covers every return path
// (Exit, Crash, `?`-propagated errors, channel disconnect).
struct RtGuard(Option<tokio::runtime::Runtime>);
impl Drop for RtGuard {
fn drop(&mut self) {
if let Some(rt) = self.0.take() {
rt.shutdown_background();
}
}
}
let rt_guard = RtGuard(Some(rt));
let rt_handle = rt_guard
.0
.as_ref()
.expect("runtime present")
.handle()
.clone();
// `--tee -` passes the stream on to standard output, so the screen is drawn on the
// terminal itself; standard output as it was is kept for the copy.
let passed = match &input {
RunInput::Cli(args) if args.tee.as_deref().is_some_and(crate::stdin::is_stdin) => {
Some(crate::tee::pass_stdout_on().map_err(|e| color_eyre::eyre::eyre!(e))?)
}
_ => None,
};
let mut terminal = match ratatui::try_init() {
Ok(terminal) => QuietTerminal(Some(terminal)),
Err(e) => {
// No screen to keep up, so nothing to wait behind: a configuration that
// cannot be used, or a named file that is not there, is the more useful
// thing to say, as each always came first.
if config.is_none() {
startup::load_config(&input)?;
}
// Without a screen nothing is opened, so the specs are not loaded to look.
if let Some(missing) =
App::missing_named_path(startup::named_paths(&input), &Default::default())
{
return Err(std::io::Error::new(
std::io::ErrorKind::NotFound,
format!("File not found: {}", missing.display()),
)
.into());
}
return Err(color_eyre::eyre::eyre!(
"datui requires an interactive terminal (TTY). No terminal detected: {}. \
There is no TTY inside a Jupyter notebook or when output is piped or \
redirected; run from a terminal with stdout connected to it.",
e
));
}
};
// Handed back on every way out of this function, after the reader below has let go.
let mut screen = TakenTerminal { restored: false };
// Anything written to stderr from here on would be drawn over the screen; it goes
// to the log until this drops, on every way out of this function.
let session = logging::TuiSession::begin(restore_terminal);
push_keyboard_flags();
// Asked before the settings are read, so the answer is usually in by the time they
// are; under an explicit `theme.mode` it is read and dropped. The reader takes it
// off the input stream, so nothing waits here.
let asked = terminal_color::supported()
&& config.as_ref().is_none_or(|c| c.theme.follow)
&& terminal_color::ask(&mut std::io::stdout());
let mut background = None;
let (tx, rx) = mpsc::channel::<AppEvent>();
{
let tx = tx.clone();
session.wake_with(move || {
let _ = tx.send(AppEvent::Wake);
});
}
let mut reader = terminal_input::TerminalInput::start(tx.clone())?;
// Only for the datui binary: the handlers stay for the life of the process, and a
// host such as Python keeps its own.
#[cfg(any(unix, windows))]
if matches!(input, RunInput::Cli(_)) {
quit_on_signals(&rt_handle, &tx);
}
// The settings are files, so they are read on a worker while the keys are already
// being read: a slow mount shows a screen saying so, and Ctrl+C or Ctrl+Q leave it.
let waiting_on = startup::named(&input);
{
let tx = tx.clone();
std::thread::Builder::new()
.name("datui-settings".into())
.spawn(move || {
let read = logging::catch_panic(|| startup::read(input, config))
.unwrap_or_else(|panic| Err(color_eyre::eyre::eyre!(panic)));
let _ = tx.send(AppEvent::SettingsRead(Box::new(read)));
})?;
}
let mut backlog = Vec::new();
let grace_ends = std::time::Instant::now() + startup::GRACE;
let mut waiting_shown = false;
let settings = loop {
let timeout = if waiting_shown {
std::time::Duration::MAX
} else {
grace_ends.saturating_duration_since(std::time::Instant::now())
};
match rx.recv_timeout(timeout) {
Ok(AppEvent::SettingsRead(read)) => break *read,
Ok(AppEvent::TerminalBackground(mode)) => background = Some(mode),
Ok(AppEvent::Terminal(crossterm::event::Event::Key(key)))
if key.modifiers.contains(KeyModifiers::CONTROL)
&& matches!(key.code, KeyCode::Char('c') | KeyCode::Char('q')) =>
{
reader.stop();
screen.restore();
return Ok(None);
}
Ok(AppEvent::Crash(msg)) => {
reader.stop();
screen.restore();
return Err(color_eyre::eyre::eyre!(msg));
}
// A signal, before there was an app to quit.
Ok(AppEvent::Exit) => {
reader.stop();
screen.restore();
return Ok(None);
}
Ok(event) => {
if waiting_shown
&& matches!(
event,
AppEvent::Terminal(crossterm::event::Event::Resize(..))
)
{
terminal
.get()
.draw(|frame| startup::draw_waiting(frame, waiting_on.as_deref()))?;
}
// Typed before there was an app to take it: handled, in order, first.
backlog.push(event);
}
Err(_) => {
terminal
.get()
.draw(|frame| startup::draw_waiting(frame, waiting_on.as_deref()))?;
let _ = std::io::stdout().flush();
waiting_shown = true;
}
}
};
let startup::Settings {
config,
theme,
input,
opts,
notes,
} = match settings {
Ok(settings) => settings,
Err(e) => {
reader.stop();
screen.restore();
return Err(e);
}
};
// The first frame is not held for the terminal's answer: one that is already in
// is used, else this terminal's last one (see `App::settle_first_palette`).
let background = (asked && config.theme.follow)
.then(|| startup::take_answer(&rx, background, &mut backlog))
.flatten();
if config.theme.follow && terminal_color::supported() {
follow_focus(&mut std::io::stdout());
}
// Choose the glyph alphabet before the first frame: on a terminal that is not
// doing UTF-8, box-drawing characters render as replacement boxes and make the
// UI harder to read rather than prettier.
glyphs::init_with_overrides(config.display.unicode, &config.glyphs.overrides);
// Taken once the settings say so; handed back with the screen.
pointer::capture(config.display.mouse, &mut std::io::stdout());
let mut app = App::new_with_views(tx.clone(), rt_handle, theme, config, views);
app.settle_first_palette(background);
if let Some(out) = passed {
app.pass_stdout_to(out);
}
app.startup_view = opts.view.clone();
// A developer's overlay: an environment variable, not a flag.
let debug_env = std::env::var_os("DATUI_DEBUG").is_some_and(|v| !v.is_empty() && v != "0");
if opts.debug || debug_env {
app.enable_debug();
}
// Show the first frame immediately; the open it announces is handled right after.
let open = match input {
// No paths: open the home screen instead of loading anything.
RunInput::Paths(paths, _) if paths.is_empty() => {
app.enter_home();
None
}
RunInput::Paths(paths, opts) => {
// Whether each path is there, and whether a directory was named, is asked
// after this frame, on a worker; the frame says what is being opened.
app.set_loading_phase("Scanning input", 10);
if let [path] = paths.as_slice() {
app.name_what_is_loading(path.clone());
}
Some(AppEvent::OpenNamed(paths, opts))
}
RunInput::LazyFrame(lf, opts) => {
app.set_loading_phase("Scanning input", 10);
Some(AppEvent::OpenLazyFrame(lf, opts))
}
RunInput::Cli(_) | RunInput::Host(..) => {
unreachable!("read_settings resolves the command line")
}
};
// Declared before the pump, so it drops after it: the app's own files go with the
// app, and this then removes what a worker was still writing.
let _sweep = app.exit_sweep();
let input_tx = tx.clone();
let mut pump = EventPump::new(app, tx, rx);
// The open goes out before the keys typed while the settings were read, so they
// meet it as they would any open in flight: Ctrl+O puts it down, `q` quits. Sent
// on the channel instead, it lost to a Ctrl+O offered ahead of the channel and
// opened behind the home screen, or behind whatever was opened from there.
pump.handle_first(backlog.into_iter().chain(open));
let end = pump.run(|app| {
if let Some(open) = app.take_external_open() {
let mouse = app.mouse_enabled();
let focus = app.follows_terminal() && terminal_color::supported();
let note = open_externally(&open, &mut reader, &input_tx, mouse, focus, terminal.get());
app.external_opened(&open, note);
}
// Between frames, so the question is never written into the middle of one.
if app.take_background_query() && terminal_color::supported() {
terminal_color::ask(&mut std::io::stdout());
}
terminal
.get()
.draw(|frame| frame.render_widget(app, frame.area()))?;
let _ = std::io::stdout().flush();
Ok(())
})?;
let result = conclude(end, &pump.app, capture, &mut reader, &mut screen);
// stderr is the terminal again once the session is over.
drop(session);
// Not `eprintln!`, which panics when a hangup has taken the terminal away.
for note in notes {
let _ = writeln!(std::io::stderr(), "datui: {note}");
}
// Quit with the recording kept going: it goes on until its stream ends, with the
// terminal handed back. A signal now ends the process as it always would.
if let Some((tee, handle)) = pump.app.recording_after_exit() {
let to = if tee.to_stdout() {
format!("passing standard input on to {}", tee.name())
} else {
format!("recording standard input to {}", tee.path.display())
};
let _ = writeln!(
std::io::stderr(),
"datui: {to} until it ends (Ctrl+C stops it)"
);
handle.spool().wait();
let done = if tee.to_stdout() {
"datui: standard input ended".to_string()
} else {
format!("datui: saved {}", tee.path.display())
};
let _ = writeln!(std::io::stderr(), "{done}");
}
result
}
/// Open a value the inspector wrote: a program that takes the terminal gets it
/// (the key reader stopped, the screen and raw mode handed back) until it
/// returns; an opener is only started. Says what went wrong, if anything.
fn open_externally(
open: &external_open::ExternalOpen,
reader: &mut terminal_input::TerminalInput,
tx: &std::sync::mpsc::Sender<AppEvent>,
mouse: bool,
focus: bool,
terminal: &mut ratatui::DefaultTerminal,
) -> Option<String> {
let program = external_open::program_for(open.document, |name| std::env::var(name).ok());
let result = match &program {
external_open::Program::Opener(_) => external_open::run(&program, &open.path),
external_open::Program::Wait(_) => {
reader.stop();
restore_terminal();
let result = external_open::run(&program, &open.path);
let _ = crossterm::terminal::enable_raw_mode();
let _ = crossterm::execute!(
std::io::stdout(),
crossterm::terminal::EnterAlternateScreen,
crossterm::cursor::Hide
);
push_keyboard_flags();
pointer::capture(mouse, &mut std::io::stdout());
if focus {
follow_focus(&mut std::io::stdout());
}
let _ = terminal.clear();
match terminal_input::TerminalInput::start(tx.clone()) {
Ok(started) => *reader = started,
Err(e) => {
let _ = tx.send(AppEvent::Crash(format!("Could not read keys again: {e}")));
}
}
result
}
};
result.err().map(|e| e.to_string())
}
/// `text` as a sentence for a flash: its first letter capitalized.
fn sentence(text: &str) -> String {
let mut chars = text.chars();
match chars.next() {
Some(first) => first.to_uppercase().chain(chars).collect(),
None => String::new(),
}
}