datui-lib 0.4.4

Data Exploration in the Terminal (library)
Documentation
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
//! The home screen: pick a dataset without first knowing where it is.
//!
//! It lists *roots* (places to look) and *catalogs* (named datasets and
//! directories):
//!
//! 1. **The working directory.**
//! 2. **Catalogs**: `catalog.toml` (Ctrl+D adds to it), the files `catalogs` lists, and
//!    the bundled `public` catalog. A directory in one is a row to step into.
//!
//! `RECENT` lists every dataset opened, grouped under the directory or prefix it
//! lives in; `Enter` on such a place row browses it. It is derived state, cheap to
//! rebuild or discard.

pub mod catalog;
pub mod codebook;
pub mod discover;
pub mod fuzzy;
pub(crate) mod home_app;
pub(crate) mod home_keys;
pub mod home_preview;
pub mod locality;
pub mod search;

use crate::home::discover::{Entry, EntryKind};
use std::path::{Path, PathBuf};

/// Where a root came from, shown subtly in the UI.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum RootOrigin {
    Cwd,
    /// Derived from the desktop's own recently-used list.
    Desktop,
}

impl RootOrigin {
    pub fn note(self) -> &'static str {
        match self {
            RootOrigin::Cwd => "current directory",
            RootOrigin::Desktop => "opened elsewhere",
        }
    }
}

/// Directories holding data files the desktop recorded you opening
/// (`recently-used.xbel`), so a fresh install has places to suggest. Only the
/// directories are used, never the files: the list holds whatever was opened
/// anywhere, which may be private.
pub fn desktop_recent_dirs() -> Vec<PathBuf> {
    let Some(data_dir) = dirs::data_dir() else {
        return Vec::new();
    };
    let path = data_dir.join("recently-used.xbel");
    let Ok(contents) = std::fs::read_to_string(&path) else {
        return Vec::new();
    };
    dirs_from_xbel(&contents)
}

/// Extract directories of data files from XBEL content. Scans for
/// `href="file://…"` rather than parsing XML, to avoid an XML dependency.
pub fn dirs_from_xbel(contents: &str) -> Vec<PathBuf> {
    const PREFIX: &str = "href=\"file://";
    let mut dirs: Vec<PathBuf> = Vec::new();

    for chunk in contents.split(PREFIX).skip(1) {
        let Some(end) = chunk.find('"') else { continue };
        let decoded = percent_decode(&chunk[..end]);
        let file = PathBuf::from(decoded);
        // Only files datui can open that still exist.
        if !crate::home::discover::is_data_file(&file) || !file.is_file() {
            continue;
        }
        let Some(parent) = file.parent() else {
            continue;
        };
        if parent.as_os_str().is_empty() {
            continue;
        }
        let parent = parent.to_path_buf();
        if !dirs.contains(&parent) {
            dirs.push(parent);
        }
    }

    dirs
}

/// Decode `%20`-style escapes in a file URI.
fn percent_decode(raw: &str) -> String {
    let bytes = raw.as_bytes();
    let mut out: Vec<u8> = Vec::with_capacity(bytes.len());
    let mut i = 0;
    while i < bytes.len() {
        if bytes[i] == b'%' && i + 2 < bytes.len() {
            let hex = std::str::from_utf8(&bytes[i + 1..i + 3]).ok();
            if let Some(byte) = hex.and_then(|h| u8::from_str_radix(h, 16).ok()) {
                out.push(byte);
                i += 3;
                continue;
            }
        }
        out.push(bytes[i]);
        i += 1;
    }
    String::from_utf8_lossy(&out).into_owned()
}

/// The service's word for the top of an object-store place: a source, account,
/// bucket or container. `None` otherwise, including directories inside a bucket,
/// which are labeled by their contents like local ones.
pub fn object_place_label(path: &Path) -> Option<&'static str> {
    if cloud_source_id(path).is_some() {
        return Some("source");
    }
    if cloud_account(path).is_some() {
        return Some("account");
    }
    let text = path.to_string_lossy();
    if let Some((_, _, key)) = crate::cloud::source::azure_parts(&text) {
        return key.trim_matches('/').is_empty().then_some("container");
    }
    let (scheme, rest) = text.split_once("://")?;
    if !matches!(scheme, "s3" | "s3a" | "gs" | "gcs") {
        return None;
    }
    let rest = rest.trim_end_matches('/');
    if rest.is_empty() {
        return None;
    }
    (!rest.contains('/')).then_some("bucket")
}

/// A directory in an object store that has not said what it holds yet.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CloudLook {
    /// Not asked about: `…`.
    Waiting,
    /// Its peek is out, or its answer has not reached the row: a spinner.
    Looking,
    /// Its peek failed: `?` until Ctrl+R.
    Failed,
}

/// What a home row is called, decided once for the list and the pane beside it.
#[derive(Debug, Default, PartialEq)]
pub struct RowLabel {
    /// The list's word beside the name: a count, a kind, the curated word, a source id.
    pub short: String,
    /// The pane's `kind` line, which has the room to say it in words.
    pub words: String,
    /// `short` is the word a source or catalog gives the place.
    pub curated: bool,
    /// `short` is the source id the path names, or that the source is gone.
    pub source: bool,
    /// The source the path names has left the config.
    pub missing_source: bool,
}

/// What `entry` is called. `look` is a bucket directory's look-up state, drawn at
/// spinner `frame`; `known_sources` are the sources a URL can name, `None` where
/// the trail already names it.
pub fn describe(
    entry: &Entry,
    place_kind: Option<&'static str>,
    look: Option<CloudLook>,
    frame: usize,
    known_sources: Option<&[crate::config::CloudConnectionConfig]>,
) -> RowLabel {
    // The door is an action: labels, curated words and source ids describe the
    // directory, not the door.
    if entry.opens_whole_directory {
        return RowLabel::default();
    }
    let g = crate::glyphs::get();
    // A source's word for a place it names (`dataset`, `project`), or `missing` for an
    // absent catalog dataset, ahead of any count so the row stays marked curated.
    let curated =
        place_kind.filter(|_| matches!(entry.kind, EntryKind::Directory | EntryKind::Unknown));
    let look_glyph = look.map(|look| match look {
        CloudLook::Waiting => g.ellipsis,
        CloudLook::Looking => g.spinner[frame % g.spinner.len()],
        CloudLook::Failed => "?",
    });
    let short = match curated {
        Some(word) => word.to_string(),
        // An uncounted directory: a bucket's own word, else that it is being looked into.
        None if entry.kind == EntryKind::Directory && entry.holds.formats.is_empty() => {
            object_place_label(&entry.path)
                .or(look_glyph)
                .map(str::to_string)
                .unwrap_or_else(|| entry.label().into_owned())
        }
        None => entry.label().into_owned(),
    };
    // Two stores can hold the same bucket and key, so a row named by source says
    // which, or that the source has left the config.
    let path_text = entry.path.to_string_lossy();
    let named = crate::cloud::source::split_source_id(&path_text).0;
    let missing_source = named
        .is_some_and(|id| known_sources.is_some_and(|known| !known.iter().any(|k| k.name == id)));
    let (short, source) = match (named, known_sources) {
        (Some(id), Some(_)) if short.is_empty() && missing_source => {
            (format!("source not found: {id}"), true)
        }
        (Some(id), Some(_)) if short.is_empty() => (id.to_string(), true),
        // Nothing known yet: an ellipsis claims nothing.
        _ if short.is_empty() && entry.kind == EntryKind::Unknown => {
            (g.ellipsis.to_string(), false)
        }
        _ => (short, false),
    };
    let words = match (&entry.table, entry.kind) {
        (Some(table), _) => {
            let of = match (&entry.format_spec, table.format) {
                (Some(spec), _) => spec.clone(),
                (None, Some(format)) => format.name().to_string(),
                (None, None) => String::new(),
            };
            format!("{of} {}", table.kind).trim_start().to_string()
        }
        (None, _) if curated.is_some() => curated.unwrap_or_default().to_string(),
        (None, EntryKind::File) => match (
            &entry.format_spec,
            crate::FileFormat::from_path(&entry.path),
        ) {
            (Some(spec), _) => format!("{spec} file"),
            (None, Some(format)) => format!("{} file", format.name()),
            // Named nothing, and found by its bytes to be data.
            (None, None) => "data file".to_string(),
        },
        (None, EntryKind::Hive) => "hive table".to_string(),
        (None, EntryKind::MultiFile) => "multi-file table".to_string(),
        (None, kind) if kind.is_lake_table() => {
            format!(
                "{} table",
                kind.lake_name().unwrap_or_default().to_lowercase()
            )
        }
        (None, EntryKind::Directory) => match look {
            Some(CloudLook::Failed) => format!("? {} listing failed, Ctrl+R retries", g.middot),
            // Nothing to say yet; the row's spinner shows it is being looked into.
            Some(_) => String::new(),
            None => object_place_label(&entry.path)
                .unwrap_or("directory")
                .to_string(),
        },
        _ => String::new(),
    };
    RowLabel {
        short,
        words,
        curated: curated.is_some(),
        source,
        missing_source,
    }
}

/// How a cloud source is addressed on home: `cloud://<id>`, naming the level above
/// its buckets, which no real URL can.
pub const CLOUD_PLACE: &str = "cloud://";

/// Lines kept between the cursor and the edge of the list while it can scroll.
const SCROLL_MARGIN: usize = 2;

/// The first line of a `height`-line list that keeps `selected` on screen, given it
/// started at `top`. Scrolls only enough to keep the cursor [`SCROLL_MARGIN`] lines
/// from an edge, and never leaves empty space below the last line.
pub(crate) fn settle_top(top: usize, selected: usize, height: usize, total: usize) -> usize {
    if height == 0 {
        return top.min(selected);
    }
    // A short list keeps the cursor reachable at every line.
    let margin = SCROLL_MARGIN.min((height - 1) / 2);
    let top = if selected < top + margin {
        selected.saturating_sub(margin)
    } else if selected + margin >= top + height {
        selected + margin + 1 - height
    } else {
        top
    };
    top.min(total.saturating_sub(height))
}

/// The place for one cloud source.
pub fn cloud_place(id: &str) -> PathBuf {
    PathBuf::from(format!("{CLOUD_PLACE}{id}"))
}

/// The source ID of a `cloud://<id>` place.
pub fn cloud_source_id(path: &Path) -> Option<String> {
    let text = path.to_string_lossy();
    let id = text.strip_prefix(CLOUD_PLACE)?.trim_end_matches('/');
    (!id.is_empty() && !id.contains('/')).then(|| id.to_string())
}

/// The source ID and account of a `cloud://<id>/<account>` place: an Azure storage
/// account, which has no URL of its own.
pub fn cloud_account(path: &Path) -> Option<(String, String)> {
    let text = path.to_string_lossy();
    let rest = text.strip_prefix(CLOUD_PLACE)?.trim_end_matches('/');
    let (id, account) = rest.split_once('/')?;
    (!id.is_empty() && !account.is_empty() && !account.contains('/'))
        .then(|| (id.to_string(), account.to_string()))
}

/// Whether `url` is `root` or inside it, for URLs of any provider.
fn within(url: &str, root: &str) -> bool {
    #[cfg(feature = "cloud")]
    {
        crate::cloud::cloud_sources::is_within(url, root)
    }
    #[cfg(not(feature = "cloud"))]
    {
        let (url, root) = (url.trim_end_matches('/'), root.trim_end_matches('/'));
        url == root || url.strip_prefix(root).is_some_and(|r| r.starts_with('/'))
    }
}

/// Whether two locations are one place, however a trailing slash, an Azure URL or a
/// local path's separators and drive letter are spelled.
fn same_place(a: &Path, b: &Path) -> bool {
    place_key(a) == place_key(b)
}

/// A location as [`same_place`] compares it.
fn place_key(path: &Path) -> String {
    let text = path.to_string_lossy();
    // A catalog may write `C:/data/x.csv` where a listing or a recent has
    // `C:\data\x.csv`; compare local paths by their components.
    if !text.contains("://") {
        return crate::config::path_place(path).display().to_string();
    }
    #[cfg(feature = "cloud")]
    {
        crate::cloud::source::canonical_cloud_place(&text)
    }
    #[cfg(not(feature = "cloud"))]
    {
        text.trim_end_matches('/').to_string()
    }
}

/// The catalogs' datasets and bookmarks indexed by place, for per-row lookups.
#[derive(Debug, Default)]
pub struct CatalogPlaces {
    /// Catalog and dataset, by [`place_key`]: the first listed of two at one place.
    datasets: std::collections::HashMap<String, (usize, usize)>,
    /// Catalog, dataset and bookmark, by [`place_key`].
    bookmarks: std::collections::HashMap<String, (usize, usize, usize)>,
    /// How many datasets and bookmarks the catalogs held; catalogs set other than by
    /// [`HomeState::set_catalogs`] are scanned instead.
    counted: (usize, usize),
}

impl CatalogPlaces {
    fn of(catalogs: &[ShownCatalog]) -> Self {
        let mut places = CatalogPlaces {
            counted: Self::count(catalogs),
            ..Default::default()
        };
        for (c, catalog) in catalogs.iter().enumerate() {
            for (d, dataset) in catalog.datasets.iter().enumerate() {
                places
                    .datasets
                    .entry(place_key(&dataset.location))
                    .or_insert((c, d));
                for (b, (_, place)) in dataset.bookmarks.iter().enumerate() {
                    places
                        .bookmarks
                        .entry(place_key(place))
                        .or_insert((c, d, b));
                }
            }
        }
        places
    }

    fn count(catalogs: &[ShownCatalog]) -> (usize, usize) {
        let datasets = catalogs.iter().flat_map(|c| c.datasets.iter());
        (
            datasets.clone().count(),
            datasets.map(|d| d.bookmarks.len()).sum(),
        )
    }

    /// Whether the index answers for `catalogs`: a lookup it misses is a miss.
    fn indexes(&self, catalogs: &[ShownCatalog]) -> bool {
        self.counted == Self::count(catalogs)
    }
}

/// What is left of `url` below `root`, which it is [`within`].
fn within_rest(url: &str, root: &str) -> String {
    let canonical = |u: &str| match crate::cloud::source::azure_parts(u) {
        Some((account, container, path)) => {
            crate::cloud::source::azure_url(&account, &container, &path)
        }
        None => u.to_string(),
    };
    let (url, root) = (canonical(url), canonical(root));
    url.strip_prefix(root.trim_end_matches('/'))
        .unwrap_or("")
        .to_string()
}

/// Whether `path` is a place in an object store: `s3://`, `gs://`, or Azure.
pub fn is_object_store_url(path: &Path) -> bool {
    let text = path.to_string_lossy();
    let scheme = text
        .split_once("://")
        .map(|(s, _)| s.to_ascii_lowercase())
        .unwrap_or_default();
    matches!(scheme.as_str(), "s3" | "s3a" | "gs" | "gcs")
        || crate::cloud::source::azure_parts(&text).is_some()
}

/// The URL that opens a cloud directory as one dataset: with a trailing slash, so
/// it is scanned as a prefix rather than fetched as an object.
pub fn directory_dataset_url(path: &Path) -> PathBuf {
    let text = path.to_string_lossy();
    if text.ends_with('/') {
        path.to_path_buf()
    } else {
        PathBuf::from(format!("{text}/"))
    }
}

/// The `(all files)` row: opens the browsed directory as one table whatever its
/// label, so a wrong label costs at most a keystroke. Offered in every directory,
/// local or remote, and built from the listing on screen.
fn whole_directory_row(dir: &Path, rows: &[Entry], remote: bool) -> Option<Entry> {
    // A `cloud://<id>/<account>` place is an Azure account: its children are
    // containers, with no URL to open.
    if cloud_account(dir).is_some() {
        return None;
    }
    let directories: Vec<String> = rows
        .iter()
        .filter(|r| !matches!(r.kind, EntryKind::File | EntryKind::Other))
        .map(|r| format!("{}/", r.name))
        .collect();
    let objects: Vec<(String, u64)> = rows
        .iter()
        .filter(|r| matches!(r.kind, EntryKind::File | EntryKind::Other))
        .map(|r| (r.path.to_string_lossy().into_owned(), r.size.unwrap_or(1)))
        .collect();
    // Each route classifies in its own terms: the listing drops dotted names (so a
    // local `.hoodie` would be missed by the cloud classifier), and the cloud Iceberg
    // rule is looser by design. Nothing remote is read here: a listing of a dead share
    // freezes the UI, so local classification uses rows the probe already read.
    let (kind, holds) = if remote || is_object_store_url(dir) {
        #[cfg(feature = "cloud")]
        {
            crate::cloud::cloud_browse::look_at_listing(
                &dir.to_string_lossy(),
                &directories,
                &objects,
            )
        }
        // Without the cloud feature there is no remote classifier, and reading the share
        // is what this avoids: the door is offered without a kind (losing only the lake
        // check; its label is suppressed anyway).
        #[cfg(not(feature = "cloud"))]
        {
            let _ = (&directories, &objects);
            (EntryKind::Unknown, Default::default())
        }
    } else {
        crate::home::discover::look_at_directory(dir)
    };
    // No door into a directory with nothing to open (empty, or only `_SUCCESS`).
    // Judged by what the directory holds, not only the listed rows: Spark and GBIF
    // part files have no extension and list nothing, yet open by their bytes.
    // Extensionless files and subdirectories count; files no reader takes do not.
    let openable_row = rows.iter().any(|r| r.kind != EntryKind::Other);
    if !openable_row && holds_nothing_to_open(&holds) {
        return None;
    }
    let mut entry = Entry::directory(&directory_dataset_url(dir));
    entry.kind = kind;
    // This listing's tally; it may differ from the directory's own row upstairs,
    // counted from one peek page.
    entry.holds = holds;
    entry.opens_whole_directory = true;
    entry.name = door_name(&entry, rows);
    Some(entry)
}

/// The directory a door opens, named as the section title names it: no source id
/// (`s3://lab@bucket` is `bucket`), and an Azure container by its name.
/// `file_name` rather than splitting on `/`, for the filesystem root and Windows.
fn door_base_name(dir: &Path) -> String {
    let text = dir.to_string_lossy();
    if let Some((_, container, key)) = crate::cloud::source::azure_parts(&text) {
        let leaf = key.trim_matches('/').rsplit('/').next().unwrap_or("");
        if leaf.is_empty() {
            container
        } else {
            leaf.to_string()
        }
    } else {
        let (_, plain) = crate::cloud::source::split_source_id(&text);
        std::path::Path::new(plain.as_ref())
            .file_name()
            .map(|n| n.to_string_lossy().into_owned())
            .unwrap_or_else(|| plain.into_owned())
    }
}

/// What a directory's door opens, from the kind and the tally already in hand.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DoorKind {
    /// `key=value` partitions: one table, read with its partition columns.
    Hive,
    /// Files of one format that read as one table.
    OneSchema,
    /// Files of one format whose footers or headers disagree: the read is a union.
    SchemasDiffer,
    /// One data file, beside whatever else.
    Single,
    /// A Delta, Iceberg or Hudi root, whose files are not its rows.
    Lake,
    /// More than one format, files beside subdirectories, or only subdirectories.
    Mixed,
    /// Nothing has said what is here.
    Unknown,
}

/// Which [`DoorKind`] a door is. A directory the footers or headers turned down as
/// one table is `Directory` with one format in its tally; the listing alone calls
/// it `MultiFile`.
pub fn door_kind(door: &Entry) -> DoorKind {
    let holds = &door.holds;
    match door.kind {
        EntryKind::Hive => DoorKind::Hive,
        EntryKind::MultiFile => DoorKind::OneSchema,
        k if k.is_lake_table() => DoorKind::Lake,
        EntryKind::Unknown => DoorKind::Unknown,
        _ => {
            let Some(format) = holds.one_format() else {
                return DoorKind::Mixed;
            };
            if holds.directories > 0 {
                return DoorKind::Mixed;
            }
            let files = holds.data_files();
            if files == 1 {
                return DoorKind::Single;
            }
            let mostly_data = files * 2 >= files + holds.not_read + holds.unnamed;
            let reads_many = crate::FileFormat::from_name(format)
                .is_some_and(crate::FileFormat::reads_many_files);
            if mostly_data && reads_many {
                DoorKind::SchemasDiffer
            } else {
                DoorKind::Mixed
            }
        }
    }
}

/// Whether stepping into a directory puts the cursor on its door: only when the door
/// opens the one dataset the directory's own `Enter` opens; elsewhere it would start
/// an unasked combined read.
pub fn door_lands(door: &Entry) -> bool {
    matches!(door_kind(door), DoorKind::Hive | DoorKind::OneSchema)
}

/// A format as prose names it, by the name the listing counted: `Parquet`, `CSV`.
fn format_title(name: &str) -> String {
    crate::FileFormat::from_name(name)
        .map_or_else(|| name.to_ascii_uppercase(), |f| f.title().to_string())
}

/// The partition keys a hive door names: from the footer pass's layout, else the
/// `key=value` names listed (in a bucket, the levels seen so far).
fn door_keys(door: &Entry, rows: &[Entry]) -> Vec<String> {
    if let Some(layout) = door.cost.partitions.as_ref()
        && !layout.keys.is_empty()
    {
        return layout.keys.clone();
    }
    let mut keys: Vec<String> = Vec::new();
    for row in rows {
        if let Some((key, _)) = row.name.split_once('=')
            && !key.is_empty()
            && !keys.iter().any(|k| k == key)
        {
            keys.push(key.to_string());
        }
    }
    keys
}

/// The door's name: the directory, and what `Enter` on it opens.
pub fn door_name(door: &Entry, rows: &[Entry]) -> String {
    let name = door_base_name(&door.path);
    let holds = &door.holds;
    let more = if holds.truncated { "+" } else { "" };
    let files = |format: &str| {
        let count = holds.data_files();
        let word = if count == 1 { "file" } else { "files" };
        format!("{count}{more} {} {word}", format_title(format))
    };
    let what = match door_kind(door) {
        DoorKind::Hive => {
            let keys = door_keys(door, rows);
            if keys.is_empty() {
                "hive table".to_string()
            } else {
                format!("hive table: {}", keys.join(", "))
            }
        }
        DoorKind::OneSchema => match (holds.model_weights(), holds.one_format()) {
            (Some((format, count)), _) => {
                let word = if count == 1 { "file" } else { "files" };
                format!("model, {count}{more} {} {word}", format_title(format))
            }
            (None, Some(format)) => format!("{}, one schema", files(format)),
            (None, None) => "one table".to_string(),
        },
        DoorKind::SchemasDiffer => {
            format!(
                "{}, schemas differ",
                files(holds.one_format().unwrap_or(""))
            )
        }
        DoorKind::Single => files(holds.one_format().unwrap_or("")),
        DoorKind::Lake => format!(
            "{} files, not the table",
            door.kind.lake_name().unwrap_or_default()
        ),
        DoorKind::Mixed => "all files, mixed".to_string(),
        DoorKind::Unknown => "all files".to_string(),
    };
    format!("{name} ({what})")
}

/// What `Enter` on a door that is not one table reads and leaves out, for the
/// details pane. Locally: the commonest format's files directly inside, or a whole
/// Parquet scan when there are subdirectories or no own files. In an object store:
/// the prefix scanned whole in its commonest format.
pub fn door_reads(door: &Entry) -> Option<(String, Option<String>)> {
    if !matches!(door_kind(door), DoorKind::Mixed | DoorKind::Single) {
        return None;
    }
    let holds = &door.holds;
    let more = if holds.truncated { "+" } else { "" };
    let plain = holds.directories.saturating_sub(holds.partitions);
    let directories = |n: usize| {
        let word = if n == 1 { "directory" } else { "directories" };
        format!("{n}{more} {word}")
    };
    let Some((format, count)) = holds.formats.first() else {
        return (holds.directories > 0).then(|| ("every Parquet file below".to_string(), None));
    };
    // A prefix in an object store is scanned whole, subdirectories included.
    let remote = is_object_store_url(&door.path);
    let below = remote || holds.partitions > 0 || (holds.formats.len() == 1 && format == "parquet");
    let below = below && holds.directories > 0;
    let reads = if below && remote {
        format!("every {format} file below")
    } else if below {
        "every Parquet file below".to_string()
    } else {
        format!("{count}{more} {format}")
    };
    let mut skips: Vec<String> = holds
        .formats
        .iter()
        .skip(1)
        .map(|(name, n)| format!("{n}{more} {name}"))
        .collect();
    if !below && plain > 0 {
        skips.push(directories(plain));
    }
    Some((reads, (!skips.is_empty()).then(|| skips.join(", "))))
}

/// Name files a format spec's glob matches as data, under the spec's name, from the
/// name alone. Files a spec's magic matches were named by the scan's sniff.
pub fn name_by_spec(formats: &crate::formats::Registry, rows: &mut [Entry]) {
    if formats.is_empty() {
        return;
    }
    let mut named = false;
    for row in rows
        .iter_mut()
        .filter(|r| r.kind == EntryKind::Other && r.format_spec.is_none())
    {
        if let Some(spec) = formats.by_glob(&row.path, false).first() {
            discover::name_spec_file(row, spec);
            named = true;
        }
    }
    // Delimited text a delimited spec's glob names keeps its place and gains the name.
    for row in rows.iter_mut().filter(|r| {
        r.kind == EntryKind::File
            && r.format_spec.is_none()
            && discover::data_format(&r.path).is_some_and(|f| f.separator().is_some())
    }) {
        if let Some(spec) = formats
            .by_glob(&row.path, false)
            .into_iter()
            .find(|s| s.is_delimited())
        {
            row.format_spec = Some(spec.name.clone());
        }
    }
    // Data sorts first, and these rows are data now.
    if named {
        discover::sort_entries(rows);
    }
}

/// Whether a directory holds nothing a `(all files)` row could read. Shared by the
/// door and the details pane so they agree.
pub fn holds_nothing_to_open(holds: &discover::Holds) -> bool {
    holds.formats.is_empty() && holds.directories == 0 && holds.unnamed == 0
}

/// Whether `path` is one of datui's own `cloud://` places rather than a real location.
pub fn is_cloud_place(path: &Path) -> bool {
    path.to_string_lossy().starts_with(CLOUD_PLACE)
}

/// Whether `path` is the root of a bucket: `s3://bucket`, `s3://<id>@bucket`,
/// `gs://bucket`, with no prefix.
fn is_bucket_root(path: &Path) -> bool {
    let text = path.to_string_lossy();
    if let Some((_, _, key)) = crate::cloud::source::azure_parts(&text) {
        return key.trim_matches('/').is_empty();
    }
    let Some((scheme, rest)) = text.split_once("://") else {
        return false;
    };
    matches!(scheme, "s3" | "s3a" | "gs" | "gcs") && {
        let rest = rest.trim_end_matches('/');
        !rest.is_empty() && !rest.contains('/')
    }
}

/// The location one level up from `path`, or `None` at the top. A URL's top is its
/// bucket or host (`Path::parent` would make `gs://bucket` into `gs:`).
pub fn parent_location(path: &Path) -> Option<PathBuf> {
    if !matches!(
        crate::cloud::source::input_source(path),
        crate::cloud::source::InputSource::Local(_)
    ) {
        let s = path.to_string_lossy();
        let (scheme, rest) = s.split_once("://")?;
        let (up, _) = rest.trim_end_matches('/').rsplit_once('/')?;
        return Some(PathBuf::from(format!("{scheme}://{up}")));
    }
    path.parent()
        .filter(|p| !p.as_os_str().is_empty() && *p != path)
        .map(Path::to_path_buf)
}

/// Whether reading `path` could block: an object-store or HTTP URL, or a network
/// filesystem. Decides what home may touch on the UI thread; answered from the
/// string and mount table alone.
pub fn is_remote_path(path: &Path) -> bool {
    is_cloud_place(path)
        || !matches!(
            crate::cloud::source::input_source(path),
            crate::cloud::source::InputSource::Local(_)
        )
        || is_network_path(path)
}

/// Whether `path` is on a network filesystem by the mount table (longest matching
/// mount point). False where the table is unavailable: a hint, never a gate. Uses
/// [`crate::home::locality::Mounts::cached`], since this is asked per row per frame.
pub fn is_network_path(path: &Path) -> bool {
    crate::home::locality::Mounts::cached().is_network(path)
}

/// The mount-table logic against a fixture, for tests (e.g. an NFS share shadowing
/// an autofs entry at the same path).
#[doc(hidden)]
pub fn network_fs_for_test(mountinfo: &str, path: &Path) -> bool {
    crate::home::locality::Mounts::parse(mountinfo).is_network(path)
}

/// A place datui will look, and whether it can currently be read.
#[derive(Debug, Clone)]
pub struct Root {
    pub path: PathBuf,
    pub origin: RootOrigin,
    /// True when the root is on a network filesystem.
    pub network: bool,
    /// False when the directory cannot be read (an unmounted NAS, a deleted scratch
    /// dir); shown, not hidden.
    pub available: bool,
}

/// A titled group of rows on the home screen.
#[derive(Debug, Clone, Default)]
pub struct Section {
    pub title: String,
    /// The section's state at the far end of the rule: its filesystem, `first 5000` for
    /// a cut listing, what a search covered. Why it exists is `origin`.
    pub subtitle: Option<String>,
    /// Why a path-titled section is here (`current directory`, `configured`), as a chip
    /// beside the count.
    pub origin: Option<&'static str>,
    /// The directory a path-titled section lists (a root, or the browsed directory);
    /// the title is abbreviated and cannot be turned back into a path.
    pub root: Option<PathBuf>,
    pub rows: Vec<Entry>,
    /// The row opening this section's directory as one table, kept out of `rows`: its
    /// path is the directory's own, so among the rows it would collide in every
    /// path-keyed map with the directory's row one level up. Nothing that walks `rows`
    /// or matches [`Row::Entry`] can reach it.
    pub door: Option<Entry>,
    /// Set when a root could not be read, so the UI can say why it is empty.
    pub unavailable: bool,
    /// What to say instead of "unavailable", when there is more to say (a refused bucket
    /// listing's reason and fix).
    pub unavailable_note: Option<String>,
    /// Starts folded unless opened: places that are context rather than the reason you
    /// came (directories promoted from recents, the desktop's list).
    pub folded_by_default: bool,
    /// The remote root whose background probe fills this section in.
    pub remote_root: Option<PathBuf>,
    /// The probe had not answered when this listing was built: empty means "wait".
    pub waiting: bool,
    /// Rows are grouped under the place each lives in, with a place row. Set on
    /// `RECENT`, whose rows come from anywhere.
    pub grouped_by_place: bool,
    /// What the dataset index remembers each place to be, for a grouped section's place
    /// rows; from the cache, never a read.
    pub place_labels: std::collections::HashMap<PathBuf, String>,
}

impl Section {
    /// A section of `rows` under `title`, everything else at its default.
    pub fn titled(title: impl Into<String>, rows: Vec<Entry>) -> Self {
        Section {
            title: title.into(),
            rows,
            ..Default::default()
        }
    }
}

/// Where a cloud source's listing stands.
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub enum CloudStatus {
    /// Asked, and no answer yet.
    #[default]
    Listing,
    /// Not asked and not listed before; its rows, if any, are the config's named
    /// buckets. Entering it or Ctrl+R lists it.
    Unlisted,
    /// Listed, now or on an earlier run.
    Listed,
    /// The listing was refused or never answered: `short` for the row, `detail` (what
    /// happened, how to fix) for the details pane.
    Failed { short: String, detail: String },
}

/// One cloud source on home: a row under `CLOUD` and its bucket list. Kept apart
/// from [`Section`] to survive rebuilds: re-listing buckets would be a billed round
/// trip per keystroke.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct CloudSource {
    /// The source ID, as in `[[cloud.connections]]` and `s3://<id>@bucket`.
    pub id: String,
    /// The row's name.
    pub label: String,
    /// The API spoken.
    pub api: crate::cloud::source::ProviderKind,
    /// The account, endpoint or project, and where the login came from.
    pub note: String,
    /// Bucket URLs, most useful first: `s3://bucket`, `s3://<id>@bucket`, `gs://bucket`.
    pub buckets: Vec<PathBuf>,
    pub status: CloudStatus,
    /// When the buckets were listed, when they were.
    pub listed_at: Option<std::time::SystemTime>,
    /// A listing is out for buckets already on screen from an earlier run.
    pub refreshing: bool,
    /// Listed, or being listed, this session. Entering a source that is not lists it.
    pub asked: bool,
    /// `key  value` lines for the details pane: endpoint, region, login.
    pub details: Vec<(String, String)>,
    /// Lines for the details pane of places inside the source: an Azure account's
    /// subscription, region and namespace.
    pub place_details: std::collections::HashMap<PathBuf, Vec<(String, String)>>,
}

impl CloudSource {
    /// What the row says instead of a size: the bucket count, or why there is none.
    pub fn count_text(&self) -> String {
        match &self.status {
            CloudStatus::Failed { short, .. } if self.buckets.is_empty() => short.clone(),
            CloudStatus::Listing if self.buckets.is_empty() => String::new(),
            CloudStatus::Unlisted if self.buckets.is_empty() => "not listed".to_string(),
            _ => {
                let (one, many) = match self.api {
                    crate::cloud::source::ProviderKind::Azure => ("account", "accounts"),
                    crate::cloud::source::ProviderKind::Gcs => ("project", "projects"),
                    crate::cloud::source::ProviderKind::S3 => ("bucket", "buckets"),
                };
                match self.buckets.len() {
                    0 => format!("no {many}"),
                    1 => format!("1 {one}"),
                    n => format!("{n} {many}"),
                }
            }
        }
    }

    /// Mark a listing as out: a spinner in place of the count when there are no buckets
    /// to show yet, beside it when there are.
    pub fn begin_listing(&mut self) {
        self.asked = true;
        if self.status == CloudStatus::Unlisted && self.buckets.is_empty() {
            self.status = CloudStatus::Listing;
        } else {
            self.refreshing = true;
        }
    }

    /// Whether the row should show a spinner.
    pub fn busy(&self) -> bool {
        self.refreshing || (self.status == CloudStatus::Listing && self.buckets.is_empty())
    }

    /// Whether the row reports a failure.
    pub fn failed(&self) -> bool {
        matches!(self.status, CloudStatus::Failed { .. })
    }
}

/// A catalog as the home screen shows it: a section of named datasets, local and remote
/// alike.
#[derive(Debug, Clone, PartialEq)]
pub struct ShownCatalog {
    /// The catalog's id, as `[home] hide` names it.
    pub id: String,
    /// The section's title.
    pub label: String,
    /// `catalog.toml`, a listed file, or the bundled catalog.
    pub origin: crate::home::catalog::Origin,
    /// What the catalog says it is, for its heading's details.
    pub description: String,
    /// The file it was read from; none for the bundled one.
    pub file: Option<PathBuf>,
    pub datasets: Vec<ShownDataset>,
    /// Left out for a mistake: the one line its section says instead of rows.
    pub broken: Option<String>,
}

/// One dataset of a [`ShownCatalog`].
#[derive(Debug, Clone, Default, PartialEq)]
pub struct ShownDataset {
    /// The row's name.
    pub name: String,
    /// The local path with `~` and `$VAR` expanded, or the URL.
    pub location: PathBuf,
    /// `key  value` lines for the details pane.
    pub details: Vec<(String, String)>,
    /// What the catalog says a remote file weighs, until it is measured.
    pub size: Option<u64>,
    /// What its columns mean, when the catalog says.
    pub codebook: Option<std::sync::Arc<crate::home::codebook::Codebook>>,
    /// Places inside it to start from, by name, listed under its row.
    pub bookmarks: Vec<(String, PathBuf)>,
    /// The entry as its catalog writes it: what the Documentation view shows.
    pub entry: std::sync::Arc<crate::home::catalog::Dataset>,
}

impl ShownCatalog {
    /// A catalog as the home screen shows it.
    pub fn from_catalog(catalog: &crate::home::catalog::Catalog) -> Self {
        Self {
            id: catalog.id.clone(),
            label: catalog.label.clone(),
            origin: catalog.origin,
            description: catalog.description.clone(),
            file: catalog.file.clone(),
            datasets: catalog
                .datasets
                .iter()
                .map(|dataset| {
                    let location = dataset.location();
                    let mut details: Vec<(String, String)> = [
                        ("about", &dataset.description),
                        ("publisher", &dataset.publisher),
                        ("license", &dataset.license),
                        ("homepage", &dataset.homepage),
                        ("documentation", &dataset.documentation),
                    ]
                    .into_iter()
                    .filter(|(_, value)| !value.is_empty())
                    .map(|(key, value)| (key.to_string(), value.clone()))
                    .collect();
                    match &dataset.url {
                        None => details.push(("path".to_string(), display_path(&location))),
                        Some(url) => {
                            details.push(("url".to_string(), url.clone()));
                            details.push(("login".to_string(), login_of(dataset)));
                        }
                    }
                    ShownDataset {
                        name: dataset.name.clone(),
                        location,
                        details,
                        size: dataset.size,
                        codebook: crate::home::codebook::Codebook::of(dataset)
                            .map(std::sync::Arc::new),
                        bookmarks: dataset
                            .bookmarks
                            .iter()
                            .map(|(name, path)| (name.clone(), dataset.bookmark_location(path)))
                            .collect(),
                        entry: std::sync::Arc::new(dataset.clone()),
                    }
                })
                .collect(),
            broken: None,
        }
    }

    /// A catalog file left out for a mistake, as a section that says what is wrong.
    pub fn from_broken(broken: &crate::home::catalog::Broken) -> Self {
        Self {
            id: broken.id.clone(),
            label: broken.id.clone(),
            origin: broken.origin,
            description: String::new(),
            file: None,
            datasets: Vec::new(),
            broken: Some(broken.callout()),
        }
    }

    /// The chip beside the section's title: where its datasets are written. Each is
    /// one of [`CATALOG_ORIGINS`].
    pub fn origin_note(&self) -> &'static str {
        match self.origin {
            crate::home::catalog::Origin::Mine => "catalog.toml",
            crate::home::catalog::Origin::Listed | crate::home::catalog::Origin::Folder => {
                "catalog"
            }
            crate::home::catalog::Origin::Bundled => BUNDLED_ORIGIN,
        }
    }
}

/// The chip on the bundled catalog's section.
pub const BUNDLED_ORIGIN: &str = "comes with datui";

/// The chips a catalog's section carries, and nothing else does.
pub const CATALOG_ORIGINS: [&str; 3] = ["catalog.toml", "catalog", BUNDLED_ORIGIN];

/// Whether a section's origin chip says it is a catalog.
pub fn is_catalog_origin(origin: &str) -> bool {
    CATALOG_ORIGINS.contains(&origin)
}

/// How a catalog URL is read, in words: what `auth` and `connection` say.
pub fn login_of(dataset: &crate::home::catalog::Dataset) -> String {
    match dataset.object_store_auth() {
        Some(crate::config::DatasetAuth::Connection(connection)) => connection,
        Some(crate::config::DatasetAuth::Anonymous) | None => "none".to_string(),
        Some(crate::config::DatasetAuth::Auto) => "auto".to_string(),
    }
}

/// The catalogs home shows, in order. The bundled catalog keeps only what this
/// build can open (all its datasets are remote); a user's catalog is shown whole.
/// An empty catalog has no section.
pub fn catalogs(config: &crate::config::AppConfig) -> Vec<ShownCatalog> {
    let mut out: Vec<ShownCatalog> = config
        .shown_catalogs()
        .iter()
        .filter_map(|catalog| {
            let mut shown = ShownCatalog::from_catalog(catalog);
            if catalog.origin == crate::home::catalog::Origin::Bundled {
                shown
                    .datasets
                    .retain(|d| crate::cloud::source::opens_in_this_build(&d.location));
            }
            (!shown.datasets.is_empty()).then_some(shown)
        })
        .collect();
    // A broken file's section says so, before the bundled catalog, unless it is hidden.
    let at = out
        .iter()
        .position(|c| c.origin == crate::home::catalog::Origin::Bundled)
        .unwrap_or(out.len());
    let broken: Vec<ShownCatalog> = config
        .broken_catalogs
        .iter()
        .filter(|b| !config.home.hide.contains(&b.id))
        .map(ShownCatalog::from_broken)
        .collect();
    out.splice(at..at, broken);
    out
}

/// The row for one catalog dataset. Reads nothing but a local path's directory
/// entry; a remote dataset is named by its URL.
fn catalog_entry(
    dataset: &ShownDataset,
    network_check: fn(&Path) -> bool,
    missing: &mut std::collections::HashSet<PathBuf>,
) -> Entry {
    let path = &dataset.location;
    let local = matches!(
        crate::cloud::source::input_source(path),
        crate::cloud::source::InputSource::Local(_)
    );
    let mut entry = if is_object_store_url(path) {
        if names_a_file(path) {
            entry_for_path(path, true)
        } else {
            Entry::directory(path)
        }
    } else if !local || network_check(path) {
        entry_for_path(path, true)
    } else if path.exists() {
        entry_for_path(path, false)
    } else {
        missing.insert(path.clone());
        let mut entry = entry_for_path(path, true);
        entry.kind = EntryKind::Unknown;
        entry
    };
    entry.name = dataset.name.clone();
    entry
}

/// The column notes of the catalog dataset `path` is or is inside (the innermost),
/// among datasets with notes.
pub fn codebook_for(
    catalogs: &[ShownCatalog],
    path: &Path,
) -> Option<std::sync::Arc<crate::home::codebook::Codebook>> {
    let text = path.to_string_lossy();
    catalogs
        .iter()
        .flat_map(|c| c.datasets.iter())
        .filter(|d| d.codebook.is_some())
        .filter(|d| d.location == path || within(&text, &d.location.to_string_lossy()))
        .max_by_key(|d| d.location.to_string_lossy().trim_end_matches('/').len())
        .and_then(|d| d.codebook.clone())
}

/// The catalog entry `path` is or is inside, with its catalog's label: the innermost,
/// or the first listed of two at one place.
pub fn catalog_entry_for(
    catalogs: &[ShownCatalog],
    path: &Path,
) -> Option<(String, std::sync::Arc<crate::home::catalog::Dataset>)> {
    let text = path.to_string_lossy();
    catalogs
        .iter()
        .flat_map(|c| c.datasets.iter().map(move |d| (c, d)))
        .filter(|(_, d)| {
            d.location == path
                || same_place(&d.location, path)
                || within(&text, &d.location.to_string_lossy())
        })
        .rev()
        .max_by_key(|(_, d)| d.location.to_string_lossy().trim_end_matches('/').len())
        .map(|(c, d)| (c.label.clone(), d.entry.clone()))
}

/// The row for a bookmark inside one of a catalog's datasets.
fn bookmark_entry(name: &str, place: &Path, network_check: fn(&Path) -> bool) -> Entry {
    let local = matches!(
        crate::cloud::source::input_source(place),
        crate::cloud::source::InputSource::Local(_)
    );
    let mut entry = if is_object_store_url(place) && !names_a_file(place) {
        Entry::directory(place)
    } else {
        entry_for_path(place, !local || network_check(place) || !place.exists())
    };
    entry.name = name.to_string();
    entry
}

/// A catalog's section.
fn catalog_section(
    catalog: &ShownCatalog,
    network_check: fn(&Path) -> bool,
    missing: &mut std::collections::HashSet<PathBuf>,
) -> Section {
    // Each dataset, and under it its bookmarks.
    let rows = catalog
        .datasets
        .iter()
        .flat_map(|dataset| {
            std::iter::once(catalog_entry(dataset, network_check, missing)).chain(
                dataset
                    .bookmarks
                    .iter()
                    .map(|(name, place)| bookmark_entry(name, place, network_check)),
            )
        })
        .collect();
    Section {
        origin: Some(catalog.origin_note()),
        unavailable: catalog.broken.is_some(),
        unavailable_note: catalog.broken.clone(),
        ..Section::titled(catalog.label.clone(), rows)
    }
}

/// What measuring a dataset yielded; each part absent when unknowable without
/// reading the data.
#[derive(Debug, Clone, Default, PartialEq)]
pub struct Measured {
    pub rows: Option<usize>,
    pub cols: Option<usize>,
    /// Whether `cols` is a floor rather than a total. See [`crate::home::discover::Entry`].
    pub cols_sampled: bool,
    pub size: Option<u64>,
    /// Column names, when the format gave them up for free.
    pub columns: Vec<String>,
    /// What opening it costs: compression, layout, partitioning, carried to the screen
    /// with the row count.
    pub cost: crate::home::discover::Cost,
    /// What the footers said it is when that differs from its filenames (separate tables
    /// make a plain directory); usually `None`. See [`crate::home::discover::enrich`].
    pub kind: Option<crate::home::discover::EntryKind>,
    /// What one listing found, which the row's label says; only the classify pass counts
    /// a local directory.
    pub holds: crate::home::discover::Holds,
}

/// How rows are ordered within each section.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub enum SortMode {
    /// Each section's natural order: recency under Recent, name under a directory.
    #[default]
    Natural,
    /// Largest first: the question is "what is big in here".
    Size,
    /// Most recently changed first: the question is "what moved".
    Modified,
    /// Most rows first.
    Rows,
}

impl SortMode {
    /// What this mode does in a section, since `Natural` means recency or name depending
    /// on where the cursor is.
    pub fn label_in(self, section_is_recency_ordered: bool) -> &'static str {
        match self {
            SortMode::Natural if section_is_recency_ordered => "recent",
            SortMode::Natural => "name",
            SortMode::Size => "size",
            SortMode::Modified => "modified",
            SortMode::Rows => "rows",
        }
    }

    pub fn next(self) -> Self {
        match self {
            SortMode::Natural => SortMode::Size,
            SortMode::Size => SortMode::Modified,
            SortMode::Modified => SortMode::Rows,
            SortMode::Rows => SortMode::Natural,
        }
    }
}

/// One line of the home screen; headers are selectable to fold. Places and `more`
/// rows are view rows, not entries: an [`Entry`]'s kind triggers probes,
/// measurement and caching, none of which may happen to a place.
#[derive(Debug, Clone)]
pub enum Row<'a> {
    Header {
        section: usize,
        /// Rows this section holds under the current filter.
        matches: usize,
        collapsed: bool,
    },
    Entry {
        section: usize,
        entry: &'a Entry,
        /// Drawn two cells in, under the place row above it.
        nested: bool,
        /// How it answers the filter, for the marks the row is drawn with.
        hit: Hit,
    },
    /// The directory or prefix the entries below it live in, under `RECENT`.
    Place {
        section: usize,
        path: PathBuf,
        /// What the place was last found to be (`hive`, `12 parquet`), from the dataset
        /// index only, never a read.
        label: Option<String>,
        /// The filesystem it is on, or the object store's scheme.
        source: Option<String>,
        /// How many recents live there, whether or not the filter shows them.
        held: usize,
    },
    /// The door opening the browsed directory as one table; see [`Section::door`]. Not
    /// an `Entry` row, since it carries the directory's own path.
    Door { section: usize, entry: &'a Entry },
    /// What a cap is hiding: `RECENT`'s, `… 13 more in 5 places`, or a directory's
    /// at the root listing, `… 4,958 more` (`places` is 0).
    More {
        section: usize,
        hidden: usize,
        places: usize,
        /// Sorted by rows with some hidden rows still being measured, so the order may
        /// change.
        measuring: bool,
    },
    /// The last row of a browsed directory hiding unreadable files (`… 10 files with no
    /// reader`), so a directory of notes does not look empty. `Enter` shows them, as
    /// `Ctrl+A` does.
    Hidden { section: usize, count: usize },
    /// The way up, first in a directory's section: `..`; Enter goes to the parent, as
    /// Backspace does.
    Up { section: usize },
}

impl Row<'_> {
    pub fn section(&self) -> usize {
        match self {
            Row::Header { section, .. }
            | Row::Entry { section, .. }
            | Row::Door { section, .. }
            | Row::Place { section, .. }
            | Row::More { section, .. }
            | Row::Hidden { section, .. }
            | Row::Up { section } => *section,
        }
    }
}

/// How a row answers the filter: its score, and whether its name or a column matched.
/// Scored when rows are listed; the matched characters are found when drawn, for the
/// rows on screen rather than every row of thousands.
#[derive(Debug, Clone, Copy, Default, PartialEq)]
pub struct Hit {
    pub score: i32,
    /// Index into the entry's `columns` of the matching column, when the name did not
    /// match.
    pub column: Option<usize>,
}

impl Hit {
    /// The column that matched, when the name did not.
    pub fn column_of<'a>(&self, entry: &'a Entry) -> Option<&'a str> {
        self.column
            .and_then(|i| entry.columns.get(i))
            .map(String::as_str)
    }

    /// Character positions to mark for `filter`: in the name, or in the matched
    /// column's name. From the same alignment that scored it.
    pub fn positions(&self, filter: &str, entry: &Entry) -> Vec<usize> {
        match self.column_of(entry) {
            Some(column) => substring_positions(filter, column),
            None => fuzzy_positions(filter, &entry.name),
        }
    }
}

/// [`HomeState::visible`]'s rows, cached until their inputs change: building scores
/// every row and sorts each section, and every pass reads them. `HomeState`'s own
/// row-changing methods drop it; fields set elsewhere (filter, sort, folds) are
/// compared on every read.
#[derive(Debug, Default)]
pub struct RowsCache {
    built: std::cell::RefCell<Option<View>>,
    builds: std::cell::Cell<usize>,
    /// The last build's hits, kept for the next build when only some rows changed
    /// (measured, or `Found` replaced): see [`HomeState::rows_changed`].
    rescored: std::cell::RefCell<Option<Hits>>,
}

/// Every row's match against the filter, by section and row index; `None` where the row
/// does not match. A section with no hits is scored at the next build.
#[derive(Debug, Clone)]
struct Hits {
    filter: String,
    sections: Vec<Option<Vec<Option<Hit>>>>,
}

/// The rows as last built, and what they were built from.
#[derive(Debug)]
struct View {
    key: ViewKey,
    slots: Vec<Slot>,
    hits: Hits,
    /// See [`HomeState::has_any_dataset`].
    has_dataset: bool,
}

/// The rows' inputs not changed only through [`HomeState`]'s methods, plus the
/// sections' shape, so a stale index is never read.
#[derive(Debug, PartialEq)]
struct ViewKey {
    filter: String,
    sort: SortMode,
    hide_unreadable: bool,
    recent_expanded: bool,
    shown_whole: std::collections::HashSet<PathBuf>,
    view_height: usize,
    browsing: Option<PathBuf>,
    folds: std::collections::HashMap<String, bool>,
    shape: Vec<(usize, bool)>,
}

impl ViewKey {
    fn of(home: &HomeState) -> Self {
        ViewKey {
            filter: home.filter.clone(),
            sort: home.sort,
            hide_unreadable: home.hide_unreadable,
            recent_expanded: home.recent_expanded,
            shown_whole: home.shown_whole.clone(),
            view_height: home.view_height,
            browsing: home.browsing.clone(),
            folds: home.folds.clone(),
            shape: (home.sections.iter())
                .map(|s| (s.rows.len(), s.door.is_some()))
                .collect(),
        }
    }

    /// Whether `home` would make this key, compared without cloning.
    fn matches(&self, home: &HomeState) -> bool {
        self.filter == home.filter
            && self.sort == home.sort
            && self.hide_unreadable == home.hide_unreadable
            && self.recent_expanded == home.recent_expanded
            && self.shown_whole == home.shown_whole
            && self.view_height == home.view_height
            && self.browsing == home.browsing
            && self.folds == home.folds
            && self.shape.iter().copied().eq(home
                .sections
                .iter()
                .map(|s| (s.rows.len(), s.door.is_some())))
    }
}

/// A built row, by index into the sections for the rows that are entries.
#[derive(Debug)]
enum Slot {
    /// A row that holds no entry, as it is drawn.
    Plain(Row<'static>),
    Entry {
        section: usize,
        index: usize,
        nested: bool,
        hit: Hit,
    },
    Door {
        section: usize,
    },
}

/// The place a recent lives in: its directory or object-store prefix; a bare bucket
/// or host is its own place.
pub fn place_of(path: &Path) -> PathBuf {
    parent_location(path).unwrap_or_else(|| path.to_path_buf())
}

/// Whether a place can be listed: a directory or an object-store prefix. An HTTP
/// server has no listing, so a URL recent's place is a heading, not a door.
pub fn place_is_browsable(path: &Path) -> bool {
    is_cloud_place(path)
        || is_object_store_url(path)
        || matches!(
            crate::cloud::source::input_source(path),
            crate::cloud::source::InputSource::Local(_)
        )
}

/// A row's identity apart from its index, to find it again after a rebuild or a cap
/// change: the cursor's index points at different rows whenever a listing lands or
/// the terminal resizes, which would make `Enter` open the wrong row.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum RowKey {
    Header(String),
    Entry(PathBuf),
    /// The door, by the directory it opens; keyed as an `Entry` it would land on the
    /// directory's row.
    Door(PathBuf),
    Place(PathBuf),
    More(String),
    Hidden(String),
    Up(String),
}

/// Home screen state.
#[derive(Debug)]
pub struct HomeState {
    pub sections: Vec<Section>,
    /// Fuzzy filter over every row in every section.
    pub filter: String,
    /// The filter kept from before a dataset opened, shown selected: the next character
    /// replaces it and `~` opens the path prompt. Any other key keeps it.
    pub filter_selected: bool,
    /// The most search matches listed under `Found`: `[home.search] max_results`.
    pub search_limit: usize,
    /// Hide files datui has no reader for (default); `Ctrl+A` flips it.
    pub hide_unreadable: bool,
    /// The format specs on the search path: files one names by glob list as data under
    /// its name.
    pub formats: std::sync::Arc<crate::formats::Registry>,
    /// The lake table being browsed and its format, said on its heading for the whole
    /// browse.
    pub lake_here: Option<(PathBuf, &'static str)>,
    /// Index into the flattened list of currently visible rows.
    pub selected: usize,
    /// First row of the last frame, as an index into [`HomeState::visible`]. Written by
    /// the renderer (which knows the list's height) and read by
    /// [`HomeState::unclassified_visible`], so what is looked into is what is on
    /// screen. Zero until the first frame.
    pub scroll: usize,
    /// How many rows the last frame had room for. See [`HomeState::scroll`].
    pub view_height: usize,
    /// True while the user is typing a path directly.
    pub path_input_active: bool,
    pub path_input: String,
    /// What the `~` prompt lists: the directory being typed and the names in it.
    pub path_listing: Option<PathListing>,
    /// The name ↑↓ put the cursor on, among those the last segment matches.
    pub path_pick: Option<usize>,
    /// Directory the user has descended into, if any. `None` means the root listing.
    pub browsing: Option<PathBuf>,
    /// Where the browse began (entered from the root listing or jumped to). Esc climbs
    /// back to here, then to the listing, never above. `None`: `browsing` is the start.
    pub browse_start: Option<PathBuf>,
    /// Transient message (e.g. a path that does not exist).
    pub status: Option<String>,
    /// How a path is judged network-backed; swappable so the never-touch-remote rule is
    /// testable without a remote.
    pub network_check: fn(&Path) -> bool,
    /// How often and how lately each recent was opened, ranking matches. Set with
    /// [`HomeState::set_visits`], which relists.
    pub visits: std::collections::HashMap<PathBuf, crate::cache::Visits>,
    /// The recent opened last: the cursor lands here, one Enter from the last file.
    pub newest_recent: Option<PathBuf>,
    /// Where the listing of each network root and remote directory is.
    pub probes: Probes,
    /// The names a filter asked the server for, in a cloud directory cut short.
    pub narrowed: Option<Narrowed>,
    /// What peeked cloud directories hold (`hive`, `multi`), kept for the session so each
    /// is peeked once.
    pub cloud_kinds: std::collections::HashMap<PathBuf, (EntryKind, crate::home::discover::Holds)>,
    /// How rows are ordered inside each section.
    pub sort: SortMode,
    /// A listing is being built on a worker; the previous one stays on screen.
    pub listing_in_flight: bool,
    /// True while a measurement batch is out, so only one is in flight at a time.
    pub measure_in_flight: bool,
    /// A classification batch is out. One at a time, each chosen from the viewport then,
    /// so fast paging does not queue every row it passed.
    pub classify_in_flight: bool,
    /// Cloud directories with a peek out, kept apart from [`Self::cloud_kinds`] so no
    /// answer is claimed before the request returns.
    pub peeking: std::collections::HashSet<PathBuf>,
    /// Cloud directories whose peek failed: not asked again until Ctrl+R, labeled `?`
    /// (not `dir`, which would claim no data inside).
    pub peek_failed: std::collections::HashSet<PathBuf>,
    /// Row and column counts already read, by path, so each dataset is measured once a
    /// session.
    pub enriched: std::collections::HashMap<PathBuf, Measured>,
    /// Paths recorded in `enriched` since the rows last took them in.
    pub unapplied: std::collections::HashSet<PathBuf>,
    /// Sections folded (`true`) or opened by the user, by title so it survives rebuilds
    /// that renumber sections, and cached across restarts. Unlisted sections take their
    /// default. Use [`HomeState::toggle_collapsed`] and [`HomeState::set_collapsed`].
    pub folds: std::collections::HashMap<String, bool>,
    /// The saved folds are to be read again with the next listing, arriving with the
    /// rows they fold.
    pub folds_owed: bool,
    /// Datasets found by walking below the working directory.
    pub search: SearchState,
    /// What earlier runs measured, by path: the index the listing was annotated from,
    /// kept so search results fill in the same way (the filter matches columns).
    pub known: std::collections::HashMap<PathBuf, crate::cache::DatasetFacts>,
    /// Cloud sources found on this machine or in the config, with their buckets. Empty
    /// without cloud credentials, which is normal.
    pub cloud: Vec<CloudSource>,
    /// The catalogs shown, a section each. Set with [`HomeState::set_catalogs`], which
    /// indexes their places.
    pub catalogs: Vec<ShownCatalog>,
    /// HTTP(S) catalog files whose size was asked for this session (a HEAD).
    pub sized: std::collections::HashSet<PathBuf>,
    /// HTTP(S) catalog files a HEAD showed unavailable (missing, no server); retried on
    /// Ctrl+R.
    pub web_gone: std::collections::HashMap<PathBuf, crate::error_display::HttpGone>,
    /// Local datasets of a catalog that the last listing found missing.
    pub missing: std::collections::HashSet<PathBuf>,
    /// When the current wait for a remote listing began, for the elapsed time on screen.
    pub waiting_since: Option<std::time::Instant>,
    /// `RECENT` shows every place, for the session: `Enter` on its `… N more` row.
    pub recent_expanded: bool,
    /// Directory sections shown whole rather than cut, by directory, for the session.
    /// See [`HomeState::show_all`].
    pub shown_whole: std::collections::HashSet<PathBuf>,
    /// The listings entered from, outermost first, to restore the cursor on the way out.
    /// See [`HomeState::leave_mark`].
    pub trail: Vec<Mark>,
    /// The row the cursor returns to once the returned-to listing lands; held across
    /// listings while rows arrive, dropped when the user moves.
    pub returning: Option<RowKey>,
    /// How far down the returned-to row was, so it comes back on the same line.
    pub returning_line: Option<usize>,
    /// The cursor is where [`HomeState::select_first_entry`] put it and has not moved: a
    /// door the footers later turn down sends it to the first row.
    pub landing: bool,
    /// The rows as last listed. See [`RowsCache`].
    pub rows_cache: RowsCache,
    /// The catalogs' places. See [`HomeState::set_catalogs`].
    pub catalog_places: CatalogPlaces,
}

/// Where the cursor was in a listing the user went inside from, by row identity:
/// the listing is rebuilt on a worker and may change.
#[derive(Debug, Clone)]
pub struct Mark {
    /// The listing left: the place browsed, or `None` for the root listing.
    pub place: Option<PathBuf>,
    pub key: Option<RowKey>,
    /// The filter typed there, which entering cleared.
    pub filter: String,
    /// The search below that place, if finished or not started; a running walk is
    /// dropped on leaving and restarted.
    pub search: Option<SearchState>,
    /// Rows between the top of the list and the cursor.
    pub line: usize,
}

/// One recursive walk below the working directory. Kept apart from `sections`,
/// which rebuild often: re-walking each time would cost per keystroke.
#[derive(Debug, Clone, Default)]
pub struct SearchState {
    /// Where the walk started. `None` means no search has been asked for yet.
    pub root: Option<PathBuf>,
    /// Which walk this is, so an earlier walk's scoring is never taken for this one's.
    pub epoch: u64,
    /// Every data file found so far, unfiltered, in arrival batches; shared with the
    /// scoring worker without copying.
    pub results: Vec<std::sync::Arc<[Entry]>>,
    /// How many files `results` holds.
    pub indexed: usize,
    /// The last scored matches, possibly for an older filter or fewer files while a
    /// scoring is out.
    pub matches: Option<crate::home::search::Matches>,
    /// A scoring is out on a worker.
    pub scoring: bool,
    /// Directory entries examined, for the progress note.
    pub scanned: usize,
    /// A walk is out. Results may still be arriving.
    pub running: bool,
    /// The walk has finished, successfully or against a limit.
    pub done: bool,
    /// Why the walk stopped short, when it did.
    pub limited: Option<String>,
}

/// Below this many files the filter is scored inline: a millisecond or two, answered
/// in the key's frame.
const SCORE_INLINE_MAX: usize = 2_000;

/// Match score per unit of frecency, up to ten units: a daily file outranks a
/// slightly better name match, never a far better one.
const FRECENCY_LIFT: f64 = 3.0;

/// What a worker needs to score the filter against a walk's files.
#[derive(Debug, Clone)]
pub struct ScoreJob {
    pub epoch: u64,
    pub results: Vec<std::sync::Arc<[Entry]>>,
    pub query: String,
    pub base: Option<crate::home::search::Matches>,
    pub limit: usize,
}

impl SearchState {
    /// Forget everything, because the place being searched has changed.
    pub fn reset(&mut self) {
        *self = Self::default();
    }

    /// Set the files found, all at once: what a walk would have handed over in batches.
    pub fn set_results(&mut self, results: Vec<Entry>) {
        self.indexed = results.len();
        self.results = vec![results.into()];
        self.matches = None;
    }

    /// Every file found, in the order found.
    pub fn files(&self) -> impl Iterator<Item = &Entry> {
        self.results.iter().flat_map(|batch| batch.iter())
    }

    /// Whether the matches in hand are for `query` over every file found.
    fn scored_for(&self, query: &str) -> bool {
        self.matches
            .as_ref()
            .is_some_and(|m| m.query == query && m.upto == self.indexed)
    }

    /// The matches to narrow from for `query`, and how many files scoring it will look at.
    fn base_for(&self, query: &str) -> (Option<&crate::home::search::Matches>, usize) {
        match self.matches.as_ref() {
            Some(m) if m.narrows_to(query) && m.upto <= self.indexed => {
                (Some(m), m.ids.len() + self.indexed - m.upto)
            }
            _ => (None, self.indexed),
        }
    }
}

impl Default for HomeState {
    fn default() -> Self {
        Self {
            sections: Vec::new(),
            cloud: Vec::new(),
            catalogs: Vec::new(),
            sized: std::collections::HashSet::new(),
            web_gone: Default::default(),
            missing: Default::default(),
            filter: String::new(),
            search_limit: crate::config::SearchConfig::default().max_results,
            rows_cache: RowsCache::default(),
            catalog_places: CatalogPlaces::default(),
            hide_unreadable: true,
            formats: Default::default(),
            lake_here: None,
            selected: 0,
            scroll: 0,
            view_height: 0,
            path_input_active: false,
            path_input: String::new(),
            path_listing: None,
            path_pick: None,
            filter_selected: false,
            browsing: None,
            browse_start: None,
            status: None,
            network_check: is_remote_path,
            visits: Default::default(),
            newest_recent: None,
            sort: SortMode::default(),
            listing_in_flight: false,
            measure_in_flight: false,
            classify_in_flight: false,
            peeking: std::collections::HashSet::new(),
            probes: Probes::default(),
            narrowed: None,
            cloud_kinds: std::collections::HashMap::new(),
            peek_failed: std::collections::HashSet::new(),
            waiting_since: None,
            enriched: std::collections::HashMap::new(),
            unapplied: Default::default(),
            folds: std::collections::HashMap::new(),
            folds_owed: false,
            search: SearchState::default(),
            known: Default::default(),
            recent_expanded: false,
            shown_whole: Default::default(),
            trail: Vec::new(),
            returning: None,
            returning_line: None,
            landing: false,
        }
    }
}

/// Everything [`build_listing`] needs, gathered on the UI thread so the worker never
/// reaches into the app.
#[derive(Debug, Clone)]
pub struct ListingRequest {
    pub recents: Vec<PathBuf>,
    pub desktop_dirs: Vec<PathBuf>,
    pub browsing: Option<PathBuf>,
    /// See [`HomeState::probes`].
    pub probes: Probes,
    /// See [`HomeState::narrowed`].
    pub narrowed: Option<Narrowed>,
    pub network_check: fn(&Path) -> bool,
    /// Cloud sources and the buckets already enumerated for them.
    pub cloud: Vec<CloudSource>,
    /// The catalogs to list.
    pub catalogs: Vec<ShownCatalog>,
    /// What earlier runs measured: a row whose size and mtime still match is filled in
    /// from here before anything is read.
    pub known: std::collections::HashMap<PathBuf, crate::cache::DatasetFacts>,
    /// The format specs on the search path: a file one reads as several variants is a
    /// place listing its variants.
    pub formats: std::sync::Arc<crate::formats::Registry>,
}

/// What a listing pass produced.
#[derive(Debug, Clone, Default)]
pub struct Listing {
    pub sections: Vec<Section>,
    /// Local datasets of a catalog that do not exist.
    pub missing: std::collections::HashSet<PathBuf>,
}

impl Listing {
    /// Add each row's own path to `visits` where its canonical path has visits: recents
    /// are keyed canonically, catalogs spell paths as written (`/var/…` vs
    /// `/private/var/…` on macOS, short names on Windows). Canonicalizing touches the
    /// filesystem, so this runs on the listing worker, only for local rows named like a
    /// visited file.
    pub fn alias_visits(
        &self,
        visits: &mut std::collections::HashMap<PathBuf, crate::cache::Visits>,
    ) {
        let names: std::collections::HashSet<std::ffi::OsString> = visits
            .keys()
            .filter_map(|p| p.file_name().map(|n| n.to_os_string()))
            .collect();
        let mut aliases = Vec::new();
        for row in self.sections.iter().flat_map(|s| &s.rows) {
            let path = &row.path;
            if visits.contains_key(path)
                || row.table.is_some()
                || !path.file_name().is_some_and(|n| names.contains(n))
                || is_network_path(path)
            {
                continue;
            }
            if let Some(v) = crate::canonical::canonicalize(path)
                .ok()
                .and_then(|canonical| visits.get(&canonical))
            {
                aliases.push((path.clone(), *v));
            }
        }
        visits.extend(aliases);
    }
}

/// Find out what a row is, then what is in it, in one pass on the same filesystem
/// (`measure_row` does nothing for a plain directory), reading files as the following
/// open will: the command line passes the user's reader settings; listing passes use
/// the defaults, as a home open does.
pub fn look_into_as(entry: &Entry, as_read: &crate::formats::schema_union::ReadAs) -> Entry {
    let mut probe = classify_row(entry);
    measure_row(&mut probe, entry, as_read, None);
    probe
}

/// The first half of [`look_into_as`]: what a row nothing has looked into is.
fn classify_row(entry: &Entry) -> Entry {
    let mut probe = entry.clone();
    if probe.kind == EntryKind::Unknown && probe.path.is_dir() {
        let (kind, holds) = discover::look_at_directory(&probe.path);
        probe.kind = kind;
        probe.holds = holds;
    }
    probe
}

/// The second half of [`look_into_as`]: what is in it, from the files or from what an
/// open kept in `remembered`.
fn measure_row(
    probe: &mut Entry,
    entry: &Entry,
    as_read: &crate::formats::schema_union::ReadAs,
    remembered: Option<&crate::cache::CacheManager>,
) {
    discover::enrich_with(probe, as_read, remembered);
    probe.size = probe.size.or(entry.size);
    probe.modified = probe.modified.or(entry.modified);
}

/// Look into a batch of rows on a worker, passing each answer to `each` and caching
/// what was learned. Shared by the measure and classify passes. Every row is
/// classified before any is measured: a kind is one directory read, a count up to
/// sixty-four footers.
pub fn look_into_batch(
    rows: Vec<Entry>,
    cache: &crate::cache::CacheManager,
    mut each: impl FnMut(PathBuf, Measured),
) {
    let as_read = crate::formats::schema_union::ReadAs::default();
    let classified: Vec<(Entry, Entry)> = rows
        .into_iter()
        .map(|entry| {
            let probe = classify_row(&entry);
            if probe.kind != entry.kind {
                each(entry.path.clone(), measured_from(&probe, &entry));
            }
            (probe, entry)
        })
        .collect();

    let mut facts = Vec::new();
    for (mut probe, entry) in classified {
        measure_row(&mut probe, &entry, &as_read, Some(cache));
        facts.extend(facts_for(&probe));
        each(entry.path.clone(), measured_from(&probe, &entry));
    }
    // Cache what was learned; each record carries its size and mtime and invalidates
    // itself when they change.
    cache.record_dataset_facts(&facts);
}

/// Fold a measured probe into the record kept for a row.
pub fn measured_from(probe: &Entry, original: &Entry) -> Measured {
    Measured {
        rows: probe.rows,
        cols: probe.cols,
        cols_sampled: probe.cols_sampled,
        size: probe.size.or(original.size),
        columns: probe.columns.clone(),
        kind: (probe.kind != original.kind).then_some(probe.kind),
        holds: probe.holds.clone(),
        // The source is resolved from the live mount table on every listing; only what the
        // file said of itself carries forward.
        cost: crate::home::discover::Cost {
            source: None,
            ..probe.cost.clone()
        },
    }
}

/// Where the listing of one network root or remote directory stands; read off the UI
/// thread (see [`HomeState::pending_probes`]).
#[derive(Debug, Clone)]
pub enum Probe {
    /// Still being read: the rows so far, in the order they came.
    Listing(Vec<Entry>),
    /// Answered. `cut_short`: the listing stopped at [`discover::MAX_ENTRIES_PER_DIR`].
    Listed {
        rows: std::sync::Arc<[Entry]>,
        cut_short: bool,
    },
    /// Did not answer, with why when the service said.
    Unreachable(Option<String>),
}

/// Every probe, by the place it lists.
#[derive(Debug, Clone, Default)]
pub struct Probes(std::collections::HashMap<PathBuf, Probe>);

impl Probes {
    /// The rows of a listing that has answered.
    pub fn listed(&self, place: &Path) -> Option<&[Entry]> {
        match self.0.get(place)? {
            Probe::Listed { rows, .. } => Some(rows),
            _ => None,
        }
    }

    /// The rows read so far of a listing still going on.
    pub fn so_far(&self, place: &Path) -> Option<&[Entry]> {
        match self.0.get(place)? {
            Probe::Listing(rows) => Some(rows),
            _ => None,
        }
    }

    /// Answered, or written off: nothing more to ask.
    pub fn settled(&self, place: &Path) -> bool {
        matches!(
            self.0.get(place),
            Some(Probe::Listed { .. } | Probe::Unreachable(_))
        )
    }

    pub fn cut_short(&self, place: &Path) -> bool {
        matches!(
            self.0.get(place),
            Some(Probe::Listed {
                cut_short: true,
                ..
            })
        )
    }

    pub fn unreachable(&self, place: &Path) -> bool {
        matches!(self.0.get(place), Some(Probe::Unreachable(_)))
    }

    /// Why the listing was refused, when the service said.
    pub fn error(&self, place: &Path) -> Option<&str> {
        match self.0.get(place)? {
            Probe::Unreachable(why) => why.as_deref(),
            _ => None,
        }
    }

    /// What the place lists now: its answer, the rows so far in the finished order
    /// (bucket directories above objects, or [`discover::sort_entries`]), or nothing.
    fn rows(&self, place: &Path) -> Vec<Entry> {
        match self.0.get(place) {
            Some(Probe::Listed { rows, .. }) => rows.to_vec(),
            Some(Probe::Listing(rows)) => {
                let mut rows = rows.clone();
                if is_object_store_url(place) {
                    rows.sort_by_key(|row| row.kind != EntryKind::Directory);
                } else {
                    discover::sort_entries(&mut rows);
                }
                rows
            }
            _ => Vec::new(),
        }
    }

    /// The places answered, with their rows.
    pub fn answered(&self) -> impl Iterator<Item = (&PathBuf, &[Entry])> {
        self.0.iter().filter_map(|(place, probe)| match probe {
            Probe::Listed { rows, .. } => Some((place, &rows[..])),
            _ => None,
        })
    }

    /// A row an answered probe produced for this exact path, if any.
    fn entry(&self, path: &Path) -> Option<Entry> {
        self.answered()
            .flat_map(|(_, rows)| rows.iter())
            .find(|e| e.path == path)
            .cloned()
    }

    /// Rows read since the last batch, while the listing is still out.
    pub fn read(&mut self, place: &Path, rows: &[Entry]) {
        if let Probe::Listing(so_far) = self
            .0
            .entry(place.to_path_buf())
            .or_insert(Probe::Listing(Vec::new()))
        {
            so_far.extend_from_slice(rows);
        }
    }

    pub fn insert(&mut self, place: PathBuf, probe: Probe) {
        self.0.insert(place, probe);
    }

    /// Forget a place's listing, so it is asked for again.
    pub fn forget(&mut self, place: &Path) {
        self.0.remove(place);
    }

    /// Forget a listing still being read: it was stopped.
    pub fn stopped(&mut self, place: &Path) {
        if let Some(Probe::Listing(_)) = self.0.get(place) {
            self.0.remove(place);
        }
    }

    fn listed_mut(&mut self, place: &Path) -> Option<&mut std::sync::Arc<[Entry]>> {
        match self.0.get_mut(place)? {
            Probe::Listed { rows, .. } => Some(rows),
            _ => None,
        }
    }
}

/// What a cut-short cloud directory holds under one name prefix, asked of the server
/// for a typed filter.
#[derive(Debug, Clone)]
pub struct Narrowed {
    pub dir: PathBuf,
    /// The start of every name asked for (`STATION=USW`).
    pub prefix: String,
    pub rows: Vec<Entry>,
    /// These stopped at the cap too.
    pub truncated: bool,
}

/// Build the home listing. A free function so it runs on a worker: it is home's only
/// filesystem access, and a wedged mount, FIFO or failing disk blocks here, so never
/// call it from the drawing thread.
pub fn build_listing(request: &ListingRequest) -> Listing {
    let ListingRequest {
        recents,
        desktop_dirs,
        browsing,
        probes,
        narrowed,
        network_check,
        cloud,
        catalogs,
        known,
        formats,
    } = request;
    let network_check = *network_check;
    // One read of the mount table for the listing: a kernel-generated file, so it cannot
    // block on a share that stopped answering.
    let mounts = crate::home::locality::Mounts::current();
    let mut sections: Vec<Section> = Vec::new();

    // Inside a cloud source: its buckets, and nothing else.
    if let Some(id) = browsing.as_deref().and_then(cloud_source_id) {
        let source = cloud.iter().find(|s| s.id == id);
        let rows = source
            .map(|s| s.buckets.iter().map(|b| bucket_entry(b)).collect())
            .unwrap_or_default();
        let failure = source.and_then(|s| match &s.status {
            CloudStatus::Failed { short, .. } => Some(short.clone()),
            _ => None,
        });
        sections.push(Section {
            subtitle: source.map(|s| s.note.clone()).filter(|n| !n.is_empty()),
            unavailable: source.is_none() || failure.is_some(),
            unavailable_note: if source.is_none() {
                Some("source not found".to_string())
            } else {
                failure
            },
            waiting: source.is_some_and(|s| s.busy()),
            ..Section::titled(source.map(|s| s.label.clone()).unwrap_or(id), rows)
        });
        annotate(&mut sections, known, network_check, &mounts);
        return Listing {
            sections,
            ..Default::default()
        };
    }

    // Descended into a directory: show only that.
    if let Some(dir) = browsing.clone() {
        // A remote directory is never read here (that freezes the UI): rows come from the
        // background probe, and the section is empty until it answers.
        let remote = network_check(&dir);
        // A remote listing still being read shows what it has, and says so.
        let so_far = remote && probes.so_far(&dir).is_some();
        // A SQLite database is a place too, whose rows are its tables.
        let database = !remote && dir.is_file();
        let (mut rows, truncated) = if remote {
            (probes.rows(&dir), probes.cut_short(&dir))
        } else if database {
            let tables = discover::database_rows(&dir);
            let rows = if tables.is_empty() {
                discover::variant_rows(&dir, formats)
            } else {
                tables
            };
            (rows, false)
        } else {
            let scan = discover::scan_dir_specs(&dir, formats);
            // A Hugging Face cache's splits, before the files they are made of.
            let mut rows = discover::split_rows(&dir);
            rows.extend(scan.entries);
            (rows, scan.truncated)
        };
        // A cut-short level holds the names a filter asked the server for, beside the first
        // of the rest.
        let narrowed = narrowed
            .as_ref()
            .filter(|n| remote && truncated && n.dir == dir);
        if let Some(narrowed) = narrowed {
            let listed: std::collections::HashSet<PathBuf> =
                rows.iter().map(|row| row.path.clone()).collect();
            rows.extend(
                narrowed
                    .rows
                    .iter()
                    .filter(|row| !listed.contains(&row.path))
                    .cloned(),
            );
        }
        // Otherwise a directory cut at the cap looks like one holding exactly that many.
        let subtitle = if so_far {
            Some(format!(
                "{} so far",
                crate::numfmt::group_chrome(rows.len())
            ))
        } else if truncated {
            let first = format!(
                "first {}",
                crate::numfmt::group_chrome(discover::MAX_ENTRIES_PER_DIR)
            );
            Some(match narrowed {
                Some(n) => format!(
                    "{first} + {}{} {}*",
                    crate::numfmt::group_chrome(n.rows.len()),
                    if n.truncated { "+" } else { "" },
                    n.prefix
                ),
                None => first,
            })
        } else {
            None
        };
        let unavailable = remote && probes.unreachable(&dir);
        // The first row inside any directory opens all of it, since `Enter` below opens one
        // file: the other door.
        let mut door = (!database)
            .then(|| whole_directory_row(&dir, &rows, remote))
            .flatten();
        // What an earlier run's footers made of this directory, as for its row upstairs
        // (fingerprinted by its mtime), so a directory of separate tables reads the same in
        // both places.
        if !remote
            && let Some(door) = door.as_mut()
            && let Ok(meta) = std::fs::metadata(&dir)
        {
            door.modified = meta.modified().ok();
            apply_known_facts(door, known, false);
            door.modified = None;
            door.name = door_name(door, &rows);
        }
        // The URL without a source id (the trail names the source); an Azure account or
        // container by name, not its long URL.
        let title = {
            let text = dir.to_string_lossy();
            if let Some(dataset) = catalogs
                .iter()
                .flat_map(|c| c.datasets.iter())
                .find(|d| is_object_store_url(&d.location) && same_place(&d.location, &dir))
            {
                dataset.name.clone()
            } else if let Some((_, account)) = cloud_account(&dir) {
                account
            } else if let Some((_, container, key)) = crate::cloud::source::azure_parts(&text) {
                format!("{container}/{}", key.trim_matches('/'))
                    .trim_end_matches('/')
                    .to_string()
            } else {
                match crate::cloud::source::split_source_id(&text) {
                    (Some(_), plain) => plain.into_owned(),
                    (None, _) => display_path(&dir),
                }
            }
        };
        sections.push(Section {
            subtitle,
            root: Some(dir.clone()),
            unavailable,
            // A browsed remote place that did not answer has nothing to add; a refused listing
            // says why.
            unavailable_note: probes.error(&dir).map(str::to_string),
            // Its wait replaces the whole list until rows arrive (`awaiting_listing`), then sits
            // on the heading; no `remote_root`.
            waiting: so_far,
            door,
            ..Section::titled(title, rows)
        });
        annotate(&mut sections, known, network_check, &mounts);
        return Listing {
            sections,
            ..Default::default()
        };
    }

    // Recents that still exist, most recent first.
    let recent_rows: Vec<Entry> = recents
        .iter()
        // `exists()` stats, so a remote entry is trusted and dropped only if its probe says
        // it is gone.
        .filter(|p| {
            network_check(p)
                || p.exists()
                || crate::formats::members::split(p).is_some()
                || crate::formats::members::split_variant(p, formats).is_some()
                || crate::formats::hf_splits::split_place(p).is_some()
        })
        // No display cap: the store bounds it, the header counts it, and the section folds.
        .map(|p| {
            // Reuse the containing root probe's classification, so a dataset reads the same
            // under its directory and under Recent.
            if let Some(known) = probes.entry(p) {
                return known;
            }
            if let Some(variant) = discover::variant_row(p, formats) {
                return variant;
            }
            if !network_check(p)
                && let Some(split) = discover::split_row(p)
            {
                return split;
            }
            let mut entry = entry_for_path(p, network_check(p));
            if !network_check(p) {
                discover::name_unlisted_file(&mut entry, formats);
            }
            // A dataset opened from a catalog keeps the catalog's name, not its URL's last
            // segment.
            if let Some(dataset) = catalogs
                .iter()
                .flat_map(|c| &c.datasets)
                .find(|d| d.location == *p)
            {
                entry.name = dataset.name.clone();
            }
            entry
        })
        .collect();

    // Desktop-derived places are collected rather than expanded — see below.
    let mut elsewhere: Vec<Entry> = Vec::new();

    let roots = HomeState::roots_with(desktop_dirs, network_check);
    let mut root_sections: Vec<(RootOrigin, Section)> = Vec::new();
    // Where the current-directory section is and the names it lists, for the RECENT
    // dedupe below.
    let mut cwd_listing: Option<(PathBuf, std::collections::HashSet<std::ffi::OsString>)> = None;
    for root in roots {
        // A desktop place is a directory to step into, never expanded: its contents may be
        // private (whatever was opened anywhere). Enter is the ask.
        if root.origin == RootOrigin::Desktop {
            if root.available {
                let mut entry = Entry::directory(&root.path);
                // The full place ("~/Downloads"), since the section has no path of its own.
                entry.name = display_path(&root.path);
                elsewhere.push(entry);
            }
            continue;
        }

        // A local root is scanned here (truncation is known for it); a remote one shows
        // only what its background probe returned, since scanning it can freeze datui.
        let mut truncated = false;
        let rows = if root.network {
            truncated = probes.cut_short(&root.path);
            probes.rows(&root.path)
        } else if root.available {
            let scan = discover::scan_dir_specs(&root.path, formats);
            truncated = scan.truncated;
            scan.entries
        } else {
            Vec::new()
        };
        if root.origin == RootOrigin::Cwd {
            // Compared canonically, as recents are stored; a network cwd as spelled, since
            // canonicalizing would stat a mount that may not answer.
            let key = if root.network {
                root.path.clone()
            } else {
                crate::canonical::canonicalize(&root.path).unwrap_or_else(|_| root.path.clone())
            };
            let names = rows
                .iter()
                .filter_map(|row| row.path.file_name().map(|n| n.to_os_string()))
                .collect();
            cwd_listing = Some((key, names));
        }
        // An unreadable root stays: a dead share is what the section heading reports.
        let unreachable = root.network && probes.unreachable(&root.path);
        let waiting = root.network && !unreachable && probes.listed(&root.path).is_none();
        // Flag a network root, the one that will be slow or stop answering, by its
        // filesystem (nfs4, cifs, fuse.sshfs fail differently) when the mount table agrees
        // it is remote; otherwise just "network", rather than lose the warning.
        let described = mounts.describe(&root.path);
        let fstype = if described.network() {
            described.fstype
        } else {
            "network".to_string()
        };
        // Say when the list is a prefix: a directory cut at the cap otherwise looks like one
        // holding exactly that many.
        let mut state: Vec<String> = Vec::new();
        if truncated {
            state.push(format!(
                "first {}",
                crate::numfmt::group_chrome(discover::MAX_ENTRIES_PER_DIR)
            ));
        }
        if root.network {
            state.push(fstype);
        }
        root_sections.push((
            root.origin,
            Section {
                subtitle: (!state.is_empty()).then(|| crate::glyphs::dotted(&state.join(" · "))),
                origin: Some(root.origin.note()),
                root: Some(root.path.clone()),
                unavailable: !root.available || unreachable,
                remote_root: root.network.then(|| root.path.clone()),
                waiting,
                ..Section::titled(display_path(&root.path), rows)
            },
        ));
    }

    // The current directory's section sits right below RECENT, so a recent it already
    // lists is dropped from RECENT. Decided by what that section contains, not by path:
    // a recent the scan did not surface (hidden, past the cap) stays under RECENT.
    // Browsing returned above with no RECENT at all.
    let recent_rows: Vec<Entry> = match &cwd_listing {
        None => recent_rows,
        Some((cwd, names)) => recent_rows
            .into_iter()
            .filter(|row| {
                let place = place_of(&row.path);
                // A local place is resolved before comparing, as the store resolves; a remote one
                // is compared as written, to avoid a stat.
                let place = if network_check(&place) {
                    place
                } else {
                    crate::canonical::canonicalize(&place).unwrap_or(place)
                };
                place != *cwd || !row.path.file_name().is_some_and(|n| names.contains(n))
            })
            .collect(),
    };
    if !recent_rows.is_empty() {
        let place_labels = place_labels(&recent_rows, known, network_check);
        sections.push(Section {
            // Every trace of recent use lives here, the places as rows of this section.
            grouped_by_place: true,
            place_labels,
            ..Section::titled(HomeState::RECENT_SECTION, recent_rows)
        });
    }

    // Ordered by why you came: what you opened last, where you are, the object stores
    // your credentials reach (deliberate setup, unreachable by directory listing), then
    // the catalogs.
    sections.extend(root_sections.into_iter().map(|(_, s)| s));

    // One section for all cloud sources, each a row to step into; buckets are listed
    // one level down, once per session.
    if !cloud.is_empty() {
        sections.push(Section::titled(
            HomeState::CLOUD_SECTION.to_string(),
            cloud.iter().map(source_entry).collect(),
        ));
    }

    // Catalogs in order: yours, the listed files, then the bundled one.
    let mut missing = std::collections::HashSet::new();
    for catalog in catalogs {
        let mut section = catalog_section(catalog, network_check, &mut missing);
        // A catalog's local file is named by a spec whose glob matches; nothing is read.
        for row in section.rows.iter_mut().filter(|r| {
            r.kind == EntryKind::File
                && r.format_spec.is_none()
                && r.table.is_none()
                && !network_check(&r.path)
                && !discover::is_data_file(&r.path)
        }) {
            if let Some(spec) = formats.by_glob(&row.path, false).first() {
                discover::name_spec_file(row, spec);
            }
        }
        sections.push(section);
    }

    if !elsewhere.is_empty() {
        sections.push(Section {
            // Places to look, not datasets: folded until asked for.
            folded_by_default: true,
            ..Section::titled("Elsewhere", elsewhere)
        });
    }

    // Fill in earlier measurements that still match: the listing already stat'ed every
    // row, and the mount table is read once and resolved as strings, so nothing blocks.
    annotate(&mut sections, known, network_check, &mounts);

    Listing { sections, missing }
}

/// The key a record about `path` is filed under in the dataset index. Opens record
/// under the resolved URL (`s3://bucket/x` for `s3://lab@bucket/x`, one `abfss://`
/// spelling for Azure) while recents are stored as typed; both must meet here.
pub fn index_key(path: &Path) -> PathBuf {
    let text = path.to_string_lossy();
    if let Some((account, container, key)) = crate::cloud::source::azure_parts(&text) {
        return PathBuf::from(crate::cloud::source::azure_url(&account, &container, &key));
    }
    match crate::cloud::source::split_source_id(&text) {
        (Some(_), plain) => PathBuf::from(plain.into_owned()),
        (None, _) => path.to_path_buf(),
    }
}

/// A record about `path`, under the path itself or the key an open files it under.
fn known_facts<'a>(
    known: &'a std::collections::HashMap<PathBuf, crate::cache::DatasetFacts>,
    path: &Path,
) -> Option<&'a crate::cache::DatasetFacts> {
    known.get(path).or_else(|| known.get(&index_key(path)))
}

/// What the dataset index remembers each row's place to be (`bitcoin/  2 parquet`),
/// from records this build's classifier would have written, skipping `dir`. A local
/// place must still match its recorded mtime, as `apply_known_facts` requires; a
/// remote place is taken as recorded.
fn place_labels(
    rows: &[Entry],
    known: &std::collections::HashMap<PathBuf, crate::cache::DatasetFacts>,
    network_check: fn(&Path) -> bool,
) -> std::collections::HashMap<PathBuf, String> {
    let mut labels = std::collections::HashMap::new();
    for row in rows {
        let place = place_of(&row.path);
        if labels.contains_key(&place) {
            continue;
        }
        let Some(facts) = known_facts(known, &place) else {
            continue;
        };
        if facts.classified_by != crate::home::discover::CLASSIFIER_VERSION {
            continue;
        }
        if !network_check(&place) {
            let same_mtime = std::fs::metadata(&place)
                .and_then(|m| m.modified())
                .ok()
                .and_then(|m| m.duration_since(std::time::UNIX_EPOCH).ok())
                .is_some_and(|d| d.as_secs() == facts.mtime);
            if !same_mtime {
                continue;
            }
        }
        let Some(kind) = facts.kind else {
            continue;
        };
        let mut probe = Entry::directory(&place);
        probe.kind = kind;
        probe.holds = facts.holds.clone();
        let label = probe.label();
        if !label.is_empty() && !label.starts_with("dir") {
            labels.insert(place, label.into_owned());
        }
    }
    labels
}

/// Fill every row in with what is already known about it and where it lives. Called
/// from each of `build_listing`'s exits, so no exit's rows miss facts.
fn annotate(
    sections: &mut [Section],
    known: &std::collections::HashMap<PathBuf, crate::cache::DatasetFacts>,
    network_check: fn(&Path) -> bool,
    mounts: &crate::home::locality::Mounts,
) {
    for section in sections {
        for row in &mut section.rows {
            if is_cloud_place(&row.path) {
                row.cost.source = Some("cloud".to_string());
                continue;
            }
            apply_known_facts(row, known, network_check(&row.path));
            row.cost.source = Some(mounts.describe(&row.path).fstype);
        }
        // The door reads where its directory is (the local glyph on local disk).
        if let Some(door) = section.door.as_mut()
            && !is_cloud_place(&door.path)
        {
            door.cost.source = Some(mounts.describe(&door.path).fstype);
        }
    }
}

/// Write a measurement into its row.
fn fold_measured(row: &mut Entry, m: &Measured) {
    row.measured = true;
    row.rows = m.rows;
    row.cols = m.cols;
    row.cols_sampled = m.cols_sampled;
    if let Some(kind) = m.kind {
        row.kind = kind;
    }
    if m.size.is_some() {
        row.size = m.size;
    }
    if !m.columns.is_empty() && row.columns != m.columns {
        row.columns.clone_from(&m.columns);
    }
    if !m.holds.is_empty() && row.holds != m.holds {
        row.holds.clone_from(&m.holds);
    }
    // Keep the source (from the mount table); take everything else (from the file).
    take_cost(row, &m.cost);
}

/// Take a measured or remembered row cost, keeping what came from elsewhere: its
/// location (mount table) and a spec file's variant count (an older record lacks
/// it, which would leave no tables to list).
fn take_cost(row: &mut Entry, cost: &discover::Cost) {
    let source = row.cost.source.take();
    let variants = row.cost.tables.filter(|_| row.format_spec.is_some());
    row.cost = cost.clone();
    row.cost.source = source;
    if variants.is_some() {
        row.cost.tables = variants;
    }
}

/// Apply a cached measurement to a row. A local row must still match its size and
/// mtime (the listing already stat'ed it). A remote row cannot be stat'ed safely, so
/// its cached facts are used as is: a stale count beats none for hard-to-reach data.
fn apply_known_facts(
    row: &mut Entry,
    known: &std::collections::HashMap<PathBuf, crate::cache::DatasetFacts>,
    remote: bool,
) {
    let Some(facts) = known_facts(known, &row.path) else {
        return;
    };

    // What an earlier run found this directory to be: listings no longer read
    // directories, so rows arrive `Unknown`. Only directories a run measured (with a
    // size) have a record; what it saves is re-reading footers that found separate
    // tables. Checked before the file fingerprint below, which never matches a
    // directory: the directory's mtime is its fingerprint, moving as files come and go.
    // Gated on the classifier version, since `is_one_table` is version-sensitive.
    if !remote
        && matches!(row.kind, EntryKind::Unknown | EntryKind::MultiFile)
        && facts.classified_by == crate::home::discover::CLASSIFIER_VERSION
        && let Some(kind) = facts.kind
    {
        let same_mtime = row
            .modified
            .and_then(|m| m.duration_since(std::time::UNIX_EPOCH).ok())
            .is_some_and(|d| d.as_secs() == facts.mtime);
        if same_mtime {
            row.kind = kind;
            // The holdings come back with the kind: a row given its kind from the cache is
            // never looked into again, so a missing count would stay missing all session.
            if row.holds.is_empty() {
                row.holds = facts.holds.clone();
            }
            // Only kind and count: both come from the directory's names, which its mtime
            // fingerprints. Footer facts (width, size, columns) can change by a file rewritten
            // in place, which a directory mtime cannot see, so they are not restored.
        }
    }

    if !remote {
        let same_bytes = row.size.map(|s| s == facts.size).unwrap_or(false)
            && row
                .modified
                .and_then(|m| m.duration_since(std::time::UNIX_EPOCH).ok())
                .map(|d| d.as_secs() == facts.mtime)
                .unwrap_or(false);
        if !same_bytes {
            return;
        }
    }

    row.rows = facts.rows;
    row.cols = facts.cols;
    row.cols_sampled = facts.cols_sampled;
    if !facts.columns.is_empty() {
        row.columns = facts.columns.clone();
    }
    // The source comes from the live mount table afterwards; only what the file said of
    // itself is restored.
    take_cost(row, &facts.cost);
    if remote {
        // A remote row was never stat'ed, so these are all it has; a record with no size
        // (one object's footer read) gives none rather than zero.
        if facts.size > 0 {
            row.size = row.size.or(Some(facts.size));
        }
        // What it was last seen to be, not what its name suggests, so a dataset reads the
        // same in every section. Only from this classifier version: an older record could
        // call a Delta root `multifile`.
        if row.kind == EntryKind::Unknown
            && facts.classified_by == crate::home::discover::CLASSIFIER_VERSION
            && let Some(kind) = facts.kind
        {
            row.kind = kind;
            // The holdings come back with the kind, or the row would say `dir` all session.
            if row.holds.is_empty() {
                row.holds = facts.holds.clone();
            }
        }
    }
}

/// The record to keep for a row that has just been measured.
pub fn facts_for(entry: &Entry) -> Option<(PathBuf, crate::cache::DatasetFacts)> {
    let size = entry.size?;
    let mtime = entry
        .modified?
        .duration_since(std::time::UNIX_EPOCH)
        .ok()?
        .as_secs();
    if entry.rows.is_none() && entry.columns.is_empty() && entry.cost == Default::default() {
        return None; // Nothing learned worth keeping.
    }
    Some((
        entry.path.clone(),
        crate::cache::DatasetFacts {
            mtime,
            size,
            rows: entry.rows,
            cols: entry.cols,
            cols_sampled: entry.cols_sampled,
            columns: entry.columns.clone(),
            kind: Some(entry.kind),
            holds: entry.holds.clone(),
            classified_by: crate::home::discover::CLASSIFIER_VERSION,
            // The source is where it is now: paths move between mounts.
            cost: crate::home::discover::Cost {
                source: None,
                ..entry.cost.clone()
            },
        },
    ))
}

/// How well an entry answers the filter, by name or by column (the footer's column
/// names: "which has a `customer_id`?"). A name match always outranks a column
/// match. Higher is better, as in fzf; see [`crate::home::fuzzy`].
pub fn match_score(filter: &str, entry: &Entry) -> Option<i32> {
    match crate::home::fuzzy::best_match(filter, &entry.name) {
        Some(m) => Some(m.score),
        // Below every name match; a column match is a substring test with no score of its
        // own.
        None => matching_column(filter, entry).map(|_| -COLUMN_MATCH_PENALTY),
    }
}

/// [`match_score`], with what the row is marked by when drawn.
pub fn match_hit(filter: &str, entry: &Entry) -> Option<Hit> {
    Needle::new(filter).hit(entry)
}

/// A filter prepared once for scoring a whole listing: lowered once, not once per row
/// and column.
struct Needle<'a> {
    filter: &'a str,
    lower: String,
}

impl<'a> Needle<'a> {
    fn new(filter: &'a str) -> Self {
        Needle {
            filter,
            lower: filter.to_lowercase(),
        }
    }

    fn hit(&self, entry: &Entry) -> Option<Hit> {
        let named = crate::home::fuzzy::best_match_with(self.filter, &entry.name, |score, _| Hit {
            score,
            column: None,
        });
        if named.is_some() {
            return named;
        }
        Some(Hit {
            score: -COLUMN_MATCH_PENALTY,
            column: Some(self.column(entry)?),
        })
    }

    /// The first column of `entry` containing the filter, case-insensitively.
    fn column(&self, entry: &Entry) -> Option<usize> {
        if self.filter.is_empty() {
            return None;
        }
        (entry.columns.iter()).position(|c| contains_folded(c, &self.lower))
    }
}

/// Whether `haystack` contains `lower` (already lowercase), ignoring case; without
/// allocating when both are ASCII.
fn contains_folded(haystack: &str, lower: &str) -> bool {
    if haystack.is_ascii() && lower.is_ascii() {
        let (hay, needle) = (haystack.as_bytes(), lower.as_bytes());
        return needle.is_empty()
            || hay
                .windows(needle.len())
                .any(|w| w.eq_ignore_ascii_case(needle));
    }
    haystack.to_lowercase().contains(lower)
}

/// How far a column match sits below any name match: more than any name score, so
/// they never interleave.
const COLUMN_MATCH_PENALTY: i32 = 1_000_000;

/// The first column of `entry` containing `filter`, case-insensitively. Substring,
/// not subsequence: fuzzy matching dozens of names matches nearly everything.
pub fn matching_column<'a>(filter: &str, entry: &'a Entry) -> Option<&'a str> {
    (Needle::new(filter).column(entry)).map(|i| entry.columns[i].as_str())
}

/// Character positions in `haystack` that `needle` matched, from the same alignment
/// that scored it.
pub fn fuzzy_positions(needle: &str, haystack: &str) -> Vec<usize> {
    crate::home::fuzzy::best_match(needle, haystack)
        .map(|m| m.positions)
        .unwrap_or_default()
}

/// Character positions of the first case-insensitive occurrence of `needle`, since
/// column matching is a substring test.
pub fn substring_positions(needle: &str, haystack: &str) -> Vec<usize> {
    if needle.is_empty() {
        return Vec::new();
    }
    let hay: Vec<char> = haystack.to_lowercase().chars().collect();
    let need: Vec<char> = needle.to_lowercase().chars().collect();
    if need.len() > hay.len() {
        return Vec::new();
    }
    for start in 0..=(hay.len() - need.len()) {
        if hay[start..start + need.len()] == need[..] {
            return (start..start + need.len()).collect();
        }
    }
    Vec::new()
}

/// Whether and how well `needle` matches `haystack` (higher is better), through
/// [`crate::home::fuzzy::best_match`] like every ranking and highlight.
pub fn fuzzy_score(needle: &str, haystack: &str) -> Option<i32> {
    crate::home::fuzzy::best_match(needle, haystack).map(|m| m.score)
}

impl HomeState {
    /// Roots from the working directory, then the desktop's data directories;
    /// duplicates collapse to the first. Recents' places are `RECENT` rows, not roots.
    pub fn roots(desktop_dirs: &[PathBuf]) -> Vec<Root> {
        Self::roots_with(desktop_dirs, is_remote_path)
    }

    /// As [`HomeState::roots`], with the network test injected.
    pub fn roots_with(desktop_dirs: &[PathBuf], is_network: fn(&Path) -> bool) -> Vec<Root> {
        let mut roots: Vec<Root> = Vec::new();
        let mut seen: Vec<PathBuf> = Vec::new();

        let push =
            |path: PathBuf, origin: RootOrigin, roots: &mut Vec<Root>, seen: &mut Vec<PathBuf>| {
                // The network test reads only the mount table, safe on a blocking path.
                let network = is_network(&path);

                // Canonicalizing and listing touch the filesystem and block on a dead NFS share
                // (indefinitely on a `hard` mount): a remote root is taken as is and probed in the
                // background.
                let key = if network {
                    path.clone()
                } else {
                    crate::canonical::canonicalize(&path).unwrap_or_else(|_| path.clone())
                };
                if seen.contains(&key) {
                    return;
                }
                seen.push(key);

                let available = if network {
                    true // unknown until probed; assumed present so it is listed
                } else {
                    std::fs::read_dir(&path).is_ok()
                };
                roots.push(Root {
                    path,
                    origin,
                    available,
                    network,
                });
            };

        // Where you are comes first. Standing in a directory is the strongest
        // statement of what you are working on right now — stronger than a directory
        // configured months ago.
        if let Ok(cwd) = std::env::current_dir() {
            push(cwd, RootOrigin::Cwd, &mut roots, &mut seen);
        }

        // Last, and weakest: places the desktop says you have opened data from. Only
        // useful before datui has recents of its own.
        for dir in desktop_dirs {
            push(dir.clone(), RootOrigin::Desktop, &mut roots, &mut seen);
        }

        roots
    }

    /// Build the home listing: recents first (on a mount, the dataset you want was
    /// usually opened before), then roots scanned one level deep.
    pub fn rebuild(&mut self, recents: &[PathBuf]) {
        self.rebuild_with(recents, &[])
    }

    /// As [`HomeState::rebuild`], plus directories derived from the desktop's own
    /// recently-used list.
    pub fn rebuild_with(&mut self, recents: &[PathBuf], desktop_dirs: &[PathBuf]) {
        let request = ListingRequest {
            recents: recents.to_vec(),
            desktop_dirs: desktop_dirs.to_vec(),
            browsing: self.browsing.clone(),
            probes: self.probes.clone(),
            narrowed: self.narrowed.clone(),
            network_check: self.network_check,
            cloud: self.cloud.clone(),
            catalogs: self.catalogs.clone(),
            // The synchronous path (tests, library callers) uses no cache: exactly what is on
            // disk now.
            known: Default::default(),
            formats: self.formats.clone(),
        };
        let listing = build_listing(&request);
        self.apply_listing(listing);
    }

    /// Install a listing built elsewhere, keeping the cursor on whatever it was on.
    pub fn apply_listing(&mut self, listing: Listing) {
        let returning = self.returning.take();
        let previous = returning.clone().or_else(|| self.selected_key());
        // Rows landing above the cursor move the list, not the cursor.
        let line = self.selected.saturating_sub(self.scroll);
        let mut listing = listing;
        for section in &mut listing.sections {
            name_by_spec(&self.formats, &mut section.rows);
        }
        self.sections = listing.sections;
        self.changed();
        self.missing = listing.missing;
        // Browsing, the first section is the directory browsed.
        if let (Some(browsing), Some((dir, format))) = (&self.browsing, &self.lake_here)
            && browsing == dir
            && let Some(section) = self.sections.first_mut()
        {
            let note = crate::glyphs::dotted(&format!(
                "{} · not read as a table",
                format.to_ascii_lowercase()
            ));
            section.subtitle = Some(match section.subtitle.take() {
                Some(state) => crate::glyphs::dotted(&format!("{note} · {state}")),
                None => note,
            });
        }
        // Search results outlive rebuilds (they came from a walk): put them back.
        self.sync_search_section();
        // So does what this session looked into: rebuilds read names cheaply, and kinds and
        // counts cost round trips.
        self.apply_measurements();

        // Keep the cursor on its row across refreshes, not back at the top on every result.
        let placed = self.reselect(previous);
        // A returned-to row is where the user left it, not a landing a late footer may
        // move.
        if placed && returning.is_some() {
            self.landing = false;
        }
        if !placed {
            self.select_first_entry();
            // The returned-to row may be in a later listing (a remote place still answering, a
            // search still walking).
            if self.rows_still_arriving() {
                self.returning = returning;
            }
        } else if returning.is_some() {
            self.scroll_to_returning_line();
        } else {
            self.scroll = self.selected.saturating_sub(line);
        }
        self.follow_selection();
    }

    /// Remember the cursor before going inside something, so leaving returns to it. Call
    /// before `browsing` changes.
    pub fn leave_mark(&mut self) {
        let mark = Mark {
            place: self.browsing.clone(),
            key: self.selected_key(),
            filter: self.filter.clone(),
            // A scoring out now would answer while away and be dropped; the kept copy asks
            // again on return.
            search: (!self.search.running).then(|| SearchState {
                scoring: false,
                ..self.search.clone()
            }),
            line: self.selected.saturating_sub(self.scroll),
        };
        // A place already on the trail is being re-entered from elsewhere; its old mark is
        // over.
        self.trail.retain(|m| m.place != mark.place);
        self.trail.push(mark);
    }

    /// Return to the place now browsed, from `from`: its filter, search, and the cursor
    /// on its row once the listing lands. Call after `browsing` is set. A place never
    /// entered from (Backspace above the browse start) puts the cursor on the place just
    /// left.
    pub fn come_back(&mut self, from: Option<PathBuf>) {
        let to = self.browsing.clone();
        let mark = self
            .trail
            .iter()
            .rposition(|m| m.place == to)
            .map(|at| self.trail.split_off(at).remove(0));
        match mark {
            Some(mark) => {
                self.filter = mark.filter;
                self.search = mark.search.unwrap_or_default();
                self.returning = mark.key;
                self.returning_line = Some(mark.line);
            }
            None => {
                self.filter.clear();
                self.search.reset();
                self.returning = from.map(RowKey::Entry);
                self.returning_line = None;
            }
        }
    }

    /// Whether rows may yet arrive without the user asking: a remote listing still
    /// answering, or a search still walking.
    fn rows_still_arriving(&self) -> bool {
        self.listing_in_flight
            || self.sections_waiting()
            || self.awaiting_listing().is_some()
            || self.search.running
    }

    /// Put the cursor on the row being returned to, if it has arrived.
    fn settle_return(&mut self) {
        let Some(key) = self.returning.clone() else {
            return;
        };
        if let Some(idx) = self.place_key(&key) {
            self.selected = idx;
            self.returning = None;
            self.landing = false;
            self.scroll_to_returning_line();
            self.follow_selection();
        } else if !self.rows_still_arriving() {
            self.returning = None;
        }
    }

    /// Put the row just returned to on the line it was left on, when that is known.
    fn scroll_to_returning_line(&mut self) {
        if let Some(line) = self.returning_line.take() {
            self.scroll = self.selected.saturating_sub(line);
        }
    }

    /// What the cursor is on, as something that survives the rows changing.
    pub fn selected_key(&self) -> Option<RowKey> {
        let title = |section: usize| self.sections.get(section).map(|s| s.title.clone());
        Some(match self.selected_row()? {
            Row::Header { section, .. } => RowKey::Header(title(section)?),
            Row::More { section, .. } => RowKey::More(title(section)?),
            Row::Hidden { section, .. } => RowKey::Hidden(title(section)?),
            Row::Up { section } => RowKey::Up(title(section)?),
            Row::Entry { entry, .. } => RowKey::Entry(entry.path.clone()),
            Row::Door { entry, .. } => RowKey::Door(entry.path.clone()),
            Row::Place { path, .. } => RowKey::Place(path),
        })
    }

    /// Put the cursor back on the row `key` names, if still listed; true if placed. A
    /// row the cap now hides counts as placed on its `more` row. Otherwise the cursor
    /// is clamped so it never sits past the end.
    pub fn reselect(&mut self, key: Option<RowKey>) -> bool {
        let Some(key) = key else {
            self.clamp_selection();
            return false;
        };
        match self.place_key(&key) {
            Some(idx) => {
                self.selected = idx;
                true
            }
            None => {
                self.clamp_selection();
                false
            }
        }
    }

    /// Where the row `key` names is on screen. A row a directory's cut hides makes that
    /// directory show whole; a row `RECENT`'s cap hides answers with its `more` row.
    fn place_key(&mut self, key: &RowKey) -> Option<usize> {
        if let Some(found) = self.listed(key) {
            return Some(found);
        }
        if let RowKey::Entry(path) = key
            && let Some(root) = self
                .sections
                .iter()
                .find(|s| !s.grouped_by_place && s.rows.iter().any(|r| r.path == *path))
                .and_then(|s| s.root.clone())
            && self.shown_whole.insert(root.clone())
        {
            match self.listed(key) {
                Some(found) => return Some(found),
                None => {
                    self.shown_whole.remove(&root);
                }
            }
        }
        self.behind_recent_cap(key)
    }

    /// Where the row `key` names is on screen, as it is listed now.
    fn listed(&self, key: &RowKey) -> Option<usize> {
        self.visible().iter().position(|row| match (row, key) {
            (Row::Entry { entry, .. }, RowKey::Entry(path)) => entry.path == *path,
            (Row::Door { entry, .. }, RowKey::Door(path)) => entry.path == *path,
            (Row::Place { path, .. }, RowKey::Place(wanted)) => path == wanted,
            (Row::Header { section, .. }, RowKey::Header(title))
            | (Row::More { section, .. }, RowKey::More(title))
            | (Row::Hidden { section, .. }, RowKey::Hidden(title))
            | (Row::Up { section }, RowKey::Up(title)) => self
                .sections
                .get(*section)
                .is_some_and(|s| s.title == *title),
            _ => false,
        })
    }

    /// The `more` row of `RECENT` when its cap hides the row `key` names.
    fn behind_recent_cap(&self, key: &RowKey) -> Option<usize> {
        let (RowKey::Entry(path) | RowKey::Place(path)) = key else {
            return None;
        };
        self.visible().iter().position(|row| {
            matches!(row, Row::More { section, .. }
            if self.sections.get(*section).is_some_and(|s| {
                s.grouped_by_place
                    && s.rows
                        .iter()
                        .any(|r| r.path == *path || place_of(&r.path) == *path)
            }))
        })
    }

    /// Tell the listing the list's height, keeping the cursor on its row: `RECENT`'s cap
    /// is a share of the height. Called every frame; only a change does work.
    pub fn set_view_height(&mut self, height: usize) {
        if height == self.view_height {
            return;
        }
        let key = self.selected_key();
        self.view_height = height;
        self.reselect(key);
    }

    /// Settle the viewport now, as the next frame will: the look-into pass is asked for
    /// when a listing lands, before that frame, and a stale `scroll` from the replaced
    /// listing would spend the batch on rows nobody sees.
    fn follow_selection(&mut self) {
        let rows = self.row_count();
        self.scroll = settle_top(self.scroll, self.selected, self.view_height, rows);
    }

    /// Whether a section is folded: the user's last choice, else its default. Never
    /// while browsing: the browsed listing is the whole screen, and a fold remembered
    /// for a section once titled by that path must not hide it.
    fn section_folded(&self, section: &Section) -> bool {
        if self.browsing.is_some() {
            return false;
        }
        self.folds
            .get(&section.title)
            .copied()
            .unwrap_or(section.folded_by_default)
    }

    /// Whether a section is collapsed.
    pub fn is_collapsed(&self, section: usize) -> bool {
        self.sections
            .get(section)
            .is_some_and(|s| self.section_folded(s))
    }

    /// Collapse or expand a section.
    pub fn toggle_collapsed(&mut self, section: usize) {
        let folded = self.is_collapsed(section);
        self.set_collapsed(section, !folded);
    }

    /// Fold or unfold `section`, remembered by title. Nothing is remembered while
    /// browsing: the browsed listing never folds, and its path is also a root section's
    /// title.
    pub fn set_collapsed(&mut self, section: usize, collapsed: bool) {
        if self.browsing.is_some() {
            return;
        }
        let Some(title) = self.sections.get(section).map(|s| s.title.clone()) else {
            return;
        };
        self.folds.insert(title, collapsed);
    }

    /// Move the cursor to the next (`delta` > 0) or previous section header, wrapping.
    pub fn jump_section(&mut self, delta: isize) {
        self.returning = None;
        self.landing = false;
        let rows = self.visible();
        let headers: Vec<usize> = rows
            .iter()
            .enumerate()
            .filter(|(_, r)| matches!(r, Row::Header { .. }))
            .map(|(i, _)| i)
            .collect();
        if headers.is_empty() {
            return;
        }
        let current = self.selected;
        self.selected = if delta > 0 {
            headers
                .iter()
                .copied()
                .find(|&h| h > current)
                .unwrap_or(headers[0])
        } else {
            headers
                .iter()
                .rev()
                .copied()
                .find(|&h| h < current)
                .unwrap_or(*headers.last().unwrap())
        };
    }

    /// Whether the listing holds anywhere to go, folded or not: datasets, lake tables
    /// (not readable as tables, but places), and rows not yet looked into, which may be
    /// datasets. Hence [`EntryKind::is_dataset`], not [`EntryKind::is_known_dataset`].
    pub fn has_any_dataset(&self) -> bool {
        self.view().has_dataset
    }

    /// Title of the section of recursive search results; fold state is keyed by title.
    pub const SEARCH_SECTION: &'static str = "Found";

    /// Title of the section listing cloud sources.
    pub const CLOUD_SECTION: &'static str = "Cloud";

    /// Title of the section listing what has been opened, grouped by place.
    pub const RECENT_SECTION: &'static str = "Recent";

    /// The source a place belongs to: `cloud://<id>` itself, a bucket named with a
    /// source (`s3://<id>@bucket`), or a bucket some source listed.
    pub fn cloud_source_of(&self, path: &Path) -> Option<&CloudSource> {
        if let Some(id) = cloud_source_id(path) {
            return self.cloud.iter().find(|s| s.id == id);
        }
        if let Some((id, _)) = cloud_account(path) {
            return self.cloud.iter().find(|s| s.id == id);
        }
        let text = path.to_string_lossy();
        if let Some((account, _, _)) = crate::cloud::source::azure_parts(&text) {
            return self.azure_account_place(&account).and_then(|place| {
                cloud_account(&place).and_then(|(id, _)| self.cloud.iter().find(|s| s.id == id))
            });
        }
        if let (Some(id), _) = crate::cloud::source::split_source_id(&text) {
            return self.cloud.iter().find(|s| s.id == id);
        }
        if let Some(project) =
            Self::google_bucket_root(path).and_then(|b| self.project_of_bucket(&b))
        {
            return cloud_account(&project)
                .and_then(|(id, _)| self.cloud.iter().find(|s| s.id == id));
        }
        let (_, plain) = crate::cloud::source::split_source_id(&text);
        let (scheme, rest) = plain.split_once("://")?;
        let bucket = rest.split('/').next()?;
        let root = PathBuf::from(format!("{scheme}://{bucket}"));
        self.cloud.iter().find(|s| s.buckets.contains(&root))
    }

    /// One level up from `path`. A bucket's parent is its source, so Backspace from a
    /// bucket returns to the source.
    pub fn parent_of(&self, path: &Path) -> Option<PathBuf> {
        if cloud_source_id(path).is_some() {
            return None;
        }
        // Out of a remote dataset's root goes back to its catalog, not into a bucket that
        // may not be listable.
        if let Some((_, dataset)) = self.remote_dataset_of(path) {
            let place = &dataset.location;
            if same_place(path, place) {
                return None;
            }
            let up = self.parent_within(path)?;
            // The dataset's own place, as listed, so its listing is found again.
            return Some(if same_place(&up, place) {
                place.clone()
            } else {
                up
            });
        }
        if let Some((id, _)) = cloud_account(path) {
            return Some(cloud_place(&id));
        }
        let text = path.to_string_lossy();
        if let Some((account, container, key)) = crate::cloud::source::azure_parts(&text) {
            let key = key.trim_matches('/');
            if key.is_empty() {
                return self.azure_account_place(&account);
            }
            let up = key.rsplit_once('/').map(|(up, _)| up).unwrap_or("");
            let up = if up.is_empty() {
                String::new()
            } else {
                format!("{up}/")
            };
            return Some(PathBuf::from(crate::cloud::source::azure_url(
                &account, &container, &up,
            )));
        }
        if is_bucket_root(path) {
            // A Google bucket's parent is the project it was listed under.
            if let Some(project) = self.project_of_bucket(path) {
                return Some(project);
            }
            return self.cloud_source_of(path).map(|s| cloud_place(&s.id));
        }
        parent_location(path)
    }

    /// One level up inside a bucket or container, whatever the provider.
    fn parent_within(&self, path: &Path) -> Option<PathBuf> {
        let text = path.to_string_lossy();
        if let Some((account, container, key)) = crate::cloud::source::azure_parts(&text) {
            let key = key.trim_matches('/');
            let up = key.rsplit_once('/').map(|(up, _)| up).unwrap_or("");
            let up = if up.is_empty() {
                String::new()
            } else {
                format!("{up}/")
            };
            return Some(PathBuf::from(crate::cloud::source::azure_url(
                &account, &container, &up,
            )));
        }
        parent_location(path)
    }

    /// The remote catalog dataset `path` is in: the innermost, or the first listed of two
    /// at the same place.
    fn remote_dataset_of(&self, path: &Path) -> Option<(&ShownCatalog, &ShownDataset)> {
        if !is_object_store_url(path) {
            return None;
        }
        let text = path.to_string_lossy();
        self.catalogs
            .iter()
            .flat_map(|c| c.datasets.iter().map(move |d| (c, d)))
            .filter(|(_, d)| {
                is_object_store_url(&d.location) && within(&text, &d.location.to_string_lossy())
            })
            .rev()
            .max_by_key(|(_, d)| d.location.to_string_lossy().trim_end_matches('/').len())
    }

    /// The catalog dataset listed at `path` itself.
    pub fn catalog_dataset(&self, path: &Path) -> Option<(&ShownCatalog, &ShownDataset)> {
        let places = &self.catalog_places;
        if places.indexes(&self.catalogs) {
            let key = place_key(path);
            let &(c, d) = places.datasets.get(&key)?;
            let catalog = self.catalogs.get(c)?;
            if let Some(dataset) = catalog.datasets.get(d)
                && place_key(&dataset.location) == key
            {
                return Some((catalog, dataset));
            }
        }
        self.catalogs
            .iter()
            .flat_map(|c| c.datasets.iter().map(move |d| (c, d)))
            .find(|(_, d)| same_place(&d.location, path))
    }

    /// The dataset a bookmark is listed under, and the bookmark's name.
    pub fn bookmark(&self, path: &Path) -> Option<(&ShownDataset, &str)> {
        let places = &self.catalog_places;
        if places.indexes(&self.catalogs) {
            let key = place_key(path);
            let &(c, d, b) = places.bookmarks.get(&key)?;
            if let Some(dataset) = self.catalogs.get(c).and_then(|c| c.datasets.get(d))
                && let Some((name, place)) = dataset.bookmarks.get(b)
                && place_key(place) == key
            {
                return Some((dataset, name.as_str()));
            }
        }
        self.catalogs
            .iter()
            .flat_map(|c| c.datasets.iter())
            .find_map(|d| {
                d.bookmarks
                    .iter()
                    .find(|(_, place)| same_place(place, path))
                    .map(|(name, _)| (d, name.as_str()))
            })
    }

    /// What a catalog says an unmeasured HTTP(S) file weighs, shown as `~33 MB`.
    pub fn size_hint(&self, path: &Path) -> Option<u64> {
        self.catalog_dataset(path).and_then(|(_, d)| d.size)
    }

    /// The place of an Azure account, from whichever source lists it.
    fn azure_account_place(&self, account: &str) -> Option<PathBuf> {
        self.cloud
            .iter()
            .flat_map(|s| s.buckets.iter())
            .find(|place| cloud_account(place).is_some_and(|(_, a)| a == account))
            .cloned()
    }

    /// How far an object-store directory is in being looked into, while its row has no
    /// label. `None` once labeled, or for other rows.
    pub fn cloud_look(&self, entry: &Entry) -> Option<CloudLook> {
        if entry.kind != EntryKind::Directory
            || !entry.holds.is_empty()
            || !is_object_store_url(&entry.path)
            || is_cloud_place(&entry.path)
            || object_place_label(&entry.path).is_some()
        {
            return None;
        }
        if self.peeking.contains(&entry.path) {
            return Some(CloudLook::Looking);
        }
        if self.peek_failed.contains(&entry.path) {
            return Some(CloudLook::Failed);
        }
        match self.cloud_kinds.get(&entry.path) {
            None => Some(CloudLook::Waiting),
            // Answered, but the row is from a listing being rebuilt: `dir` meanwhile would claim
            // no data.
            Some((kind, holds)) if *kind != EntryKind::Directory || !holds.is_empty() => {
                Some(CloudLook::Looking)
            }
            Some(_) => None,
        }
    }

    /// What a source or catalog calls a place: a catalog's remote dataset, or `missing`
    /// for an absent local one.
    pub fn place_kind(&self, path: &Path) -> Option<&'static str> {
        if self.missing.contains(path) {
            return Some("missing");
        }
        if let Some((id, _)) = cloud_account(path) {
            return self
                .cloud
                .iter()
                .any(|s| s.id == id && s.api == crate::cloud::source::ProviderKind::Gcs)
                .then_some("project");
        }
        // A bookmark inside a catalog dataset opens whole, as a dataset does.
        (is_object_store_url(path)
            && (self
                .catalog_dataset(path)
                .is_some_and(|(_, d)| is_object_store_url(&d.location))
                || (self.browsing.is_none() && self.bookmark(path).is_some())))
        .then_some("dataset")
    }

    /// The project place a Google bucket was listed under, when it was.
    fn project_of_bucket(&self, bucket_root: &Path) -> Option<PathBuf> {
        let root = bucket_root.to_string_lossy();
        let root = root.trim_end_matches('/');
        self.probes
            .answered()
            .filter(|(place, _)| cloud_account(place).is_some())
            .find(|(_, rows)| {
                rows.iter()
                    .any(|row| row.path.to_string_lossy().trim_end_matches('/') == root)
            })
            .map(|(place, _)| place.clone())
    }

    /// The bucket root of a Google URL: `gs://bucket`.
    fn google_bucket_root(path: &Path) -> Option<PathBuf> {
        let text = path.to_string_lossy();
        let rest = text
            .strip_prefix("gs://")
            .or_else(|| text.strip_prefix("gcs://"))?;
        let bucket = rest.split('/').next().filter(|b| !b.is_empty())?;
        Some(PathBuf::from(format!("gs://{bucket}")))
    }

    /// Details-pane lines for a place a cloud source listed or a catalog names, when
    /// it has any.
    pub fn place_details(&self, path: &Path) -> Option<&[(String, String)]> {
        self.cloud
            .iter()
            .find_map(|s| s.place_details.get(path))
            .or_else(|| self.catalog_dataset(path).map(|(_, d)| &d.details))
            .or_else(|| self.bookmark(path).map(|(d, _)| &d.details))
            .map(Vec::as_slice)
    }

    /// The location as the title bar names it; cloud places read as a trail through the
    /// source's label.
    pub fn location_label(&self, path: &Path) -> String {
        let sep = crate::glyphs::get().trail;
        // Inside a catalog's remote dataset: the catalog, the dataset's name, and the path
        // below it.
        if let Some((catalog, dataset)) = self.remote_dataset_of(path) {
            let text = path.to_string_lossy();
            let rest = within_rest(&text, &dataset.location.to_string_lossy());
            let mut parts = vec![catalog.label.clone(), dataset.name.clone()];
            parts.extend(
                rest.split('/')
                    .filter(|p| !p.is_empty())
                    .map(str::to_string),
            );
            return parts.join(&format!(" {sep} "));
        }
        if let Some(source) = self.cloud_source_of(path) {
            let mut parts = vec!["cloud".to_string(), source.label.clone()];
            let text = path.to_string_lossy();
            if let Some((_, account)) = cloud_account(path) {
                parts.push(account);
            } else if let Some((account, container, key)) = crate::cloud::source::azure_parts(&text)
            {
                parts.push(account);
                parts.push(container);
                parts.extend(key.split('/').filter(|p| !p.is_empty()).map(str::to_string));
            } else if cloud_source_id(path).is_none() {
                if let Some((_, project)) = Self::google_bucket_root(path)
                    .and_then(|b| self.project_of_bucket(&b))
                    .as_deref()
                    .and_then(cloud_account)
                {
                    parts.push(project);
                }
                let (_, plain) = crate::cloud::source::split_source_id(&text);
                if let Some((_, rest)) = plain.split_once("://") {
                    parts.extend(
                        rest.split('/')
                            .filter(|p| !p.is_empty())
                            .map(str::to_string),
                    );
                }
            }
            return parts.join(&format!(" {sep} "));
        }
        display_path(path)
    }

    /// Put the search results into `sections`, or take them out, after every rebuild and
    /// batch. Only while there is a filter: unfiltered, everything matches.
    pub fn sync_search_section(&mut self) {
        // The other sections keep their hits: results land in batches while the user types.
        let found = (self.sections.iter()).position(|s| s.title == Self::SEARCH_SECTION);
        let mut kept = self.take_hits();
        self.sections.retain(|s| s.title != Self::SEARCH_SECTION);
        self.changed();
        if let (Some(hits), Some(at)) = (kept.as_mut(), found)
            && at < hits.sections.len()
        {
            hits.sections.remove(at);
        }
        *self.rows_cache.rescored.get_mut() = kept;

        if self.filter.is_empty() {
            return;
        }
        // Bucket names already listed; nothing is fetched (that would bill per keystroke).
        let cloud_rows: Vec<Entry> = if self.browsing.is_none() {
            self.cloud
                .iter()
                .flat_map(|source| {
                    source.buckets.iter().map(move |bucket| {
                        let mut entry = bucket_entry(bucket);
                        entry.name = format!(
                            "{} {} {}",
                            source.label,
                            crate::glyphs::get().trail,
                            entry.name
                        );
                        entry.cost.source = Some(source.api.name().to_string());
                        entry
                    })
                })
                .filter(|e| match_score(&self.filter, e).is_some())
                .collect()
        } else {
            Vec::new()
        };
        self.score_search_inline();
        let local = self.search.root.is_some()
            && (self.search.indexed > 0 || self.search.running || self.search.limited.is_some());
        if !local {
            if !cloud_rows.is_empty() {
                let subtitle =
                    crate::glyphs::dotted(&format!("cloud · {} names", cloud_rows.len()));
                self.sections.push(Section {
                    subtitle: Some(subtitle),
                    ..Section::titled(Self::SEARCH_SECTION, cloud_rows)
                });
            }
            return;
        }

        // The last scored matches; those for an older filter (a scoring out) are rescored
        // here once, so nothing stale shows.
        let matches = self.search.matches.as_ref();
        // A dataset already listed under its directory does not appear again under the
        // search. Only rows named as a match can be one: the rest of thousands are passed
        // over on a short name, not a path hashed component by component.
        let names: std::collections::HashSet<&std::ffi::OsStr> = (matches.iter())
            .flat_map(|m| m.top.iter())
            .map(|e| e.path.file_name().unwrap_or(e.path.as_os_str()))
            .collect();
        let listed: std::collections::HashSet<&PathBuf> = (self.sections.iter())
            .flat_map(|s| s.rows.iter().map(|r| &r.path))
            .filter(|path| path.file_name().is_none_or(|name| names.contains(name)))
            .collect();
        let kept: Vec<(&Entry, i32)> = matches
            .map(|m| {
                let fresh = m.query == self.filter;
                m.top
                    .iter()
                    .zip(m.scores.iter().copied())
                    .filter(|(e, _)| !listed.contains(&e.path))
                    .filter_map(|(e, score)| {
                        if fresh {
                            Some((e, score))
                        } else {
                            match_score(&self.filter, e).map(|s| (e, s))
                        }
                    })
                    .collect()
            })
            .unwrap_or_default();
        let mut rows: Vec<Entry> = kept.into_iter().map(|(e, _)| e.clone()).collect();
        rows.extend(cloud_rows);
        // The walk's rows are snapshots: what this session measured of them is folded in
        // here, as nothing measures them again.
        for row in &mut rows {
            if let Some(m) = self.enriched.get(&row.path) {
                fold_measured(row, m);
            }
        }

        // Say an empty result when the walk stopped short: "no match" may be wrong then.
        let partial = self.search.limited.is_some();
        let scored = self.search.scored_for(&self.filter);
        if rows.is_empty() && !self.search.running && !partial && scored {
            return;
        }

        let subtitle = self.found_subtitle(rows.is_empty());

        self.sections.push(Section {
            subtitle: Some(subtitle),
            ..Section::titled(Self::SEARCH_SECTION, rows)
        });
    }

    /// `Found`'s rule: where it looked, how many matched, how far the walk got.
    fn found_subtitle(&self, empty: bool) -> String {
        let root = self.search.root.clone().unwrap_or_default();
        let dot = crate::glyphs::get().middot;
        let files = crate::numfmt::group_chrome(self.search.indexed);
        let files = if self.search.indexed == 1 {
            format!("{files} file")
        } else {
            format!("{files} files")
        };
        let subtitle = display_path(&root);
        // How many matched when more matched than listed; last, since the rule cuts long
        // notes from the start.
        let counted = self
            .search
            .matches
            .as_ref()
            .filter(|m| m.query == self.filter && m.ids.len() > m.top.len())
            .map(|m| {
                format!(
                    " {dot} {} of {} matches",
                    crate::numfmt::group_chrome(m.top.len()),
                    crate::numfmt::group_chrome(m.ids.len())
                )
            })
            .unwrap_or_default();
        if self.search.running {
            format!(
                "{subtitle} {dot} searching {}{counted}",
                crate::numfmt::group_chrome(self.search.scanned)
            )
        } else if !self.search.scored_for(&self.filter) {
            format!("{subtitle} {dot} matching {files}")
        } else if empty {
            match &self.search.limited {
                Some(limit) => format!("{subtitle} {dot} no match in {files} {dot} {limit}"),
                None => format!("{subtitle} {dot} no match in {files}"),
            }
        } else {
            let searched = crate::numfmt::group_chrome(self.search.scanned);
            match &self.search.limited {
                Some(limit) => {
                    format!("{subtitle} {dot} {limit} {dot} {searched} searched{counted}")
                }
                None => format!("{subtitle} {dot} {searched} searched{counted}"),
            }
        }
    }

    /// Fold a batch of search results in, keeping the list free of duplicates.
    pub fn search_batch(&mut self, root: &Path, mut found: Vec<Entry>, scanned: usize) {
        // A batch from a walk the user left is dropped: walks are abandoned, not cancelled.
        if self.search.root.as_deref() != Some(root) {
            return;
        }
        // Earlier measurements give found rows their shape and columns (the filter matches
        // columns), strictly fingerprinted by size and mtime.
        for row in &mut found {
            apply_known_facts(row, &self.known, false);
        }
        self.search.scanned = scanned;
        let start = self.search.indexed;
        let batch: std::sync::Arc<[Entry]> = found.into();
        if !batch.is_empty() {
            self.search.indexed += batch.len();
            self.search.results.push(batch.clone());
        }
        // Carry matches forward over the new files alone: rescoring everything per batch
        // delayed typed keys during large walks.
        let limit = self.search_limit;
        let changed = match self.search.matches.as_mut() {
            Some(m) if m.query == self.filter && m.upto == start => m.extend(&batch, start, limit),
            _ => {
                let before = self.search.matches.as_ref().map(|m| m.upto);
                self.score_search_inline();
                self.search.matches.as_ref().map(|m| m.upto) != before
            }
        };
        // Rebuild `Found` only when its contents changed; otherwise only its progress moves.
        let found_listed = self
            .sections
            .iter()
            .position(|s| s.title == Self::SEARCH_SECTION);
        match found_listed {
            Some(at) if !changed => {
                let empty = self.sections[at].rows.is_empty();
                self.sections[at].subtitle = Some(self.found_subtitle(empty));
            }
            _ => self.sync_search_section(),
        }
        self.settle_return();
    }

    /// Score the filter against the files found, here and now, when that is cheap.
    fn score_search_inline(&mut self) {
        if self.filter.is_empty() || self.search.scored_for(&self.filter) {
            return;
        }
        let (base, looks_at) = self.search.base_for(&self.filter);
        if looks_at > SCORE_INLINE_MAX {
            return;
        }
        let scored =
            crate::home::search::score(&self.search.results, &self.filter, base, self.search_limit);
        self.search.matches = Some(scored);
    }

    /// The scoring a worker should do next, if owed (the filter changed, or files
    /// arrived). One at a time; the next is asked when it answers.
    pub fn score_job(&mut self) -> Option<ScoreJob> {
        if self.filter.is_empty() || self.search.scoring || self.search.scored_for(&self.filter) {
            return None;
        }
        let base = self.search.base_for(&self.filter).0.cloned();
        self.search.scoring = true;
        Some(ScoreJob {
            epoch: self.search.epoch,
            results: self.search.results.clone(),
            query: self.filter.clone(),
            base,
            limit: self.search_limit,
        })
    }

    /// A worker's scoring is in.
    pub fn search_scored(&mut self, epoch: u64, scored: crate::home::search::Matches) {
        if epoch != self.search.epoch {
            return;
        }
        self.search.scoring = false;
        // Batches may have carried the same filter's matches further meanwhile.
        if self
            .search
            .matches
            .as_ref()
            .is_some_and(|m| m.query == scored.query && m.upto >= scored.upto)
        {
            return;
        }
        self.search.matches = Some(scored);
        self.sync_search_section();
        // If typing left the cursor on nothing, the first match is where it belongs.
        if !matches!(self.selected_row(), Some(Row::Entry { .. })) {
            self.select_first_entry();
        }
        self.settle_return();
    }

    /// Record that the walk under `root` has finished.
    pub fn search_finished(&mut self, root: &Path, scanned: usize, limited: Option<String>) {
        if self.search.root.as_deref() != Some(root) {
            return;
        }
        self.search.running = false;
        self.search.done = true;
        // Never fewer than the batches reported: a walk that died says nothing of its own.
        self.search.scanned = self.search.scanned.max(scanned);
        self.search.limited = limited;
        self.sync_search_section();
        self.settle_return();
    }

    /// Lines on screen: a header per non-empty section, then its matching rows unless
    /// folded. Results stay grouped while filtering, to show where a dataset lives.
    pub fn visible(&self) -> Vec<Row<'_>> {
        let view = self.view();
        view.slots.iter().map(|slot| self.row(slot)).collect()
    }

    /// Which rows of [`HomeState::visible`] are headers, without building the rows.
    pub fn header_rows(&self) -> Vec<bool> {
        (self.view().slots.iter())
            .map(|slot| matches!(slot, Slot::Plain(Row::Header { .. })))
            .collect()
    }

    /// How many rows the filter matches: the sum of the section headers' counts.
    pub fn matched(&self) -> usize {
        (self.view().slots.iter())
            .map(|slot| match slot {
                Slot::Plain(Row::Header { matches, .. }) => *matches,
                _ => 0,
            })
            .sum()
    }

    /// How many rows [`HomeState::visible`] lists.
    pub fn row_count(&self) -> usize {
        self.view().slots.len()
    }

    /// Row `index` of [`HomeState::visible`].
    pub fn row_at(&self, index: usize) -> Option<Row<'_>> {
        self.view().slots.get(index).map(|slot| self.row(slot))
    }

    /// How many times the rows were built, for the test that a frame builds them at most
    /// once.
    pub fn rows_built(&self) -> usize {
        self.rows_cache.builds.get()
    }

    /// The rows have changed under the cache: built again on the next read.
    fn changed(&mut self) {
        *self.rows_cache.built.get_mut() = None;
        *self.rows_cache.rescored.get_mut() = None;
    }

    /// The hits the cache holds for the rows as listed now, for the filter as it is;
    /// the rows are built again on the next read.
    fn take_hits(&mut self) -> Option<Hits> {
        let built = self.rows_cache.built.get_mut().take();
        match self.rows_cache.rescored.get_mut().take() {
            Some(hits) => Some(hits),
            None => built
                .filter(|view| view.key.matches(self))
                .map(|view| view.hits),
        }
        .filter(|hits| hits.filter == self.filter)
    }

    /// Only `touched` rows (section, index) changed, in what they hold rather than in
    /// name or number: the next build scores those again and keeps the last build's
    /// hits for the rest.
    fn rows_changed(&mut self, touched: &[(usize, usize)]) {
        let Some(mut hits) = self.take_hits() else {
            return;
        };
        let needle = Needle::new(&self.filter);
        for &(si, i) in touched {
            if let Some(Some(section)) = hits.sections.get_mut(si)
                && let Some(hit) = section.get_mut(i)
            {
                *hit = needle.hit(&self.sections[si].rows[i]);
            }
        }
        *self.rows_cache.rescored.get_mut() = Some(hits);
    }

    /// Show these catalogs.
    pub fn set_catalogs(&mut self, catalogs: Vec<ShownCatalog>) {
        self.catalog_places = CatalogPlaces::of(&catalogs);
        self.catalogs = catalogs;
        self.changed();
    }

    /// How often and how lately each recent was opened, which ranks matches.
    pub fn set_visits(&mut self, visits: std::collections::HashMap<PathBuf, crate::cache::Visits>) {
        self.visits = visits;
        self.changed();
    }

    /// Show the whole of the section the `more` row at `section` cuts.
    pub fn show_all(&mut self, section: usize) {
        match self.sections.get(section) {
            Some(s) if s.grouped_by_place => self.recent_expanded = true,
            Some(Section {
                root: Some(root), ..
            }) => {
                self.shown_whole.insert(root.clone());
            }
            _ => {}
        }
    }

    /// With the cursor on a row the cut would hide in a section shown whole, cut it back,
    /// the cursor on the row standing for the rest. Whether it did.
    pub fn cut_again(&mut self, section: usize) -> bool {
        let Some(key) = self.selected_key() else {
            return false;
        };
        let Some(s) = self.sections.get(section) else {
            return false;
        };
        let title = s.title.clone();
        let whole = if s.grouped_by_place {
            None
        } else {
            match s.root.clone() {
                Some(root) => Some(root),
                None => return false,
            }
        };
        let was_whole = match &whole {
            None => std::mem::replace(&mut self.recent_expanded, false),
            Some(root) => self.shown_whole.remove(root),
        };
        if !was_whole {
            return false;
        }
        if self.listed(&key).is_some() {
            match whole {
                None => self.recent_expanded = true,
                Some(root) => {
                    self.shown_whole.insert(root);
                }
            }
            return false;
        }
        self.reselect(Some(RowKey::More(title)));
        true
    }

    /// The sections, to change in place; rows are relisted on the next read.
    pub fn sections_mut(&mut self) -> &mut Vec<Section> {
        self.changed();
        &mut self.sections
    }

    fn view(&self) -> std::cell::Ref<'_, View> {
        let fresh = self
            .rows_cache
            .built
            .borrow()
            .as_ref()
            .is_some_and(|view| view.key.matches(self));
        if !fresh {
            let view = self.build_view();
            self.rows_cache.builds.set(self.rows_cache.builds.get() + 1);
            *self.rows_cache.built.borrow_mut() = Some(view);
        }
        std::cell::Ref::map(self.rows_cache.built.borrow(), |view| {
            view.as_ref().expect("built above")
        })
    }

    fn row<'a>(&'a self, slot: &Slot) -> Row<'a> {
        match slot {
            Slot::Plain(row) => row.clone(),
            Slot::Entry {
                section,
                index,
                nested,
                hit,
            } => Row::Entry {
                section: *section,
                entry: &self.sections[*section].rows[*index],
                nested: *nested,
                hit: *hit,
            },
            Slot::Door { section } => Row::Door {
                section: *section,
                entry: self.sections[*section]
                    .door
                    .as_ref()
                    .expect("the shape says it has a door"),
            },
        }
    }

    fn build_view(&self) -> View {
        // Taken, so a build for any other change scores every row.
        let kept = (self.rows_cache.rescored.take()).filter(|hits| {
            hits.filter == self.filter && hits.sections.len() <= self.sections.len()
        });
        let hits = self.score_rows(kept);
        View {
            key: ViewKey::of(self),
            slots: self.slots(&hits),
            hits,
            has_dataset: self
                .sections
                .iter()
                .flat_map(|s| s.rows.iter())
                .any(|e| e.kind.is_dataset() || e.kind.is_lake_table()),
        }
    }

    /// Every row scored against the filter, but for the sections `kept` holds for the
    /// rows as listed.
    fn score_rows(&self, kept: Option<Hits>) -> Hits {
        let needle = Needle::new(&self.filter);
        let mut kept = kept.map(|hits| hits.sections).unwrap_or_default();
        let sections =
            (self.sections.iter().enumerate())
                .map(|(si, section)| {
                    let reused = (kept.get_mut(si).and_then(Option::take))
                        .filter(|hits| hits.len() == section.rows.len());
                    Some(reused.unwrap_or_else(|| {
                        (section.rows.iter()).map(|row| needle.hit(row)).collect()
                    }))
                })
                .collect();
        Hits {
            filter: self.filter.clone(),
            sections,
        }
    }

    fn slots(&self, hits: &Hits) -> Vec<Slot> {
        let mut out: Vec<Slot> = Vec::new();
        for (si, section) in self.sections.iter().enumerate() {
            let mut matched: Vec<(usize, Hit)> = (section.rows.iter())
                .zip(hits.sections[si].iter().flatten())
                .enumerate()
                .filter(|(_, (row, _))| !(self.hide_unreadable && row.hidden_by_default()))
                .filter_map(|(i, (_, hit))| hit.map(|hit| (i, hit)))
                .collect();

            // Drop a section with nothing to show, unless it stands for a named or current root,
            // its rows are on the way, or it has a door (extensionless part files list nothing,
            // and the door is the only way to read them).
            let keep_empty = section.unavailable || section.waiting || section.origin.is_some();
            let has_door = section.door.is_some() && self.filter.is_empty();
            // Only inside a directory, where an empty listing needs a reason; root sections
            // leave them out quietly.
            let hidden =
                if self.browsing.is_some() && self.hide_unreadable && self.filter.is_empty() {
                    section
                        .rows
                        .iter()
                        .filter(|row| row.hidden_by_default())
                        .count()
                } else {
                    0
                };
            // `Found` shows empty only to say something: a walk stopped short with no match.
            let says_why = section.title == Self::SEARCH_SECTION;
            if matched.is_empty()
                && !has_door
                && hidden == 0
                && !says_why
                && !(keep_empty && self.filter.is_empty())
            {
                continue;
            }

            // Rank by match quality within a section (unfiltered, scores tie and the curated
            // order stays). Ties go to the shorter name, fzf's tiebreak. Frecency lifts an
            // often-opened row by up to a few characters' worth of match.
            let entry = |i: usize| &section.rows[i];
            if !self.filter.is_empty() {
                let now = std::time::SystemTime::now()
                    .duration_since(std::time::UNIX_EPOCH)
                    .map(|d| d.as_secs())
                    .unwrap_or_default();
                let lifted = |i: usize, score: i32| {
                    let frecency = self
                        .visits
                        .get(&entry(i).path)
                        .map_or(0.0, |v| v.frecency(now));
                    score.saturating_add((frecency.min(10.0) * FRECENCY_LIFT) as i32)
                };
                matched.sort_by_cached_key(|(i, hit)| {
                    (
                        std::cmp::Reverse(lifted(*i, hit.score)),
                        entry(*i).name.len(),
                    )
                });
            }

            // An explicit sort overrides both; rows with nothing to sort by go last, not as
            // zero.
            match self.sort {
                SortMode::Natural => {}
                SortMode::Size => {
                    matched.sort_by_key(|(i, _)| std::cmp::Reverse(entry(*i).size.unwrap_or(0)));
                }
                SortMode::Rows => {
                    matched.sort_by_key(|(i, _)| std::cmp::Reverse(entry(*i).rows.unwrap_or(0)));
                }
                SortMode::Modified => {
                    matched.sort_by_key(|(i, _)| {
                        std::cmp::Reverse(
                            entry(*i)
                                .modified
                                .and_then(|m| m.duration_since(std::time::UNIX_EPOCH).ok())
                                .map(|d| d.as_secs())
                                .unwrap_or(0),
                        )
                    });
                }
            }

            let collapsed = self.section_folded(section);
            out.push(Slot::Plain(Row::Header {
                section: si,
                // The door is not counted: it opens the directory, so three files must not read four.
                matches: matched.len(),
                collapsed,
            }));
            if collapsed {
                continue;
            }
            // The way up first, as `..` in any listing.
            let root = section.root.as_deref();
            if self.filter.is_empty()
                && root
                    .is_some_and(|root| self.browsing.is_some() || self.parent_of(root).is_some())
            {
                out.push(Slot::Plain(Row::Up { section: si }));
            }
            // The door next, whatever the sort. Not while filtering: its `all files` name
            // fuzzy-matches most of the alphabet.
            if has_door {
                out.push(Slot::Door { section: si });
            }
            // At the root listing a huge directory shows its first rows (a share of the
            // height) and one row for the rest, so it does not bury the sections below. A filter
            // searches them all.
            let shown = match root {
                Some(_)
                    if self.browsing.is_none()
                        && self.filter.is_empty()
                        && self.view_height > 0
                        && !root.is_some_and(|root| self.shown_whole.contains(root)) =>
                {
                    (self.view_height * 2 / 5).max(8)
                }
                _ => usize::MAX,
            };
            let rest = if matched.len() > shown.saturating_add(1) {
                matched.split_off(shown)
            } else {
                Vec::new()
            };
            // Sorted by rows, rows past the cut are measured too, or the biggest shown would pass
            // for the biggest of all.
            let measuring = self.sort == SortMode::Rows
                && rest.iter().any(|(i, _)| self.wants_measuring(entry(*i)));
            if section.grouped_by_place {
                self.slots_by_place(si, section, &matched, &mut out);
            } else {
                // A bookmark sits under its dataset while rows keep the catalog's order.
                let in_order = self.sort == SortMode::Natural
                    && self.filter.is_empty()
                    && section.origin.is_some_and(is_catalog_origin)
                    && section.root.is_none();
                out.extend(matched.into_iter().map(|(index, hit)| Slot::Entry {
                    section: si,
                    index,
                    nested: in_order && self.bookmark(&entry(index).path).is_some(),
                    hit,
                }));
            }
            if !rest.is_empty() {
                out.push(Slot::Plain(Row::More {
                    section: si,
                    hidden: rest.len(),
                    places: 0,
                    measuring,
                }));
            }
            if hidden > 0 {
                out.push(Slot::Plain(Row::Hidden {
                    section: si,
                    count: hidden,
                }));
            }
        }
        out
    }

    /// A grouped section's rows under each one's place, and what the cap hides. Places
    /// in order of their newest row; within a place the rows keep `matched`'s order, so
    /// a sort orders each place. Whole places are shown newest first until a third of
    /// the height is used (at least one), then one `… N more in M places` row. A filter
    /// shows every match, without the cap.
    fn slots_by_place(
        &self,
        si: usize,
        section: &Section,
        matched: &[(usize, Hit)],
        out: &mut Vec<Slot>,
    ) {
        let places: Vec<PathBuf> = section.rows.iter().map(|row| place_of(&row.path)).collect();
        let mut order: Vec<&PathBuf> = Vec::new();
        for place in &places {
            if !order.contains(&place) {
                order.push(place);
            }
        }
        let groups: Vec<(&PathBuf, Vec<&(usize, Hit)>)> = order
            .into_iter()
            .filter_map(|place| {
                let rows: Vec<&(usize, Hit)> = matched
                    .iter()
                    .filter(|(i, _)| places[*i] == *place)
                    .collect();
                (!rows.is_empty()).then_some((place, rows))
            })
            .collect();

        // Before the first frame there is no height; a caller with no screen gets it whole.
        let capped = !self.recent_expanded && self.filter.is_empty() && self.view_height > 0;
        let budget = self.view_height / 3;
        let mut used = 0usize;
        let mut shown = 0usize;
        for (place, rows) in &groups {
            let cost = 1 + rows.len();
            if capped && shown > 0 && used + cost > budget {
                break;
            }
            out.push(Slot::Plain(Row::Place {
                section: si,
                path: (*place).clone(),
                label: section.place_labels.get(*place).cloned(),
                // A place's rows share its filesystem, so the first speaks for it (from `annotate`).
                source: section.rows[rows[0].0].cost.source.clone(),
                held: places.iter().filter(|p| p == place).count(),
            }));
            out.extend(rows.iter().map(|(index, hit)| Slot::Entry {
                section: si,
                index: *index,
                nested: true,
                hit: *hit,
            }));
            used += cost;
            shown += 1;
        }
        if shown < groups.len() {
            out.push(Slot::Plain(Row::More {
                section: si,
                hidden: groups[shown..].iter().map(|(_, rows)| rows.len()).sum(),
                places: groups.len() - shown,
                measuring: false,
            }));
        }
    }

    /// The names the `~` prompt offers: those in the typed directory matching its last
    /// segment, best first; hidden names only after a typed dot.
    pub fn path_candidates(&self) -> Vec<&PathName> {
        let Some(listing) = self
            .path_listing
            .as_ref()
            .filter(|l| l.dir == typed_dir(&self.path_input))
        else {
            return Vec::new();
        };
        let segment = &self.path_input[listing.dir.len()..];
        let mut matched: Vec<(&PathName, i32)> = listing
            .names
            .iter()
            .filter(|n| !n.name.starts_with('.') || segment.starts_with('.'))
            .filter_map(|n| {
                if segment.is_empty() {
                    return Some((n, 0));
                }
                // Prefix matches first, as a shell completes; then fuzzy matches.
                let prefix = n.name.starts_with(segment) as i32 * 1_000_000;
                fuzzy_score(segment, &n.name).map(|score| (n, prefix + score))
            })
            .collect();
        matched.sort_by(|(a, sa), (b, sb)| sb.cmp(sa).then_with(|| a.name.cmp(&b.name)));
        matched.into_iter().map(|(n, _)| n).collect()
    }

    /// Put the `~` prompt's pick on the first match, or none, so the list always shows
    /// what Enter and Tab take. ↑ from the first takes the typed path as is.
    pub fn pick_first_path(&mut self) {
        self.path_pick = (!self.path_candidates().is_empty()).then_some(0);
    }

    /// The path the picked candidate names, with its separator when it is a directory.
    pub fn picked_path(&self) -> Option<String> {
        let pick = self.path_pick?;
        let name = *self.path_candidates().get(pick)?;
        let dir = typed_dir(&self.path_input);
        let mut path = format!("{dir}{}", name.name);
        if name.dir {
            path.push(separator_in(dir));
        }
        Some(path)
    }

    /// What Tab makes of the typed path: the sole candidate, or the candidates' longest
    /// common start. `None` when it adds nothing.
    pub fn path_completion(&self) -> Option<String> {
        let dir = typed_dir(&self.path_input);
        let segment = &self.path_input[dir.len()..];
        let candidates = self.path_candidates();
        match candidates.as_slice() {
            [] => None,
            [one] => {
                let mut path = format!("{dir}{}", one.name);
                if one.dir {
                    path.push(separator_in(dir));
                }
                Some(path)
            }
            many => {
                let starting: Vec<&str> = many
                    .iter()
                    .map(|n| n.name.as_str())
                    .filter(|n| n.starts_with(segment))
                    .collect();
                let first = starting.first()?;
                let shared = starting
                    .iter()
                    .skip(1)
                    .fold(first.to_string(), |acc, n| common_prefix(&acc, n));
                (shared.len() > segment.len()).then(|| format!("{dir}{shared}"))
            }
        }
    }

    /// Every URL the screen knows (catalogs, buckets, listings, the index): what `s3://`
    /// completes from.
    pub fn known_urls(&self) -> Vec<String> {
        let mut urls: Vec<String> = Vec::new();
        let mut add = |path: &Path| {
            let text = path.to_string_lossy();
            if text.contains("://") && !is_cloud_place(path) {
                urls.push(text.into_owned());
            }
        };
        for catalog in &self.catalogs {
            for dataset in &catalog.datasets {
                add(&dataset.location);
            }
        }
        for source in &self.cloud {
            for bucket in &source.buckets {
                add(bucket);
            }
        }
        for (root, rows) in self.probes.answered() {
            add(root);
            for row in rows.iter() {
                add(&row.path);
            }
        }
        for path in self.known.keys() {
            add(path);
        }
        for section in &self.sections {
            for row in &section.rows {
                add(&row.path);
            }
        }
        urls
    }

    /// The highlighted row, whatever it is.
    pub fn selected_row(&self) -> Option<Row<'_>> {
        self.row_at(self.selected)
    }

    /// The highlighted row when it is a dataset, door included (it can be opened; it is
    /// [`Row::Door`] only to stay out of path-keyed maps).
    pub fn selected_entry(&self) -> Option<&Entry> {
        match self.selected_row()? {
            Row::Entry { entry, .. } | Row::Door { entry, .. } => Some(entry),
            _ => None,
        }
    }

    /// Whether the cursor is on the door rather than on something in the directory.
    pub fn selection_is_the_door(&self) -> bool {
        matches!(self.selected_row(), Some(Row::Door { .. }))
    }

    /// The recents that live in `place`: what `Delete` on its row forgets.
    pub fn recents_in(&self, place: &Path) -> Vec<PathBuf> {
        self.sections
            .iter()
            .filter(|s| s.grouped_by_place)
            .flat_map(|s| s.rows.iter())
            .filter(|row| place_of(&row.path) == place)
            .map(|row| row.path.clone())
            .collect()
    }

    /// The section the highlighted row belongs to.
    pub fn selected_section(&self) -> Option<usize> {
        self.selected_row().map(|r| r.section())
    }

    /// The catalog whose section heading is selected, if the selection is one.
    pub fn selected_catalog(&self) -> Option<&ShownCatalog> {
        if !self.selection_is_header() {
            return None;
        }
        let section = self.sections.get(self.selected_section()?)?;
        let origin = section.origin?;
        self.catalogs
            .iter()
            .find(|c| c.label == section.title && c.origin_note() == origin)
    }

    /// Whether the highlighted row is a section header.
    pub fn selection_is_header(&self) -> bool {
        matches!(self.selected_row(), Some(Row::Header { .. }))
    }

    /// Remote roots that have neither answered nor been written off, for the caller to
    /// probe off the UI thread.
    pub fn pending_probes(&self) -> Vec<PathBuf> {
        let check = self.network_check;
        let mut out = Vec::new();
        // From the section, not its subtitle (which names the filesystem, not "network").
        for root in self.sections.iter().filter_map(|s| s.remote_root.as_ref()) {
            if !self.probes.settled(root) && !out.contains(root) {
                out.push(root.clone());
            }
        }
        // A browsed remote directory: its rows can only come from a probe, and the root scan
        // above does not cover it.
        if let Some(dir) = &self.browsing
            && check(dir)
            && cloud_source_id(dir).is_none()
            && !self.probes.settled(dir)
            && !out.contains(dir)
        {
            out.push(dir.clone());
        }
        out
    }

    /// Whether the browsed directory is below the browse start, so Esc has a level to
    /// climb before the listing.
    pub fn below_browse_start(&self) -> bool {
        let (Some(dir), Some(start)) = (&self.browsing, &self.browse_start) else {
            return false;
        };
        if dir == start {
            return false;
        }
        // Up through parents, not a path prefix: `s3://bucket` sits below `cloud://<id>`.
        let mut current = self.parent_of(dir);
        let mut steps = 0;
        while let Some(place) = current {
            if &place == start {
                return true;
            }
            steps += 1;
            if steps > 64 {
                break;
            }
            current = self.parent_of(&place);
        }
        false
    }

    /// Whether any section on screen is still waiting for its rows.
    pub fn sections_waiting(&self) -> bool {
        self.sections.iter().any(|s| s.waiting)
    }

    /// The remote location being browsed, while its listing has not come back.
    pub fn awaiting_listing(&self) -> Option<&Path> {
        let dir = self.browsing.as_deref()?;
        if cloud_source_id(dir).is_some() {
            return self
                .cloud_source_of(dir)
                .is_some_and(|s| s.status == CloudStatus::Listing && s.buckets.is_empty())
                .then_some(dir);
        }
        ((self.network_check)(dir) && !self.probes.settled(dir)).then_some(dir)
    }

    /// Record what a probe found. An empty listing is still an answer.
    pub fn probe_ready(&mut self, root: PathBuf, rows: Vec<Entry>, cut_short: bool) {
        self.probes.insert(
            root.clone(),
            Probe::Listed {
                rows: rows.into(),
                cut_short,
            },
        );
        self.apply_cloud_kinds(&root);
    }

    /// Label the rows of a cloud listing with what peeking inside them found.
    pub fn apply_cloud_kinds(&mut self, root: &Path) {
        let Some(rows) = self.probes.listed_mut(root) else {
            return;
        };
        // Copied only when a listing being built still holds these rows.
        for row in std::sync::Arc::make_mut(rows).iter_mut() {
            if row.kind == EntryKind::Directory
                && let Some((kind, holds)) = self.cloud_kinds.get(&row.path)
            {
                row.kind = *kind;
                // The label is what the peek counted (`12 parquet`), as on disk. Only when there is
                // something: a claim without a count must not erase one the row has.
                if !holds.is_empty() {
                    row.holds = holds.clone();
                }
            }
        }
    }

    /// Cloud directories on or near the screen not yet peeked into, at most `limit`, the
    /// highlighted first. The cloud twin of [`Self::unclassified_visible`]: a peek is a
    /// request, worth spending on rows someone is looking at.
    pub fn cloud_directories_to_peek(&self, limit: usize) -> Vec<PathBuf> {
        if limit == 0 {
            return Vec::new();
        }
        let view = self.view();
        let mut out: Vec<PathBuf> = Vec::new();
        for entry in self.entries_near_cursor(&view, limit) {
            // Object-store directories by URL (`read_dir` on `s3://` finds nothing), hence a pass
            // of their own.
            if !is_object_store_url(&entry.path) || is_cloud_place(&entry.path) {
                continue;
            }
            if !matches!(entry.kind, EntryKind::Directory | EntryKind::Unknown) {
                continue;
            }
            // Catalog datasets and their bookmarks are listed by name; their store is not asked
            // until opened or entered.
            if self.browsing.is_none()
                && (self.catalog_dataset(&entry.path).is_some()
                    || self.bookmark(&entry.path).is_some())
            {
                continue;
            }
            // Asked and answered, or asked and still out.
            if self.cloud_kinds.contains_key(&entry.path)
                || self.peeking.contains(&entry.path)
                || self.peek_failed.contains(&entry.path)
            {
                continue;
            }
            if out.contains(&entry.path) {
                continue;
            }
            out.push(entry.path.clone());
            if out.len() >= limit {
                break;
            }
        }
        out
    }

    /// Record that a probe could not read the root, and why when the service said.
    pub fn probe_failed(&mut self, root: PathBuf, why: Option<String>) {
        self.probes.insert(root, Probe::Unreachable(why));
    }

    /// Measure a batch of rows on the calling thread, for tests and library callers with
    /// safe paths. The app never calls this: footer reads can block, so the UI thread
    /// only picks rows ([`HomeState::unmeasured_visible`]) and a worker reads.
    pub fn measure_now(&mut self, limit: usize) -> bool {
        let wanted = self.unmeasured_visible(limit);
        let more = self.unmeasured_visible(limit + 1).len() > wanted.len();
        for entry in wanted {
            let mut probe = entry.clone();
            discover::enrich(&mut probe);
            self.record_measurement(entry.path.clone(), measured_from(&probe, &entry));
        }
        self.apply_new_measurements();
        more
    }

    /// Rows not yet measured, up to `limit`: those on or near the screen first (see
    /// [`HomeState::entries_near_cursor`]), so what is shown is measured before the rest;
    /// then the rest of the rows listed, from the top, so a column name matches any of
    /// them; sorted by rows, then the rows a cut hides.
    pub fn unmeasured_visible(&self, limit: usize) -> Vec<Entry> {
        let view = self.view();
        let mut out: Vec<Entry> = Vec::new();
        let listed = view.slots.iter().filter_map(|slot| self.entry_of(slot));
        // Before the first frame there is no screen: the top of the list.
        let near: Box<dyn Iterator<Item = &Entry>> = if self.view_height == 0 {
            Box::new(std::iter::empty())
        } else {
            Box::new(self.entries_near_cursor(&view, limit))
        };
        for entry in near.chain(listed) {
            if self.wants_measuring(entry) && !out.iter().any(|e| e.path == entry.path) {
                out.push(entry.clone());
                if out.len() >= limit {
                    return out;
                }
            }
        }
        // Sorted by rows, rows past a cut are measured after those on screen: the sort needs
        // every count.
        for slot in &view.slots {
            let Slot::Plain(Row::More {
                section,
                measuring: true,
                ..
            }) = slot
            else {
                continue;
            };
            for entry in &self.sections[*section].rows {
                if self.wants_measuring(entry) && !out.iter().any(|e| e.path == entry.path) {
                    out.push(entry.clone());
                    if out.len() >= limit {
                        return out;
                    }
                }
            }
        }
        out
    }

    /// Whether `entry` is a local dataset with no count yet that measuring would give.
    fn wants_measuring(&self, entry: &Entry) -> bool {
        if entry.rows.is_some() || entry.measured || self.enriched.contains_key(&entry.path) {
            return false;
        }
        // The kind settles it before the mount table: this runs per row per frame, and the
        // cheap question first keeps thousands of rows free.
        if matches!(
            entry.kind,
            EntryKind::Directory | EntryKind::Unknown | EntryKind::Other
        ) || entry.kind.is_lake_table()
        {
            return false;
        }
        // Remote rows are measured by their root's probe; a second thread on a share that
        // may never answer is never reclaimed.
        !(self.network_check)(&entry.path)
    }

    /// Look into a batch of rows on the calling thread; for tests and library callers,
    /// like [`HomeState::measure_now`].
    pub fn classify_now(&mut self, limit: usize) -> bool {
        let wanted = self.unclassified_visible(limit);
        let more = self.unclassified_visible(limit + 1).len() > wanted.len();
        for entry in wanted {
            let probe = look_into_as(&entry, &Default::default());
            self.record_measurement(entry.path.clone(), measured_from(&probe, &entry));
        }
        self.apply_new_measurements();
        more
    }

    /// Rows on or near the screen not yet looked into, up to `limit`. Unlike
    /// [`HomeState::unmeasured_visible`], which walks from the top, this asks the
    /// viewport plus a screen either side (thousands of partitions would take seconds to
    /// reach the cursor): the highlighted row first (about to be acted on), then the
    /// screen, the screen below, the screen above.
    pub fn unclassified_visible(&self, limit: usize) -> Vec<Entry> {
        if limit == 0 {
            return Vec::new();
        }
        let view = self.view();
        let mut out: Vec<Entry> = Vec::new();
        for entry in self.entries_near_cursor(&view, limit) {
            if entry.kind != EntryKind::Unknown || self.missing.contains(&entry.path) {
                continue;
            }
            // Already looked into, even if that settled nothing: re-asking would stat per frame.
            if self.enriched.contains_key(&entry.path) {
                continue;
            }
            // Object-store places are peeked by listing; see
            // [`HomeState::cloud_directories_to_peek`].
            if is_object_store_url(&entry.path) || is_cloud_place(&entry.path) {
                continue;
            }
            // The same dataset may be listed twice (directory and Recent); look once.
            if out.iter().any(|e| e.path == entry.path) {
                continue;
            }
            out.push(entry.clone());
            if out.len() >= limit {
                break;
            }
        }
        out
    }

    /// The entry rows near the cursor: the highlighted one, the screen, the screen below,
    /// the screen above.
    fn entries_near_cursor<'a>(
        &'a self,
        view: &'a View,
        limit: usize,
    ) -> impl Iterator<Item = &'a Entry> + 'a {
        // Before the first frame there is no height: take the top of the list, a batch's
        // worth.
        let height = if self.view_height == 0 {
            limit
        } else {
            self.view_height
        };
        let rows = view.slots.len();
        let top = self.scroll.min(rows);
        let ahead = top.saturating_add(2 * height).min(rows);
        let behind = top.saturating_sub(height);
        std::iter::once(self.selected)
            .chain(top..ahead)
            .chain(behind..top)
            .filter_map(|i| self.entry_of(view.slots.get(i)?))
    }

    /// The entry a row shows, when it is an entry row.
    fn entry_of(&self, slot: &Slot) -> Option<&Entry> {
        match slot {
            Slot::Entry { section, index, .. } => Some(&self.sections[*section].rows[*index]),
            _ => None,
        }
    }

    /// Record what measuring `path` found, for [`HomeState::apply_new_measurements`] to
    /// fold into its rows.
    pub fn record_measurement(&mut self, path: PathBuf, measured: Measured) {
        self.unapplied.insert(path.clone());
        self.enriched.insert(path, measured);
    }

    /// Record a size alone: a measurement that landed meanwhile keeps the rest.
    pub fn record_size(&mut self, path: PathBuf, measured: Measured) {
        match self.enriched.get_mut(&path) {
            Some(known) => {
                known.size = measured.size;
                self.unapplied.insert(path);
            }
            None => self.record_measurement(path, measured),
        }
    }

    /// Fold every known measurement into the rows currently listed, as a new listing
    /// needs.
    pub fn apply_measurements(&mut self) {
        self.unapplied.clear();
        for section in &mut self.sections {
            // The door too: it reads its directory's slot on purpose, showing numbers already
            // measured upstairs; nothing writes the door's answer.
            for row in section.rows.iter_mut().chain(section.door.iter_mut()) {
                if let Some(m) = self.enriched.get(&row.path) {
                    fold_measured(row, m);
                }
            }
            // The door's name says what it opens, and a measurement can change that: the
            // footers turn a directory of files down as one table, or name its keys.
            if let Some(door) = section.door.as_mut() {
                door.name = door_name(door, &section.rows);
            }
        }
        self.changed();
        self.land_again();
    }

    /// Fold the measurements recorded since the last fold into their rows. Only those
    /// rows change and are scored again: measurements land a file at a time while the
    /// user types, over listings of thousands.
    pub fn apply_new_measurements(&mut self) {
        if self.unapplied.is_empty() {
            return;
        }
        let unapplied = std::mem::take(&mut self.unapplied);
        // A row's name first: hashing a short name is cheaper than hashing a whole path
        // component by component, and few of thousands of rows are among those measured.
        // Equal paths have equal names, so no measured row is passed over.
        let names: std::collections::HashSet<&std::ffi::OsStr> = (unapplied.iter())
            .map(|path| path.file_name().unwrap_or(path.as_os_str()))
            .collect();
        let new = |path: &Path| {
            path.file_name().is_none_or(|name| names.contains(name)) && unapplied.contains(path)
        };
        let mut touched: Vec<(usize, usize)> = Vec::new();
        let mut any = false;
        for (si, section) in self.sections.iter_mut().enumerate() {
            let mut here = false;
            for (i, row) in section.rows.iter_mut().enumerate() {
                if new(&row.path)
                    && let Some(m) = self.enriched.get(&row.path)
                {
                    fold_measured(row, m);
                    touched.push((si, i));
                    here = true;
                }
            }
            if let Some(door) = section.door.as_mut()
                && new(&door.path)
                && let Some(m) = self.enriched.get(&door.path)
            {
                fold_measured(door, m);
                here = true;
            }
            if here && let Some(door) = section.door.as_mut() {
                door.name = door_name(door, &section.rows);
            }
            any |= here;
        }
        if any {
            self.rows_changed(&touched);
            self.land_again();
        }
    }

    /// Landed on a door the footers have since turned down, and not moved: the cursor
    /// goes where it would have landed had they been read first.
    fn land_again(&mut self) {
        if self.landing
            && let Some(Row::Door { entry, .. }) = self.row_at(self.selected)
            && !door_lands(entry)
        {
            self.selected = self.landing_row();
            self.follow_selection();
        }
    }

    /// Put the cursor on the first dataset rather than the first header, so the
    /// preview pane has something to show without a keypress.
    ///
    /// Inside a directory that is the door when the door opens the directory as one
    /// dataset (see [`door_lands`]), and the first thing in it otherwise.
    pub fn select_first_entry(&mut self) {
        self.returning = None;
        self.landing = true;
        self.selected = self.landing_row();
    }

    /// Where [`HomeState::select_first_entry`] puts the cursor.
    fn landing_row(&self) -> usize {
        let rows = self.visible();
        // Recent is ranked by frecency, and the last file opened is still one Enter away.
        if self.filter.is_empty()
            && let Some(newest) = self.newest_recent.as_ref()
            && let Some(at) = rows.iter().position(|r| {
                matches!(r, Row::Entry { section, entry, .. }
                    if entry.path == *newest
                        && self.sections[*section].title == Self::RECENT_SECTION)
            })
        {
            return at;
        }
        let first = rows
            .iter()
            .position(|r| matches!(r, Row::Entry { .. } | Row::Door { .. }));
        let first = match first.and_then(|i| rows.get(i)) {
            Some(Row::Door { entry, .. }) if !door_lands(entry) => rows
                .iter()
                .position(|r| matches!(r, Row::Entry { .. } | Row::Hidden { .. }))
                .or(first),
            _ => first,
        };
        // A directory of files datui cannot open: the row that says so.
        first
            .or_else(|| rows.iter().position(|r| matches!(r, Row::Hidden { .. })))
            .unwrap_or(0)
    }

    pub fn clamp_selection(&mut self) {
        let n = self.row_count();
        if n == 0 {
            self.selected = 0;
        } else if self.selected >= n {
            self.selected = n - 1;
        }
    }

    /// Put the selection on row `index` of what is listed, as a click does.
    pub fn select(&mut self, index: usize) {
        if index < self.row_count() {
            self.returning = None;
            self.landing = false;
            self.selected = index;
        }
    }

    pub fn move_selection(&mut self, delta: isize) {
        self.returning = None;
        self.landing = false;
        let n = self.row_count();
        if n == 0 {
            return;
        }
        let cur = self.selected as isize;
        let next = (cur + delta).rem_euclid(n as isize);
        self.selected = next as usize;
    }

    /// Move the selection `delta` rows, stopping at the ends rather than wrapping (a
    /// wrapped page jump lands somewhere unexpected; single steps wrap).
    pub fn page_selection(&mut self, delta: isize) {
        self.returning = None;
        self.landing = false;
        let n = self.row_count();
        if n == 0 {
            return;
        }
        // Saturating, so Home and End are a page of `isize::MIN` or `isize::MAX`.
        let next = (self.selected as isize)
            .saturating_add(delta)
            .clamp(0, n as isize - 1);
        self.selected = next as usize;
    }
}

/// The row for a cloud source under `CLOUD`.
fn source_entry(source: &CloudSource) -> Entry {
    Entry {
        path: cloud_place(&source.id),
        kind: EntryKind::Directory,
        name: source.label.clone(),
        size: None,
        modified: source.listed_at,
        rows: None,
        cols: None,
        cols_sampled: false,
        columns: Vec::new(),
        cost: Default::default(),
        holds: Default::default(),
        opens_whole_directory: false,
        measured: false,
        format_spec: None,
        table: None,
    }
}

/// The row for one bucket.
fn bucket_entry(url: &Path) -> Entry {
    let mut entry = Entry::directory(url);
    // The bucket name, without a source id, rather than the URL's last segment.
    let text = url.to_string_lossy();
    let (_, plain) = crate::cloud::source::split_source_id(&text);
    entry.name = plain
        .rsplit('/')
        .find(|part| !part.is_empty())
        .unwrap_or("")
        .to_string();
    entry
}

/// Whether a remote path's name says it is a file: a data extension or any dot in its
/// last segment. A trailing slash is always a prefix.
pub fn names_a_file(path: &Path) -> bool {
    let named = path.to_string_lossy();
    // `file_name`, not a split on `/`, which on Windows took the whole path as the last
    // segment.
    let dotted = !named.ends_with('/')
        && path
            .file_name()
            .map(|last| last.to_string_lossy())
            .is_some_and(|last| last.trim_start_matches('.').contains('.'));
    discover::is_data_file(path) || dotted
}

/// Build an entry for a path that is already known (a recent), classifying it.
fn entry_for_path(path: &Path, remote: bool) -> Entry {
    // A table inside a SQLite database, which nothing on disk is named.
    if !remote && let Some(table) = discover::table_row(path) {
        return table;
    }
    let mut holds = discover::Holds::default();
    // Classifying and stat'ing touch the filesystem, so a remote entry is listed by name
    // until its probe lands.
    let kind = if remote {
        // The name alone: an extension (readable or not) makes it a file, so → never enters
        // `data.dat` as a prefix; anything else stays Unknown, not a plain directory, which
        // would contradict its root's probe later. A trailing slash is a prefix whatever
        // the name (`exports/`, `2024.01.15/`).
        if names_a_file(path) {
            EntryKind::File
        } else {
            EntryKind::Unknown
        }
    } else if path.is_dir() {
        let (kind, found) = discover::look_at_directory(path);
        holds = found;
        kind
    } else {
        EntryKind::File
    };
    let mut entry = Entry {
        path: path.to_path_buf(),
        kind,
        name: path
            .file_name()
            .map(|n| n.to_string_lossy().into_owned())
            .unwrap_or_else(|| path.to_string_lossy().into_owned()),
        size: None,
        modified: None,
        rows: None,
        cols: None,
        cols_sampled: false,
        columns: Vec::new(),
        cost: Default::default(),
        holds,
        opens_whole_directory: false,
        measured: false,
        format_spec: None,
        table: None,
    };
    if !remote && let Ok(meta) = std::fs::metadata(path) {
        if meta.is_file() {
            entry.size = Some(meta.len());
        }
        entry.modified = meta.modified().ok();
    }
    entry
}

/// Abbreviate a path with `~` for display.
pub fn display_path(path: &Path) -> String {
    if let Some(home) = dirs::home_dir()
        && let Ok(rest) = path.strip_prefix(&home)
    {
        if rest.as_os_str().is_empty() {
            return "~".to_string();
        }
        // The platform's separator, so Windows reads `~\data\a.csv`.
        return format!("~{}{}", std::path::MAIN_SEPARATOR, rest.display());
    }
    path.display().to_string()
}

/// Complete a partly typed path against its directory: the longest unambiguous
/// extension of `typed` and the candidate count. Reads a directory, so only on a
/// worker.
pub fn complete_path(typed: &str) -> (String, usize) {
    let expanded = expand_user_path(typed);
    // `\` is a separator on Windows too: `C:\data\` lists inside `data`.
    let is_separator = |c: char| c == '/' || (cfg!(windows) && c == '\\');
    let typed_ends_in_sep = typed.ends_with(is_separator);

    let (dir, prefix) = if typed_ends_in_sep {
        (expanded.clone(), String::new())
    } else {
        match (expanded.parent(), expanded.file_name()) {
            (Some(parent), Some(name)) => {
                (parent.to_path_buf(), name.to_string_lossy().into_owned())
            }
            _ => (expanded.clone(), String::new()),
        }
    };

    let Ok(entries) = std::fs::read_dir(&dir) else {
        return (typed.to_string(), 0);
    };

    let mut names: Vec<String> = entries
        .flatten()
        .filter_map(|e| {
            let name = e.file_name().to_string_lossy().into_owned();
            // Dotfiles only when a dot is typed.
            if name.starts_with('.') && !prefix.starts_with('.') {
                return None;
            }
            name.starts_with(&prefix).then_some(name)
        })
        .collect();
    if names.is_empty() {
        return (typed.to_string(), 0);
    }
    names.sort();

    // The candidates' common prefix: further would be guessing.
    let shared = names
        .iter()
        .skip(1)
        .fold(names[0].clone(), |acc, name| common_prefix(&acc, name));

    let mut completed = typed.to_string();
    completed.truncate(typed.len() - prefix.len());
    completed.push_str(&shared);

    // A single directory gets the separator being typed, so the next Tab descends
    // (`C:\Users\` stays `\`).
    if names.len() == 1 && dir.join(&shared).is_dir() && !completed.ends_with(is_separator) {
        let separator = typed
            .chars()
            .rev()
            .find(|c| is_separator(*c))
            .unwrap_or(std::path::MAIN_SEPARATOR);
        completed.push(separator);
    }
    (completed, names.len())
}

/// One name in the directory the `~` prompt is typing.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PathName {
    pub name: String,
    /// A directory, prefix or bucket: completed with a separator, and gone into.
    pub dir: bool,
}

/// What the `~` prompt lists: the directory part of what is typed, and what is in it.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct PathListing {
    /// The typed text up to and including its last separator, as typed.
    pub dir: String,
    pub names: Vec<PathName>,
    /// Reading the directory failed: there is nothing to list, and the prompt says so.
    pub failed: bool,
}

/// The most names a typed directory lists: a prompt finds one name by typing.
const PATH_LISTING_MAX: usize = 5_000;

/// The directory part of a typed path, through its last separator; for a URL at
/// least its scheme (`s3://`), so buckets list under it.
pub fn typed_dir(typed: &str) -> &str {
    let is_separator = |c: char| c == '/' || (cfg!(windows) && c == '\\');
    let floor = typed.find("://").map_or(0, |at| at + 3);
    match typed[floor..].rfind(is_separator) {
        Some(at) => &typed[..floor + at + 1],
        None => &typed[..floor],
    }
}

/// The separator a directory completed under `dir` ends with: a URL's `/`, or the
/// one being typed.
fn separator_in(dir: &str) -> char {
    if typed_dir_is_url(dir) {
        return '/';
    }
    dir.chars()
        .rev()
        .find(|c| *c == '/' || (cfg!(windows) && *c == '\\'))
        .unwrap_or(std::path::MAIN_SEPARATOR)
}

/// Whether a typed directory is a URL, listed from what datui knows rather than read.
pub fn typed_dir_is_url(dir: &str) -> bool {
    dir.contains("://")
}

/// A local directory typed at `~`, for the prompt's list; reads it, so runs on a
/// worker. Nothing typed lists the working directory.
pub fn list_typed_dir(dir: &str) -> PathListing {
    let path = if dir.is_empty() {
        PathBuf::from(".")
    } else {
        expand_user_path(dir)
    };
    let Ok(entries) = std::fs::read_dir(&path) else {
        return PathListing {
            dir: dir.to_string(),
            names: Vec::new(),
            failed: true,
        };
    };
    let mut names: Vec<PathName> = entries
        .flatten()
        .take(PATH_LISTING_MAX)
        .map(|e| {
            let name = e.file_name().to_string_lossy().into_owned();
            // A link to a directory is one to go into.
            let dir = e.file_type().is_ok_and(|t| t.is_dir())
                || (e.file_type().is_ok_and(|t| t.is_symlink()) && e.path().is_dir());
            PathName { name, dir }
        })
        .collect();
    names.sort_by(|a, b| a.name.cmp(&b.name));
    PathListing {
        dir: dir.to_string(),
        names,
        failed: false,
    }
}

/// The names one level below `dir` among `urls`: how `s3://`, `gs://` and `az://`
/// complete, from what was listed, opened or cataloged. Nothing is asked of the store.
pub fn names_under(dir: &str, urls: impl IntoIterator<Item = String>) -> PathListing {
    let mut names: Vec<PathName> = Vec::new();
    for url in urls {
        // An Azure URL is known in its full form; `az://container/` is how one is typed.
        let forms = match crate::cloud::source::azure_parts(&url) {
            Some((_, container, key)) => vec![url.clone(), format!("az://{container}/{key}")],
            None => vec![url],
        };
        for form in forms {
            let Some(rest) = form.strip_prefix(dir) else {
                continue;
            };
            let (name, more) = match rest.split_once('/') {
                Some((name, more)) => (name, Some(more)),
                None => (rest, None),
            };
            if name.is_empty() {
                continue;
            }
            // Something below it, a trailing slash, or no extension: a bucket or prefix, as for
            // a recent.
            let is_dir = more.is_some() || !names_a_file(Path::new(&form));
            match names.iter_mut().find(|n| n.name == name) {
                Some(known) => known.dir |= is_dir,
                None => names.push(PathName {
                    name: name.to_string(),
                    dir: is_dir,
                }),
            }
        }
    }
    names.sort_by(|a, b| a.name.cmp(&b.name));
    PathListing {
        dir: dir.to_string(),
        names,
        failed: false,
    }
}

fn common_prefix(a: &str, b: &str) -> String {
    a.chars()
        .zip(b.chars())
        .take_while(|(x, y)| x == y)
        .map(|(x, _)| x)
        .collect()
}

/// Expand `~` and `$VAR` in a path the user typed.
pub fn expand_user_path(raw: &str) -> PathBuf {
    crate::config::expand_config_path(raw)
}

#[cfg(test)]
mod holds_flow_tests {
    use super::*;

    /// The list and the pane are told one thing: the curated word in both, and for a
    /// bucket directory, where looking into it is.
    #[test]
    fn a_rows_label_is_one_decision_for_the_list_and_the_pane() {
        let mut directory = Entry::for_test(Path::new("s3://bucket/warehouse"), "warehouse");
        directory.kind = EntryKind::Directory;
        let said = |look, place_kind| describe(&directory, place_kind, look, 0, None);
        let g = crate::glyphs::get();

        let waiting = said(Some(CloudLook::Waiting), None);
        assert_eq!(
            (waiting.short.as_str(), waiting.words.as_str()),
            (g.ellipsis, "")
        );
        assert_eq!(said(Some(CloudLook::Looking), None).words, "");
        assert!(
            said(Some(CloudLook::Failed), None)
                .words
                .contains("listing failed")
        );
        assert_eq!(said(None, None).words, "directory");
        let curated = said(None, Some("dataset"));
        assert_eq!(
            (curated.short.as_str(), curated.words.as_str()),
            ("dataset", "dataset")
        );
        assert!(curated.curated);

        directory.holds = crate::home::discover::Holds {
            formats: vec![("parquet".to_string(), 12)],
            ..Default::default()
        };
        let counted = describe(&directory, None, None, 0, None);
        assert_eq!(counted.short, "12 parquet");
        assert_eq!(
            counted.words, "directory",
            "the count is the pane's `contains` line"
        );
        let curated = describe(&directory, Some("dataset"), None, 0, None);
        assert_eq!(
            curated.short, "dataset",
            "the curated word wins over the count"
        );

        directory.opens_whole_directory = true;
        assert_eq!(
            describe(&directory, Some("dataset"), None, 0, None),
            RowLabel::default()
        );
    }

    /// A path under the home directory is written the way it is typed back: `~\` on
    /// Windows, and `~\` typed at the prompt expands.
    #[cfg(windows)]
    #[test]
    fn a_windows_home_path_is_shown_and_typed_with_backslashes() {
        let home = dirs::home_dir().unwrap();
        let path = home.join("data").join("a.csv");
        let shown = display_path(&path);
        assert_eq!(shown, r"~\data\a.csv");
        assert_eq!(expand_user_path(&shown), path);
    }

    /// The last segment of a Windows path is after its last `\`, so a dot higher up
    /// does not make a directory a file.
    #[cfg(windows)]
    #[test]
    fn a_dot_above_a_windows_recent_does_not_make_it_a_file() {
        let path = Path::new(r"C:\Users\RUNNER~1\AppData\Local\Temp\.tmpAzMMTE\orders");
        assert_eq!(entry_for_path(path, true).kind, EntryKind::Unknown);
        let file = Path::new(r"C:\Users\RUNNER~1\AppData\Local\Temp\.tmpAzMMTE\a.parquet");
        assert_eq!(entry_for_path(file, true).kind, EntryKind::File);
    }

    fn counted(n: usize) -> crate::home::discover::Holds {
        crate::home::discover::Holds {
            formats: vec![("parquet".to_string(), n)],
            ..Default::default()
        }
    }

    /// The claim `peek_cloud_directories` stakes before its answers arrive, so a rebuild
    /// in the meantime does not ask the store again: a `Directory` that counted nothing.
    fn in_flight() -> (EntryKind, crate::home::discover::Holds) {
        (
            EntryKind::Directory,
            crate::home::discover::Holds::default(),
        )
    }

    #[test]
    fn a_claim_staked_before_a_peek_lands_keeps_the_count_a_row_already_has() {
        let root = std::path::PathBuf::from("s3://bucket/warehouse");
        let path = root.join("orders");
        let mut row = Entry::for_test(&path, "orders");
        row.kind = EntryKind::Directory;
        // Restored from the facts cache on the way in, which is the only reason a
        // remote row has a count before anything peeked at it.
        row.holds = counted(15);

        let mut home = HomeState::default();
        home.probe_ready(root.clone(), vec![row], false);
        home.cloud_kinds.insert(path, in_flight());
        home.apply_cloud_kinds(&root);

        assert_eq!(
            home.probes.listed(&root).unwrap()[0].holds.label(),
            "15 parquet",
            "the placeholder erased a count the row already had"
        );
    }

    #[test]
    fn a_cloud_directory_waits_then_looks_then_answers() {
        let path = std::path::PathBuf::from("gs://pitscope/seasons");
        let mut row = Entry::for_test(&path, "seasons");
        row.kind = EntryKind::Directory;
        let mut home = HomeState::default();

        assert_eq!(
            home.cloud_look(&row),
            Some(CloudLook::Waiting),
            "not asked yet"
        );
        home.peeking.insert(path.clone());
        assert_eq!(
            home.cloud_look(&row),
            Some(CloudLook::Looking),
            "being looked into"
        );
        home.peeking.remove(&path);

        // Answered with a count the drawn row does not carry yet: still looking, not
        // `dir`, which would claim there is no data inside.
        home.cloud_kinds
            .insert(path.clone(), (EntryKind::Directory, counted(12)));
        assert_eq!(home.cloud_look(&row), Some(CloudLook::Looking));
        home.cloud_kinds
            .insert(path.clone(), (EntryKind::Hive, Default::default()));
        assert_eq!(home.cloud_look(&row), Some(CloudLook::Looking));

        // Answered with nothing to count: `dir` is the truth.
        home.cloud_kinds.insert(path.clone(), in_flight());
        assert_eq!(home.cloud_look(&row), None, "answered");

        // A failed peek is not an answer, and is not asked again until Ctrl+R. The
        // picker reads the listing, so the row has to be on it.
        let mut home = HomeState {
            network_check: |_| true,
            ..Default::default()
        };
        let root = std::path::PathBuf::from("gs://pitscope");
        home.probe_ready(root.clone(), vec![row.clone()], false);
        home.browsing = Some(root);
        home.rebuild(&[]);
        assert_eq!(
            home.cloud_directories_to_peek(4),
            std::slice::from_ref(&path)
        );
        home.peek_failed.insert(path.clone());
        assert_eq!(home.cloud_look(&row), Some(CloudLook::Failed));
        assert!(home.cloud_directories_to_peek(4).is_empty());

        // A row that already says what it holds, a bucket, and a local directory never
        // wait on a peek.
        let mut counted_row = Entry::for_test(&path.join("x"), "x");
        counted_row.kind = EntryKind::Directory;
        counted_row.holds = counted(3);
        assert_eq!(home.cloud_look(&counted_row), None);
        let mut bucket = Entry::for_test(std::path::Path::new("gs://pitscope"), "pitscope");
        bucket.kind = EntryKind::Directory;
        assert_eq!(home.cloud_look(&bucket), None);
        let mut local = Entry::for_test(std::path::Path::new("/data/seasons"), "seasons");
        local.kind = EntryKind::Directory;
        assert_eq!(home.cloud_look(&local), None);
    }

    #[test]
    fn a_peeks_answer_replaces_the_count_a_row_had() {
        let root = std::path::PathBuf::from("s3://bucket/warehouse");
        let path = root.join("orders");
        let mut row = Entry::for_test(&path, "orders");
        row.kind = EntryKind::Directory;
        row.holds = counted(15);

        let mut home = HomeState::default();
        home.probe_ready(root.clone(), vec![row], false);
        home.cloud_kinds
            .insert(path, (EntryKind::MultiFile, counted(40)));
        home.apply_cloud_kinds(&root);

        assert_eq!(
            home.probes.listed(&root).unwrap()[0].holds.label(),
            "40 parquet"
        );
        assert_eq!(
            home.probes.listed(&root).unwrap()[0].kind,
            EntryKind::MultiFile
        );
    }

    #[test]
    fn a_peek_answers_only_the_rows_that_asked() {
        let root = std::path::PathBuf::from("s3://bucket/warehouse");
        // A row the listing already settled. Its path is in `cloud_kinds` — a peek was
        // answered for it once — and it must not be read back over the top of a kind
        // the listing was surer of.
        let settled = root.join("sales");
        let mut row = Entry::for_test(&settled, "sales");
        row.kind = EntryKind::Hive;
        row.holds = counted(40);

        let mut home = HomeState::default();
        home.probe_ready(root.clone(), vec![row], false);
        home.cloud_kinds
            .insert(settled, (EntryKind::Directory, counted(1)));
        home.apply_cloud_kinds(&root);

        assert_eq!(home.probes.listed(&root).unwrap()[0].kind, EntryKind::Hive);
        assert_eq!(
            home.probes.listed(&root).unwrap()[0].holds.label(),
            "40 parquet"
        );
    }

    /// A peek is a request, so it is spent on the row the cursor is on.
    ///
    /// The picker took the first forty-eight directories of each listing, once per
    /// session: a bucket of two hundred prefixes had forty-eight labelled and the rest
    /// reading `dir` however long you spent on them, and paging straight past those
    /// forty-eight spent every request on rows nobody saw.
    #[test]
    fn a_peek_goes_to_the_row_the_cursor_is_on_and_is_never_asked_twice() {
        let root = std::path::PathBuf::from("s3://bucket/warehouse");
        let mut home = HomeState {
            network_check: |_| true,
            ..Default::default()
        };
        let rows: Vec<Entry> = ["a", "b", "c", "d", "e"]
            .iter()
            .map(|n| {
                let mut row = Entry::for_test(&root.join(n), n);
                row.kind = EntryKind::Directory;
                row
            })
            .collect();
        home.probe_ready(root.clone(), rows, false);
        home.browsing = Some(root.clone());
        home.view_height = 10;
        home.rebuild(&[]);
        // One already answered, and one with a request already out.
        home.cloud_kinds
            .insert(root.join("b"), (EntryKind::MultiFile, counted(3)));
        home.peeking.insert(root.join("c"));

        // The cursor on `e`, the last row: it is asked about first, though four rows
        // above it have never been looked into. That is the whole change.
        home.selected = home
            .visible()
            .iter()
            .position(|r| matches!(r, Row::Entry { entry, .. } if entry.name == "e"))
            .expect("the row is listed");
        let asked = home.cloud_directories_to_peek(3);
        assert_eq!(
            asked.first(),
            Some(&root.join("e")),
            "the highlighted row is the one about to be acted on"
        );
        assert_eq!(asked.len(), 3, "the budget is a budget");
        assert!(
            !asked.contains(&root.join("b")),
            "a directory already looked into is not asked again"
        );
        assert!(
            !asked.contains(&root.join("c")),
            "nor one with a request already out"
        );
    }

    #[test]
    fn a_measurement_that_counted_nothing_keeps_the_count_a_row_already_has() {
        let path = std::path::PathBuf::from("/data/warehouse/orders");
        let mut row = Entry::for_test(&path, "orders");
        row.kind = EntryKind::Directory;
        row.holds = counted(15);

        let mut home = HomeState::default();
        home.sections.push(Section::titled("Here", vec![row]));
        // A measurement of a file carries no `holds`, and the same struct measures both.
        home.enriched.insert(
            path,
            Measured {
                kind: Some(EntryKind::Directory),
                ..Default::default()
            },
        );
        home.apply_measurements();

        assert_eq!(
            home.sections[0].rows[0].holds.label(),
            "15 parquet",
            "a measurement with nothing to say erased the label"
        );
    }
}

#[cfg(test)]
mod look_into_batch_tests {
    use super::*;
    use polars::prelude::*;

    /// Every row in a batch is labeled before any is measured: a kind is one directory
    /// read, and a count can be sixty-four footers.
    #[test]
    fn every_kind_is_sent_before_any_count() {
        let dir = tempfile::tempdir().unwrap();
        let cache_dir = tempfile::tempdir().unwrap();
        let cache = crate::cache::CacheManager::with_dir(cache_dir.path().to_path_buf());
        let mut rows = Vec::new();
        for name in ["a", "b"] {
            let partition = dir.path().join(name).join("year=2024");
            std::fs::create_dir_all(&partition).unwrap();
            let mut frame = df!("x" => [1i32, 2, 3]).unwrap();
            let file = std::fs::File::create(partition.join("part.parquet")).unwrap();
            ParquetWriter::new(file).finish(&mut frame).unwrap();
            rows.push(Entry::new(dir.path().join(name), EntryKind::Unknown));
        }

        let mut sent = Vec::new();
        look_into_batch(rows, &cache, |path, m| {
            let name = path.file_name().unwrap().to_string_lossy().into_owned();
            sent.push((name, m.kind, m.rows));
        });

        let hive = Some(EntryKind::Hive);
        assert_eq!(
            sent,
            vec![
                ("a".to_string(), hive, None),
                ("b".to_string(), hive, None),
                ("a".to_string(), hive, Some(3)),
                ("b".to_string(), hive, Some(3)),
            ]
        );
    }
}

#[cfg(test)]
mod known_facts_tests {
    use super::*;
    use crate::cache::DatasetFacts;

    /// What a directory holds comes back with its kind, on both routes.
    ///
    /// A row given a kind from the cache is never looked into again — `look_into_as` only
    /// classifies an `Unknown`, and `unclassified_visible` skips anything else. So a
    /// count left behind is left behind for the session: the row says `dir` about a
    /// directory of fifteen Parquet files, and `enrich` goes on to describe it by
    /// whatever is in its subdirectories.
    #[test]
    fn what_a_directory_holds_is_restored_beside_its_kind() {
        let holds = crate::home::discover::Holds {
            formats: vec![("parquet".to_string(), 15)],
            ..Default::default()
        };
        for (path, remote) in [
            (
                std::path::PathBuf::from("s3://bucket/warehouse/orders"),
                true,
            ),
            (std::path::PathBuf::from("/data/warehouse/orders"), false),
        ] {
            let facts = DatasetFacts {
                mtime: 0,
                size: 4096,
                // A row count a directory's record has no business carrying, to prove
                // the gate below still turns it away.
                rows: Some(999),
                cols: Some(72),
                cols_sampled: false,
                columns: vec!["lat".to_string()],
                // A directory of separate tables: the kind the footers settled on, its
                // width, and no row count, because a sum over them is not a number.
                kind: Some(EntryKind::Directory),
                classified_by: crate::home::discover::CLASSIFIER_VERSION,
                holds: holds.clone(),
                cost: Default::default(),
            };
            let mut row = Entry::directory(&path);
            row.kind = EntryKind::Unknown;
            row.modified = Some(std::time::UNIX_EPOCH);
            // No size, which is what a listing gives a directory — and what makes the
            // byte fingerprint below unable to speak for one.
            assert_eq!(row.size, None);
            let index = std::collections::HashMap::from([(path.clone(), facts)]);

            apply_known_facts(&mut row, &index, remote);
            assert_eq!(row.kind, EntryKind::Directory, "{path:?}");
            assert_eq!(row.label(), "15 parquet", "{path:?}");
            // And nothing the footers said. A directory's mtime moves when an entry
            // is added, removed or renamed; a file rewritten in place moves nothing,
            // and the width, the size and the column names all change with it. Only
            // locally — a remote row is never measured here at all, so the record is
            // all it will ever have and it takes the whole of it.
            if !remote {
                assert_eq!(row.rows, None, "{path:?}");
                assert_eq!(row.cols, None, "{path:?}");
                assert_eq!(row.size, None, "{path:?}");
                assert!(row.columns.is_empty(), "{path:?}");
            }
        }
    }

    /// A dataset's own counts are not restored beside its kind. `unmeasured_visible`
    /// skips a row that already has a row count, so restoring one would stop a `hive`
    /// or a `multi` directory ever being measured again — and its size and its codec,
    /// which nothing else fills in, would be blank for the rest of the session.
    #[test]
    fn a_datasets_counts_are_measured_rather_than_restored() {
        let path = std::path::PathBuf::from("/data/warehouse/events");
        let facts = DatasetFacts {
            mtime: 0,
            size: 4096,
            rows: Some(1_200_000),
            cols: Some(58),
            cols_sampled: false,
            columns: vec!["ts".to_string()],
            kind: Some(EntryKind::MultiFile),
            classified_by: crate::home::discover::CLASSIFIER_VERSION,
            holds: crate::home::discover::Holds {
                formats: vec![("parquet".to_string(), 15)],
                ..Default::default()
            },
            cost: Default::default(),
        };
        let mut row = Entry::directory(&path);
        row.kind = EntryKind::Unknown;
        row.modified = Some(std::time::UNIX_EPOCH);
        let index = std::collections::HashMap::from([(path.clone(), facts)]);

        apply_known_facts(&mut row, &index, false);
        assert_eq!(row.kind, EntryKind::MultiFile, "the kind comes back");
        assert_eq!(row.label(), "15 parquet", "and what it holds");
        assert_eq!(
            row.rows, None,
            "but not the count: the measuring pass skips a row that has one"
        );
        assert_eq!(row.cols, None);
    }

    /// A kind recorded by a build that classified differently is not restored.
    ///
    /// A remote row was never stat'ed, so its cached kind is all it has and is restored
    /// rather than re-derived. That makes it a way for an answer this build would not
    /// give to come back: a Delta root measured before lake tables were recognized was
    /// recorded as `multifile`, and restoring that opens it as one table again — #237
    /// read back off disk. Everything else in the record is a measurement rather than a
    /// judgement, and survives.
    #[test]
    fn a_kind_from_an_older_classifier_is_not_restored() {
        let remote = std::path::PathBuf::from("s3://bucket/warehouse/orders");
        let facts = |classified_by| DatasetFacts {
            mtime: 0,
            size: 4096,
            rows: Some(1_000),
            cols: Some(7),
            cols_sampled: false,
            columns: vec!["id".into(), "amount".into()],
            kind: Some(EntryKind::MultiFile),
            classified_by,
            cost: Default::default(),
            holds: Default::default(),
        };
        let unprobed = || {
            let mut row = Entry::directory(&remote);
            row.kind = EntryKind::Unknown;
            row
        };

        let index = |classified_by| {
            std::collections::HashMap::from([(remote.clone(), facts(classified_by))])
        };

        let mut row = unprobed();
        apply_known_facts(
            &mut row,
            &index(crate::home::discover::CLASSIFIER_VERSION),
            true,
        );
        assert_eq!(
            row.kind,
            EntryKind::MultiFile,
            "this build's own answer comes back"
        );

        let mut row = unprobed();
        apply_known_facts(&mut row, &index(0), true);
        assert_eq!(
            row.kind,
            EntryKind::Unknown,
            "an older build's does not: it may be a lake table this one would recognize"
        );
        assert_eq!(
            row.rows,
            Some(1_000),
            "but what it measured is still measured"
        );
        assert_eq!(row.columns, vec!["id".to_string(), "amount".to_string()]);
    }
}

#[cfg(test)]
mod build_feature_tests {
    use super::*;

    /// The built-in catalog lists only what this build can open; with neither `cloud`
    /// nor `http` the section is gone rather than a list of failures.
    #[test]
    fn the_builtin_catalog_lists_only_what_this_build_opens() {
        let urls: Vec<String> = catalogs(&crate::config::AppConfig::default())
            .into_iter()
            .filter(|c| c.origin == crate::home::catalog::Origin::Bundled)
            .flat_map(|c| c.datasets)
            .map(|d| d.location.to_string_lossy().into_owned())
            .collect();
        let web = urls.iter().filter(|u| u.starts_with("https://")).count();
        let stores = urls
            .iter()
            .filter(|u| is_object_store_url(Path::new(u)))
            .count();
        assert_eq!(web + stores, urls.len(), "{urls:?}");
        assert_eq!(web > 0, cfg!(feature = "http"), "{urls:?}");
        assert_eq!(stores > 0, cfg!(feature = "cloud"), "{urls:?}");
    }

    /// An empty `examples.toml` of the user's replaces the Example datasets with
    /// nothing, and an empty catalog has no section: the section is gone.
    #[test]
    fn an_empty_examples_toml_hides_the_section() {
        let mut config = crate::config::AppConfig::default();
        // The examples are all HTTP or S3: a build that reads neither has none.
        assert_eq!(
            catalogs(&config)
                .iter()
                .any(|c| c.origin == crate::home::catalog::Origin::Bundled),
            cfg!(any(feature = "http", feature = "cloud"))
        );
        config.read_catalogs = vec![
            crate::home::catalog::parse(
                "label = \"Mine\"\n",
                crate::home::catalog::EXAMPLES,
                crate::home::catalog::Origin::Folder,
                None,
            )
            .unwrap(),
        ];
        assert!(catalogs(&config).is_empty(), "{:?}", catalogs(&config));
    }

    /// A catalog of the user's stays whole whatever the build: the user named it, and
    /// opening a dataset it cannot read says why.
    #[test]
    fn a_users_catalog_is_shown_whole() {
        let mut config = crate::config::AppConfig::default();
        let mine = crate::home::catalog::parse(
            r#"
            [bucket]
            name = "Bucket"
            url = "s3://bucket/prefix/"
            [web]
            name = "Web"
            url = "https://example.com/data.csv"
            "#,
            crate::home::catalog::MINE,
            crate::home::catalog::Origin::Mine,
            None,
        )
        .unwrap();
        config.read_catalogs = vec![mine];
        let shown = catalogs(&config);
        let mine = shown.iter().find(|c| c.id == "mine").unwrap();
        assert_eq!(mine.datasets.len(), 2);
        assert_eq!(mine.label, crate::home::catalog::MINE_LABEL);
    }
}

#[cfg(test)]
mod place_tests {
    use super::same_place;
    use std::path::Path;

    #[test]
    fn local_paths_are_one_place_however_spelled() {
        assert!(same_place(
            Path::new("/data/./sales/"),
            Path::new("/data/sales")
        ));
        assert!(!same_place(
            Path::new("/data/sales"),
            Path::new("/data/sale")
        ));
        assert!(same_place(
            Path::new("s3://bucket/dir/"),
            Path::new("s3://bucket/dir")
        ));
        if cfg!(windows) {
            assert!(same_place(
                Path::new("c:/data/sales.csv"),
                Path::new(r"C:\data\sales.csv")
            ));
        }
    }
}