git/intake/main
internal/tui/tui.go
1678 lines40 KB last change raw · history
   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
package tui

import (
	"context"
	"fmt"
	"path/filepath"
	"sort"
	"strings"
	"time"

	"charm.land/bubbles/v2/spinner"
	tea "charm.land/bubbletea/v2"
	"charm.land/lipgloss/v2"

	"intake/internal/config"
	"intake/internal/plan"
	"intake/internal/scan"
	"intake/internal/sheet"
)

type Screen int

const (
	ScreenBrowse Screen = iota
	ScreenJournal
	ScreenReview
	ScreenCoverPicker
	ScreenWorking
	ScreenDone
)

type Step int

const (
	StepMetadata Step = iota
	StepTracks
	StepCover
	StepConfirm
	stepCount
)

func (s Step) String() string {
	switch s {
	case StepMetadata:
		return "metadata"
	case StepTracks:
		return "track names"
	case StepCover:
		return "cover art"
	case StepConfirm:
		return "confirm"
	}
	return "?"
}

type Options struct {
	Config       config.Config
	StagingDir   string
	Prepare      func(ctx context.Context, dir string) (plan.Plan, error)
	Run          func(ctx context.Context, album scan.Album, progress func(string)) (Outcome, error)
	CoverOptions func(ctx context.Context, p plan.Plan) ([]CoverOption, error)
	ApplyCover   func(p *plan.Plan, choice CoverOption) error
	Journal      func(ctx context.Context) ([]JournalEntry, error)
	ForgetSource func(source string) error
	UnfileSource func(source string, progress func(string)) error
	Batch        func(ctx context.Context, progress func(string)) (BatchReport, error)
	Attention    func(ctx context.Context) (map[string]string, error)
	Existing     func(ctx context.Context, p plan.Plan) (Existing, bool)
	Collide      func(albumDir, mode string)
}

// Existing is an album intake already has, found before this one is filed.
type Existing struct {
	ArchiveDir  string
	LibraryDir  string
	AlbumArtist string
	Album       string
	Tracks      int
	SamePath    bool
}

// BatchReport is what an unattended run did, for the summary at the end.
type BatchReport struct {
	Filed  []string
	Parked []string
	Failed []string
}

// JournalEntry is one line of the journal screen: what intake remembers about an
// album, and what is actually on disk.
type JournalEntry struct {
	Source      string
	AlbumArtist string
	Album       string
	Phase       string
	ArchiveDir  string
	LibraryDir  string
	Tracks      int
	Placeholder bool
	Library     bool
	Missing     bool
	Unrecorded  bool
}

// journalAction is one thing that can be done to a record.
type journalAction struct {
	key   string
	label string
	run   func(m *Module, e JournalEntry) tea.Cmd
	// confirm says the action changes something on disk and needs a second key.
	confirm string
}

type Outcome struct {
	ArchiveDir  string
	LibraryDir  string
	Files       int
	Bytes       int64
	Cover       string
	Placeholder bool
	Library     bool
	Warnings    []string
}

type CoverOption struct {
	Path          string
	Source        string
	Label         string
	Width, Height int
	Bytes         int64
	Placeholder   bool
}

type Module struct {
	opts Options
	st   Styles
	prev *ImagePreviewer

	screen Screen
	step   Step

	albums    []scan.Album
	cursor    int
	filter    string
	filtering bool
	offset    int

	current  scan.Album
	pl       plan.Plan
	cover    CoverOption
	hasCover bool

	covers        []CoverOption
	coverCursor   int
	coversFetched bool
	placed        *kittyPlacement

	attention map[string]string
	existing  *Existing
	collision string
	// restored is set when the album's saved edits were applied on opening it.
	restored      bool
	batch         *BatchReport
	journal       []JournalEntry
	journalCursor int
	journalOffset int
	// journalMenu is -1 while the list itself has the keys, and otherwise the
	// index of the action being chosen for the selected record.
	journalMenu   int
	journalAction int
	// journalConfirm is set while an action that moves files waits for a second
	// enter.
	journalConfirm bool

	sheetPath   string
	sheetBefore *sheet.Sheet

	events chan tea.Msg

	log     []string
	follow  bool
	spin    spinner.Model
	outcome Outcome

	width, height int
	status        string
	err           error
	ready         bool
	cancel        context.CancelFunc
	scroll        int
	// helpOpen is the '?' overlay. It is a flag rather than a Screen because
	// the screen underneath keeps all of its state, so closing the overlay
	// puts the user back exactly where they were.
	helpOpen bool
}

type albumsMsg struct {
	albums []scan.Album
	err    error
}

type planMsg struct {
	album scan.Album
	plan  plan.Plan
	err   error
}

type coversMsg struct {
	covers []CoverOption
	err    error
}

type journalMsg struct {
	entries []JournalEntry
	err     error
}

type journalDoneMsg struct {
	note string
	err  error
}

func (m Module) journalCmd() tea.Cmd {
	get := m.opts.Journal
	return func() tea.Msg {
		entries, err := get(context.Background())
		return journalMsg{entries: entries, err: err}
	}
}

type existingMsg struct {
	existing Existing
	found    bool
}

func (m Module) existingCmd(p plan.Plan) tea.Cmd {
	get := m.opts.Existing
	return func() tea.Msg {
		e, ok := get(context.Background(), p)
		return existingMsg{existing: e, found: ok}
	}
}

type attentionMsg struct {
	reasons map[string]string
	err     error
}

func (m Module) attentionCmd() tea.Cmd {
	get := m.opts.Attention
	return func() tea.Msg {
		reasons, err := get(context.Background())
		return attentionMsg{reasons: reasons, err: err}
	}
}

type doneMsg struct {
	outcome Outcome
	err     error
	// batch, when set, means the working screen was filing a whole staging
	// directory rather than one album.
	batch *BatchReport
}

type progressMsg string

type replantMsg struct{}

func waitForProgress(ch chan tea.Msg) tea.Cmd {
	return func() tea.Msg {
		msg, ok := <-ch
		if !ok {
			return nil
		}
		return msg
	}
}

func New(opts Options) Module {
	st := DefaultStyles()
	sp := spinner.New(spinner.WithSpinner(spinner.Dot), spinner.WithStyle(st.ListSpinner))
	m := Module{
		opts:   opts,
		st:     st,
		prev:   NewImagePreviewer(),
		spin:   sp,
		status: "reading staging",
		// -1 means the journal list itself has the keys; 0 would mean the first
		// action menu is open before anything asked for it.
		journalMenu: -1,
	}
	return m
}

func (m Module) Init() tea.Cmd {
	return tea.Batch(m.spin.Tick, m.scanCmd())
}

func (m Module) scanCmd() tea.Cmd {
	dir := m.opts.StagingDir
	return func() tea.Msg {
		albums, err := scan.StagingWith(context.Background(), dir, scan.Options{Probe: false})
		return albumsMsg{albums: albums, err: err}
	}
}

func (m Module) planCmd(a scan.Album) tea.Cmd {
	prepare := m.opts.Prepare
	return func() tea.Msg {
		p, err := prepare(context.Background(), a.Dir)
		return planMsg{album: a, plan: p, err: err}
	}
}

func (m Module) coversCmd(p plan.Plan) tea.Cmd {
	get := m.opts.CoverOptions
	return func() tea.Msg {
		covers, err := get(context.Background(), p)
		return coversMsg{covers: covers, err: err}
	}
}

func (m Module) runCmd(ctx context.Context, a scan.Album) tea.Cmd {
	run := m.opts.Run
	events := m.events
	return func() tea.Msg {
		go func() {
			defer close(events)
			outcome, err := run(ctx, a, func(line string) {
				select {
				case events <- progressMsg(line):
				case <-ctx.Done():
				}
			})
			select {
			case events <- doneMsg{outcome: outcome, err: err}:
			case <-ctx.Done():
			}
		}()
		msg, ok := <-events
		if !ok {
			return nil
		}
		return msg
	}
}

func (m Module) batchCmd(ctx context.Context) tea.Cmd {
	run, events := m.opts.Batch, m.events
	return func() tea.Msg {
		go func() {
			defer close(events)
			rep, err := run(ctx, func(line string) {
				select {
				case events <- progressMsg(line):
				case <-ctx.Done():
				}
			})
			select {
			case events <- doneMsg{err: err, batch: &rep}:
			case <-ctx.Done():
			}
		}()
		msg, ok := <-events
		if !ok {
			return nil
		}
		return msg
	}
}

func (m Module) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
	switch msg := msg.(type) {
	case tea.WindowSizeMsg:
		m.width, m.height = msg.Width, msg.Height
		return m, nil

	case albumsMsg:
		m.ready = true
		if msg.err != nil {
			m.err = msg.err
			m.status = "could not read staging"
			return m, nil
		}
		sort.Slice(msg.albums, func(i, j int) bool { return msg.albums[i].Name < msg.albums[j].Name })
		m.albums = msg.albums
		m.cursor, m.offset = 0, 0
		m.status = fmt.Sprintf("%d album(s) staged", len(m.albums))
		if m.opts.Attention != nil {
			return m, m.attentionCmd()
		}
		return m, nil

	case planMsg:
		if msg.err != nil {
			m.err, m.status = msg.err, msg.err.Error()
			return m, nil
		}
		clear := m.clearPlacedCover()
		m.resetAlbum()
		m.current, m.pl = msg.album, msg.plan
		m.screen, m.step, m.scroll = ScreenReview, StepMetadata, 0
		m.status = fmt.Sprintf("%d track%s read", len(msg.plan.Tracks), plural(len(msg.plan.Tracks)))
		if m.restoreSavedEdits() {
			m.status = "restored the edits you saved for this album; press D to discard them"
		}
		if m.opts.Existing != nil {
			return m, tea.Batch(clear, m.existingCmd(msg.plan))
		}
		return m, clear

	case coversMsg:
		m.coversFetched = true
		if msg.err != nil {
			m.status = msg.err.Error()
			return m, nil
		}
		m.covers = msg.covers
		m.coverCursor = 0
		if len(m.covers) == 0 {
			m.status = "no cover candidates found; continuing without one"
			m.screen = ScreenReview
			m.step = StepConfirm
			return m, nil
		}
		m.screen = ScreenCoverPicker
		m.scroll = 0
		m.status = fmt.Sprintf("%d candidate%s", len(m.covers), plural(len(m.covers)))
		// Prepare every candidate in the background before the user starts
		// moving through them, so a render rarely has to stage one itself.
		paths := make([]string, 0, len(m.covers))
		for _, c := range m.covers {
			paths = append(paths, c.Path)
		}
		m.prev.Warm(paths)
		m.renderCoverPreview(m.coverPreviewWidth(), m.coverPreviewHeight())
		return m, tea.Batch(m.placeCover(), replantTick())

	case existingMsg:
		if msg.found {
			e := msg.existing
			m.existing = &e
			// Keeping both is the default, because nothing else can lose an
			// album, and the other two are one keypress away.
			m.collision = "both"
			m.status = "you already have this album"
			return m, nil
		}
		m.existing, m.collision = nil, ""
		return m, nil

	case attentionMsg:
		if msg.err == nil {
			m.attention = msg.reasons
		}
		return m, nil

	case doneMsg:
		if msg.err != nil {
			m.err, m.status = msg.err, msg.err.Error()
			if msg.batch == nil {
				m.screen = ScreenReview
				return m, nil
			}
		}
		if msg.batch == nil {
			// The album is filed, so the edits it was built from have done their
			// job; leaving them would apply last time's corrections to a fresh
			// folder of the same name.
			m.sheetStore().Delete(m.current.Dir)
			m.restored = false
		}
		m.outcome, m.batch = msg.outcome, msg.batch
		m.screen = ScreenDone
		m.scroll = 0
		switch {
		case msg.batch != nil:
			m.status = fmt.Sprintf("%d filed, %d parked, %d failed", len(msg.batch.Filed), len(msg.batch.Parked), len(msg.batch.Failed))
		default:
			m.status = "filed"
		}
		return m, nil

	case journalMsg:
		m.ready = true
		if msg.err != nil {
			m.status = msg.err.Error()
			return m, nil
		}
		m.journal = msg.entries
		m.journalCursor, m.journalOffset, m.journalMenu = 0, 0, -1
		m.journalConfirm = false
		m.screen = ScreenJournal
		m.status = fmt.Sprintf("%d record%s", len(m.journal), plural(len(m.journal)))
		return m, nil

	case journalDoneMsg:
		if msg.err != nil {
			m.status = msg.err.Error()
			return m, m.journalCmd()
		}
		m.status = msg.note
		return m, m.journalCmd()

	case editorFinishedMsg:
		if msg.err != nil {
			m.status = "editor exited with an error: " + msg.err.Error()
			return m, nil
		}
		m.applyEditedSheet(msg)
		return m, nil

	case progressMsg:
		m.log = append(m.log, string(msg))
		if len(m.log) > 500 {
			m.log = m.log[len(m.log)-500:]
		}
		if m.follow {
			// applyScroll clamps this to the last screenful, so the line that
			// just arrived is the one on screen.
			m.scroll = 1 << 30
		}
		return m, waitForProgress(m.events)

	case replantMsg:
		if m.screen != ScreenCoverPicker || m.placed == nil {
			return m, nil
		}
		return m, tea.Tick(replantInterval, func(time.Time) tea.Msg { return replantMsg{} })

	case spinner.TickMsg:
		var cmd tea.Cmd
		m.spin, cmd = m.spin.Update(msg)
		return m, cmd

	case tea.KeyPressMsg:
		return m.handleKey(msg)
	}
	return m, nil
}

func (m Module) handleKey(msg tea.KeyPressMsg) (tea.Model, tea.Cmd) {
	key := msg.String()
	if key == "ctrl+c" {
		return m.quit()
	}
	// The overlay takes every key but the ones that close it, so nothing on
	// the screen underneath fires by accident while somebody is reading it.
	if m.helpOpen {
		switch key {
		case "?", "esc":
			m.helpOpen = false
		}
		return m, nil
	}
	if key == "?" {
		m.helpOpen = true
		return m, nil
	}

	switch m.screen {
	case ScreenBrowse:
		return m.keyBrowse(key, msg)
	case ScreenJournal:
		return m.keyJournal(key)
	case ScreenReview:
		return m.keyReview(key)
	case ScreenCoverPicker:
		return m.keyCover(key)
	case ScreenWorking:
		switch key {
		case "q", "esc":
			return m.quit()
		case "up", "k":
			m.follow = false
			if m.scroll > 0 {
				m.scroll--
			}
		case "down", "j":
			m.follow = true
			m.scroll++
		case "pgup":
			m.follow = false
			m.scroll -= m.body().H - 1
			if m.scroll < 0 {
				m.scroll = 0
			}
		case "pgdown":
			m.scroll += m.body().H - 1
		case "home", "g":
			m.scroll = 0
		}
		return m, nil
	case ScreenDone:
		switch key {
		case "q":
			return m.quit()
		case "up", "k":
			if m.scroll > 0 {
				m.scroll--
			}
			return m, nil
		case "down", "j":
			m.scroll++
			return m, nil
		case "pgup":
			m.scroll -= m.body().H - 1
			if m.scroll < 0 {
				m.scroll = 0
			}
			return m, nil
		case "pgdown":
			m.scroll += m.body().H - 1
			return m, nil
		case "enter", "esc":
			m.screen = ScreenBrowse
			m.scroll, m.batch = 0, nil
			return m, m.scanCmd()
		}
		return m, nil
	}
	return m, nil
}

func (m Module) keyBrowse(key string, msg tea.KeyPressMsg) (tea.Model, tea.Cmd) {
	if m.filtering {
		switch key {
		case "enter":
			m.filtering = false
			m.cursor, m.offset = 0, 0
			return m, nil
		case "esc":
			m.filtering, m.filter = false, ""
			m.cursor, m.offset = 0, 0
			return m, nil
		case "backspace":
			if m.filter != "" {
				m.filter = m.filter[:len(m.filter)-1]
			}
			m.cursor, m.offset = 0, 0
			return m, nil
		default:
			if len(msg.Text) > 0 {
				m.filter += msg.Text
				m.cursor, m.offset = 0, 0
			}
			return m, nil
		}
	}

	visible := m.visibleAlbums()
	switch key {
	case "q":
		return m.quit()
	case "/":
		m.filtering = true
		return m, nil
	case "r":
		m.status = "refreshing"
		return m, m.scanCmd()
	case "J":
		if m.opts.Journal == nil {
			m.status = "this build cannot read the journal"
			return m, nil
		}
		m.status = "reading the journal"
		return m, m.journalCmd()
	case "n":
		return m.jumpToAttention()
	case "b":
		if m.opts.Batch == nil {
			m.status = "this build cannot file a batch"
			return m, nil
		}
		ready := 0
		for _, a := range m.visibleAlbums() {
			if a.Status == scan.StatusReady {
				ready++
			}
		}
		if ready == 0 {
			m.status = "nothing ready to file"
			return m, nil
		}
		ctx, cancel := context.WithCancel(context.Background())
		m.cancel = cancel
		m.events = make(chan tea.Msg, 64)
		m.screen, m.log, m.follow, m.batch = ScreenWorking, nil, true, nil
		m.status = fmt.Sprintf("filing %d album(s) unattended", ready)
		return m, tea.Batch(m.spin.Tick, m.batchCmd(ctx))
	case "up", "k":
		if m.cursor > 0 {
			m.cursor--
		}
	case "down", "j":
		if m.cursor < len(visible)-1 {
			m.cursor++
		}
	case "home", "g":
		m.cursor = 0
	case "end", "G":
		m.cursor = len(visible) - 1
	case "enter":
		if m.cursor < len(visible) {
			m.status = "reading " + visible[m.cursor].Name
			return m, m.planCmd(visible[m.cursor])
		}
	}
	return m, nil
}

// jumpToAttention moves the cursor to the next album an unattended run parked.
func (m Module) jumpToAttention() (tea.Model, tea.Cmd) {
	visible := m.visibleAlbums()
	if len(visible) == 0 || len(m.attention) == 0 {
		m.status = "no album is waiting on you"
		return m, nil
	}
	for i := 1; i <= len(visible); i++ {
		j := (m.cursor + i) % len(visible)
		if _, ok := m.attention[visible[j].Dir]; ok {
			m.cursor = j
			m.status = m.attention[visible[j].Dir]
			return m, nil
		}
	}
	m.status = "no album is waiting on you"
	return m, nil
}

func (m Module) keyReview(key string) (tea.Model, tea.Cmd) {
	// An album that is already in the library takes the keys first: 'k' would
	// otherwise scroll, which is exactly the kind of small collision that makes
	// somebody press the wrong thing at the wrong moment.
	if m.existing != nil && m.step == StepMetadata {
		switch key {
		case "k":
			m.collision = "both"
			m.status = "filing beside " + filepath.Base(m.existing.ArchiveDir)
			return m, nil
		case "r":
			m.collision = "replace"
			m.status = "will replace " + filepath.Base(m.existing.ArchiveDir)
			return m, nil
		case "s":
			m.screen, m.existing, m.collision = ScreenBrowse, nil, ""
			m.status = "left it alone; you already have it"
			return m, m.scanCmd()
		}
	}
	switch key {
	case "up", "k":
		if m.scroll > 0 {
			m.scroll--
		}
		return m, nil
	case "down", "j":
		m.scroll++
		return m, nil
	case "pgup":
		m.scroll -= m.body().H - 1
		if m.scroll < 0 {
			m.scroll = 0
		}
		return m, nil
	case "pgdown":
		m.scroll += m.body().H - 1
		return m, nil
	case "home", "g":
		m.scroll = 0
		return m, nil
	case "G":
		m.scroll = 1 << 20
		return m, nil
	}

	switch key {
	case "q":
		return m.quit()
	case "esc":
		if m.step > StepMetadata {
			m.step--
			m.scroll = 0
			return m, nil
		}
		m.screen = ScreenBrowse
		return m, nil
	case "e":
		return m.openSheetEditor()
	case "D":
		if m.restored {
			m.sheetStore().Delete(m.current.Dir)
			m.restored = false
			m.status = "discarded the saved edits; reopen the album to start from the tags again"
			return m, nil
		}
	case "enter":
		return m.nextStep()
	}
	return m, nil
}

func (m Module) nextStep() (tea.Model, tea.Cmd) {
	switch m.step {
	case StepMetadata:
		if m.pl.Blocked() {
			m.status = "cannot continue: " + firstIssue(m.pl)
			return m, nil
		}
		m.step, m.scroll = StepTracks, 0
		return m, nil
	case StepTracks:
		m.step, m.scroll = StepCover, 0
		return m, nil
	case StepCover:
		if m.opts.CoverOptions == nil {
			m.step, m.scroll = StepConfirm, 0
			return m, nil
		}
		if !m.coversFetched {
			m.status = "finding cover art"
			return m, m.coversCmd(m.pl)
		}
		if len(m.covers) == 0 {
			m.step, m.scroll = StepConfirm, 0
			return m, nil
		}
		m.screen, m.scroll = ScreenCoverPicker, 0
		return m, nil
	case StepConfirm:
		if m.opts.Collide != nil && m.collision != "" {
			m.opts.Collide(m.current.Dir, m.collision)
		}
		ctx, cancel := context.WithCancel(context.Background())
		m.cancel = cancel
		m.events = make(chan tea.Msg, 64)
		m.screen, m.log = ScreenWorking, nil
		m.follow = true
		m.status = "working"
		return m, tea.Batch(m.spin.Tick, m.runCmd(ctx, m.current))
	}
	return m, nil
}

func (m Module) keyCover(key string) (tea.Model, tea.Cmd) {
	switch key {
	case "q":
		return m.quit()
	case "e":
		// The whole album stays editable while a cover is being chosen: this is
		// where a wrong album artist is usually noticed.
		return m.openSheetEditor()
	case "esc":
		m.screen = ScreenReview
		m.scroll = 0
		return m, m.clearPlacedCover()
	case "up", "k":
		if m.coverCursor > 0 {
			m.coverCursor--
			m.scroll = 0
			m.renderCoverPreview(m.coverPreviewWidth(), m.coverPreviewHeight())
			return m, m.placeCover()
		}
		return m, nil
	case "down", "j":
		if m.coverCursor < len(m.covers)-1 {
			m.coverCursor++
			m.scroll = 0
			m.renderCoverPreview(m.coverPreviewWidth(), m.coverPreviewHeight())
			return m, m.placeCover()
		}
		return m, nil
	case "enter":
		if m.coverCursor < len(m.covers) {
			picked := m.covers[m.coverCursor]
			if m.opts.ApplyCover != nil {
				if err := m.opts.ApplyCover(&m.pl, picked); err != nil {
					m.status = err.Error()
					return m, nil
				}
			}
			m.cover, m.hasCover = picked, true
		}
		m.screen, m.step, m.scroll = ScreenReview, StepConfirm, 0
		return m, m.clearPlacedCover()
	}
	return m, nil
}

const replantInterval = 90 * time.Millisecond

func replantTick() tea.Cmd {
	return tea.Tick(replantInterval, func(time.Time) tea.Msg { return replantMsg{} })
}

func (m *Module) renderCoverPreview(width, height int) {
	if m.coverCursor < 0 || m.coverCursor >= len(m.covers) {
		return
	}
	m.prev.Render(m.covers[m.coverCursor].Path, width, height)
}

func (m *Module) clearPlacedCover() tea.Cmd {
	if m.placed == nil {
		return nil
	}
	cmd := clearImage(*m.placed)
	m.placed = nil
	return cmd
}

// resetAlbum drops the review state carried over from the previous album.
//
// Only screen, step and scroll used to be reset when a new album opened, so a
// second album inherited the first one's chosen cover and its candidate list --
// and coversFetched stayed true, meaning the new album never fetched its own
// candidates at all and the picker offered up the previous album's art. Nothing
// downstream would catch that: the cover is a valid image, so every verification
// step passes and the album archives with the wrong sleeve.
func (m *Module) resetAlbum() {
	m.cover, m.hasCover = CoverOption{}, false
	m.covers, m.coverCursor, m.coversFetched = nil, 0, false
	m.placed = nil
	m.sheetPath, m.sheetBefore = "", nil
	m.journalMenu, m.journalAction, m.journalConfirm = -1, 0, false
	m.batch, m.existing, m.collision, m.restored = nil, nil, "", false
	m.log, m.outcome = nil, Outcome{}
	m.err, m.status = nil, ""
	m.scroll, m.offset = 0, 0
}

func (m Module) quit() (tea.Model, tea.Cmd) {
	if m.cancel != nil {
		m.cancel()
	}
	return m, tea.Quit
}

func firstIssue(p plan.Plan) string {
	for _, i := range p.Issues {
		if i.Hard {
			return i.Text
		}
	}
	return "unknown issue"
}

func (m Module) visibleAlbums() []scan.Album {
	if m.filter == "" {
		return m.albums
	}
	q := strings.ToLower(m.filter)
	var out []scan.Album
	for _, a := range m.albums {
		if strings.Contains(strings.ToLower(a.Name), q) {
			out = append(out, a)
		}
	}
	return out
}

func (m Module) View() tea.View {
	v := tea.NewView(m.render())
	v.AltScreen = true
	if m.mode() == modeTiny {
		v.AltScreen = false
		v.MouseMode = tea.MouseModeNone
	}
	return v
}

func (m Module) render() string {
	if m.mode() == modeTiny {
		return m.renderTiny()
	}

	header := m.renderHeader()
	footer := m.renderFooter()
	bodyH := m.body().H

	var body string
	if m.helpOpen {
		body = m.helpOverlay(bodyH)
	} else {
		body = m.renderBody()
		body = m.applyScroll(body, m.width, bodyH)
		body = clamp(fitHeight(body, bodyH), m.width, bodyH)
	}

	parts := []string{header}
	if m.railShown() {
		parts = append(parts, m.renderRail())
	}
	parts = append(parts, body, footer)
	return lipgloss.JoinVertical(lipgloss.Left, parts...)
}

func (m Module) renderHeader() string {
	left := m.st.AppName.Render("intake")
	var crumbs []string
	switch m.screen {
	case ScreenBrowse:
		crumbs = append(crumbs, m.st.CrumbDim.Render("staging"))
	case ScreenJournal:
		crumbs = append(crumbs, m.st.CrumbDim.Render("journal"))
	case ScreenReview, ScreenCoverPicker, ScreenWorking, ScreenDone:
		crumbs = append(crumbs, m.st.CrumbDim.Render("staging"))
		if m.current.Name != "" {
			crumbs = append(crumbs, m.st.CrumbDim.Render("›"))
			crumbs = append(crumbs, m.st.Crumb.Render(m.current.Name))
		}
		if m.screen == ScreenReview {
			crumbs = append(crumbs, m.st.CrumbDim.Render("›"))
			crumbs = append(crumbs, m.st.Accent.Render(m.step.String()))
		}
	}
	right := ""
	if n := len(m.albums); n > 0 && m.screen == ScreenBrowse {
		right = m.st.Status.Render(fmt.Sprintf("%d album%s", n, plural(n)))
	}
	mid := strings.Join(crumbs, " ")
	if mid == "" {
		mid = m.st.CrumbDim.Render("library intake")
	}
	line := left + m.st.Dim.Render(" │ ") + mid
	if right != "" {
		pad := m.width - displayWidth(line) - displayWidth(right) - 1
		if pad < 1 {
			return clamp(line, m.width, 1)
		}
		line += strings.Repeat(" ", pad) + right + " "
	}
	return clamp(line, m.width, 1)
}

func (m Module) railShown() bool {
	return m.screen == ScreenReview && m.mode() >= modeCompact
}

func (m Module) renderRail() string {
	cells := make([]string, 0, stepCount)
	for s := Step(0); s < stepCount; s++ {
		label := s.String()
		switch {
		case s < m.step:
			cells = append(cells, m.st.Success.Render("✓ "+label))
		case s == m.step:
			cells = append(cells, m.st.Selected.Render(" "+label+" "))
		default:
			cells = append(cells, m.st.Dim.Render("  "+label))
		}
	}
	return " " + clamp(strings.Join(cells, m.st.Dim.Render("  ·  ")), m.width-1, 1)
}

func (m Module) renderFooter() string {
	hints := m.footerHelp()
	parts := make([]string, 0, len(hints))
	for _, h := range hints {
		parts = append(parts, m.st.HintKey.Render(h.key)+" "+m.st.HintDesc.Render(h.desc))
	}
	left := " " + strings.Join(parts, m.st.Dim.Render("  ·  "))
	status := ""
	if m.err != nil {
		status = m.st.Error.Render(m.err.Error())
	} else if m.status != "" {
		status = m.st.Status.Render(m.status)
	}
	line := left
	if status != "" {
		gap := m.width - displayWidth(left) - displayWidth(status) - 2
		if gap < 1 {
			return clamp(left, m.width, 1)
		}
		line = left + strings.Repeat(" ", gap) + status + " "
	}
	return clamp(line, m.width, 1)
}

// helpKey is one row of the help overlay: a key, or a few keys that do the same
// thing written as "a / b", and the one line that says what it does.
type helpKey struct {
	key  string
	desc string
}

// helpKeys is every key that does something on the screen that is open. The
// overlay draws the whole list and the footer picks a few rows out of it, so
// the two cannot end up describing the same key differently.
//
// Filtering, the journal action menu and the review screen's collision choices
// are states of their screen rather than screens of their own, so their keys
// are only here while those states are on.
func (m Module) helpKeys() []helpKey {
	switch m.screen {
	case ScreenBrowse:
		if m.filtering {
			return []helpKey{
				{"enter", "apply filter"},
				{"esc", "cancel filter"},
				{"backspace", "delete a character"},
			}
		}
		return []helpKey{
			{"↑↓ / k j", "move"},
			{"g / home", "first"},
			{"G / end", "last"},
			{"/", "filter"},
			{"enter", "review"},
			{"b", "file all"},
			{"n", "next that needs you"},
			{"J", "journal"},
			{"r", "refresh"},
			{"q", "quit"},
		}
	case ScreenJournal:
		if m.journalMenu >= 0 {
			confirm := "do it"
			if m.journalConfirm {
				confirm = "confirm"
			}
			return []helpKey{
				{"↑↓ / k j", "choose"},
				{"enter", confirm},
				{"esc / q", "close the menu"},
			}
		}
		return []helpKey{
			{"↑↓ / k j", "move"},
			{"g / home", "first"},
			{"G / end", "last"},
			{"enter", "actions"},
			{"r", "refresh"},
			{"esc", "back"},
			{"q", "quit"},
		}
	case ScreenReview:
		if m.existing != nil && m.step == StepMetadata {
			// The collision choices take k and r before the scroll keys get a
			// chance, so the arrow is the only way left to scroll up.
			return []helpKey{
				{"k", "keep both"},
				{"r", "replace it"},
				{"s", "skip this album"},
				{"↑", "scroll up"},
				{"↓ / j", "scroll down"},
				{"pgup / pgdown", "scroll a page"},
				{"g / home", "top"},
				{"G", "bottom"},
				{"enter", "continue"},
				{"e", "edit album"},
				{"esc", "back"},
				{"q", "quit"},
			}
		}
		keys := []helpKey{
			{"↑↓ / k j", "scroll"},
			{"pgup / pgdown", "scroll a page"},
			{"g / home", "top"},
			{"G", "bottom"},
			{"enter", "continue"},
			{"e", "edit album"},
		}
		// Only offered when there is something to discard: an album whose saved
		// edits were applied when it was opened.
		if m.restored {
			keys = append(keys, helpKey{"D", "discard the edits saved for this album"})
		}
		return append(keys, helpKey{"esc", "back"}, helpKey{"q", "quit"})
	case ScreenCoverPicker:
		return []helpKey{
			{"↑↓ / k j", "choose"},
			{"enter", "use this cover"},
			{"e", "edit album"},
			{"esc", "cancel"},
			{"q", "quit"},
		}
	case ScreenWorking:
		return []helpKey{
			{"↑ / k", "scroll up, stop following"},
			{"↓ / j", "scroll down, follow"},
			{"pgup / pgdown", "scroll a page"},
			{"g / home", "top"},
			{"esc / q", "stop watching"},
		}
	case ScreenDone:
		return []helpKey{
			{"↑↓ / k j", "scroll"},
			{"pgup / pgdown", "scroll a page"},
			{"enter / esc", "file another"},
			{"q", "quit"},
		}
	}
	return nil
}

// helpToggle is the key that opens and closes the overlay. The footer says what
// it does from outside the overlay; helpGlobal says what it does from inside.
func helpToggle() helpKey {
	return helpKey{"?", "help"}
}

// helpGlobal is what works on every screen whatever else is open.
func helpGlobal() []helpKey {
	return []helpKey{
		{"? / esc", "close help"},
		{"ctrl+c", "quit"},
	}
}

// footerKeys names the rows from helpKeys that the footer has room for. The
// overlay is the reference; the footer is the reminder.
func (m Module) footerKeys() []string {
	switch m.screen {
	case ScreenBrowse:
		if m.filtering {
			return []string{"enter", "esc"}
		}
		return []string{"↑↓ / k j", "/", "enter", "b", "n", "J", "r", "q"}
	case ScreenJournal:
		if m.journalMenu >= 0 {
			return []string{"↑↓ / k j", "enter", "esc / q"}
		}
		return []string{"↑↓ / k j", "enter", "r", "esc", "q"}
	case ScreenReview:
		if m.existing != nil && m.step == StepMetadata {
			return []string{"k", "r", "s"}
		}
		return []string{"↑↓ / k j", "enter", "e", "esc", "q"}
	case ScreenCoverPicker:
		return []string{"↑↓ / k j", "enter", "e", "esc"}
	case ScreenWorking:
		return []string{"esc / q"}
	case ScreenDone:
		return []string{"enter / esc", "q"}
	}
	return nil
}

// footerHelp is the footer's half of the help. The descriptions come from
// helpKeys, so the footer cannot claim something about a key that the overlay
// does not. The toggle leads, because the footer is the only place the overlay
// is advertised and a footer that runs long is cut off at the right.
func (m Module) footerHelp() []helpKey {
	keys := m.helpKeys()
	hints := make([]helpKey, 0, len(keys)+1)
	hints = append(hints, helpToggle())
	for _, want := range m.footerKeys() {
		for _, k := range keys {
			if k.key == want {
				hints = append(hints, k)
				break
			}
		}
	}
	return hints
}

// helpTitle says which screen the keys belong to, since the overlay covers the
// header that would otherwise say it.
func (m Module) helpTitle() string {
	switch m.screen {
	case ScreenBrowse:
		return "Help · staging"
	case ScreenJournal:
		return "Help · journal"
	case ScreenReview:
		return "Help · review"
	case ScreenCoverPicker:
		return "Help · cover art"
	case ScreenWorking:
		return "Help · filing"
	case ScreenDone:
		return "Help · done"
	}
	return "Help"
}

// helpOverlay draws the keys for the screen that is open over the body it
// covers. It is sized to that body, and a list too long for the terminal is cut
// off rather than allowed to push the layout past the screen. The rows that
// close the overlay and quit are the last to be dropped, because they are the
// only way out of a list that does not fit.
func (m Module) helpOverlay(height int) string {
	screen := m.helpKeys()
	globals := helpGlobal()

	// The panel spends three lines on its borders and title before any key is
	// drawn.
	avail := height - 3
	if avail < 1 {
		avail = 1
	}
	for len(screen)+len(globals) > avail && len(screen) > 0 {
		screen = screen[:len(screen)-1]
	}
	for len(screen)+len(globals) > avail && len(globals) > 0 {
		globals = globals[:len(globals)-1]
	}
	// The rule between the screen's keys and the global ones is cosmetic, so it
	// is only drawn when there is a line to spare.
	rule := len(screen) > 0 && len(globals) > 0 && len(screen)+len(globals) < avail

	rows := append(append([]helpKey{}, screen...), globals...)
	keyW := 0
	for _, r := range rows {
		if w := displayWidth(r.key); w > keyW {
			keyW = w
		}
	}
	contentW := keyW
	for _, r := range rows {
		if w := keyW + 2 + displayWidth(r.desc); w > contentW {
			contentW = w
		}
	}
	if contentW < 1 {
		contentW = 1
	}

	var b strings.Builder
	row := func(r helpKey) {
		b.WriteString(m.st.HintKey.Render(r.key + strings.Repeat(" ", keyW-displayWidth(r.key))))
		b.WriteString("  " + m.st.HintDesc.Render(r.desc) + "\n")
	}
	for _, r := range screen {
		row(r)
	}
	if rule {
		b.WriteString(m.st.Rule.Render(strings.Repeat("─", contentW)) + "\n")
	}
	for _, r := range globals {
		row(r)
	}
	panel := panelWith(m.st, m.helpTitle(), strings.TrimRight(b.String(), "\n"), m.width, true)
	return lipgloss.Place(m.width, height, lipgloss.Center, lipgloss.Center, clamp(panel, m.width, height))
}

func (m Module) applyScroll(content string, width, height int) string {
	lines := strings.Split(content, "\n")
	for i, l := range lines {
		lines[i] = truncateTo(l, width)
	}
	if len(lines) <= height {
		return strings.Join(lines, "\n")
	}
	off := m.scroll
	maxOff := len(lines) - height
	if off > maxOff {
		off = maxOff
	}
	if off < 0 {
		off = 0
	}
	window := lines[off : off+height]

	if height >= 3 {
		if off > 0 {
			window[0] = m.st.Dim.Render("  ↑ more")
		}
		if off+height < len(lines) {
			window[len(window)-1] = m.st.Dim.Render(fmt.Sprintf("  ↓ more (%d%%)", (off+height)*100/len(lines)))
		}
	}
	return strings.Join(window, "\n")
}

func (m Module) coverPreviewWidth() int {
	area := m.body()
	listW := m.width / 3
	if listW < 34 {
		listW = 34
	}
	w := m.width - listW - 2
	if w < 20 {
		return 0
	}
	_ = area
	return w
}

func (m Module) coverPreviewHeight() int {
	area := m.body()
	h := area.H - 1
	if h < 5 {
		h = 5
	}
	return h
}

func (m Module) renderBody() string {
	switch m.screen {
	case ScreenBrowse:
		return m.viewBrowse()
	case ScreenJournal:
		return m.viewJournal()
	case ScreenReview:
		return m.viewReview()
	case ScreenCoverPicker:
		return m.viewCoverPicker()
	case ScreenWorking:
		return m.viewWorking()
	case ScreenDone:
		return m.viewDone()
	}
	return ""
}

func (m Module) renderTiny() string {
	if m.helpOpen {
		return clamp(" intake · help: ? or esc closes", m.width, 1)
	}
	what := "staging"
	switch m.screen {
	case ScreenJournal:
		what = "journal"
	case ScreenReview:
		what = fmt.Sprintf("%s · %s", m.current.Name, m.step)
	case ScreenWorking:
		what = "working: " + m.current.Name
	case ScreenDone:
		what = "done: " + m.current.Name
	case ScreenCoverPicker:
		what = "choose cover"
	}
	return clamp(" intake · "+what, m.width, 1)
}

func (m Module) viewBrowse() string {
	if !m.ready {
		return m.st.Muted.Render("  " + m.spin.View() + " reading staging ...")
	}
	if m.err != nil {
		return m.st.Error.Render("  " + m.err.Error())
	}
	if len(m.albums) == 0 {
		return m.emptyStaging()
	}

	visible := m.visibleAlbums()
	if len(visible) == 0 {
		return m.st.Muted.Render("  no album matches " + fmt.Sprintf("%q", m.filter))
	}

	area := m.body()
	rows := area.H - 1
	if rows < 1 {
		rows = 1
	}
	perItem := 2
	if m.mode() == modeMini {
		perItem = 1
	}
	capacity := rows / perItem
	if capacity < 1 {
		capacity = 1
	}

	if m.cursor < m.offset {
		m.offset = m.cursor
	}
	if m.cursor >= m.offset+capacity {
		m.offset = m.cursor - capacity + 1
	}
	end := m.offset + capacity
	if end > len(visible) {
		end = len(visible)
	}

	var b strings.Builder
	head := "ALBUMS"
	if m.filtering || m.filter != "" {
		head = "FILTER " + m.filter
	}
	b.WriteString(" " + m.st.Heading.Render(head) + "\n")

	for i := m.offset; i < end; i++ {
		a := visible[i]
		selected := i == m.cursor
		name := a.Name
		if selected {
			b.WriteString(m.st.Marker.Render(" ▸ ") + m.st.Selected.Render(" "+truncateTo(name, m.width-8)+" ") + "\n")
		} else {
			b.WriteString("   " + m.st.Value.Render(truncateTo(name, m.width-6)) + "\n")
		}
		if perItem == 2 {
			desc := m.albumSummary(a)
			if selected {
				b.WriteString(m.st.SelectedDim.Render("   "+truncateTo(desc, m.width-8)) + "\n")
			} else {
				b.WriteString("   " + m.st.Muted.Render(truncateTo(desc, m.width-6)) + "\n")
			}
		}
	}
	if end < len(visible) {
		b.WriteString(m.st.Dim.Render(fmt.Sprintf("   … %d more", len(visible)-end)))
	}
	return b.String()
}

func (m Module) emptyStaging() string {
	lines := []string{
		"",
		"  " + m.st.Title.Render("Nothing is staged yet"),
		"",
		"  " + m.st.Muted.Render("Put album folders in:"),
		"  " + m.st.Accent.Render(m.opts.StagingDir),
		"",
		"  " + m.st.Muted.Render("intake expects one folder per album. Multi-disc"),
		"  " + m.st.Muted.Render("releases may keep their discs in subfolders."),
		"",
		"  " + m.st.Muted.Render("Press r to look again."),
	}
	return strings.Join(lines, "\n")
}

func (m Module) albumSummary(a scan.Album) string {
	if reason, ok := m.attention[a.Dir]; ok {
		return "needs you: " + reason
	}
	parts := []string{string(a.Status)}
	switch {
	case len(a.Tracks) > 0:
		parts = append(parts, fmt.Sprintf("%d track%s", len(a.Tracks), plural(len(a.Tracks))))
	case a.FileCount > 0:
		parts = append(parts, fmt.Sprintf("%d file%s", a.FileCount, plural(a.FileCount)))
	}
	if note := strings.Join(a.Notes, "; "); note != "" {
		parts = append(parts, note)
	}
	return strings.Join(parts, " · ")
}

func maxInt(a, b int) int {
	if a > b {
		return a
	}
	return b
}

func plural(n int) string {
	if n == 1 {
		return ""
	}
	return "s"
}

// journalActions is what can be done to a record, in the order the menu shows
// them. Nothing here deletes audio: forgetting a record leaves every file where
// it is, and un-filing moves the masters back rather than copying and deleting
// them, so an interruption can only leave the album in one place or the other.
func (m Module) journalActions() []journalAction {
	return []journalAction{
		{
			key:   "forget",
			label: "forget it (leave every file where it is)",
			run: func(m *Module, e JournalEntry) tea.Cmd {
				if m.opts.ForgetSource == nil {
					return nil
				}
				src := e.Source
				return func() tea.Msg {
					if err := m.opts.ForgetSource(src); err != nil {
						return journalDoneMsg{err: err}
					}
					return journalDoneMsg{note: "forgotten; the files are untouched"}
				}
			},
		},
		{
			key:     "unfile",
			label:   "move the masters back to staging (removes the library copy)",
			confirm: "press enter again to move it, or esc to cancel",
			run: func(m *Module, e JournalEntry) tea.Cmd {
				if m.opts.UnfileSource == nil {
					return nil
				}
				src := e.Source
				return func() tea.Msg {
					var last string
					err := m.opts.UnfileSource(src, func(line string) { last = line })
					if err != nil {
						return journalDoneMsg{err: err}
					}
					if last == "" {
						last = "moved back to staging"
					}
					return journalDoneMsg{note: last}
				}
			},
		},
	}
}

func (m Module) keyJournal(keyp string) (tea.Model, tea.Cmd) {
	if m.journalMenu >= 0 {
		return m.keyJournalMenu(keyp)
	}
	switch keyp {
	case "q":
		return m.quit()
	case "esc":
		m.screen, m.scroll = ScreenBrowse, 0
		return m, nil
	case "up", "k":
		if m.journalCursor > 0 {
			m.journalCursor--
		}
	case "down", "j":
		if m.journalCursor < len(m.journal)-1 {
			m.journalCursor++
		}
	case "home", "g":
		m.journalCursor = 0
	case "end", "G":
		m.journalCursor = len(m.journal) - 1
	case "r":
		m.status = "refreshing"
		return m, m.journalCmd()
	case "enter":
		if m.journalCursor >= len(m.journal) {
			return m, nil
		}
		if m.journal[m.journalCursor].Unrecorded {
			// There is no record here to forget or to move: the album is on disk
			// and the journal knows nothing about it. Saying so is more useful
			// than offering actions that cannot work.
			m.status = m.journal[m.journalCursor].Album + ": in the archive with no record, so there is nothing to forget or un-file"
			return m, nil
		}
		m.journalMenu, m.journalAction, m.journalConfirm = 0, 0, false
	}
	return m, nil
}

func (m Module) keyJournalMenu(keyp string) (tea.Model, tea.Cmd) {
	actions := m.journalActions()
	if m.journalCursor >= len(m.journal) {
		m.journalMenu = -1
		return m, nil
	}
	switch keyp {
	case "esc", "q":
		m.journalMenu, m.journalAction, m.journalConfirm = -1, 0, false
		m.batch, m.existing, m.collision, m.restored = nil, nil, "", false
		return m, nil
	case "up", "k":
		if m.journalAction > 0 {
			m.journalAction--
		}
		return m, nil
	case "down", "j":
		if m.journalAction < len(actions)-1 {
			m.journalAction++
		}
		return m, nil
	case "enter":
		a := actions[m.journalAction]
		if a.confirm != "" && !m.journalConfirm {
			// One key to choose, one to confirm. This is the only place in the
			// interface that moves an album already in the archive, so it does
			// not happen on a single stray keypress.
			m.journalConfirm = true
			m.status = a.confirm
			return m, nil
		}
		return m.runJournalAction(a)
	}
	return m, nil
}

func (m Module) runJournalAction(a journalAction) (tea.Model, tea.Cmd) {
	entry := m.journal[m.journalCursor]
	m.journalMenu, m.journalAction, m.journalConfirm = -1, 0, false
	m.batch, m.existing, m.collision, m.restored = nil, nil, "", false
	return m, a.run(&m, entry)
}