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
use crate::analysis::analysis_modal::{
    AnalysisFocus, AnalysisTool, DetailScroll, EvidenceRead, SetupRow,
};
use crate::analysis::data_quality::{
    ColumnQualityProfile, CopyPlan, DataQualityPlan, DataQualityResults, IntervalClock,
    IntervalFact, NoCopy, ObservationKind, QualityComparison, QualityCompute, QualityGrain,
    QualityMetric, QualityPage, QualityPrecision, QualityScope, SegmentCount,
    TemporalLatencyProfile, TemporalRole, interval_label, window_cadence,
};
use crate::analysis::quality_report::{
    CHECKS_SHOWN, Check, Coverage, Evidence, EvidenceRows, FindingOrder, FindingsView, Outcome,
    QualityReport, Severity, advice, coverage, describe, verdict,
};
use crate::analysis::quality_trends::{
    GapCheck, GapKind, Gaps, TrendBar, TrendMeasure, TrendRow, TrendView, trend_view,
};
use crate::config::Theme;
use crate::glyphs;
use crate::numfmt;
use crate::render::context::RenderContext;
use crate::render::layout::dialog_in;
use crate::table::DataTableState;
use crate::widgets::ui::{FormValue, Picker, Surface};
use polars::prelude::{DataType, Schema};
use ratatui::buffer::Buffer;
use ratatui::layout::{Alignment, Constraint, Direction, Layout, Rect};
use ratatui::style::{Modifier, Style};
use ratatui::text::{Line, Span};
use ratatui::widgets::{Cell, Paragraph, Row, StatefulWidget, Table, TableState, Widget, Wrap};

/// What Setup says about the draft beside its rows, all of it known without a read.
#[derive(Debug, Clone, Default)]
pub struct SetupView<'a> {
    /// The columns a time role can be given: date and time columns, then text.
    pub time_candidates: &'a [String],
    /// A run would start from the rows the last run read.
    pub reuses_sample: bool,
    /// A random sample reads seeded runs of one file instead of streaming it.
    pub reads_blocks: bool,
    /// Where a run gets exact segment totals: see [`SegmentCount`].
    pub segment_count: SegmentCount,
    /// The rows this draft's sample reads were read earlier and released to the
    /// memory budget.
    pub released: bool,
    /// The session cache holds this draft's report.
    pub cached: bool,
    /// The report on screen was measured with exactly this draft.
    pub unchanged: bool,
    /// The draft differs from the report on screen only in the windows it expects or
    /// what its segments are compared with: Run works both out from the report.
    pub relabel_only: bool,
    /// Setup holds edits Esc would discard.
    pub edited: bool,
    /// Why Enter did not run.
    pub note: Option<&'a str>,
    /// A cancelled run still going, while the screen should say so.
    pub cancelling: Option<Cancelling>,
    /// The rows runs kept for reuse, which `d` releases.
    pub kept: Option<KeptRows>,
    /// How a full scan of a remote source reads it: see [`CopyPlan`].
    pub copy: CopyPlan,
    /// This dataset's local copy was released, so a Run fetches it again.
    pub copy_released: bool,
}

/// The rows Data Quality's sampled runs kept this session, for later runs to reuse.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct KeptRows {
    /// Samples kept: one per scope, method, size and seed a run read.
    pub samples: usize,
    pub rows: usize,
    /// Near enough to budget by: [`crate::analysis::data_quality::QualitySample::estimated_bytes`].
    pub bytes: usize,
    /// Bytes on disk in a full scan's local copy of a remote source; 0 for none.
    pub copy_bytes: u64,
}

impl KeptRows {
    /// `100,000 rows kept · 12.4 MiB`, or `2 samples, 200,000 rows kept · …`, with
    /// `, local copy · 17.3 MiB` after it when a full scan's copy is kept.
    pub fn label(&self) -> String {
        let middot = glyphs::get().middot;
        let copy = format!(
            "local copy {middot} {}",
            crate::numfmt::bytes(self.copy_bytes)
        );
        if self.samples == 0 {
            return copy;
        }
        let rows = format!(
            "{} {} kept {middot} {}",
            numfmt::group_chrome(self.rows),
            if self.rows == 1 { "row" } else { "rows" },
            crate::numfmt::bytes(self.bytes as u64)
        );
        let rows = if self.samples > 1 {
            format!("{} samples, {rows}", numfmt::group_chrome(self.samples))
        } else {
            rows
        };
        if self.copy_bytes > 0 {
            format!("{rows}, {copy}")
        } else {
            rows
        }
    }
}

/// A cancelled run that has not exited yet.
#[derive(Debug, Clone, Copy)]
pub struct Cancelling {
    /// When the cancel came.
    pub since: std::time::Instant,
    /// The cancel came during a read of the source nothing can stop, which runs to
    /// its end; otherwise the run is stopping and has outlasted its batch.
    pub read_runs_out: bool,
}

impl Cancelling {
    /// What is still going, after "Cancelling: " or "Run waits: ".
    fn what(self) -> &'static str {
        if self.read_runs_out {
            "source read finishing"
        } else {
            "run stopping"
        }
    }
}

pub struct DataQualityWidgetConfig<'a> {
    /// The clean entry's checks table shows every check, not the first few.
    pub checks_expanded: bool,
    pub state: &'a DataTableState,
    pub plan: &'a DataQualityPlan,
    /// The plan the result on screen was measured with; the header says this one
    /// even while Setup edits another.
    pub measured: &'a DataQualityPlan,
    pub results: Option<&'a DataQualityResults>,
    pub from_cache: bool,
    pub metric: QualityMetric,
    pub segment_index: usize,
    /// The interval a detail shows.
    pub interval_index: usize,
    /// The Trends line a bar detail shows.
    pub trend_line: usize,
    /// The Expected editor, while it is open.
    pub expected_form: Option<&'a crate::analysis::analysis_modal::ExpectedForm>,
    pub segments_by_change: bool,
    pub page: QualityPage,
    pub setup: SetupView<'a>,
    pub plan_field: usize,
    pub show_access: bool,
    pub observation_detail: bool,
    /// How Overview narrows and orders the findings.
    pub findings: &'a FindingsView,
    /// The rows the report measured are still in memory: a finding's open from them.
    pub rows_kept: bool,
    /// A read of a finding's rows, waiting for Enter.
    pub evidence_read: Option<&'a EvidenceRead>,
    pub focus: AnalysisFocus,
    pub theme: &'a Theme,
    /// The dialogs' Surfaces and the table's number formatting.
    pub ctx: &'a RenderContext,
    /// One column's declared intent, being edited over the Column intent list.
    pub intent_form: Option<&'a crate::analysis::intent_modal::IntentForm>,
    /// The dialog writing the report on screen to a file.
    pub export_form: Option<&'a crate::analysis::quality_export::ExportForm>,
}

/// The tool list's width: the width every analysis tool gives it, and none on a
/// terminal too narrow to hold it beside the result (a picker pops up instead).
fn data_quality_sidebar_width(width: u16) -> u16 {
    if width >= 76 {
        crate::widgets::analysis::sidebar_width(width)
    } else {
        0
    }
}

/// Where the result goes: under the header and the page tabs, left of the tool list.
/// The Sample form fills it before the first run.
pub(crate) fn main_pane(area: Rect) -> Rect {
    Rect {
        y: area.y + 2,
        height: area.height.saturating_sub(2),
        width: area
            .width
            .saturating_sub(data_quality_sidebar_width(area.width)),
        ..area
    }
}

pub fn render(
    config: DataQualityWidgetConfig<'_>,
    table_state: &mut TableState,
    sidebar_state: &mut TableState,
    detail_scroll: &mut DetailScroll,
    area: Rect,
    buf: &mut Buffer,
) {
    let sidebar_width = data_quality_sidebar_width(area.width);
    let vertical = Layout::default()
        .direction(Direction::Vertical)
        .constraints([
            Constraint::Length(1),
            Constraint::Length(1),
            Constraint::Fill(1),
        ])
        .split(area);

    render_header(&config, vertical[0], buf);
    render_tabs(&config, vertical[1], buf);

    let body = if sidebar_width > 0 {
        let horizontal = Layout::default()
            .direction(Direction::Horizontal)
            .constraints([Constraint::Fill(1), Constraint::Length(sidebar_width)])
            .split(vertical[2]);
        render_sidebar(&config, sidebar_state, horizontal[1], buf);
        horizontal[0]
    } else {
        vertical[2]
    };

    match config.page {
        QualityPage::Setup => render_setup(&config, body, buf),
        QualityPage::TimeRoles => render_time_roles(&config, table_state, body, buf),
        QualityPage::IntervalPairs => render_interval_pairs(&config, table_state, body, buf),
        QualityPage::Intent => {
            crate::widgets::quality_intent::render_list(&config, table_state, body, buf)
        }
        QualityPage::Overview => render_overview(&config, table_state, body, buf),
        QualityPage::Columns => render_columns(&config, table_state, body, buf),
        QualityPage::Segments => render_segments(&config, table_state, body, buf),
        QualityPage::SegmentDetail => {
            render_segment_detail(&config, table_state, config.segment_index, body, buf)
        }
        QualityPage::Trends => render_trends(&config, table_state, body, buf),
        QualityPage::TrendDetail => render_trend_detail(&config, table_state, body, buf),
        QualityPage::Gaps => render_gaps(&config, table_state, body, buf),
        QualityPage::ExpectedWindows => render_expected_windows(&config, body, buf),
        QualityPage::Intervals => render_intervals(&config, table_state, body, buf),
        QualityPage::IntervalDetail => render_interval_detail(&config, table_state, body, buf),
        QualityPage::Detail => render_detail(&config, table_state, body, buf),
    }

    // An overlay owns the keys: what it covers takes no clicks.
    if config.intent_form.is_some()
        || config.export_form.is_some()
        || config.show_access
        || config.observation_detail
    {
        crate::app::pointer::record(area, crate::app::pointer::Hit::Modal);
    }
    if let Some(form) = config.intent_form {
        crate::widgets::quality_intent::render_form(form, &config, area, buf);
    } else if let Some(form) = config.export_form {
        crate::widgets::quality_export::render(form, &config, area, buf);
    } else if config.show_access {
        render_access_plan(&config, area, buf);
    } else if config.observation_detail {
        render_finding_detail(&config, table_state, detail_scroll, area, buf);
    } else if sidebar_width == 0 && config.focus == AnalysisFocus::Sidebar {
        crate::app::pointer::record(area, crate::app::pointer::Hit::Modal);
        render_narrow_tool_picker(&config, sidebar_state, area, buf);
    }
    // A staged read of rows sits over whatever asked for it: a finding or a count.
    if let Some(read) = config.evidence_read {
        crate::app::pointer::record(area, crate::app::pointer::Hit::Modal);
        render_evidence_read(&config, read, area, buf);
    }
}

/// One line, as every analysis tool heads its result: the tool, then what the
/// numbers were measured on.
fn render_header(config: &DataQualityWidgetConfig<'_>, area: Rect, buf: &mut Buffer) {
    let middot = glyphs::get().middot;
    let mut text = "Data Quality".to_string();
    if let Some(results) = config.results.filter(|_| !config.page.is_setup()) {
        let plan = config.measured;
        text.push_str(&format!(" {middot} {}", measured_on(plan, results)));
        if plan.grain != QualityGrain::Dataset {
            text.push_str(&format!(" {middot} {}", plan.grain.label()));
        }
        if plan.comparison != QualityComparison::None {
            text.push_str(&format!(
                " {middot} compared with {}",
                plan.comparison_label()
            ));
        }
    }
    let mut spans = vec![Span::raw(text)];
    if config.from_cache && !config.page.is_setup() {
        spans.push(Span::styled(
            "  [session cache]",
            Style::default().fg(config.theme.dimmed()),
        ));
    }
    // State, not a message: it holds until the worker exits, on every page.
    if let Some(cancelling) = config.setup.cancelling {
        spans.push(Span::styled(
            format!(
                "  Cancelling: {} {} {}",
                cancelling.what(),
                glyphs::get().middot,
                crate::numfmt::clock(cancelling.since.elapsed())
            ),
            Style::default().fg(config.theme.warning()),
        ));
    }
    Paragraph::new(Line::from(spans))
        .style(crate::widgets::analysis::header_style(
            config.theme.controls_bg(),
            config.theme.table_header(),
        ))
        .render(area, buf);
}

/// The rows a result was measured on, in the words the other tools' headers use.
fn measured_on(plan: &DataQualityPlan, results: &DataQualityResults) -> String {
    let sample = plan.sample();
    let scope = if plan.scope == QualityScope::CurrentView {
        String::new()
    } else {
        format!(" {} {}", glyphs::get().middot, plan.scope.label())
    };
    match (results.precision, results.total_rows) {
        (QualityPrecision::Metadata, _) => format!("file metadata only, no values read{scope}"),
        (QualityPrecision::Exact, _) => sample.outcome(results.evaluated_rows, None, None),
        (QualityPrecision::Sampled, Some(total)) => {
            sample.outcome(total, Some(results.evaluated_rows), results.per_value)
        }
        (QualityPrecision::Sampled, None) => format!(
            "sample of {} rows{scope}",
            numfmt::group_chrome(results.evaluated_rows)
        ),
    }
}

/// The pages, with the one shown carrying the accent. `←→` walk them. Setup is not
/// one of them: it names itself alone, since the report's pages do not apply to it.
fn render_tabs(config: &DataQualityWidgetConfig<'_>, area: Rect, buf: &mut Buffer) {
    if config.page.is_setup() {
        Paragraph::new(Line::styled(
            "Setup",
            Style::default()
                .fg(config.theme.accent())
                .add_modifier(Modifier::BOLD),
        ))
        .render(area, buf);
        return;
    }
    let shown = config.page.tab();
    let dimmed = Style::default().fg(config.theme.dimmed());
    let accent = Style::default()
        .fg(config.theme.accent())
        .add_modifier(Modifier::BOLD);
    // Each title padded a space each side, a space between: a click on one presses
    // its number, as typed.
    let mut spans = Vec::new();
    let mut clicks = Vec::new();
    for (i, page) in QualityPage::TABS.iter().enumerate() {
        if i > 0 {
            spans.push(Span::styled(" ", dimmed));
        }
        spans.push(Span::styled(" ", dimmed));
        clicks.push((
            spans.len(),
            crate::app::pointer::Hit::Key(crossterm::event::KeyEvent::new(
                crossterm::event::KeyCode::Char(char::from(b'1' + i as u8)),
                crossterm::event::KeyModifiers::NONE,
            )),
        ));
        spans.push(Span::styled(
            page.title(),
            if *page == shown { accent } else { dimmed },
        ));
        spans.push(Span::styled(" ", dimmed));
    }
    let line = Line::from(spans);
    crate::app::pointer::record_spans(area, &line, clicks);
    Paragraph::new(line).style(dimmed).render(area, buf);
}
/// One line of Setup, top to bottom.
enum SetupLine {
    Rule(&'static str, Option<String>),
    Row(SetupRow),
    Note(String, bool),
}

/// Data Quality Setup: every run setting in four sections, and what the run will read.
/// Reads nothing (only schema, on-screen rows and kept runs); edits wait for Enter.
fn render_setup(config: &DataQualityWidgetConfig<'_>, area: Rect, buf: &mut Buffer) {
    let plan = config.plan;
    let view = &config.setup;
    let ctx = config.ctx;
    // A short terminal gives up the top margin before it gives up a row.
    let top = u16::from(area.height >= 20);
    let area = Rect {
        x: area.x + 1,
        y: area.y + top,
        width: area.width.saturating_sub(2),
        height: area.height.saturating_sub(top),
    };
    if area.height < 3 || area.width < 20 {
        return;
    }
    let width = area.width as usize;
    let schema = config.state.quality_schema(&plan.scope);

    let mut lines = vec![
        SetupLine::Rule("Rows & sample", None),
        SetupLine::Row(SetupRow::Sample),
    ];
    let texts = config.state.quality_text_columns(&plan.scope).len();
    let times = view.time_candidates.len().saturating_sub(texts);
    let counts = [(times, "date or time"), (texts, "text")]
        .into_iter()
        .filter(|(count, _)| *count > 0)
        .map(|(count, kind)| format!("{} {kind}", numfmt::group_chrome(count)))
        .collect::<Vec<_>>()
        .join(&format!(" {} ", glyphs::get().middot));
    lines.push(SetupLine::Rule(
        "Columns",
        (!counts.is_empty()).then_some(counts),
    ));
    lines.push(SetupLine::Row(SetupRow::TextAsTime));
    lines.push(SetupLine::Row(SetupRow::TimeRoles));
    lines.push(SetupLine::Row(SetupRow::Intervals));
    lines.push(SetupLine::Row(SetupRow::Intent));
    for (note, warn) in column_notes(plan, schema) {
        for line in crate::widgets::info::wrap_to(&note, width.saturating_sub(2)) {
            lines.push(SetupLine::Note(line, warn));
        }
    }
    lines.push(SetupLine::Rule("Study", None));
    for row in [
        SetupRow::Grain,
        SetupRow::Expected,
        SetupRow::Compare,
        SetupRow::Values,
        SetupRow::Latency,
        SetupRow::WindowBy,
    ] {
        lines.push(SetupLine::Row(row));
    }
    lines.push(SetupLine::Rule("Read", view.kept.map(|kept| kept.label())));
    let read_start = lines.len();
    for note in read_lines(config) {
        for line in crate::widgets::info::wrap_to(&note, width.saturating_sub(2)) {
            lines.push(SetupLine::Note(line, false));
        }
    }

    // The status line keeps the bottom row: why Run waits, or that it would
    // replace the report on screen.
    let status = setup_status(config);
    let body_height = area.height.saturating_sub(1) as usize;
    // Scrolled only as far as the focused row needs; the read summary, last, is
    // what gives way, and says how much of it is off screen.
    let focused = lines
        .iter()
        .position(
            |line| matches!(line, SetupLine::Row(row) if *row == SetupRow::at(config.plan_field)),
        )
        .unwrap_or(0);
    let (offset, room) = if lines.len() <= body_height {
        (0, body_height)
    } else {
        // One line counts what is cut, unless scrolling reaches the end anyway.
        let room = body_height.saturating_sub(1);
        let offset = (focused + 1).saturating_sub(room);
        if offset + body_height >= lines.len() {
            (lines.len() - body_height, body_height)
        } else {
            (offset, room)
        }
    };
    let end = (offset + room).min(lines.len());
    let cut = lines.len() - end;
    let dimmed = Style::default().fg(ctx.dimmed);
    let warning = Style::default().fg(ctx.warning);
    if cut > 0 {
        Paragraph::new(Line::styled(
            format!(
                "  {} {cut} more{}",
                glyphs::get().ellipsis,
                if end >= read_start { " (p)" } else { "" }
            ),
            dimmed,
        ))
        .render(
            Rect {
                y: area.y + room as u16,
                height: 1,
                ..area
            },
            buf,
        );
    }
    for (index, line) in lines.iter().enumerate().take(end).skip(offset) {
        let row_area = Rect {
            y: area.y + (index - offset) as u16,
            height: 1,
            ..area
        };
        match line {
            SetupLine::Rule(title, chip) => crate::widgets::ui::SectionRule {
                title,
                chip: chip.as_deref(),
            }
            .render(row_area, buf, ctx),
            SetupLine::Row(row) => {
                let (value, placeholder) = setup_value(config, *row);
                let room = (row_area.width as usize).saturating_sub(1 + SETUP_LABEL_WIDTH as usize);
                let value = glyphs::fit(&value, room);
                crate::widgets::ui::FormRow {
                    label: row.label(),
                    value: if placeholder {
                        FormValue::Placeholder(&value)
                    } else {
                        FormValue::Choice(&value)
                    },
                    focused: config.focus == AnalysisFocus::Main
                        && SetupRow::at(config.plan_field) == *row,
                    label_width: SETUP_LABEL_WIDTH,
                }
                .render(row_area, buf, ctx);
            }
            SetupLine::Note(text, warn) => {
                Paragraph::new(Line::styled(
                    format!("  {text}"),
                    if *warn { warning } else { dimmed },
                ))
                .render(row_area, buf);
            }
        }
    }
    if let Some((text, warn)) = status {
        Paragraph::new(Line::styled(text, if warn { warning } else { dimmed })).render(
            Rect {
                y: area.y + area.height - 1,
                height: 1,
                ..area
            },
            buf,
        );
    }
}

/// Where Setup's values start, past the rail gutter and the longest label.
const SETUP_LABEL_WIDTH: u16 = 15;

/// A Setup row's value, and whether it is a placeholder: a choice not made, or one
/// that has nothing to apply to yet.
fn setup_value(config: &DataQualityWidgetConfig<'_>, row: SetupRow) -> (String, bool) {
    let plan = config.plan;
    match row {
        SetupRow::Sample => {
            // The view's rows, when counted: a sample never promises more.
            let sample = plan.sample();
            let known = (sample.scope == crate::analysis::data_quality::QualityScope::CurrentView)
                .then(|| config.state.num_rows_if_valid())
                .flatten();
            (sample.summary_within(known), false)
        }
        SetupRow::TextAsTime if plan.time_formats.is_empty() => {
            ("none: every text column is text".to_string(), true)
        }
        // The columns here, each one's format on its own line below.
        SetupRow::TextAsTime => (
            plan.time_formats
                .iter()
                .map(|format| format.column.as_str())
                .collect::<Vec<_>>()
                .join(", "),
            false,
        ),
        SetupRow::TimeRoles if config.setup.time_candidates.is_empty() => {
            ("none: no date, time or text columns".to_string(), true)
        }
        SetupRow::TimeRoles if plan.temporal_roles.is_empty() => ("none".to_string(), true),
        SetupRow::TimeRoles => (
            plan.temporal_roles
                .iter()
                .map(|role| format!("{} = {}", role.role.label(), role.column))
                .collect::<Vec<_>>()
                .join(", "),
            false,
        ),
        SetupRow::Intervals if plan.candidate_pairs().is_empty() => {
            ("needs two time roles".to_string(), true)
        }
        SetupRow::Intervals if plan.interval_pairs().is_empty() => ("none".to_string(), true),
        SetupRow::Intervals => (
            plan.interval_pairs()
                .into_iter()
                .map(interval_label)
                .collect::<Vec<_>>()
                .join(", "),
            false,
        ),
        SetupRow::Intent if plan.intent.is_empty() => ("none".to_string(), true),
        SetupRow::Intent => (plan.intent.summary(), false),
        SetupRow::Grain => (plan.grain.label(), false),
        SetupRow::Expected => match (&plan.grain, plan.expected.as_ref()) {
            (QualityGrain::TimeWindows { every, .. }, Some(expected)) => (
                format!(
                    "{}, {}",
                    expected.cadence_label(every),
                    expected.range_label()
                ),
                false,
            ),
            (QualityGrain::TimeWindows { .. }, None) => {
                ("none: no window is a gap".to_string(), true)
            }
            _ => ("needs a time-window grain".to_string(), true),
        },
        SetupRow::Compare => (
            match (plan.comparison, plan.baseline_segment.as_deref()) {
                (QualityComparison::Baseline, Some(segment)) => format!("baseline {segment}"),
                (comparison, _) => comparison.choice_label().to_string(),
            },
            false,
        ),
        SetupRow::Values if plan.compute == QualityCompute::Metadata => {
            ("file metadata only".to_string(), false)
        }
        SetupRow::Values => ("read".to_string(), false),
        SetupRow::Latency if plan.interval_pairs().is_empty() => {
            ("needs an interval to measure".to_string(), true)
        }
        SetupRow::Latency => (
            crate::analysis::analysis_modal::threshold_label(plan.latency_threshold_seconds),
            plan.latency_threshold_seconds.is_none(),
        ),
        SetupRow::WindowBy => match (&plan.grain, plan.interval_clock) {
            (QualityGrain::TimeWindows { .. }, _) if plan.interval_pairs().is_empty() => {
                ("needs an interval".to_string(), true)
            }
            (QualityGrain::TimeWindows { column, .. }, IntervalClock::Grain) => {
                (format!("the grain's column, {column}"), false)
            }
            (QualityGrain::TimeWindows { .. }, clock) => (clock.label().to_string(), false),
            _ => ("needs a time-window grain".to_string(), true),
        },
    }
}

/// What the columns section says under its rows: text that needs a format before
/// it can be read as time, and which intervals the roles measure, or why none.
fn column_notes(plan: &DataQualityPlan, schema: &Schema) -> Vec<(String, bool)> {
    let mut notes = plan
        .time_formats
        .iter()
        .map(|format| {
            (
                format!("{} read as {}", format.column, format.label()),
                false,
            )
        })
        .collect::<Vec<_>>();
    let mut unread = Vec::new();
    let grain = match &plan.grain {
        QualityGrain::TimeWindows { column, .. } => Some(column.as_str()),
        _ => None,
    };
    for column in plan
        .temporal_roles
        .iter()
        .map(|role| role.column.as_str())
        .chain(grain)
    {
        if !plan.reads_as_time(column, schema) && !unread.contains(&column) {
            unread.push(column);
        }
    }
    for column in unread {
        notes.push((
            format!(
                "{column}: text, no format {} set Text as time",
                glyphs::get().middot
            ),
            true,
        ));
    }
    // A role that is in no interval measures nothing; say which, and where to fix it.
    let unpaired = plan.unpaired_roles();
    if plan.temporal_roles.len() == 1 {
        notes.push((
            crate::glyphs::dotted("1 role, no interval · add one under Time roles"),
            true,
        ));
    } else if !unpaired.is_empty() {
        notes.push((
            format!(
                "In no interval: {} {} set Intervals",
                unpaired
                    .iter()
                    .map(|role| role.label())
                    .collect::<Vec<_>>()
                    .join(", "),
                glyphs::get().middot
            ),
            true,
        ));
    }
    // An interval from a time with no zone to an instant reads the first as UTC.
    let mut naive = Vec::new();
    for (start, end) in plan.interval_pairs() {
        let (Some(start), Some(end)) = (plan.role_column(start), plan.role_column(end)) else {
            continue;
        };
        for (column, other) in [(start, end), (end, start)] {
            if plan.zoned(column, schema) == Some(false)
                && plan.zoned(other, schema) == Some(true)
                && !naive.contains(&column)
            {
                naive.push(column);
            }
        }
    }
    if !naive.is_empty() {
        notes.push((
            format!("No time zone, read as UTC: {}", naive.join(", ")),
            false,
        ));
    }
    // Intent on a column the scope does not have measures nothing.
    let absent = plan
        .intent
        .declared_columns()
        .into_iter()
        .filter(|column| schema.get(column).is_none())
        .collect::<Vec<_>>();
    if !absent.is_empty() {
        notes.push((
            format!("Not in scope, not checked: {}", absent.join(", ")),
            true,
        ));
    }
    notes
}

/// What Run will read, in the order it happens, before it happens.
fn read_lines(config: &DataQualityWidgetConfig<'_>) -> Vec<String> {
    let plan = config.plan;
    let view = &config.setup;
    let state = config.state;
    let mut lines = Vec::new();
    if view.unchanged {
        lines.push(crate::glyphs::dotted(
            "Report on screen: this setup · no read",
        ));
        return lines;
    }
    if view.relabel_only {
        let compare = config.measured.compares_differently(plan);
        let expected = config.measured.expected != plan.expected;
        lines.push(glyphs::dotted(match (compare, expected) {
            (true, true) => "Changed: Compare, Expected · no read",
            (true, false) => "Changed: Compare · no read",
            _ => "Changed: Expected · no read",
        }));
        return lines;
    }
    if view.cached {
        lines.push(crate::glyphs::dotted("Session cache: this setup · no read"));
        return lines;
    }
    let scope_rows = planned_scope_rows(state, plan);
    let n = numfmt::group_chrome(plan.dataset_rows);
    match plan.compute {
        QualityCompute::Metadata => {
            lines.push("File metadata only: no values read".to_string());
            return lines;
        }
        QualityCompute::Full => {
            lines.extend(copy_lines(view, full_passes(config)));
            // Each column an interval is windowed by is a grouping of its own.
            let clocks = crate::analysis::data_quality::interval_passes(
                plan,
                state.quality_schema(&plan.scope),
            );
            if clocks > 1 {
                lines.push(format!(
                    "Window by {}: {clocks} of those passes, 1 per column",
                    plan.interval_clock.label()
                ));
            }
        }
        QualityCompute::Sample if view.reuses_sample => {
            lines.push(crate::glyphs::dotted(
                "Rows: from an earlier run · no source read",
            ));
        }
        QualityCompute::Sample => lines.push(match &plan.method {
            crate::analysis::sampling::SampleMethod::FirstRows => {
                format!("First {n} rows of the scope")
            }
            crate::analysis::sampling::SampleMethod::PerPartition { column } => format!(
                "1 pass over every eligible row {} {n} kept per {column}",
                glyphs::get().middot
            ),
            _ if scope_rows.is_some_and(|rows| rows <= plan.dataset_rows) => {
                crate::glyphs::dotted("Every row · scope no larger than the sample")
            }
            _ if view.reads_blocks => crate::glyphs::dotted(&format!(
                "Seeded runs of the file · about {n} rows, no full pass"
            )),
            _ => crate::glyphs::dotted(&format!(
                "1 streaming pass over every eligible row · seeded {n} kept"
            )),
        }),
    }
    if plan.compute == QualityCompute::Sample && !view.reuses_sample && view.released {
        lines.push(crate::glyphs::dotted(
            "Released since last read · read again",
        ));
    }
    let exact = scope_rows.is_some_and(|rows| rows <= plan.dataset_rows);
    if plan.compute == QualityCompute::Sample && !exact {
        let column = match &plan.grain {
            QualityGrain::Partition(column) | QualityGrain::TimeWindows { column, .. } => {
                column.as_str()
            }
            _ => "",
        };
        match &view.segment_count {
            SegmentCount::CountPass => lines.push(format!(
                "+1 count of {column} {} exact segment totals, kept",
                glyphs::get().middot
            )),
            SegmentCount::InSamplePass => lines.push(format!(
                "Segment totals: exact, counted by {column} in that pass"
            )),
            SegmentCount::Retained => lines.push(crate::glyphs::dotted(
                "Segment totals: from an earlier count · no read",
            )),
            SegmentCount::RolledUp(finer) => lines.push(format!(
                "Segment totals: summed from earlier {} counts",
                window_cadence(finer)
            )),
            SegmentCount::TooMany => lines.push(format!(
                "Too many segments {} to count {} choose a coarser grain",
                plan.grain.label(),
                glyphs::get().middot
            )),
            SegmentCount::NotNeeded | SegmentCount::PerValue => {}
        }
    }
    if plan.compute == QualityCompute::Sample {
        lines.push(crate::glyphs::dotted(
            "Measured in memory · no further reads",
        ));
    }
    lines.extend(intent_read_lines(plan, exact));
    // Gaps come from the segment counts the run already takes, never a read of
    // their own.
    if plan.expected_windows().is_some() && plan.compute != QualityCompute::Metadata {
        lines.push(crate::glyphs::dotted(
            "Expected windows: from the segment counts · no read",
        ));
    }
    let rows = planned_rows(state, plan)
        .map(numfmt::group_chrome)
        .unwrap_or_else(|| "unknown".to_string());
    let sampled = plan.compute == QualityCompute::Sample;
    let read = if sampled && view.reuses_sample && view.segment_count.reads() && !exact {
        "the grain's column, for the count".to_string()
    } else if sampled && view.reuses_sample {
        "none".to_string()
    } else if let Some(transfer) = copy_transfer(view.copy, state) {
        transfer
    } else if state.is_remote_source() {
        "unknown".to_string()
    } else if sampled
        && view.reads_blocks
        && !exact
        && matches!(plan.method, crate::analysis::sampling::SampleMethod::Spread)
    {
        // A ceiling of the whole file would say more than the runs read.
        "the row groups the runs fall in".to_string()
    } else {
        planned_read_label(state, plan)
    };
    lines.push(format!(
        "Rows evaluated {rows} {} {} {read}",
        glyphs::get().middot,
        if state.is_remote_source() {
            "remote transfer"
        } else {
            "local read"
        }
    ));
    lines
}

/// A full scan's first lines in Read: one pass per check, over the source, over a
/// local copy fetched first, or over one fetched earlier, and why no copy where none.
fn copy_lines(view: &SetupView<'_>, passes: usize) -> Vec<String> {
    let bytes = crate::numfmt::bytes;
    let over_source = crate::glyphs::dotted(&format!(
        "Every eligible row · up to {passes} passes over the source, 1 per check"
    ));
    match view.copy {
        CopyPlan::NotApplicable => vec![crate::glyphs::dotted(&format!(
            "Every eligible row · up to {passes} passes over the scope, 1 per check"
        ))],
        CopyPlan::Fetch {
            bytes: size,
            objects,
        } => {
            let mut lines = vec![
                crate::glyphs::dotted(&format!(
                    "1 fetch of {} ({size}) to a local copy · up to {passes} passes over it",
                    objects_label(objects),
                    size = bytes(size)
                )),
                crate::glyphs::dotted("Local copy kept for later full scans · d releases"),
            ];
            if view.copy_released {
                lines.push(crate::glyphs::dotted(
                    "Released since last copy · fetched again",
                ));
            }
            lines
        }
        CopyPlan::Kept { bytes: size, .. } => vec![crate::glyphs::dotted(&format!(
            "Every eligible row · up to {passes} passes over the local copy ({}) · no source read",
            bytes(size)
        ))],
        CopyPlan::Passes(why) => vec![
            over_source,
            match why {
                NoCopy::Off => "No local copy: quality_local_copy = 0".to_string(),
                NoCopy::SizeUnknown => "No local copy: object sizes unknown".to_string(),
                NoCopy::Unusable => "No local copy: copy did not read as the source".to_string(),
                NoCopy::PartOfTheSource => {
                    "No local copy: scope reads part of the source".to_string()
                }
                NoCopy::TooLarge { bytes: size, limit } => format!(
                    "No local copy: {} over the {} limit",
                    bytes(size),
                    bytes(limit)
                ),
                NoCopy::NoRoom {
                    bytes: size,
                    free: Some(free),
                } => format!(
                    "No local copy: {} needed, {} free on disk",
                    bytes(size),
                    bytes(free)
                ),
                NoCopy::NoRoom { free: None, .. } => {
                    "No local copy: free disk space unknown".to_string()
                }
            },
        ],
    }
}

fn objects_label(objects: usize) -> String {
    if objects == 1 {
        "1 object".to_string()
    } else {
        format!("{} objects", numfmt::group_chrome(objects))
    }
}

/// What a full scan takes from a remote source when it reads a local copy: the
/// objects once, or nothing. `None` when its passes read the source.
fn copy_transfer(copy: CopyPlan, state: &DataTableState) -> Option<String> {
    let conflicts = if state.quality_conflict_reads() > 0 {
        ", plus the conflict reads"
    } else {
        ""
    };
    match copy {
        CopyPlan::Fetch { bytes, .. } => Some(format!(
            "{}, one request per object{conflicts}",
            crate::numfmt::bytes(bytes)
        )),
        CopyPlan::Kept { .. } if conflicts.is_empty() => Some("none".to_string()),
        CopyPlan::Kept { .. } => Some("only the conflict reads".to_string()),
        _ => None,
    }
}

/// What declared intent costs, said before Run: no extra rows, but a key on a sample
/// covers only sampled rows, and on a full scan is a grouping of its own.
fn intent_read_lines(plan: &DataQualityPlan, every_row: bool) -> Vec<String> {
    if plan.intent.is_empty() {
        return Vec::new();
    }
    let key = !plan.intent.key.is_empty();
    match plan.compute {
        QualityCompute::Metadata => vec!["Column intent: not checked, needs values".to_string()],
        QualityCompute::Full if key => {
            vec![crate::glyphs::dotted(
                "Column intent: in the profile pass · key adds 1 pass",
            )]
        }
        QualityCompute::Full => {
            vec![crate::glyphs::dotted(
                "Column intent: in the profile pass · no extra pass",
            )]
        }
        QualityCompute::Sample => {
            let mut lines = vec![crate::glyphs::dotted(
                "Column intent: on the rows read · no extra read",
            )];
            if key && !every_row {
                lines.push(crate::glyphs::dotted(&format!(
                    "Key: repeats among the {} sampled rows only · Every row checks all",
                    numfmt::group_chrome(plan.dataset_rows)
                )));
            }
            lines
        }
    }
}

/// Collects a full run makes over its scope: one per check, plus a count first when the
/// scope's size is unknown.
fn full_passes(config: &DataQualityWidgetConfig<'_>) -> usize {
    let plan = config.plan;
    let state = config.state;
    let texts = state
        .quality_schema(&plan.scope)
        .iter()
        .filter(|(_, dtype)| matches!(dtype, DataType::String | DataType::Categorical(..)))
        .count();
    // Profile, most common values, duplicates, and at most one for shared nulls.
    let mut passes = 4 + texts;
    if !matches!(plan.grain, QualityGrain::Dataset) {
        passes += 1;
    }
    passes +=
        crate::analysis::data_quality::interval_passes(plan, state.quality_schema(&plan.scope));
    // The declared key is a grouping of its columns: one pass of its own.
    passes += usize::from(!plan.intent.key.is_empty());
    if planned_scope_rows(state, plan).is_none() {
        passes += 1;
    }
    passes + state.quality_conflict_reads()
}

/// Setup's bottom line: a cancelled read still running, why Enter did not run, or
/// that the draft differs from the report it would replace.
fn setup_status(config: &DataQualityWidgetConfig<'_>) -> Option<(String, bool)> {
    let view = &config.setup;
    if let Some(cancelling) = view.cancelling {
        // Said as a reason once Enter has been refused for it, short enough to keep
        // its clock beside the tool list at 80 columns.
        return Some((
            format!(
                "{}{} {} {}",
                if view.note.is_some() {
                    "Run waits: "
                } else {
                    "Cancelling: "
                },
                cancelling.what(),
                glyphs::get().middot,
                crate::numfmt::clock(cancelling.since.elapsed())
            ),
            true,
        ));
    }
    if let Some(note) = view.note {
        return Some((note.to_string(), true));
    }
    if view.edited {
        return Some((
            crate::glyphs::dotted("Edited · Enter runs, Esc discards"),
            false,
        ));
    }
    None
}

fn render_overview(
    config: &DataQualityWidgetConfig<'_>,
    table_state: &mut TableState,
    area: Rect,
    buf: &mut Buffer,
) {
    let Some(results) = config.results else {
        render_run_prompt(area, config.theme, buf);
        return;
    };
    let report = results.report();
    let all_checks = results.checks();
    let notes = config.state.notes();
    let notes_height = if notes.is_empty() {
        0
    } else {
        (notes.len() as u16).min(3) + 2
    };
    // Coverage sits under the verdict on every report, clean or not: four lines
    // where there is room, two on a short terminal.
    let coverage = coverage_lines(
        &coverage(results, all_checks, config.measured),
        area.width.saturating_sub(2),
        if area.height >= 16 { 4 } else { 2 },
        config.theme,
    );
    let sections = Layout::default()
        .direction(Direction::Vertical)
        .constraints([
            Constraint::Length(2 + coverage.len() as u16),
            Constraint::Length(notes_height),
            Constraint::Fill(1),
        ])
        .horizontal_margin(1)
        .vertical_margin(1)
        .split(area);
    render_verdict(config, report, sections[0], buf);
    Paragraph::new(coverage).render(
        Rect {
            y: sections[0].y + 1,
            height: sections[0].height.saturating_sub(1),
            ..sections[0]
        },
        buf,
    );
    if !notes.is_empty() {
        let mut lines = vec![rule_line(
            "Dataset notes",
            Some(&numfmt::group_chrome(notes.len())),
            sections[1].width,
            config.theme,
        )];
        lines.extend(notes.iter().take(3).map(|note| {
            Line::raw(format!(
                "{} {} {}",
                note.summary,
                glyphs::get().dash,
                note.scope
            ))
        }));
        Paragraph::new(lines).render(sections[1], buf);
    }

    let mut list = sections[2];
    if report.findings.is_empty() {
        let message = if report.metadata_only {
            "No values read · e Setup"
        } else if report.no_rows {
            "No rows in scope · e Setup"
        } else {
            "No columns to check"
        };
        Paragraph::new(glyphs::dotted(message))
            .wrap(Wrap { trim: true })
            .style(Style::default().fg(config.theme.dimmed()))
            .render(list, buf);
        return;
    }
    let shown = config.findings.shown(report);
    // Narrowed or reordered, the list says so above itself, and what it holds.
    let facets = config.findings.describe();
    if !facets.is_empty() && list.height > 1 {
        let total = report
            .findings
            .iter()
            .filter(|finding| finding.kind.is_some())
            .count();
        let listed = shown
            .iter()
            .filter(|index| report.findings[**index].kind.is_some())
            .count();
        let middot = glyphs::get().middot;
        let mut text = if config.findings.narrowed() {
            format!("{listed} of {total} findings")
        } else {
            format!(
                "{total} {}",
                if total == 1 { "finding" } else { "findings" }
            )
        };
        for facet in &facets {
            text.push_str(&format!(" {middot} {facet}"));
        }
        Paragraph::new(Line::styled(
            glyphs::fit(&text, list.width as usize),
            Style::default().fg(config.theme.dimmed()),
        ))
        .render(Rect { height: 1, ..list }, buf);
        list.y += 1;
        list.height -= 1;
    }
    normalize_selection(table_state, shown.len());
    if shown.is_empty() {
        Paragraph::new(glyphs::dotted("No findings match · Esc shows all"))
            .wrap(Wrap { trim: true })
            .style(Style::default().fg(config.theme.dimmed()))
            .render(list, buf);
        return;
    }
    if report.problems == 0 && report.notes == 0 && !report.metadata_only && !report.no_rows {
        // Nothing to fix: what was checked is the answer, so it is on the page
        // rather than behind the clean entry.
        let parts = Layout::default()
            .direction(Direction::Vertical)
            .constraints([Constraint::Length(3), Constraint::Fill(1)])
            .split(list);
        render_findings(config, report, table_state, parts[0], buf);
        Paragraph::new(check_lines(all_checks, parts[1].width, None, config.theme))
            .render(parts[1], buf);
        return;
    }
    render_findings(config, report, table_state, list, buf);
}

/// The answer before the evidence: a mark and the counts. What they were measured on
/// is the strip above.
fn render_verdict(
    config: &DataQualityWidgetConfig<'_>,
    report: &QualityReport,
    area: Rect,
    buf: &mut Buffer,
) {
    let g = glyphs::get();
    let theme = config.theme;
    let (mark, tone) = if report.problems > 0 {
        (g.warning, theme.warning())
    } else if report.metadata_only || report.no_rows {
        (g.middot, theme.dimmed())
    } else {
        (g.check, theme.success())
    };
    Paragraph::new(Line::from(vec![
        Span::styled(format!("{mark} "), Style::default().fg(tone)),
        Span::styled(
            verdict(report),
            Style::default().fg(tone).add_modifier(Modifier::BOLD),
        ),
    ]))
    .render(area, buf);
}

/// The label gutter of the coverage lines, as wide as its longest label and a gap.
const COVERAGE_LABEL: usize = 8;

/// The coverage under the verdict (checks, rows, limits), each a labeled line wrapping
/// between facts. Over `max_lines`, rows go first (the header states them); a cut
/// section counts what it omitted.
fn coverage_lines(
    coverage: &Coverage,
    width: u16,
    max_lines: usize,
    theme: &Theme,
) -> Vec<Line<'static>> {
    let mut sections = [
        ("Checks", coverage.checks()),
        ("Rows", coverage.rows.clone()),
        ("Limits", coverage.limits()),
    ]
    .into_iter()
    .filter(|(_, facts)| !facts.is_empty())
    .collect::<Vec<_>>();
    if sections.len() > max_lines {
        sections.retain(|(label, _)| *label != "Rows");
    }
    sections.truncate(max_lines);
    let text_width = (width as usize).saturating_sub(COVERAGE_LABEL).max(1);
    let dimmed = Style::default().fg(theme.dimmed());
    let plain = Style::default().fg(theme.text_primary());
    let mut lines = Vec::new();
    for (index, (label, facts)) in sections.iter().enumerate() {
        // Every later section keeps one line; this one may take the rest.
        let later = sections.len() - index - 1;
        let room = max_lines.saturating_sub(lines.len() + later).max(1);
        for (row, text) in pack_facts(facts, text_width, room).into_iter().enumerate() {
            let gutter = if row == 0 {
                format!("{label:<COVERAGE_LABEL$}")
            } else {
                " ".repeat(COVERAGE_LABEL)
            };
            lines.push(Line::from(vec![
                Span::styled(gutter, dimmed),
                Span::styled(text, plain),
            ]));
        }
    }
    lines
}

/// `facts` joined by middots into at most `room` lines of `width`, breaking between
/// facts. What does not fit is counted at the end of the last line: "+2 more".
fn pack_facts(facts: &[String], width: usize, room: usize) -> Vec<String> {
    let sep = format!(" {} ", glyphs::get().middot);
    let join = |line: &[String]| line.join(&sep);
    let mut lines: Vec<Vec<String>> = vec![Vec::new()];
    let mut placed = 0;
    for fact in facts {
        let fact = glyphs::fit(fact, width);
        let current = lines.last_mut().expect("one line at least");
        let mut joined = current.clone();
        joined.push(fact.clone());
        if current.is_empty() || glyphs::display_width(&join(&joined)) <= width {
            *current = joined;
        } else if lines.len() < room {
            lines.push(vec![fact]);
        } else {
            break;
        }
        placed += 1;
    }
    let left = facts.len() - placed;
    let last = lines.last_mut().expect("one line at least");
    if left > 0 {
        // Make room for the count by giving up facts from the end of the line.
        let mut dropped = left;
        loop {
            let more = format!("+{dropped} more");
            let mut line = last.clone();
            line.push(more);
            if glyphs::display_width(&join(&line)) <= width || last.is_empty() {
                *last = line;
                break;
            }
            // A line's only fact is cut short rather than given up, so a line
            // never says only how much it left out.
            if let [only] = last.as_mut_slice() {
                let room =
                    width.saturating_sub(glyphs::display_width(&format!("{sep}+{dropped} more")));
                if room >= 8 {
                    *only = glyphs::fit(only, room);
                    continue;
                }
            }
            last.pop();
            dropped += 1;
        }
    }
    lines.iter().map(|line| join(line)).collect()
}

/// A title on a rule with a flat count chip, as `SectionRule` draws it, from the
/// theme this widget is handed.
pub(crate) fn rule_line(
    title: &str,
    chip: Option<&str>,
    width: u16,
    theme: &Theme,
) -> Line<'static> {
    let mut spans = vec![
        Span::styled(
            title.to_string(),
            Style::default()
                .fg(theme.accent())
                .add_modifier(Modifier::BOLD),
        ),
        Span::raw(" "),
    ];
    let mut used = glyphs::display_width(title) + 1;
    if let Some(chip) = chip {
        let chip = format!(" {chip} ");
        used += glyphs::display_width(&chip) + 1;
        spans.push(Span::styled(
            chip,
            Style::default()
                .bg(theme.controls_bg())
                .fg(theme.text_primary()),
        ));
        spans.push(Span::raw(" "));
    }
    spans.push(Span::styled(
        glyphs::get()
            .rule_h
            .repeat((width as usize).saturating_sub(used)),
        Style::default().fg(theme.table_column_separator()),
    ));
    Line::from(spans)
}

fn severity_mark(severity: Severity, theme: &Theme) -> Span<'static> {
    let g = glyphs::get();
    match severity {
        Severity::Problem => Span::styled(g.warning, Style::default().fg(theme.warning())),
        Severity::Note => Span::styled(g.middot, Style::default().fg(theme.text_primary())),
        Severity::Clean => Span::styled(g.check, Style::default().fg(theme.success())),
    }
}

/// Findings under one rule per severity. The selection lives in finding space, so
/// the keys never land on a rule; the scroll offset lives in line space.
fn render_findings(
    config: &DataQualityWidgetConfig<'_>,
    report: &QualityReport,
    table_state: &mut TableState,
    area: Rect,
    buf: &mut Buffer,
) {
    enum Item {
        Rule(Severity, usize),
        Gap,
        Finding(usize),
    }
    // Positions in the list as shown, which is what the selection counts; each
    // severity's rule counts what is shown under it.
    let shown = config.findings.shown(report);
    let mut items = Vec::new();
    let mut current = None;
    for (position, index) in shown.iter().enumerate() {
        let finding = &report.findings[*index];
        if current != Some(finding.severity) {
            if current.is_some() {
                items.push(Item::Gap);
            }
            let count = shown
                .iter()
                .filter(|other| report.findings[**other].severity == finding.severity)
                .count();
            let count = match finding.severity {
                // The clean entry is one row naming many columns; count the columns.
                Severity::Clean => report.clean_columns,
                _ => count,
            };
            items.push(Item::Rule(finding.severity, count));
            current = Some(finding.severity);
        }
        items.push(Item::Finding(position));
    }

    let height = area.height as usize;
    if height == 0 {
        return;
    }
    let selected = table_state.selected().unwrap_or(0);
    let selected_line = items
        .iter()
        .position(|item| matches!(item, Item::Finding(index) if *index == selected))
        .unwrap_or(0);
    let mut offset = table_state.offset().min(items.len().saturating_sub(1));
    if selected_line < offset {
        // Bring the section's rule along when the selection is its first row.
        offset = if selected_line > 0 && matches!(items[selected_line - 1], Item::Rule(..)) {
            selected_line - 1
        } else {
            selected_line
        };
    } else if selected_line >= offset + height {
        offset = selected_line + 1 - height;
    }
    *table_state.offset_mut() = offset;

    // Mark, title, columns, summary. The title column fits the longest title and a
    // gap; the columns take a share of what is left, so the summary keeps the rest.
    let title_width = report
        .findings
        .iter()
        .map(|finding| glyphs::display_width(finding.title))
        .max()
        .unwrap_or(0)
        + 2;
    let width = area.width as usize;
    let lead = 4; // rail + space + mark + space
    let rest = width.saturating_sub(lead + title_width);
    let columns_width = (rest * 2 / 5).clamp(10.min(rest), 32);
    // Ordered by a number, the number is on every row, right-aligned, so the order
    // can be read; the summary gives up the room.
    let ordered = config.findings.order != FindingOrder::Ranked;
    let numbers = |finding: &crate::analysis::quality_report::Finding| {
        if finding.kind.is_none() {
            return (String::new(), String::new());
        }
        (
            numfmt::group_chrome(finding.affected_rows),
            crate::numfmt::percent_of(finding.affected_rows, finding.evaluated_rows),
        )
    };
    let widest = |pick: fn((String, String)) -> String| {
        shown
            .iter()
            .map(|index| glyphs::display_width(&pick(numbers(&report.findings[*index]))))
            .max()
            .unwrap_or(0)
    };
    let (count_width, rate_width) = (widest(|(count, _)| count), widest(|(_, rate)| rate));
    // The number the list is ordered by leads, each in its own aligned column.
    let affected = |finding: &crate::analysis::quality_report::Finding| {
        let (count, rate) = numbers(finding);
        match config.findings.order {
            FindingOrder::Rate => format!("{rate:>rate_width$}  {count:>count_width$}"),
            _ => format!("{count:>count_width$}  {rate:>rate_width$}"),
        }
    };
    let affected_width = if ordered {
        count_width + rate_width + 3
    } else {
        0
    };
    let summary_width = rest.saturating_sub(columns_width + 1 + affected_width);
    // Too narrow to say anything, the summary gives its room to the numbers.
    let (summary_width, affected_width) = if ordered && summary_width < 12 {
        (
            0,
            rest.saturating_sub(columns_width + 1).max(affected_width),
        )
    } else {
        (summary_width, affected_width)
    };
    let g = glyphs::get();
    let theme = config.theme;
    let focused = config.focus == AnalysisFocus::Main;
    for (row, item) in items.iter().skip(offset).take(height).enumerate() {
        let line_area = Rect {
            y: area.y + row as u16,
            height: 1,
            ..area
        };
        let line = match item {
            Item::Gap => continue,
            Item::Rule(severity, count) => rule_line(
                severity.heading(),
                Some(&numfmt::group_chrome(*count)),
                area.width,
                theme,
            ),
            Item::Finding(position) => {
                let finding = &report.findings[shown[*position]];
                let is_selected = *position == selected;
                let columns = glyphs::fit(&finding.columns_label(columns_width), columns_width);
                let summary = glyphs::fit(&finding.summary, summary_width);
                let mut spans = vec![
                    Span::styled(
                        if is_selected { g.rail } else { " " },
                        Style::default().fg(theme.accent()),
                    ),
                    Span::raw(" "),
                    severity_mark(finding.severity, theme),
                    Span::raw(" "),
                    Span::styled(
                        format!("{:<title_width$}", finding.title),
                        Style::default().fg(theme.text_primary()),
                    ),
                    Span::styled(
                        format!("{columns:<columns_width$} "),
                        Style::default().fg(theme.text_primary()),
                    ),
                    Span::styled(
                        if ordered {
                            format!("{summary:<summary_width$}")
                        } else {
                            summary
                        },
                        Style::default().fg(theme.dimmed()),
                    ),
                ];
                if ordered {
                    spans.push(Span::styled(
                        format!("{:>affected_width$}", affected(finding)),
                        Style::default().fg(theme.text_primary()),
                    ));
                }
                let line = Line::from(spans);
                if is_selected {
                    let style = if focused {
                        theme.highlight_style()
                    } else {
                        Style::default()
                    };
                    buf.set_style(line_area, style);
                }
                line
            }
        };
        line.render(line_area, buf);
    }
}

/// The checks the run made, one per line: mark, name, reach, outcome, and what each
/// looks for when there is room. `limit` keeps the first few; the rest are counted.
fn check_lines(
    checks: &[Check],
    width: u16,
    limit: Option<usize>,
    theme: &Theme,
) -> Vec<Line<'static>> {
    let width = width as usize;
    let dimmed = Style::default().fg(theme.dimmed());
    let g = glyphs::get();
    let shown = limit.unwrap_or(checks.len()).min(checks.len());
    let outcome = |check: &Check| match &check.outcome {
        Outcome::Passed => "passed".to_string(),
        Outcome::Found { detail, .. } => format!("{detail} flagged"),
        Outcome::Skipped(reason) => format!("skipped: {reason}"),
        Outcome::Unavailable(reason) => format!("unavailable: {reason}"),
    };
    // Each column as wide as its widest entry across checks, so showing all moves nothing;
    // what a check looks for takes the rest, wrapping under itself.
    let column = |text: &dyn Fn(&Check) -> String| {
        checks
            .iter()
            .map(|check| glyphs::display_width(&text(check)))
            .max()
            .unwrap_or(0)
            + 2
    };
    let name_width = column(&|check| check.name.to_string());
    let reach_width = column(&|check| check.applies_to.clone());
    let outcome_width = column(&outcome);
    let lead = 2 + name_width + reach_width + outcome_width;
    // Too narrow for a readable fourth column: it goes on the lines below instead.
    let (looks_indent, looks_width) = if width >= lead + 24 {
        (lead, width - lead)
    } else {
        (2, width.saturating_sub(2).max(1))
    };
    let mut lines = vec![rule_line(
        "Checks",
        Some(&numfmt::group_chrome(checks.len())),
        width as u16,
        theme,
    )];
    for check in &checks[..shown] {
        let (mark, style) = match &check.outcome {
            Outcome::Passed => (
                Span::styled(g.check, Style::default().fg(theme.success())),
                Style::default().fg(theme.text_primary()),
            ),
            Outcome::Found { tier, .. } => (
                severity_mark(*tier, theme),
                Style::default().fg(theme.text_primary()),
            ),
            Outcome::Skipped(_) | Outcome::Unavailable(_) => (Span::styled(g.dash, dimmed), dimmed),
        };
        let beside = looks_indent == lead;
        let mut spans = vec![
            mark,
            Span::raw(" "),
            Span::styled(format!("{:<name_width$}", check.name), style),
            Span::styled(format!("{:<reach_width$}", check.applies_to), dimmed),
            // Padded only when something follows it on the line: trailing spaces
            // past the frame would wrap into a blank line.
            Span::styled(
                if beside {
                    format!("{:<outcome_width$}", outcome(check))
                } else {
                    outcome(check)
                },
                style,
            ),
        ];
        let mut looks = crate::widgets::info::wrap_to(check.looks_for, looks_width).into_iter();
        if beside {
            spans.extend(looks.next().map(|text| Span::styled(text, dimmed)));
        }
        lines.push(Line::from(spans));
        lines.extend(
            looks.map(|text| Line::styled(format!("{}{text}", " ".repeat(looks_indent)), dimmed)),
        );
    }
    if shown < checks.len() {
        lines.push(Line::styled(
            format!(
                "  {} more {}",
                checks.len() - shown,
                if checks.len() - shown == 1 {
                    "check"
                } else {
                    "checks"
                }
            ),
            dimmed,
        ));
    }
    lines
}

/// A segment label as drawn: `∅` (no value) uses the null glyph (ASCII under `LANG=C`).
/// The label itself is unchanged, since evidence finds rows by it.
fn segment_text(label: &str) -> String {
    label.replace('∅', glyphs::get().null)
}

/// The finding itself: what it is in one sentence with its numbers, why it matters,
/// what to check, and the evidence, in a frame on top of the list.
fn render_finding_detail(
    config: &DataQualityWidgetConfig<'_>,
    table_state: &TableState,
    scroll: &mut DetailScroll,
    area: Rect,
    buf: &mut Buffer,
) {
    let Some(results) = config.results else {
        return;
    };
    let report = results.report();
    let Some(finding) = table_state
        .selected()
        .and_then(|position| config.findings.selected(report, position))
    else {
        return;
    };
    let theme = config.theme;
    let dimmed = Style::default().fg(theme.dimmed());
    let (headline, evidence) = describe(finding, results);
    // A reading surface: cap the measure on a wide terminal. The checks table on
    // the clean entry is a table, and may use more of the width.
    let width = area
        .width
        .saturating_sub(4)
        .min(if finding.kind.is_none() { 118 } else { 84 });
    let inner = width.saturating_sub(4).max(1);
    // The name and the columns on the frame; the facts as a list under it.
    let title = if finding.kind.is_none() {
        finding.title.to_string()
    } else {
        let name = format!("{} {} ", finding.title, glyphs::get().middot);
        let room = (width as usize).saturating_sub(glyphs::display_width(&name) + 4);
        format!("{name}{}", finding.columns_label(room))
    };
    let bullet = |text: String| {
        Line::from(vec![
            Span::styled(format!("{} ", glyphs::get().middot), dimmed),
            Span::raw(text),
        ])
    };
    let mut lines = Vec::new();
    // The frame cut the list short: the whole of it, unless the evidence lists
    // every column with its rate already.
    if finding.kind.is_some()
        && finding.columns.len() > 1
        && !finding.lists_columns()
        && finding.columns_label(usize::MAX) != finding.columns_label(inner as usize)
    {
        lines.push(bullet(finding.columns.join(", ")));
    }
    lines.push(bullet(headline));
    lines.extend(
        evidence
            .into_iter()
            .map(|line| Line::raw(format!("  {line}"))),
    );
    lines.extend(advice(finding).into_iter().map(bullet));
    if finding.kind.is_none() {
        lines.push(Line::raw(""));
        lines.extend(check_lines(
            results.checks(),
            inner,
            (!config.checks_expanded).then_some(CHECKS_SHOWN),
            theme,
        ));
    }
    if finding.kind.is_some() {
        lines.push(Line::raw(""));
        lines.push(Line::styled(
            evidence_line(config, finding, results),
            dimmed,
        ));
    }
    // Grow with the text up to the screen, then scroll inside the frame; the last
    // row counts what is below.
    let rows = lines
        .iter()
        .map(|line| crate::render::home_view::wrapped_rows(line, inner as usize))
        .sum::<usize>()
        .min(u16::MAX as usize) as u16;
    let height = (rows + 2).min(area.height.saturating_sub(2));
    let room = height.saturating_sub(2);
    scroll.max = rows.saturating_sub(room);
    scroll.offset = scroll.offset.min(scroll.max);
    // Short of the end, the last row counts what is below instead of showing text.
    let shown = if scroll.offset < scroll.max {
        room.saturating_sub(1)
    } else {
        room
    };
    let below = rows.saturating_sub(scroll.offset + shown);
    let popup = dialog_in(area, width, height);
    let content = Surface::new(&title)
        .border_style(Style::default().fg(config.ctx.modal_border_active))
        .render(popup, buf, config.ctx);
    Paragraph::new(lines)
        .wrap(Wrap { trim: false })
        .scroll((scroll.offset, 0))
        .render(
            Rect {
                height: shown.min(content.height),
                ..content
            },
            buf,
        );
    if below > 0 && content.height > shown {
        Paragraph::new(Line::styled(
            format!("{} {below} more", glyphs::get().ellipsis),
            dimmed,
        ))
        .alignment(Alignment::Right)
        .render(
            Rect {
                y: content.y + shown,
                height: 1,
                ..content
            },
            buf,
        );
    }
}

/// What Enter does with a finding's rows, in one sentence: shows the ones the run
/// kept, reads ones it did not keep once asked, or says why there are none.
fn evidence_line(
    config: &DataQualityWidgetConfig<'_>,
    finding: &crate::analysis::quality_report::Finding,
    results: &DataQualityResults,
) -> String {
    let rows = match finding.evidence(results) {
        Ok(rows) => rows,
        Err(reason) => return reason,
    };
    if let EvidenceRows::Files(QualityScope::SourceFiles(files)) = &rows {
        return format!(
            "Enter: rows of the {} named {} (asks first)",
            files.len(),
            if files.len() == 1 { "file" } else { "files" }
        );
    }
    let sampled = results.precision == QualityPrecision::Sampled;
    let noun = |count: usize| {
        format!(
            "{} {}{}",
            numfmt::group_chrome(count),
            if sampled { "sampled " } else { "" },
            if count == 1 { "row" } else { "rows" }
        )
    };
    let spec = finding.kind.map(ObservationKind::spec);
    let what = match (spec, finding.evidence_count(results)) {
        // The measurement counts rows beyond one per value; the rows that share a
        // value are always more.
        (Some(spec), _) if spec.evidence == Evidence::SharingValue => format!(
            "every {}row that shares a repeated value",
            if sampled { "sampled " } else { "" }
        ),
        (spec, Some(count)) => format!(
            "the {}{}",
            noun(count),
            spec.map_or("", |spec| spec.rows_are)
        ),
        // Grouped columns: a row missing in any of them; the table counts them.
        (_, None) => format!(
            "the {}rows with any of them",
            if sampled { "sampled " } else { "" }
        ),
    };
    if config.rows_kept {
        let together = if matches!(rows, EvidenceRows::Duplicates) {
            ", copies together"
        } else {
            ""
        };
        format!("Enter: {what}{together}")
    } else if sampled {
        format!("Enter: {what} (sample released, asks first)")
    } else if config.measured.compute == QualityCompute::Full {
        format!("Enter: {what} (full scan keeps none, asks first)")
    } else {
        format!("Enter: {what} (rows released, asks first)")
    }
}

/// A read of a finding's rows, before it reads: what, why, how much, from where.
fn render_evidence_read(
    config: &DataQualityWidgetConfig<'_>,
    read: &EvidenceRead,
    area: Rect,
    buf: &mut Buffer,
) {
    let rows = read
        .summary
        .iter()
        .map(|(label, value)| FieldRow {
            mark: None,
            label: label.to_string(),
            value: value.clone(),
        })
        .collect::<Vec<_>>();
    let width = 72.min(area.width.saturating_sub(2));
    let label_width = rows
        .iter()
        .map(|row| glyphs::display_width(&row.label))
        .max()
        .unwrap_or(0)
        + 2;
    let lines = field_lines(&rows, label_width, width.saturating_sub(4) as usize, false);
    let popup = dialog_in(area, width, lines.len() as u16 + 2);
    let content = Surface::new("Read Rows")
        .border_style(Style::default().fg(config.ctx.modal_border_active))
        .render(popup, buf, config.ctx);
    render_counted(lines, content, config.theme, buf);
}

fn render_time_roles(
    config: &DataQualityWidgetConfig<'_>,
    table_state: &mut TableState,
    area: Rect,
    buf: &mut Buffer,
) {
    let theme = config.theme;
    let dimmed = Style::default().fg(theme.dimmed());
    let accent = Style::default().fg(theme.accent());
    let columns = config.setup.time_candidates;
    let roles = TemporalRole::ALL.len() as u16;
    let sections = Layout::default()
        .direction(Direction::Vertical)
        .constraints([
            Constraint::Length(2),
            Constraint::Length(roles + 1),
            Constraint::Length(1),
            Constraint::Length(2),
            Constraint::Fill(1),
        ])
        .margin(1)
        .split(area);
    Paragraph::new(rule_line("Time roles", None, sections[0].width, theme))
        .render(sections[0], buf);

    let focused_role = TemporalRole::ALL[config.plan_field.min(TemporalRole::ALL.len() - 1)];
    let assigned = |role: TemporalRole| {
        config
            .plan
            .temporal_roles
            .iter()
            .find(|assignment| assignment.role == role)
            .map(|assignment| assignment.column.clone())
    };
    let rows = TemporalRole::ALL.iter().map(|role| {
        Row::new(vec![
            Cell::from(role.label()),
            match assigned(*role) {
                Some(column) => Cell::from(column),
                None => Cell::from(Span::styled("unassigned", dimmed)),
            },
        ])
    });
    table_state.select(Some(config.plan_field.min(TemporalRole::ALL.len() - 1)));
    let table = Table::new(rows, [Constraint::Length(20), Constraint::Fill(1)])
        .header(Row::new(["Role", "Column"]).style(dimmed))
        .row_highlight_style(theme.highlight_style())
        .highlight_symbol(glyphs::get().selector);
    StatefulWidget::render(table, sections[1], buf, table_state);

    // The candidates, with a few of their values from the rows already on screen,
    // so choosing which column is "received" is choosing among things seen.
    Paragraph::new(rule_line(
        "Date, time and text columns",
        Some(&numfmt::group_chrome(columns.len())),
        sections[3].width,
        theme,
    ))
    .render(sections[3], buf);
    if columns.is_empty() {
        Paragraph::new(Span::styled("None in this data", dimmed)).render(sections[4], buf);
        return;
    }
    let name_width = columns
        .iter()
        .map(|column| glyphs::display_width(column))
        .max()
        .unwrap_or(0)
        + 2;
    let types = columns
        .iter()
        .map(|column| {
            // Text says how it is read, so a role on it is not mistaken for a cast.
            match (
                config.plan.time_format(column),
                config.state.quality_schema(&config.plan.scope).get(column),
            ) {
                (Some(format), _) => format!("text as {}", format.kind.label()),
                (None, Some(DataType::String | DataType::Categorical(..))) => {
                    "text, no format".to_string()
                }
                (None, dtype) => dtype.map(|dtype| dtype.to_string()).unwrap_or_default(),
            }
        })
        .collect::<Vec<_>>();
    let type_width = types
        .iter()
        .map(|dtype| glyphs::display_width(dtype))
        .max()
        .unwrap_or(0)
        + 2;
    let chosen = assigned(focused_role);
    let values_width = (sections[4].width as usize).saturating_sub(2 + name_width + type_width);
    let lines = columns
        .iter()
        .zip(&types)
        .map(|(column, dtype)| {
            let is_chosen = chosen.as_deref() == Some(column.as_str());
            let values = config.state.buffered_values(column, 3);
            let values = if values.is_empty() {
                "not in the rows on screen".to_string()
            } else {
                values.join("   ")
            };
            Line::from(vec![
                Span::styled(if is_chosen { glyphs::get().rail } else { " " }, accent),
                Span::raw(" "),
                Span::styled(
                    format!("{column:<name_width$}"),
                    if is_chosen {
                        accent
                    } else {
                        Style::default().fg(theme.text_primary())
                    },
                ),
                Span::styled(format!("{dtype:<type_width$}"), dimmed),
                Span::raw(glyphs::fit(&values, values_width)),
            ])
        })
        .collect::<Vec<_>>();
    Paragraph::new(lines).render(sections[4], buf);
}

fn render_columns(
    config: &DataQualityWidgetConfig<'_>,
    table_state: &mut TableState,
    area: Rect,
    buf: &mut Buffer,
) {
    let Some(results) = config.results else {
        render_run_prompt(area, config.theme, buf);
        return;
    };
    let report = results.report();
    let sections = Layout::default()
        .direction(Direction::Vertical)
        .constraints([Constraint::Length(2), Constraint::Fill(1)])
        .margin(1)
        .split(area);
    let title = rule_line(
        "Columns",
        Some(&numfmt::group_chrome(results.columns.len())),
        sections[0].width,
        config.theme,
    );
    Paragraph::new(title).render(sections[0], buf);
    let layout = if sections[1].width >= 120 {
        2
    } else if sections[1].width >= 72 {
        1
    } else {
        0
    };
    let dimmed = Style::default().fg(config.theme.dimmed());
    let rows = results.columns.iter().enumerate().map(|(index, profile)| {
        let severity = report
            .column_status
            .get(index)
            .copied()
            .unwrap_or(Severity::Clean);
        // With no values read, a column with no finding is unknown, not clean.
        let unknown = severity == Severity::Clean && (report.no_rows || report.metadata_only);
        let mark = if unknown {
            Cell::from("")
        } else {
            Cell::from(Line::from(severity_mark(severity, config.theme)))
        };
        let findings = report
            .column_findings
            .get(index)
            .filter(|titles| !titles.is_empty())
            .map(|titles| Cell::from(titles.join(", ")))
            .unwrap_or_else(|| Cell::from(Span::styled("none", dimmed)));
        let most_common = profile
            .dominant_value
            .as_ref()
            .zip(profile.dominant_count)
            .map(|(value, count)| {
                format!(
                    "{value} ({})",
                    crate::numfmt::percent_of(count, profile.non_null_rows())
                )
            })
            .unwrap_or_else(|| "-".to_string());
        let missing = if profile.null_count == 0 {
            Cell::from(Span::styled("0", dimmed))
        } else {
            Cell::from(format!(
                "{} ({})",
                numfmt::group_chrome(profile.null_count),
                crate::numfmt::percent_of(profile.null_count, profile.evaluated_rows)
            ))
        };
        let distinct = count_label(profile.distinct_count);
        Row::new(match layout {
            2 => vec![
                mark,
                Cell::from(profile.name.clone()),
                Cell::from(profile.dtype.to_string()),
                missing,
                Cell::from(distinct),
                Cell::from(most_common),
                findings,
            ],
            1 => vec![
                mark,
                Cell::from(profile.name.clone()),
                Cell::from(profile.dtype.to_string()),
                missing,
                findings,
            ],
            _ => vec![mark, Cell::from(profile.name.clone()), findings],
        })
    });
    normalize_selection(table_state, results.columns.len());
    let (headers, widths) = match layout {
        2 => (
            vec![
                "",
                "Column",
                "Type",
                "Missing",
                "Distinct",
                "Most common",
                "Findings",
            ],
            vec![
                Constraint::Length(1),
                Constraint::Length(22),
                Constraint::Length(10),
                Constraint::Length(16),
                Constraint::Length(10),
                Constraint::Length(22),
                Constraint::Fill(1),
            ],
        ),
        1 => (
            vec!["", "Column", "Type", "Missing", "Findings"],
            vec![
                Constraint::Length(1),
                Constraint::Length(20),
                Constraint::Length(8),
                Constraint::Length(16),
                Constraint::Fill(1),
            ],
        ),
        _ => (
            vec!["", "Column", "Findings"],
            vec![
                Constraint::Length(1),
                Constraint::Length(16),
                Constraint::Fill(1),
            ],
        ),
    };
    let table = Table::new(rows, widths)
        .header(Row::new(headers).style(dimmed))
        .row_highlight_style(config.theme.highlight_style())
        .highlight_symbol(glyphs::get().selector);
    StatefulWidget::render(table, sections[1], buf, table_state);
}

fn render_segments(
    config: &DataQualityWidgetConfig<'_>,
    table_state: &mut TableState,
    area: Rect,
    buf: &mut Buffer,
) {
    let Some(results) = config.results else {
        render_run_prompt(area, config.theme, buf);
        return;
    };
    let theme = config.theme;
    let dimmed = Style::default().fg(theme.dimmed());
    let sections = Layout::default()
        .direction(Direction::Vertical)
        .constraints([Constraint::Length(2), Constraint::Fill(1)])
        .margin(1)
        .split(area);
    if config.plan.grain == QualityGrain::Dataset {
        Paragraph::new(rule_line("Segments", Some("1"), sections[0].width, theme))
            .render(sections[0], buf);
        Paragraph::new(
            "The rows are one segment. Set grain in Setup (e) to split them by file, \
             partition, row chunk or time window, and compare the parts.",
        )
        .wrap(Wrap { trim: true })
        .style(Style::default().fg(theme.text_primary()))
        .render(sections[1], buf);
        return;
    }
    Paragraph::new(rule_line(
        if config.segments_by_change {
            "Segments, largest change first"
        } else {
            "Segments"
        },
        Some(&numfmt::group_chrome(results.segments.len())),
        sections[0].width,
        theme,
    ))
    .render(sections[0], buf);
    // A thin sample per segment can only name large moves; say how thin.
    if results.precision == QualityPrecision::Sampled && !results.segments.is_empty() {
        let typical = {
            let mut rows = results
                .segments
                .iter()
                .map(|segment| segment.evaluated_rows)
                .collect::<Vec<_>>();
            rows.sort_unstable();
            rows[rows.len() / 2]
        };
        Paragraph::new(Line::styled(
            format!(
                "About {} sampled rows a segment: a change is named only when it is past \
                 sampling noise. Trends pools segments to show smaller ones",
                numfmt::group_chrome(typical)
            ),
            dimmed,
        ))
        .render(
            Rect {
                y: sections[0].y + 1,
                height: 1,
                ..sections[0]
            },
            buf,
        );
    }

    // Per segment without choosing anything: rows, emptiness, and its largest move against
    // its comparison. Enter shows the rest.
    let rows_label =
        |segment: &crate::analysis::data_quality::SegmentQualityProfile| match segment.total_rows {
            Some(total) if total != segment.evaluated_rows => format!(
                "{} of {}",
                numfmt::group_chrome(segment.evaluated_rows),
                numfmt::group_chrome(total)
            ),
            _ => numfmt::group_chrome(segment.evaluated_rows),
        };
    let label_width = results
        .segments
        .iter()
        .map(|segment| glyphs::display_width(&segment_text(&segment.label)))
        .max()
        .unwrap_or(0)
        .clamp(7, 40) as u16
        + 2;
    let rows_width = results
        .segments
        .iter()
        .map(|segment| glyphs::display_width(&rows_label(segment)))
        .max()
        .unwrap_or(0)
        .max(4) as u16
        + 2;
    let compared = config.plan.comparison != QualityComparison::None;
    let order = crate::analysis::data_quality::segment_order(results, config.segments_by_change);
    let rows = order
        .iter()
        .map(|&index| &results.segments[index])
        .map(|segment| {
            let mut cells = vec![
                Cell::from(segment_text(&segment.label)),
                Cell::from(rows_label(segment)),
                Cell::from(crate::numfmt::percent(segment.null_rate)),
            ];
            if compared {
                cells.push(Cell::from(
                    segment.largest_change.clone().unwrap_or_default(),
                ));
            }
            Row::new(cells)
        });
    let mut headers = vec!["Segment", "Rows", "Null cells"];
    let mut widths = vec![
        Constraint::Length(label_width),
        Constraint::Length(rows_width),
        Constraint::Length(12),
    ];
    if compared {
        headers.push("Largest change");
        widths.push(Constraint::Fill(1));
    }
    normalize_selection(table_state, results.segments.len());
    let table = Table::new(rows, widths)
        .header(Row::new(headers).style(dimmed))
        .row_highlight_style(theme.highlight_style())
        .highlight_symbol(glyphs::get().selector);
    StatefulWidget::render(table, sections[1], buf, table_state);
}

/// One segment's columns with every measure beside its comparison, largest move first:
/// "what changed here" without choosing a column or measure.
fn render_segment_detail(
    config: &DataQualityWidgetConfig<'_>,
    table_state: &mut TableState,
    segment_index: usize,
    area: Rect,
    buf: &mut Buffer,
) {
    let Some(results) = config.results else {
        render_run_prompt(area, config.theme, buf);
        return;
    };
    let Some(segment) = results.segments.get(segment_index) else {
        return;
    };
    let theme = config.theme;
    let dimmed = Style::default().fg(theme.dimmed());
    let changes = crate::analysis::data_quality::segment_changes(results, segment_index);
    let sections = Layout::default()
        .direction(Direction::Vertical)
        .constraints([Constraint::Length(2), Constraint::Fill(1)])
        .margin(1)
        .split(area);
    let other = segment.compared_with.as_deref();
    let title = match other {
        Some(other) => format!(
            "{} vs {}",
            segment_text(&segment.label),
            segment_text(other)
        ),
        None => segment_text(&segment.label),
    };
    Paragraph::new(rule_line(
        &title,
        Some(&numfmt::group_chrome(changes.len())),
        sections[0].width,
        theme,
    ))
    .render(sections[0], buf);
    if changes.is_empty() {
        Paragraph::new(Span::styled("Nothing measured is above zero here", dimmed))
            .render(sections[1], buf);
        return;
    }
    let name_width = changes
        .iter()
        .map(|change| glyphs::display_width(&change.column))
        .max()
        .unwrap_or(0)
        .clamp(6, 32) as u16
        + 2;
    let value_width = |label: &str| (glyphs::display_width(label).clamp(8, 24) + 2) as u16;
    let rows = changes.iter().map(|change| {
        let style = if change.clear || other.is_none() {
            Style::default()
        } else {
            dimmed
        };
        let mut cells = vec![
            Cell::from(Span::styled(change.column.clone(), style)),
            Cell::from(Span::styled(change.metric.short_label(), dimmed)),
        ];
        if other.is_some() {
            cells.push(Cell::from(
                change
                    .before
                    .map(crate::numfmt::percent)
                    .unwrap_or_else(|| "-".to_string()),
            ));
        }
        cells.push(Cell::from(crate::numfmt::percent(change.now)));
        if other.is_some() {
            cells.push(match change.change() {
                Some(points) => Cell::from(Span::styled(format!("{points:+.1} pp"), style)),
                None => Cell::from(""),
            });
        }
        Row::new(cells)
    });
    let mut headers = vec!["Column".to_string(), "Measure".to_string()];
    let mut widths = vec![Constraint::Length(name_width), Constraint::Length(10)];
    if let Some(other) = other {
        headers.push(segment_text(other));
        widths.push(Constraint::Length(value_width(&segment_text(other))));
    }
    headers.push(segment_text(&segment.label));
    widths.push(Constraint::Length(value_width(&segment_text(
        &segment.label,
    ))));
    if other.is_some() {
        headers.push("Change".to_string());
        widths.push(Constraint::Fill(1));
    }
    normalize_selection(table_state, changes.len());
    let table = Table::new(rows, widths)
        .header(Row::new(headers).style(dimmed))
        .row_highlight_style(theme.highlight_style())
        .highlight_symbol(glyphs::get().selector);
    StatefulWidget::render(table, sections[1], buf, table_state);
}

fn render_trends(
    config: &DataQualityWidgetConfig<'_>,
    table_state: &mut TableState,
    area: Rect,
    buf: &mut Buffer,
) {
    let Some(results) = config.results else {
        render_run_prompt(area, config.theme, buf);
        return;
    };
    let [area] = Layout::default()
        .direction(Direction::Vertical)
        .constraints([Constraint::Fill(1)])
        .margin(1)
        .areas(area);
    if crate::analysis::data_quality::shows_trend(config.plan, results) {
        render_trend_table(config, results, table_state, area, buf);
        return;
    }
    let [title, body] = Layout::default()
        .direction(Direction::Vertical)
        .constraints([Constraint::Length(2), Constraint::Fill(1)])
        .areas(area);
    Paragraph::new(rule_line(
        "Across segments",
        None,
        title.width,
        config.theme,
    ))
    .render(title, buf);
    let mut text = vec![Line::raw(
        "Set grain in Setup (e) to a partition column, to days, weeks or months of a \
         date, or to chunks of rows, to follow each column from one to the next.",
    )];
    // One window found is no trend, but the windows expected around it still are.
    if let Some(gaps) = crate::analysis::quality_trends::expected_gaps(config.plan, results) {
        text.extend([Line::raw(""), Line::raw(gaps_summary(config.plan, &gaps))]);
    }
    Paragraph::new(text)
        .wrap(Wrap { trim: true })
        .style(Style::default().fg(config.theme.text_primary()))
        .render(body, buf);
}

/// `count` of `of`, with its rate when there is one: `2 of 5 (40.0%)`.
fn share(count: usize, of: usize) -> String {
    if of == 0 || count == 0 {
        format!(
            "{} of {}",
            numfmt::group_chrome(count),
            numfmt::group_chrome(of)
        )
    } else {
        format!(
            "{} of {} ({})",
            numfmt::group_chrome(count),
            numfmt::group_chrome(of),
            crate::numfmt::percent(count as f64 / of as f64)
        )
    }
}

/// One row per interval per segment: which, where, and the key counts; Enter opens
/// every count, so nothing is lost to a narrow terminal.
fn render_intervals(
    config: &DataQualityWidgetConfig<'_>,
    table_state: &mut TableState,
    area: Rect,
    buf: &mut Buffer,
) {
    let Some(results) = config.results else {
        render_run_prompt(area, config.theme, buf);
        return;
    };
    let theme = config.theme;
    let plan = config.plan;
    let dimmed = Style::default().fg(theme.dimmed());
    let [title, note, body] = Layout::default()
        .direction(Direction::Vertical)
        .constraints([
            Constraint::Length(1),
            Constraint::Length(1),
            Constraint::Fill(1),
        ])
        .margin(1)
        .areas(area);
    let profiles = &results.temporal;
    let chip = (!profiles.is_empty()).then(|| numfmt::group_chrome(profiles.len()));
    Paragraph::new(rule_line(
        "Time between dates",
        chip.as_deref(),
        title.width,
        theme,
    ))
    .render(title, buf);
    if profiles.is_empty() {
        let schema = config.state.quality_schema(&plan.scope);
        let message = if config.setup.time_candidates.is_empty() {
            "No date, time or text columns"
        } else if plan.compute == QualityCompute::Metadata && !plan.interval_pairs().is_empty() {
            "File metadata only · no values read · e Setup"
        } else if plan.interval_pairs().iter().any(|(start, end)| {
            [start, end].into_iter().any(|role| {
                plan.role_column(*role)
                    .is_some_and(|column| !plan.reads_as_time(column, schema))
            })
        }) {
            "Interval column is text with no format · Text as time in Setup (e)"
        } else if !plan.interval_pairs().is_empty() {
            "No rows to measure"
        } else if !plan.candidate_pairs().is_empty() {
            "Time roles make no interval · Intervals in Setup (e)"
        } else {
            "No time roles · Time roles in Setup (e)"
        };
        Paragraph::new(glyphs::dotted(message))
            .wrap(Wrap { trim: true })
            .style(Style::default().fg(theme.text_primary()))
            .render(
                Rect {
                    height: body.height + 1,
                    ..note
                },
                buf,
            );
        return;
    }
    // What the last column counts, and out of what, said once above it.
    let threshold = plan.latency_threshold_seconds;
    let over = match threshold {
        Some(_) => format!(
            "Over: duration > {}, of rows with both ends",
            crate::analysis::analysis_modal::threshold_label(threshold)
        ),
        None => "Negative: end before start, of rows with both ends".to_string(),
    };
    Paragraph::new(Line::styled(
        glyphs::fit(&over, note.width as usize),
        dimmed,
    ))
    .render(note, buf);

    let segmented = profiles
        .iter()
        .any(|profile| profile.segment != profiles[0].segment);
    let segment_width = profiles
        .iter()
        .map(|profile| glyphs::display_width(&segment_text(&profile.segment)))
        .max()
        .unwrap_or(0)
        .clamp(8, 22) as u16;
    let last = |profile: &TemporalLatencyProfile, counted: bool| {
        let count = match profile.above_threshold_count {
            Some(count) if threshold.is_some() => count,
            _ => profile.negative_count,
        };
        let rate = crate::numfmt::percent_of(count, profile.paired_rows);
        if counted && profile.paired_rows > 0 {
            format!("{} ({rate})", numfmt::group_chrome(count))
        } else {
            rate
        }
    };
    let last_header = if threshold.is_some() {
        "Over"
    } else {
        "Negative"
    };
    let wide = body.width >= 92;
    let medium = !wide && body.width >= 44;
    let mut headers = vec!["Interval"];
    let mut fixed = vec![];
    if segmented && (wide || medium) {
        headers.push("Segment");
        fixed.push(segment_width);
    }
    if wide {
        headers.extend(["Both ends", "Missing s/e", "p50", "p95"]);
        fixed.extend([9, 12, 7, 7]);
    } else {
        headers.push("p50");
        fixed.push(7);
    }
    headers.push(last_header);
    fixed.push(if wide { 15 } else { 9 });
    // What the interval's name has left: a long one ends in an ellipsis rather
    // than losing its last letters unmarked, and the detail says it in full.
    let interval_width = (body.width as usize).saturating_sub(
        glyphs::display_width(glyphs::get().selector)
            + fixed.iter().map(|width| *width as usize + 1).sum::<usize>(),
    );
    let rows = profiles.iter().map(|profile| {
        let mut cells = vec![glyphs::fit(&profile.label(), interval_width)];
        if segmented && (wide || medium) {
            cells.push(glyphs::fit(
                &segment_text(&profile.segment),
                segment_width as usize,
            ));
        }
        if wide {
            cells.extend([
                numfmt::group_chrome(profile.paired_rows),
                format!(
                    "{} / {}",
                    numfmt::group_chrome(profile.missing_start),
                    numfmt::group_chrome(profile.missing_end)
                ),
                crate::numfmt::duration_or_dash(profile.p50_seconds),
                crate::numfmt::duration_or_dash(profile.p95_seconds),
            ]);
        } else {
            cells.push(crate::numfmt::duration_or_dash(profile.p50_seconds));
        }
        cells.push(last(profile, wide));
        Row::new(cells)
    });
    let widths = std::iter::once(Constraint::Fill(1))
        .chain(fixed.into_iter().map(Constraint::Length))
        .collect::<Vec<_>>();
    normalize_selection(table_state, profiles.len());
    let table = Table::new(rows, widths)
        .header(Row::new(headers).style(dimmed))
        .row_highlight_style(theme.highlight_style())
        .highlight_symbol(glyphs::get().selector);
    StatefulWidget::render(table, body, buf, table_state);
}

/// One interval in one segment: endpoints and every count with its denominator.
/// Missing ends, unparsed text and negative durations stay separate. Counts with rows
/// take the cursor; Enter opens them. Reads nothing.
fn render_interval_detail(
    config: &DataQualityWidgetConfig<'_>,
    table_state: &mut TableState,
    area: Rect,
    buf: &mut Buffer,
) {
    let Some(results) = config.results else {
        render_run_prompt(area, config.theme, buf);
        return;
    };
    let Some(profile) = results.temporal.get(config.interval_index) else {
        return;
    };
    let theme = config.theme;
    let plan = config.plan;
    let [title, body] = Layout::default()
        .direction(Direction::Vertical)
        .constraints([Constraint::Length(2), Constraint::Fill(1)])
        .margin(1)
        .areas(area);
    Paragraph::new(rule_line(
        &profile.label(),
        Some(&segment_text(&profile.segment)),
        title.width,
        theme,
    ))
    .render(title, buf);

    let endpoint = |role: TemporalRole, column: &str| match plan.time_format(column) {
        Some(format) if format.zoned() => format!("{}: {column}, text with offset", role.label()),
        Some(format) => format!(
            "{}: {column}, text as {}",
            role.label(),
            format.kind.label()
        ),
        None => format!("{}: {column}", role.label()),
    };
    let grain = plan.interval_grain(&profile.start_column, &profile.end_column);
    let facts = IntervalFact::ALL
        .into_iter()
        .filter_map(|fact| profile.count(fact, plan).map(|count| (fact, count)))
        .collect::<Vec<_>>();
    let selected = facts
        .get(
            table_state
                .selected()
                .unwrap_or(0)
                .min(facts.len().saturating_sub(1)),
        )
        .map(|(fact, _)| *fact);
    let plain = |label: &str, value: String| {
        (
            FieldRow {
                mark: None,
                label: label.to_string(),
                value,
            },
            None,
        )
    };
    let fact_row = |fact: IntervalFact| {
        let (count, of) = profile.count(fact, plan)?;
        let value = match fact {
            IntervalFact::Negative | IntervalFact::Zero | IntervalFact::OverThreshold => {
                format!("{} with both ends", share(count, of))
            }
            _ => share(count, of),
        };
        let mark = (selected == Some(fact))
            .then(|| Span::styled(glyphs::get().rail, Style::default().fg(theme.accent())));
        Some((
            FieldRow {
                mark,
                label: fact.label(profile),
                value,
            },
            Some(fact),
        ))
    };
    let mut rows = vec![
        plain("Start", endpoint(profile.start_role, &profile.start_column)),
        plain("End", endpoint(profile.end_role, &profile.end_column)),
    ];
    if grain != QualityGrain::Dataset {
        rows.push(plain(
            "Segment",
            format!("{}, {}", segment_text(&profile.segment), grain.label()),
        ));
    }
    rows.push(plain("Rows", numfmt::group_chrome(profile.evaluated_rows)));
    rows.push(plain(
        "Both ends",
        share(profile.paired_rows, profile.evaluated_rows),
    ));
    rows.extend(
        [
            IntervalFact::MissingStart,
            IntervalFact::MissingEnd,
            IntervalFact::UnparsedStart,
            IntervalFact::UnparsedEnd,
            IntervalFact::Negative,
            IntervalFact::Zero,
        ]
        .into_iter()
        .filter_map(fact_row),
    );
    let pair = |left: Option<i64>, right: Option<i64>| {
        format!(
            "{}, {}",
            crate::numfmt::duration_or_dash(left),
            crate::numfmt::duration_or_dash(right)
        )
    };
    rows.push(plain(
        "p50, p90",
        pair(profile.p50_seconds, profile.p90_seconds),
    ));
    rows.push(plain(
        "p95, p99",
        pair(profile.p95_seconds, profile.p99_seconds),
    ));
    rows.push(plain(
        "Maximum",
        crate::numfmt::duration_or_dash(profile.max_seconds),
    ));
    rows.push(plain(
        "Threshold",
        match profile.threshold_seconds {
            Some(_) => format!(
                "duration > {}, strictly",
                crate::analysis::analysis_modal::threshold_label(profile.threshold_seconds)
            ),
            None => "none: set Latency over in Setup".to_string(),
        },
    ));
    rows.extend(fact_row(IntervalFact::OverThreshold));

    let label_width = rows
        .iter()
        .map(|(row, _)| glyphs::display_width(&row.label))
        .max()
        .unwrap_or(0)
        + 2;
    let width = (body.width as usize).min(DETAIL_MEASURE);
    // Each row's lines, and where the selected count's start, so a short terminal
    // scrolls to keep it in view.
    let mut lines = Vec::new();
    let mut focus = 0;
    // Enter opens nothing in a row chunk or a file, so say why under the segment
    // rather than leave it to be found out.
    let closed = (!profile.segment_opens(plan)).then(|| {
        let what = match grain {
            QualityGrain::File => "a file",
            _ => "a row chunk",
        };
        Line::styled(
            glyphs::fit(
                &format!("  Rows do not open: {what} is not a filter value"),
                width,
            ),
            Style::default().fg(theme.dimmed()),
        )
    });
    for (row, fact) in &rows {
        if fact.is_some() && *fact == selected {
            focus = lines.len();
        }
        let mut row_lines = field_lines(std::slice::from_ref(row), label_width, width, true);
        if fact.is_some() && *fact == selected {
            for line in &mut row_lines {
                if let Some(label) = line.spans.get_mut(2) {
                    label.style = Style::default().fg(theme.accent());
                }
            }
        }
        lines.extend(row_lines);
        if row.label == "Segment" {
            lines.extend(closed.clone());
        }
    }
    let height = body.height as usize;
    let offset = if lines.len() > height {
        // Room for the count of what is below, and for the focused row; never
        // past the last line, which would leave the bottom blank.
        (focus + 2)
            .saturating_sub(height.saturating_sub(1))
            .min(lines.len() - height)
    } else {
        0
    };
    render_counted(lines.split_off(offset), body, theme, buf);
}

/// Which starts and ends are measured, chosen from every pair the assigned roles
/// make, the suggested ones first. Space turns the pair under the cursor on or off.
fn render_interval_pairs(
    config: &DataQualityWidgetConfig<'_>,
    table_state: &mut TableState,
    area: Rect,
    buf: &mut Buffer,
) {
    let theme = config.theme;
    let plan = config.plan;
    let dimmed = Style::default().fg(theme.dimmed());
    let [title, body] = Layout::default()
        .direction(Direction::Vertical)
        .constraints([Constraint::Length(2), Constraint::Fill(1)])
        .margin(1)
        .areas(area);
    let candidates = plan.candidate_pairs();
    let chosen = plan.interval_pairs();
    Paragraph::new(rule_line(
        "Intervals",
        Some(&format!(
            "{} of {}",
            numfmt::group_chrome(chosen.len()),
            numfmt::group_chrome(candidates.len())
        )),
        title.width,
        theme,
    ))
    .render(title, buf);
    if candidates.is_empty() {
        Paragraph::new(Span::styled(
            glyphs::dotted("Needs 2 time roles · Time roles in Setup (e)"),
            dimmed,
        ))
        .wrap(Wrap { trim: true })
        .render(body, buf);
        return;
    }
    let g = glyphs::get();
    let label_width = candidates
        .iter()
        .map(|pair| glyphs::display_width(&interval_label(*pair)))
        .max()
        .unwrap_or(0) as u16
        + 2;
    // The selector, the box, the label and the gaps between them come first.
    let columns_width = (body.width as usize)
        .saturating_sub(glyphs::display_width(g.selector) + 1 + label_width as usize + 2);
    let rows = candidates.iter().map(|pair| {
        let on = chosen.contains(pair);
        let columns = format!(
            "{} to {}",
            plan.role_column(pair.0).unwrap_or_default(),
            plan.role_column(pair.1).unwrap_or_default()
        );
        Row::new(vec![
            Cell::from(if on { g.checkbox_on } else { g.checkbox_off }),
            Cell::from(interval_label(*pair)),
            Cell::from(Span::styled(glyphs::fit(&columns, columns_width), dimmed)),
        ])
    });
    table_state.select(Some(
        config.plan_field.min(candidates.len().saturating_sub(1)),
    ));
    let table = Table::new(
        rows,
        [
            Constraint::Length(glyphs::display_width(g.checkbox_on).max(1) as u16),
            Constraint::Length(label_width),
            Constraint::Fill(1),
        ],
    )
    .row_highlight_style(theme.highlight_style())
    .highlight_symbol(g.selector);
    StatefulWidget::render(table, body, buf, table_state);
}

/// The width of the Trends table's range column.
const TREND_RANGE: u16 = 20;

/// The Trends name column width and how many bars fit in `width`; shared with a bar's
/// detail so both lay out the same.
fn trend_layout(results: &DataQualityResults, width: u16) -> (u16, usize) {
    let name_width = results
        .columns
        .iter()
        .map(|profile| glyphs::display_width(&profile.name))
        .max()
        .unwrap_or(0)
        .clamp(12, 28) as u16
        + 2;
    let bars = width.saturating_sub(trend_lead(name_width)).max(8) as usize;
    (name_width, bars)
}

/// Where a Trends line's first bar sits: past the selector, the name, the range
/// and the space after each.
fn trend_lead(name_width: u16) -> u16 {
    glyphs::display_width(glyphs::get().selector) as u16 + name_width + TREND_RANGE + 2
}

/// What a segment of `grain` is called, many of them.
fn trend_unit(grain: &QualityGrain) -> &'static str {
    match grain {
        QualityGrain::TimeWindows { every, .. } => match every.as_str() {
            "1h" => "hours",
            "1d" => "days",
            "1w" => "weeks",
            _ => "months",
        },
        QualityGrain::Partition(_) => "partitions",
        QualityGrain::File => "files",
        _ => "chunks",
    }
}

/// One of `unit`, or many.
fn counted_unit(count: usize, unit: &str) -> String {
    let unit = if count == 1 {
        unit.strip_suffix('s').unwrap_or(unit)
    } else {
        unit
    };
    format!("{} {unit}", numfmt::group_chrome(count))
}

/// A bar's mark in `line`: its level, the unsampled mark where the sample drew no row
/// (never a short or blank bar), or blank where the measure does not apply. The exact
/// rows line has a level for every bar.
fn bar_mark(line: &TrendRow, bar: &TrendBar, index: usize, g: &glyphs::Glyphs) -> &'static str {
    if bar.evaluated == 0 && line.measure != TrendMeasure::Rows {
        return g.unsampled;
    }
    match line.bars.get(index).copied().flatten() {
        Some(value) if line.high > 0.0 => {
            g.mini_bars[((value / line.high) * 7.0).round().clamp(0.0, 7.0) as usize]
        }
        Some(_) => g.mini_bars[0],
        None => " ",
    }
}

/// A line's lowest and highest bar, as the Range column says them.
fn trend_range(line: &TrendRow) -> String {
    if line.rows() {
        format!(
            "{} to {}",
            numfmt::group_chrome(line.low.round() as usize),
            numfmt::group_chrome(line.high.round() as usize)
        )
    } else if (line.high - line.low).abs() < 1e-9 {
        crate::numfmt::percent(line.high)
    } else {
        format!(
            "{} to {}",
            crate::numfmt::percent(line.low),
            crate::numfmt::percent(line.high)
        )
    }
}

/// The coarser grain `w` stages, in words: "weekly".
fn coarser_label(grain: &QualityGrain) -> String {
    match grain {
        QualityGrain::TimeWindows { every, .. } => window_cadence(every).to_string(),
        other => other.label(),
    }
}

/// What the Trends page says under its title: the span, how much of it the sample
/// reached, and the expected windows with no rows. Each fact a line.
fn trend_notes(
    config: &DataQualityWidgetConfig<'_>,
    results: &DataQualityResults,
    view: &TrendView<'_>,
) -> Vec<String> {
    let plan = config.plan;
    let unit = trend_unit(&plan.grain);
    let middot = glyphs::get().middot;
    let first = view.slots.first().map(|slot| slot.label).unwrap_or("");
    let last = view.slots.last().map(|slot| slot.label).unwrap_or("");
    let mut facts = vec![format!(
        "{} to {}, {}",
        segment_text(first),
        segment_text(last),
        counted_unit(view.slots.len(), unit)
    )];
    if view.per_bar > 1 {
        facts.push(format!("each bar {}", counted_unit(view.per_bar, unit)));
    }
    if view.sampled {
        facts.push("a bar's rate pools its sampled rows".to_string());
    }
    let mut notes = vec![facts.join(&format!(" {middot} "))];
    let (unsampled, thin) = view.coverage();
    if view.sampled && unsampled + thin > 0 {
        let mut reach = Vec::new();
        if unsampled > 0 {
            reach.push(format!(
                "{} of {} not sampled ({})",
                numfmt::group_chrome(unsampled),
                counted_unit(view.slots.len(), unit),
                glyphs::get().unsampled
            ));
        }
        if thin > 0 {
            reach.push(format!(
                "{} under {} sampled rows",
                numfmt::group_chrome(thin),
                crate::analysis::quality_report::THIN_SEGMENT_ROWS
            ));
        }
        let mut note = reach.join(", ");
        if let Some(coarser) = plan.coarser_grain() {
            note.push_str(&format!("; w stages {}", coarser_label(&coarser)));
        }
        notes.push(note);
    }
    if let Some(gaps) = crate::analysis::quality_trends::expected_gaps(plan, results) {
        notes.push(gaps_summary(plan, &gaps));
    }
    notes
}

/// The expected windows in one line, for the Trends page.
fn gaps_summary(plan: &DataQualityPlan, gaps: &Gaps) -> String {
    let every = match &plan.grain {
        QualityGrain::TimeWindows { every, .. } => every.as_str(),
        _ => "",
    };
    let cadence = plan
        .expected
        .as_ref()
        .map(|expected| expected.cadence_label(every))
        .unwrap_or_default();
    let unit = trend_unit(&plan.grain);
    match gaps {
        Gaps::NoValues => format!("Expected {cadence}: file metadata only, no windows counted"),
        Gaps::NoWindows => {
            format!("Expected {cadence}: no window found, no range set")
        }
        Gaps::TooMany { windows } => format!(
            "Expected {cadence}: {} in range, over {} {dot} narrow in Setup (e)",
            counted_unit(*windows, unit),
            numfmt::group_chrome(crate::analysis::quality_trends::MAX_EXPECTED_WINDOWS),
            dot = glyphs::get().middot,
        ),
        // From typed past the last window found, with Before blank.
        Gaps::Checked(check) if check.expected == 0 => {
            format!("Expected {cadence}: no window in range")
        }
        Gaps::Checked(check) if check.gaps() == 0 => format!(
            "Expected {cadence}: all {} have rows",
            counted_unit(check.expected, unit)
        ),
        Gaps::Checked(check) => format!(
            "Expected {cadence}, {}: {} {} g lists",
            counted_unit(check.expected, unit),
            gap_counts(check),
            glyphs::get().middot,
        ),
    }
}

/// "5 empty, 2 not sampled": each kind of gap there is, with its count.
fn gap_counts(check: &GapCheck) -> String {
    [
        (check.empty, GapKind::Empty),
        (check.unsampled, GapKind::Unsampled),
        (check.out_of_scope, GapKind::OutOfScope),
    ]
    .into_iter()
    .filter(|(count, _)| *count > 0)
    .map(|(count, kind)| format!("{} {}", numfmt::group_chrome(count), kind.label()))
    .collect::<Vec<_>>()
    .join(", ")
}

/// Each column's measure across segments as a line of bars, segment rows first. The
/// whole range fits: a bar pools consecutive segments. Missed segments get their own
/// mark; notes count them and empty expected windows.
fn render_trend_table(
    config: &DataQualityWidgetConfig<'_>,
    results: &DataQualityResults,
    table_state: &mut TableState,
    area: Rect,
    buf: &mut Buffer,
) {
    let theme = config.theme;
    let dimmed = Style::default().fg(theme.dimmed());
    let (name_width, bars) = trend_layout(results, area.width);
    let view = trend_view(results, config.metric, bars);
    let notes = trend_notes(config, results, &view)
        .iter()
        .flat_map(|note| crate::widgets::info::wrap_to(note, area.width as usize))
        .collect::<Vec<_>>();
    // A short terminal keeps the table: the notes give way first.
    let room = (area.height as usize).saturating_sub(5).max(1);
    let notes = notes.into_iter().take(room).collect::<Vec<_>>();
    let [title, note, table_area] = Layout::default()
        .direction(Direction::Vertical)
        .constraints([
            Constraint::Length(1),
            Constraint::Length(notes.len() as u16 + 1),
            Constraint::Fill(1),
        ])
        .areas(area);
    Paragraph::new(rule_line(
        &format!("{} over time", config.metric.label()),
        Some(&numfmt::group_chrome(
            view.lines
                .iter()
                .filter(|row| !row.rows())
                .map(|row| row.names.len())
                .sum::<usize>(),
        )),
        title.width,
        theme,
    ))
    .render(title, buf);
    Paragraph::new(
        notes
            .iter()
            .map(|text| Line::styled(glyphs::fit(text, note.width as usize), dimmed))
            .collect::<Vec<_>>(),
    )
    .render(note, buf);
    let g = glyphs::get();
    let accent = Style::default().fg(theme.accent());
    let lines = view.lines.iter().map(|row| {
        let spark = view
            .bars
            .iter()
            .enumerate()
            .map(|(index, bar)| {
                let mark = bar_mark(row, bar, index, g);
                Span::styled(mark, if mark == g.unsampled { dimmed } else { accent })
            })
            .collect::<Vec<_>>();
        let name =
            crate::analysis::quality_report::columns_label(&row.names, name_width as usize - 2);
        Row::new(vec![
            Cell::from(if row.rows() {
                Span::styled(name, dimmed)
            } else {
                Span::raw(name)
            }),
            Cell::from(Span::styled(trend_range(row), dimmed)),
            Cell::from(Line::from(spark)),
        ])
    });
    normalize_selection(table_state, view.lines.len());
    let table = Table::new(
        lines,
        [
            Constraint::Length(name_width),
            Constraint::Length(TREND_RANGE),
            Constraint::Fill(1),
        ],
    )
    .header(Row::new(["Column", "Range", "Trend"]).style(dimmed))
    .row_highlight_style(theme.highlight_style())
    .highlight_symbol(g.selector);
    StatefulWidget::render(table, table_area, buf, table_state);
}

/// One bar of a Trends line, drawn under the line with a pointer: its span, segments
/// pooled and read, value and denominator, sample confidence, and comparison. Reads
/// nothing; ↑↓ walk the bars.
fn render_trend_detail(
    config: &DataQualityWidgetConfig<'_>,
    table_state: &mut TableState,
    area: Rect,
    buf: &mut Buffer,
) {
    let Some(results) = config.results else {
        render_run_prompt(area, config.theme, buf);
        return;
    };
    let theme = config.theme;
    let g = glyphs::get();
    let dimmed = Style::default().fg(theme.dimmed());
    let accent = Style::default().fg(theme.accent());
    let [area] = Layout::default()
        .direction(Direction::Vertical)
        .constraints([Constraint::Fill(1)])
        .margin(1)
        .areas(area);
    let (name_width, bars) = trend_layout(results, area.width);
    let view = trend_view(results, config.metric, bars);
    let (Some(line), false) = (view.lines.get(config.trend_line), view.bars.is_empty()) else {
        Paragraph::new(glyphs::dotted("No bars · Esc Trends"))
            .style(Style::default().fg(theme.text_primary()))
            .render(area, buf);
        return;
    };
    // Held to the last bar: the keys only know an upper bound, the width decides.
    let index = table_state.selected().unwrap_or(0).min(view.bars.len() - 1);
    table_state.select(Some(index));
    let [title, spark, pointer, _, body] = Layout::default()
        .direction(Direction::Vertical)
        .constraints([
            Constraint::Length(1),
            Constraint::Length(1),
            Constraint::Length(1),
            Constraint::Length(1),
            Constraint::Fill(1),
        ])
        .areas(area);
    let measure = match line.measure {
        TrendMeasure::Rows => "Rows per segment".to_string(),
        TrendMeasure::SampledRows => "Sampled rows per segment".to_string(),
        TrendMeasure::Column(_) => config.metric.label().to_string(),
    };
    Paragraph::new(rule_line(
        &measure,
        Some(&format!(
            "bar {} of {}",
            numfmt::group_chrome(index + 1),
            numfmt::group_chrome(view.bars.len())
        )),
        title.width,
        theme,
    ))
    .render(title, buf);

    // The line as Trends drew it, its selected bar pointed at from below.
    let name = crate::analysis::quality_report::columns_label(&line.names, name_width as usize - 2);
    let lead = |text: String, width: u16| {
        format!(
            "{text}{}",
            " ".repeat((width as usize).saturating_sub(glyphs::display_width(&text)))
        )
    };
    let mut spans = vec![
        Span::raw(g.selector_blank),
        Span::raw(lead(name, name_width + 1)),
        Span::styled(lead(trend_range(line), TREND_RANGE + 1), dimmed),
    ];
    spans.extend(view.bars.iter().enumerate().map(|(at, candidate)| {
        let mark = bar_mark(line, candidate, at, g);
        let style = if mark == g.unsampled { dimmed } else { accent };
        Span::styled(
            mark,
            if at == index {
                style.add_modifier(Modifier::BOLD)
            } else {
                style
            },
        )
    }));
    Paragraph::new(Line::from(spans)).render(spark, buf);
    Paragraph::new(Line::from(vec![
        Span::raw(" ".repeat(trend_lead(name_width) as usize + index)),
        Span::styled(g.pointer, accent),
    ]))
    .render(pointer, buf);

    let rows = trend_bar_fields(config, results, &view, line, index);
    let label_width = rows
        .iter()
        .map(|row| glyphs::display_width(&row.label))
        .max()
        .unwrap_or(0)
        + 2;
    let width = (body.width as usize).min(DETAIL_MEASURE);
    render_counted(
        field_lines(&rows, label_width, width, false),
        body,
        theme,
        buf,
    );
}

/// A bar's facts as label and value rows.
fn trend_bar_fields(
    config: &DataQualityWidgetConfig<'_>,
    results: &DataQualityResults,
    view: &TrendView<'_>,
    line: &TrendRow,
    index: usize,
) -> Vec<FieldRow> {
    let plan = config.plan;
    let bar = &view.bars[index];
    let unit = trend_unit(&plan.grain);
    let row = |label: &str, value: String| FieldRow {
        mark: None,
        label: label.to_string(),
        value,
    };
    let mut rows = vec![row(
        "Span",
        segment_text(&crate::analysis::quality_trends::bar_span(
            view,
            bar,
            &plan.grain,
        )),
    )];
    // How much of the bar the run reached, before any number taken from it.
    let mut reach = vec![counted_unit(bar.segments(), unit)];
    if view.sampled {
        if bar.unsampled > 0 {
            reach.push(format!(
                "{} not sampled",
                numfmt::group_chrome(bar.unsampled)
            ));
        }
        if bar.thin > 0 {
            reach.push(format!(
                "{} under {} sampled rows",
                numfmt::group_chrome(bar.thin),
                crate::analysis::quality_report::THIN_SEGMENT_ROWS
            ));
        }
        if bar.unsampled + bar.thin == 0 {
            reach.push("each sampled".to_string());
        }
    }
    rows.push(row("Segments", reach.join(", ")));
    let every_row = !view.sampled || bar.eligible == Some(bar.evaluated);
    rows.push(row(
        "Rows",
        match (view.sampled, bar.eligible) {
            (false, Some(eligible)) => {
                format!("{}, every row read", numfmt::group_chrome(eligible))
            }
            (false, None) => format!("{}, every row read", numfmt::group_chrome(bar.evaluated)),
            (true, Some(eligible)) => format!(
                "{} sampled of {} ({})",
                numfmt::group_chrome(bar.evaluated),
                numfmt::group_chrome(eligible),
                crate::numfmt::percent_of(bar.evaluated, eligible)
            ),
            (true, None) => format!(
                "{} sampled, total not counted",
                numfmt::group_chrome(bar.evaluated)
            ),
        },
    ));
    let compared = crate::analysis::quality_trends::compared_bar(view, index, plan);
    let against = if plan.comparison == QualityComparison::Baseline {
        "Baseline bar"
    } else {
        "Previous bar"
    };
    if line.rows() {
        // Rows per segment: the mean, and the smallest and largest segment.
        let counts = view.slots[bar.slots.clone()]
            .iter()
            .map(|slot| match line.measure {
                TrendMeasure::Rows => slot.total.unwrap_or(0),
                _ => slot.evaluated,
            })
            .collect::<Vec<_>>();
        let (low, high) = (
            counts.iter().min().copied().unwrap_or(0),
            counts.iter().max().copied().unwrap_or(0),
        );
        let mean = line.bars[index].unwrap_or(0.0);
        rows.push(row(
            "Per segment",
            if low == high {
                numfmt::group_chrome(low)
            } else {
                format!(
                    "{} on average, {} to {}",
                    numfmt::group_chrome(mean.round() as usize),
                    numfmt::group_chrome(low),
                    numfmt::group_chrome(high)
                )
            },
        ));
        if let Some(other) = compared {
            let before = line.bars[other].unwrap_or(0.0);
            let moved = if before > 0.0 {
                format!(" ({:+.1}%)", (mean - before) / before * 100.0)
            } else {
                String::new()
            };
            rows.push(row(
                against,
                format!(
                    "{} to {} per segment{moved}",
                    numfmt::group_chrome(before.round() as usize),
                    numfmt::group_chrome(mean.round() as usize)
                ),
            ));
        }
        return rows;
    }
    let (count, of) = line.parts[index];
    let noun = match config.metric {
        QualityMetric::DistinctShare
        | QualityMetric::IntegerParseShare
        | QualityMetric::DecimalParseShare => "values",
        _ => "rows",
    };
    rows.push(row(
        config.metric.label(),
        if of > 0.0 {
            format!(
                "{} of {} {noun} ({})",
                numfmt::group_chrome(count.round() as usize),
                numfmt::group_chrome(of.round() as usize),
                crate::numfmt::percent(count / of)
            )
        } else if bar.evaluated == 0 {
            "none: no row sampled".to_string()
        } else {
            format!("none: no {noun} to measure")
        },
    ));
    // How sure a sample is of the rate: never a point, and never for a distinct
    // share, which does not stand for the whole.
    let interval = if of <= 0.0 {
        None
    } else if every_row {
        Some("none needed: every row read".to_string())
    } else if config.metric == QualityMetric::DistinctShare {
        Some("none: a distinct share does not stand for the whole".to_string())
    } else {
        crate::analysis::quality_trends::wilson_interval(count, of).map(|(low, high)| {
            let mut text = format!(
                "{} to {}",
                crate::numfmt::percent(low),
                crate::numfmt::percent(high)
            );
            if (of as usize) < crate::analysis::quality_report::THIN_SEGMENT_ROWS {
                text.push_str(&format!(
                    ", from under {} {noun}",
                    crate::analysis::quality_report::THIN_SEGMENT_ROWS
                ));
            }
            text
        })
    };
    if let Some(interval) = interval {
        rows.push(row("95% interval", interval));
    }
    if let Some(other) = compared {
        let other_bar = &view.bars[other];
        let exact = results.precision == QualityPrecision::Exact
            || (every_row && other_bar.eligible == Some(other_bar.evaluated));
        rows.push(row(
            against,
            match crate::analysis::quality_trends::bar_change(line, index, other, exact) {
                Some(change) => format!(
                    "{} to {}, {:+.1} points: {}",
                    crate::numfmt::percent(change.before),
                    crate::numfmt::percent(change.now),
                    change.points(),
                    // Segments never judge a distinct share: it falls as a segment
                    // grows, so bars of different sizes differ by it whatever the data.
                    if config.metric == QualityMetric::DistinctShare {
                        "not judged for a distinct share"
                    } else if change.clear {
                        "a clear change"
                    } else if change.points().abs()
                        < crate::analysis::data_quality::MATERIAL_CHANGE_PP
                    {
                        "under a point"
                    } else {
                        "within sampling noise"
                    }
                ),
                None => "nothing to compare: one side has no rate".to_string(),
            },
        ));
    }
    rows
}

/// The expected windows with no rows, a line per run of them: why, which windows,
/// and the rows a sample missed. Bounded: the runs past the cap are counted.
fn render_gaps(
    config: &DataQualityWidgetConfig<'_>,
    table_state: &mut TableState,
    area: Rect,
    buf: &mut Buffer,
) {
    let Some(results) = config.results else {
        render_run_prompt(area, config.theme, buf);
        return;
    };
    let theme = config.theme;
    let plan = config.plan;
    let dimmed = Style::default().fg(theme.dimmed());
    let [area] = Layout::default()
        .direction(Direction::Vertical)
        .constraints([Constraint::Fill(1)])
        .margin(1)
        .areas(area);
    let Some(gaps) = crate::analysis::quality_trends::expected_gaps(plan, results) else {
        Paragraph::new(
            "No expected windows: set Expected in Setup (e) to say which windows rows \
             belong in.",
        )
        .wrap(Wrap { trim: true })
        .style(Style::default().fg(theme.text_primary()))
        .render(area, buf);
        return;
    };
    let check = match &gaps {
        Gaps::Checked(check) if check.expected > 0 => check,
        _ => {
            Paragraph::new(gaps_summary(plan, &gaps))
                .wrap(Wrap { trim: true })
                .style(Style::default().fg(theme.text_primary()))
                .render(area, buf);
            return;
        }
    };
    let unit = trend_unit(&plan.grain);
    let every = check.every.as_str();
    let sampled = matches!(results.precision, QualityPrecision::Sampled);
    let mut notes = vec![format!(
        "{}, {}: {} of {} {}",
        check.column,
        crate::analysis::quality_trends::calendar_span(
            check.from,
            crate::analysis::quality_trends::floor_window(
                check.before - chrono::Duration::microseconds(1),
                every
            ),
            every
        ),
        numfmt::group_chrome(check.with_rows),
        counted_unit(check.expected, unit),
        if sampled { "sampled" } else { "with rows" }
    )];
    if check.weekend > 0 {
        notes.push(format!(
            "{} on weekends, not expected",
            counted_unit(check.weekend, unit)
        ));
    }
    // What each kind means, for the kinds listed: empty only on an exact count.
    let scope = plan.scope.label();
    for (count, text) in [
        (
            check.empty,
            format!("Empty: no rows in {scope}, by exact count"),
        ),
        (
            check.unsampled,
            match missed_rows(check) {
                Some(rows) if check.counted => format!(
                    "Not sampled: {} rows there, none drawn",
                    numfmt::group_chrome(rows)
                ),
                None if check.counted => "Not sampled: rows there, none drawn".to_string(),
                _ => "Not sampled: none drawn, rows not counted".to_string(),
            },
        ),
        (
            check.out_of_scope,
            "Out of scope: outside the time range the scope reads".to_string(),
        ),
    ] {
        if count > 0 {
            notes.push(text);
        }
    }
    let notes = notes
        .iter()
        .flat_map(|note| crate::widgets::info::wrap_to(note, area.width as usize))
        .take((area.height as usize).saturating_sub(5).max(1))
        .collect::<Vec<_>>();
    let [title, note, body] = Layout::default()
        .direction(Direction::Vertical)
        .constraints([
            Constraint::Length(1),
            Constraint::Length(notes.len() as u16 + 1),
            Constraint::Fill(1),
        ])
        .areas(area);
    Paragraph::new(rule_line(
        &format!(
            "Expected {}",
            plan.expected
                .as_ref()
                .map(|expected| expected.cadence_label(every))
                .unwrap_or_default()
        ),
        Some(&format!(
            "{} {}",
            numfmt::group_chrome(check.gaps()),
            if check.gaps() == 1 { "gap" } else { "gaps" }
        )),
        title.width,
        theme,
    ))
    .render(title, buf);
    Paragraph::new(
        notes
            .iter()
            .map(|text| Line::styled(glyphs::fit(text, note.width as usize), dimmed))
            .collect::<Vec<_>>(),
    )
    .render(note, buf);
    if check.runs.is_empty() {
        Paragraph::new("Every expected window has rows")
            .style(Style::default().fg(theme.text_primary()))
            .render(body, buf);
        return;
    }
    // The windows first, which is what a gap is; the rows a sample missed where
    // there is room for them.
    let span_width = if every == "1h" { 35 } else { 24 };
    let wide = body.width as usize >= span_width + 2 + 12 + 10 + 14 + 4;
    let rows = check
        .runs
        .iter()
        .map(|run| {
            let mut cells = vec![
                Cell::from(crate::analysis::quality_trends::calendar_span(
                    run.first, run.last, every,
                )),
                Cell::from(run.kind.label()),
                Cell::from(Span::styled(counted_unit(run.windows, unit), dimmed)),
            ];
            if wide {
                cells.push(Cell::from(Span::styled(
                    run.rows
                        .map(|rows| format!("{} rows", numfmt::group_chrome(rows)))
                        .unwrap_or_default(),
                    dimmed,
                )));
            }
            Row::new(cells)
        })
        .chain((check.more_runs > 0).then(|| {
            Row::new(vec![Cell::from(Span::styled(
                format!(
                    "{} {} more",
                    glyphs::get().ellipsis,
                    numfmt::group_chrome(check.more_runs)
                ),
                dimmed,
            ))])
        }))
        .collect::<Vec<_>>();
    normalize_selection(table_state, check.runs.len());
    let mut widths = vec![
        Constraint::Length(span_width as u16),
        Constraint::Length(12),
        Constraint::Length(10),
    ];
    let mut headers = vec!["Windows", "Gap", "Length"];
    if wide {
        widths.push(Constraint::Length(14));
        headers.push("Rows");
    }
    let table = Table::new(rows, widths)
        .header(Row::new(headers).style(dimmed))
        .row_highlight_style(theme.highlight_style())
        .highlight_symbol(glyphs::get().selector);
    StatefulWidget::render(table, body, buf, table_state);
}

/// The rows in the windows a sample missed, when every one was counted.
fn missed_rows(check: &GapCheck) -> Option<usize> {
    check
        .runs
        .iter()
        .filter(|run| run.kind == GapKind::Unsampled)
        .map(|run| run.rows)
        .sum::<Option<usize>>()
        .filter(|_| check.more_runs == 0)
}

/// Setup's Expected editor: which windows rows are expected in, and from and before
/// when. Nothing here reads; Enter writes it into the draft.
fn render_expected_windows(config: &DataQualityWidgetConfig<'_>, area: Rect, buf: &mut Buffer) {
    let ctx = config.ctx;
    let theme = config.theme;
    let Some(form) = config.expected_form else {
        return;
    };
    let [area] = Layout::default()
        .direction(Direction::Vertical)
        .constraints([Constraint::Fill(1)])
        .margin(1)
        .areas(area);
    let every = match &config.plan.grain {
        QualityGrain::TimeWindows { every, .. } => every.as_str(),
        _ => "1d",
    };
    let notes = [
        format!("Grain: {}", config.plan.grain.label()),
        "From, Before: a date or a UTC timestamp, such as 2024-01-01 or \
         2024-01-01T09:00:00Z. Blank: the first or last window found."
            .to_string(),
        "A window in range with no rows is listed under Trends (g); a weekend only \
         when every day is expected."
            .to_string(),
    ];
    let width = (area.width as usize).min(DETAIL_MEASURE);
    let mut y = area.y;
    put_line(
        rule_line("Expected Windows", None, area.width, theme),
        area,
        &mut y,
        buf,
    );
    put_line(Line::raw(""), area, &mut y, buf);
    let cadence = form.cadence_label(every);
    for (field, label) in crate::analysis::analysis_modal::EXPECTED_ROWS
        .iter()
        .enumerate()
    {
        let row_area = Rect {
            y,
            height: 1,
            ..area
        };
        if y < area.y + area.height {
            crate::widgets::ui::FormRow {
                label,
                value: match field {
                    0 => FormValue::Choice(&cadence),
                    1 => FormValue::Input(&form.from),
                    _ => FormValue::Input(&form.before),
                },
                focused: form.field == field,
                label_width: 10,
            }
            .render(row_area, buf, ctx);
            crate::app::pointer::record_field::<crate::analysis::analysis_modal::ExpectedForm>(
                row_area, field,
            );
        }
        y += 1;
    }
    put_line(Line::raw(""), area, &mut y, buf);
    let dimmed = Style::default().fg(theme.dimmed());
    for note in &notes {
        for text in crate::widgets::info::wrap_to(note, width.saturating_sub(2)) {
            put_line(Line::styled(format!("  {text}"), dimmed), area, &mut y, buf);
        }
    }
    if let Some(error) = &form.error {
        put_line(Line::raw(""), area, &mut y, buf);
        put_line(
            Line::styled(
                glyphs::fit(error, width),
                Style::default().fg(theme.warning()),
            ),
            area,
            &mut y,
            buf,
        );
    }
}

/// `line` on row `y` of `area` when it is inside it, and the next row after.
fn put_line(line: Line<'static>, area: Rect, y: &mut u16, buf: &mut Buffer) {
    if *y < area.y + area.height {
        Paragraph::new(line).render(
            Rect {
                y: *y,
                height: 1,
                ..area
            },
            buf,
        );
    }
    *y += 1;
}

/// One column's findings, then its measurements, as aligned label and value rows
/// on the page, the way Setup lays out its rows.
fn render_detail(
    config: &DataQualityWidgetConfig<'_>,
    table_state: &mut TableState,
    area: Rect,
    buf: &mut Buffer,
) {
    let Some(results) = config.results else {
        render_run_prompt(area, config.theme, buf);
        return;
    };
    let index = table_state.selected().unwrap_or(0);
    let Some(profile) = results.columns.get(index) else {
        Paragraph::new("No column selected")
            .alignment(Alignment::Center)
            .render(area, buf);
        return;
    };
    let theme = config.theme;
    let [title, body] = Layout::default()
        .direction(Direction::Vertical)
        .constraints([Constraint::Length(2), Constraint::Fill(1)])
        .margin(1)
        .areas(area);
    Paragraph::new(rule_line(&profile.name, None, title.width, theme)).render(title, buf);

    // The column's findings first, in the report's own words; the measurements
    // under them are the evidence.
    let report = results.report();
    let mut findings = report
        .findings
        .iter()
        .filter(|finding| finding.kind.is_some() && finding.columns.contains(&profile.name))
        .map(|finding| FieldRow {
            mark: Some(severity_mark(finding.severity, theme)),
            label: finding.title.to_string(),
            value: finding.summary.clone(),
        })
        .collect::<Vec<_>>();
    if findings.is_empty() {
        findings.push(FieldRow {
            mark: Some(severity_mark(Severity::Clean, theme)),
            label: "No findings".to_string(),
            value: String::new(),
        });
    }
    let measurements = detail_measurements(config.ctx, results, profile);
    let label_width = findings
        .iter()
        .chain(&measurements)
        .map(|row| glyphs::display_width(&row.label))
        .max()
        .unwrap_or(0)
        + 2;
    // A reading surface: values wrap at a comfortable measure on a wide terminal.
    let width = (body.width as usize).min(DETAIL_MEASURE);
    let mut lines = field_lines(&findings, label_width, width, true);
    lines.push(Line::raw(""));
    lines.extend(field_lines(&measurements, label_width, width, true));
    render_counted(lines, body, theme, buf);
}

/// The widest a reading surface's text runs, however wide the terminal.
const DETAIL_MEASURE: usize = 100;

/// The most of one value the Detail page shows: a range's end, the most common
/// value, a spelling.
const END_WIDTH: usize = 32;

/// A label and its value on one row, with an optional mark in the lead.
struct FieldRow {
    mark: Option<Span<'static>>,
    label: String,
    value: String,
}

/// Rows in two aligned columns: label padded to `label_width`, value wrapped under
/// itself (multi-line values keep their lines). With `marks`, a two-column lead holds
/// each row's mark.
fn field_lines(
    rows: &[FieldRow],
    label_width: usize,
    width: usize,
    marks: bool,
) -> Vec<Line<'static>> {
    let lead = if marks { 2 } else { 0 };
    let value_width = width.saturating_sub(lead + label_width).max(8);
    let mut lines = Vec::new();
    for row in rows {
        let mut values = row
            .value
            .lines()
            // A value that fits is kept as written: wrapping splits on whitespace
            // and would drop the leading, trailing or doubled spaces a value holds.
            .flat_map(|line| {
                if glyphs::display_width(line) <= value_width {
                    vec![line.to_string()]
                } else {
                    crate::widgets::info::wrap_to(line, value_width)
                }
            })
            .collect::<Vec<_>>()
            .into_iter();
        let mut spans = Vec::new();
        if marks {
            spans.push(row.mark.clone().unwrap_or_else(|| Span::raw(" ")));
            spans.push(Span::raw(" "));
        }
        spans.push(Span::raw(format!(
            "{}{}",
            row.label,
            " ".repeat(label_width.saturating_sub(glyphs::display_width(&row.label)))
        )));
        spans.extend(
            values
                .next()
                .map(|value| Span::raw(glyphs::fit(&value, value_width))),
        );
        lines.push(Line::from(spans));
        lines.extend(values.map(|value| {
            Line::raw(format!(
                "{}{}",
                " ".repeat(lead + label_width),
                glyphs::fit(&value, value_width)
            ))
        }));
    }
    lines
}

/// `lines` in `area`; when they run past it, the last row counts what is below
/// rather than showing half of it.
fn render_counted(mut lines: Vec<Line<'static>>, area: Rect, theme: &Theme, buf: &mut Buffer) {
    let height = area.height as usize;
    if lines.len() > height && height > 0 {
        let below = lines.len() - (height - 1);
        lines.truncate(height - 1);
        lines.push(Line::styled(
            format!("  {} {below} more", glyphs::get().ellipsis),
            Style::default().fg(theme.dimmed()),
        ));
    }
    Paragraph::new(lines).render(area, buf);
}

/// What was measured on a column, table-formatted. Measures not applying to its type
/// are omitted (not dashed); header facts are not repeated.
fn detail_measurements(
    ctx: &RenderContext,
    results: &DataQualityResults,
    profile: &ColumnQualityProfile,
) -> Vec<FieldRow> {
    // Long text is cut, so both ends of a range and a count after a value stay
    // in view.
    let value = |text: &str| glyphs::fit(&table_value(ctx, profile, text), END_WIDTH);
    let row = |label: &str, value: String| FieldRow {
        mark: None,
        label: label.to_string(),
        value,
    };
    let mut rows = vec![
        row("Type", profile.dtype.to_string()),
        row(
            "Missing",
            if profile.null_count == 0 {
                "0".to_string()
            } else {
                format!(
                    "{} ({})",
                    numfmt::group_chrome(profile.null_count),
                    crate::numfmt::percent_of(profile.null_count, profile.evaluated_rows)
                )
            },
        ),
    ];
    for (label, measured) in [
        ("Distinct", profile.distinct_count),
        ("Empty text", profile.empty_count),
        ("Blank text", profile.whitespace_count),
        ("NaN", profile.nan_count),
    ] {
        if let Some(measured) = measured {
            rows.push(row(label, numfmt::group_chrome(measured)));
        }
    }
    if profile.positive_infinity_count.is_some() || profile.negative_infinity_count.is_some() {
        rows.push(row(
            "Infinite",
            format!(
                "{} positive, {} negative",
                count_label(profile.positive_infinity_count),
                count_label(profile.negative_infinity_count)
            ),
        ));
    }
    if profile.min.is_some() || profile.max.is_some() {
        rows.push(row(
            "Range",
            format!(
                "{} to {}",
                profile.min.as_deref().map(value).unwrap_or_default(),
                profile.max.as_deref().map(value).unwrap_or_default()
            ),
        ));
    }
    if let Some((dominant, count)) = profile.dominant_value.as_ref().zip(profile.dominant_count) {
        rows.push(row(
            "Most common",
            format!(
                "{}, {} {}",
                value(dominant),
                numfmt::group_chrome(count),
                if count == 1 { "row" } else { "rows" }
            ),
        ));
    }
    if profile.min_length.is_some() || profile.max_length.is_some() {
        rows.push(row(
            if matches!(profile.dtype, polars::prelude::DataType::List(_)) {
                "List length"
            } else {
                "Text length"
            },
            format!(
                "{} to {}",
                count_label(profile.min_length),
                count_label(profile.max_length)
            ),
        ));
    }
    // Only the parsers that accepted something: four zeros say less than "none".
    let parses = [
        ("integer", profile.integer_parse_count),
        ("decimal", profile.decimal_parse_count),
        ("date", profile.date_parse_count),
        ("datetime", profile.datetime_parse_count),
    ];
    if parses.iter().any(|(_, count)| count.is_some()) {
        let found = parses
            .iter()
            .filter_map(|(parser, count)| {
                count
                    .filter(|count| *count > 0)
                    .map(|count| format!("{parser} {}", numfmt::group_chrome(count)))
            })
            .collect::<Vec<_>>();
        rows.push(row(
            "Parses as",
            if found.is_empty() {
                "none".to_string()
            } else {
                found.join(", ")
            },
        ));
    }
    for group in results
        .category_variants
        .iter()
        .filter(|group| group.column == profile.name)
        .take(3)
    {
        // Spellings differ by case and by spaces the table does not show, so they are
        // quoted as the finding quotes them: "West " and "West" read apart.
        rows.push(row(
            "Spellings",
            group
                .variants
                .iter()
                .map(|(variant, count)| {
                    format!(
                        "{} ({})",
                        crate::analysis::quality_report::quoted(variant, END_WIDTH),
                        numfmt::group_chrome(*count)
                    )
                })
                .collect::<Vec<_>>()
                .join("\n"),
        ));
    }
    rows
}

/// A value the profile holds as text, formatted as the table formats its column:
/// grouped, or to fixed places, when the number format says so; as-is otherwise.
fn table_value(ctx: &RenderContext, profile: &ColumnQualityProfile, text: &str) -> String {
    use polars::prelude::{AnyValue, DataType};
    let formatter = ctx
        .number_format
        .formatter_for(&profile.name, &profile.dtype);
    if formatter.is_passthrough() {
        return text.to_string();
    }
    let parsed = match profile.dtype {
        DataType::Float32 | DataType::Float64 => text.parse::<f64>().ok().map(AnyValue::Float64),
        DataType::UInt8 | DataType::UInt16 | DataType::UInt32 | DataType::UInt64 => {
            text.parse::<u64>().ok().map(AnyValue::UInt64)
        }
        _ => text.parse::<i64>().ok().map(AnyValue::Int64),
    };
    parsed
        .map(|parsed| {
            numfmt::format_any_value(&formatter, &parsed, &mut String::new()).into_owned()
        })
        .unwrap_or_else(|| text.to_string())
}

fn count_label(value: Option<usize>) -> String {
    value
        .map(numfmt::group_chrome)
        .unwrap_or_else(|| "-".to_string())
}

fn render_sidebar(
    config: &DataQualityWidgetConfig<'_>,
    sidebar_state: &mut TableState,
    area: Rect,
    buf: &mut Buffer,
) {
    crate::widgets::analysis::render_sidebar(
        area,
        buf,
        sidebar_state,
        Some(AnalysisTool::DataQuality),
        config.focus,
        config.theme,
    );
}

/// The tool list, on a terminal too narrow to keep it beside the result.
fn render_narrow_tool_picker(
    config: &DataQualityWidgetConfig<'_>,
    sidebar_state: &mut TableState,
    area: Rect,
    buf: &mut Buffer,
) {
    let tools = vec![
        "Describe",
        "Distribution Analysis",
        "Correlation Matrix",
        "Data Quality",
    ];
    let popup = dialog_in(area, 28, tools.len() as u16 + 2);
    let content = Surface::new("Analysis Tools").render(popup, buf, config.ctx);
    Picker::new(tools, sidebar_state.selected(), true)
        .on_click(crate::widgets::ui::Clicks::Tool)
        .render(content, buf, config.ctx);
}

/// What a run of the plan will read and write, before it runs.
fn render_access_plan(config: &DataQualityWidgetConfig<'_>, area: Rect, buf: &mut Buffer) {
    let state = config.state;
    let plan = config.plan;
    let remote = state.is_remote_source();
    let copy = config.setup.copy;
    let row = |label: &str, value: String| FieldRow {
        mark: None,
        label: label.to_string(),
        value,
    };
    let source_files = if plan.scope.uses_source() {
        Some(state.quality_source_file_count())
            .filter(|count| *count > 0)
            .or_else(|| state.source_file_count())
    } else {
        state.source_file_count()
    };
    let rows = [
        row(
            "Source",
            if remote { "remote" } else { "local" }.to_string(),
        ),
        row("Scope", plan.scope.label()),
        row("Grain", plan.grain.label()),
        row("Sample", compute_label(plan)),
        row(
            "Rows evaluated",
            planned_rows(state, plan)
                .map(numfmt::group_chrome)
                .unwrap_or_else(|| "unknown".to_string()),
        ),
        row(
            "Value reads",
            match copy {
                CopyPlan::Fetch { bytes, .. } => crate::glyphs::dotted(&format!(
                    "{}, fetched once · passes read the copy",
                    crate::numfmt::bytes(bytes)
                )),
                CopyPlan::Kept { bytes, .. } => {
                    format!(
                        "none: every pass reads the local copy ({})",
                        crate::numfmt::bytes(bytes)
                    )
                }
                _ if remote => "unknown".to_string(),
                _ => planned_read_label(state, plan),
            },
        ),
        row(
            "Requests",
            match copy {
                CopyPlan::Fetch { objects, .. } => format!("{objects}, one per object"),
                CopyPlan::Kept { .. } => "none".to_string(),
                _ if remote => "unknown".to_string(),
                _ => "none".to_string(),
            },
        ),
        row(
            "Known source files",
            source_files
                .map(numfmt::group_chrome)
                .unwrap_or_else(|| "unknown".to_string()),
        ),
        row(
            "Conflict values",
            match state.quality_conflict_reads() {
                0 => "none: no file holds a column in an unreadable type".to_string(),
                reads if plan.compute == QualityCompute::Full => {
                    format!("{reads} extra one-column file reads")
                }
                reads => crate::glyphs::dotted(&format!(
                    "not read · full scan adds {reads} one-column file reads"
                )),
            },
        ),
        row("Remote writes", "none".to_string()),
        row(
            "Local file writes",
            match copy {
                CopyPlan::Fetch { bytes, .. } => crate::glyphs::dotted(&format!(
                    "{} copy in the cache directory · kept until d, reopen or exit",
                    crate::numfmt::bytes(bytes)
                )),
                _ => "none".to_string(),
            },
        ),
        row("Passes", passes_label(config)),
        row("Column intent", {
            let every_row =
                planned_scope_rows(state, plan).is_some_and(|rows| rows <= plan.dataset_rows);
            let lines = intent_read_lines(plan, every_row);
            if lines.is_empty() {
                "none declared".to_string()
            } else {
                // The row names it; the lines say what it costs.
                lines
                    .iter()
                    .map(|line| line.strip_prefix("Column intent: ").unwrap_or(line))
                    .collect::<Vec<_>>()
                    .join(&format!(" {} ", glyphs::get().middot))
            }
        }),
        row(
            "Estimate basis",
            match plan.compute {
                QualityCompute::Metadata => "file footers read when the data opened".to_string(),
                QualityCompute::Sample => {
                    "rows: the sample; bytes: a ceiling, the whole scope".to_string()
                }
                QualityCompute::Full if matches!(copy, CopyPlan::Fetch { .. }) => {
                    "rows: every eligible row; bytes: the object sizes listed when it opened"
                        .to_string()
                }
                QualityCompute::Full => {
                    "rows: every eligible row; bytes: unknown, per source and its caches"
                        .to_string()
                }
            },
        ),
    ];
    let width = 72.min(area.width.saturating_sub(2));
    let label_width = rows
        .iter()
        .map(|row| glyphs::display_width(&row.label))
        .max()
        .unwrap_or(0)
        + 2;
    // The frame and its gutters take four columns of the width.
    let lines = field_lines(&rows, label_width, width.saturating_sub(4) as usize, false);
    let popup = dialog_in(area, width, lines.len() as u16 + 2);
    let content = Surface::new("Access Plan").render(popup, buf, config.ctx);
    render_counted(lines, content, config.theme, buf);
}

/// The reads a run makes over its scope, counted from the plan: honest about the
/// passes even where their bytes are unknown.
fn passes_label(config: &DataQualityWidgetConfig<'_>) -> String {
    let plan = config.plan;
    let view = &config.setup;
    match plan.compute {
        _ if view.unchanged || view.cached => "none: the report is already here".to_string(),
        QualityCompute::Metadata => "none".to_string(),
        QualityCompute::Full => match view.copy {
            CopyPlan::Fetch { .. } => format!(
                "one fetch, then up to {} over the copy, one per check",
                full_passes(config)
            ),
            CopyPlan::Kept { .. } => {
                format!(
                    "up to {} over the local copy, one per check",
                    full_passes(config)
                )
            }
            _ => format!("up to {}, one per check", full_passes(config)),
        },
        QualityCompute::Sample => {
            let sample = if view.reuses_sample {
                "none for the sample: rows already read"
            } else {
                "one sampling pass"
            };
            match view.segment_count {
                SegmentCount::CountPass => {
                    format!("{sample}, then one count of the grain's column")
                }
                SegmentCount::InSamplePass => format!("{sample}, counting the grain's rows"),
                _ => sample.to_string(),
            }
        }
    }
}

fn render_run_prompt(area: Rect, theme: &Theme, buf: &mut Buffer) {
    Paragraph::new(glyphs::dotted("No report · e Setup, then Enter"))
        .alignment(Alignment::Center)
        .style(Style::default().fg(theme.text_primary()))
        .render(area, buf);
}

fn normalize_selection(state: &mut TableState, len: usize) {
    if len == 0 {
        state.select(None);
    } else if state.selected().is_none_or(|selected| selected >= len) {
        state.select(Some(0));
    }
}

fn planned_rows(state: &DataTableState, plan: &DataQualityPlan) -> Option<usize> {
    if plan.compute == QualityCompute::Metadata {
        return Some(0);
    }
    let total = match &plan.scope {
        QualityScope::CurrentView => state.num_rows_if_valid()?,
        QualityScope::FirstRows(rows) => state.num_rows_if_valid()?.min(*rows),
        QualityScope::ViewRows { start, end } => {
            let rows = state.num_rows_if_valid()?;
            rows.min(*end).saturating_sub(start.saturating_sub(1))
        }
        _ => return None,
    };
    match plan.compute {
        QualityCompute::Metadata => unreachable!(),
        QualityCompute::Sample => Some(total.min(dataset_rows(plan))),
        QualityCompute::Full => Some(total),
    }
}

fn planned_read_bytes(state: &DataTableState, plan: &DataQualityPlan) -> Option<usize> {
    if plan.scope.uses_source() {
        return None;
    }
    let rows = match plan.compute {
        QualityCompute::Metadata => 0,
        // The head reads what it keeps; otherwise a ceiling: a spread sample is a few dozen
        // runs of one file or one stream, and per-partition streams the scope.
        QualityCompute::Sample
            if plan.method == crate::analysis::sampling::SampleMethod::FirstRows =>
        {
            planned_scope_rows(state, plan)?.min(dataset_rows(plan))
        }
        QualityCompute::Sample => planned_scope_rows(state, plan)?,
        QualityCompute::Full => return None,
    };
    Some(rows.saturating_mul(state.estimated_row_bytes()))
}

fn planned_scope_rows(state: &DataTableState, plan: &DataQualityPlan) -> Option<usize> {
    let rows = state.num_rows_if_valid()?;
    Some(match &plan.scope {
        QualityScope::FirstRows(limit) => rows.min(*limit),
        QualityScope::ViewRows { start, end } => {
            rows.min(*end).saturating_sub(start.saturating_sub(1))
        }
        QualityScope::CurrentView => rows,
        _ => return None,
    })
}

/// The planned read in words: a sample of a scope larger than itself reads at most
/// the scope, and how much less depends on the source.
pub(crate) fn planned_read_label(state: &DataTableState, plan: &DataQualityPlan) -> String {
    let bytes = planned_read_bytes(state, plan);
    let sampled = plan.compute == QualityCompute::Sample
        && plan.method != crate::analysis::sampling::SampleMethod::FirstRows
        && planned_scope_rows(state, plan).is_some_and(|rows| rows > dataset_rows(plan));
    match bytes {
        Some(bytes) if sampled => format!("up to {}", crate::numfmt::bytes(bytes as u64)),
        Some(bytes) => format!("about {}", crate::numfmt::bytes(bytes as u64)),
        None => "unknown".to_string(),
    }
}

/// What reading every row of `plan`'s scope again costs, as far as it is known: a
/// ceiling from the rows and their width, and unknown for a remote or source scope.
pub(crate) fn scope_read_label(state: &DataTableState, plan: &DataQualityPlan) -> String {
    if state.is_remote_source() {
        return "unknown".to_string();
    }
    match planned_scope_rows(state, plan) {
        Some(rows) => format!(
            "up to {}",
            crate::numfmt::bytes(rows.saturating_mul(state.estimated_row_bytes()) as u64)
        ),
        None => "unknown".to_string(),
    }
}

/// Rows a dataset-grain run keeps: the shared sample's size.
fn dataset_rows(plan: &DataQualityPlan) -> usize {
    plan.dataset_rows
}

pub(crate) fn compute_label(plan: &DataQualityPlan) -> String {
    match plan.compute {
        QualityCompute::Metadata => "metadata only".to_string(),
        QualityCompute::Sample => format!(
            "{} rows {} / seed {}",
            numfmt::group_chrome(dataset_rows(plan)),
            plan.method.label().to_lowercase(),
            plan.sample_seed
        ),
        QualityCompute::Full => "full scan".to_string(),
    }
}

#[cfg(test)]
mod tests;

/// Intervals on screen: the report's list and one interval's detail, and what
/// Setup says about roles and zones before a run.
#[cfg(test)]
mod interval_tests;

#[cfg(test)]
mod trend_tests;