gwx 2.2.1

A friendly git worktree manager with automatic paths, hooks and shell integration
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
//! Interactive worktree picker.
//!
//! The picker draws to `/dev/tty` rather than stdout, because stdout is how the
//! chosen path gets back to the shell function that performs the `cd` — it is
//! captured in a command substitution and never reaches the terminal.

use std::fs::File;
use std::io::Write;
use std::path::PathBuf;

use anyhow::{Context, Result};
use crossterm::event::{self, Event, KeyCode, KeyEvent, KeyEventKind, KeyModifiers};
use crossterm::{cursor, queue, style, terminal};

use crate::commands::clean::{
    Candidate, NAME_HEADER as CLEAN_NAME_HEADER, NOTE_HEADER, VERDICT_HEADER,
};
use crate::commands::remove::{removal_blocker, remove_worktree, RemoveOptions};
use crate::git::{self, Worktree};
use crate::repo::Repo;

/// How the picker ended.
pub enum Outcome {
    /// Move to this worktree.
    Selected(PathBuf),
    /// Nothing was chosen; the shell should stay where it is.
    Cancelled,
}

/// `true` when a terminal is available to draw on.
pub fn is_available() -> bool {
    open_tty().is_ok()
}

fn open_tty() -> Result<File> {
    File::options()
        .write(true)
        .open("/dev/tty")
        .context("no terminal available")
}

/// Whether a command should open the picker instead of printing.
///
/// Without a terminal — a script, a pipeline, CI — the caller keeps its plain
/// behaviour. A repository whose only worktree is the main one has nothing
/// worth picking from either.
pub fn should_pick(repo: &Repo) -> Result<bool> {
    if !is_available() {
        return Ok(false);
    }
    Ok(repo.worktrees()?.len() > 1)
}

/// One row of the list.
struct Item {
    name: String,
    path: PathBuf,
    head: String,
    /// Empty until [`StatusFeed`] reports back.
    note: String,
    is_current: bool,
    is_main: bool,
    worktree: Worktree,
}

/// What a Backspace did, since the key has two jobs.
#[derive(Debug, PartialEq, Eq)]
enum Backspace {
    /// Removed a character from the filter.
    ErasedFilter,
    /// Swallowed, because the filter had only just been emptied.
    Absorbed,
    /// Asked to remove the selected worktree.
    Delete,
}

/// List state: which rows exist, what was typed, where the cursor is.
struct Picker {
    items: Vec<Item>,
    filter: String,
    /// Index into the *filtered* list.
    cursor: usize,
    /// First visible row, for lists taller than the terminal.
    offset: usize,
    /// Set while Backspace is being used to erase the filter.
    ///
    /// Holding the key to clear what you typed sends a burst of Backspaces;
    /// without this, the burst would run past the empty filter and open the
    /// delete dialog. One press is swallowed at the boundary, so reaching the
    /// dialog always takes a deliberate keystroke.
    erasing: bool,
}

impl Picker {
    fn new(items: Vec<Item>) -> Self {
        Self {
            items,
            filter: String::new(),
            cursor: 0,
            offset: 0,
            erasing: false,
        }
    }

    /// Resolves what Backspace means right now.
    fn backspace(&mut self) -> Backspace {
        if !self.filter.is_empty() {
            self.pop_filter();
            self.erasing = true;
            return Backspace::ErasedFilter;
        }
        if self.erasing {
            self.erasing = false;
            return Backspace::Absorbed;
        }
        Backspace::Delete
    }

    /// Any other key ends the erasing streak.
    fn note_other_key(&mut self) {
        self.erasing = false;
    }

    /// Indices of the items matching the filter.
    ///
    /// Matching is a case-insensitive substring test over the name and the
    /// path, which is what makes typing a fragment of a branch name work.
    fn matches(&self) -> Vec<usize> {
        if self.filter.is_empty() {
            return (0..self.items.len()).collect();
        }
        let needle = self.filter.to_lowercase();
        self.items
            .iter()
            .enumerate()
            .filter(|(_, item)| {
                item.name.to_lowercase().contains(&needle)
                    || item.path.to_string_lossy().to_lowercase().contains(&needle)
            })
            .map(|(i, _)| i)
            .collect()
    }

    fn selected(&self) -> Option<&Item> {
        self.matches().get(self.cursor).map(|&i| &self.items[i])
    }

    fn move_down(&mut self) {
        let len = self.matches().len();
        if len > 0 {
            self.cursor = (self.cursor + 1) % len;
        }
    }

    fn move_up(&mut self) {
        let len = self.matches().len();
        if len > 0 {
            self.cursor = (self.cursor + len - 1) % len;
        }
    }

    fn push_filter(&mut self, c: char) {
        self.filter.push(c);
        self.clamp();
    }

    fn pop_filter(&mut self) {
        self.filter.pop();
        self.clamp();
    }

    /// Keeps the cursor inside the filtered list after it shrinks or grows.
    fn clamp(&mut self) {
        let len = self.matches().len();
        if len == 0 {
            self.cursor = 0;
        } else if self.cursor >= len {
            self.cursor = len - 1;
        }
    }

    /// Scrolls so the cursor stays visible in a window of `height` rows.
    fn scroll_into_view(&mut self, height: usize) {
        if height == 0 {
            return;
        }
        if self.cursor < self.offset {
            self.offset = self.cursor;
        } else if self.cursor >= self.offset + height {
            self.offset = self.cursor + 1 - height;
        }
    }
}

/// Restores the terminal no matter how the picker exits, including on panic.
struct Screen {
    tty: File,
}

impl Screen {
    fn open() -> Result<Self> {
        let mut tty = open_tty()?;
        terminal::enable_raw_mode().context("failed to switch the terminal to raw mode")?;
        queue!(tty, terminal::EnterAlternateScreen, cursor::Hide)?;
        tty.flush()?;
        Ok(Self { tty })
    }
}

impl Drop for Screen {
    fn drop(&mut self) {
        let _ = queue!(self.tty, cursor::Show, terminal::LeaveAlternateScreen);
        let _ = self.tty.flush();
        let _ = terminal::disable_raw_mode();
    }
}

/// How often the loop looks for status results while they are still coming.
const STATUS_POLL: std::time::Duration = std::time::Duration::from_millis(60);

/// Runs the picker until the user chooses a worktree or gives up.
pub fn pick(repo: &Repo) -> Result<Outcome> {
    let mut screen = Screen::open()?;
    let mut picker = Picker::new(load(repo)?);
    let mut status = StatusFeed::spawn(repo, &picker.items);
    let mut message: Option<String> = None;

    loop {
        draw(&mut screen.tty, &mut picker, message.as_deref())?;

        let Some(key) = next_key(&mut picker, &mut status)? else {
            // Status arrived rather than a key; show it.
            continue;
        };
        // Windows reports both press and release; act on press only.
        if key.kind != KeyEventKind::Press {
            continue;
        }
        message = None;

        let action = key_action(&key);
        if action != Action::Backspace {
            picker.note_other_key();
        }

        match action {
            Action::Cancel => return Ok(Outcome::Cancelled),
            Action::Confirm => {
                if let Some(item) = picker.selected() {
                    return Ok(Outcome::Selected(item.path.clone()));
                }
            }
            Action::Down => picker.move_down(),
            Action::Up => picker.move_up(),
            Action::Backspace => {
                if picker.backspace() == Backspace::Delete {
                    message = delete_selected(repo, &mut screen.tty, &mut picker)?;
                    status = StatusFeed::spawn(repo, &picker.items);
                }
            }
            Action::Insert(c) => picker.push_filter(c),
            Action::Delete => {
                message = delete_selected(repo, &mut screen.tty, &mut picker)?;
                status = StatusFeed::spawn(repo, &picker.items);
            }
            Action::None => {}
        }
    }
}

/// Waits for a key, redrawing when the status column fills in first.
///
/// Returns `None` when something other than a key needs the screen redrawn.
fn next_key(picker: &mut Picker, status: &mut StatusFeed) -> Result<Option<KeyEvent>> {
    loop {
        if status.is_done() {
            // Nothing left to wait for, so stop waking up to check.
            return Ok(match event::read()? {
                Event::Key(key) => Some(key),
                _ => None,
            });
        }
        if event::poll(STATUS_POLL)? {
            return Ok(match event::read()? {
                Event::Key(key) => Some(key),
                _ => None,
            });
        }
        if status.drain(&mut picker.items) {
            return Ok(None);
        }
    }
}

/// Opens the confirmation dialog and removes the worktree if confirmed.
///
/// Returns the line to show in the status area.
fn delete_selected(repo: &Repo, tty: &mut File, picker: &mut Picker) -> Result<Option<String>> {
    let Some(item) = picker.selected() else {
        return Ok(None);
    };
    let worktree = item.worktree.clone();
    let label = item.name.clone();

    if let Some(reason) = removal_blocker(repo, &worktree, true) {
        return Ok(Some(format!("cannot remove `{label}`: {reason}")));
    }
    let dirty = crate::git::is_dirty(&worktree.path).unwrap_or(false);
    let merged = worktree
        .branch
        .as_deref()
        .map(|b| crate::git::is_merged(&repo.main, b).unwrap_or(false))
        .unwrap_or(false);

    let Some(with_branch) = confirm(tty, &worktree, dirty, merged)? else {
        return Ok(None);
    };

    // Removing can take a moment. Say so before starting, or the dialog just
    // sits there after the answer and invites a second, harder press.
    working(tty, &format!("Removing {}...", worktree.path.display()))?;

    let opts = RemoveOptions {
        // The dialog already spelled out the risk, so honour the answer.
        force: true,
        with_branch,
        quiet: true,
        // Deleting from the picker is still deleting: a hook that tears down
        // containers or caches has to run whichever way it was asked for.
        no_hooks: false,
    };
    let outcome = match remove_worktree(repo, &worktree, opts) {
        Ok(()) => {
            picker.items = load(repo)?;
            picker.clamp();
            let extra = if with_branch { " and its branch" } else { "" };
            Some(format!("removed `{label}`{extra}"))
        }
        // `{e:#}` rather than `{e}`: the top of the chain says only which hook
        // failed, and what it printed before giving up is further down. The
        // line is cut to the terminal width anyway.
        Err(e) => Some(format!("failed to remove `{label}`: {e:#}")),
    };

    // Anything typed while git was working was meant for the dialog, not for
    // the list that is about to replace it.
    discard_pending_input()?;
    Ok(outcome)
}

/// Clears the screen and states what is happening, for work that blocks.
fn working(tty: &mut File, what: &str) -> Result<()> {
    let (_, rows) = terminal::size()?;
    queue!(
        tty,
        terminal::Clear(terminal::ClearType::All),
        cursor::MoveTo(0, 0),
        style::Print(what),
        cursor::MoveTo(0, rows.saturating_sub(1)),
    )?;
    tty.flush()?;
    Ok(())
}

fn discard_pending_input() -> Result<()> {
    while event::poll(std::time::Duration::from_millis(0))? {
        let _ = event::read()?;
    }
    Ok(())
}

/// Asks for confirmation. `None` means cancelled, `Some(with_branch)` confirms.
fn confirm(tty: &mut File, worktree: &Worktree, dirty: bool, merged: bool) -> Result<Option<bool>> {
    let branch = worktree.branch.clone();
    loop {
        let (_, rows) = terminal::size()?;
        queue!(
            tty,
            terminal::Clear(terminal::ClearType::All),
            cursor::MoveTo(0, 0),
            style::Print("Remove this worktree?"),
            cursor::MoveTo(0, 2),
            style::Print(format!("  {}", worktree.path.display())),
        )?;

        let mut row = 4;
        if dirty {
            queue!(
                tty,
                cursor::MoveTo(0, row),
                style::Print("  ! uncommitted changes will be lost"),
            )?;
            row += 1;
        }
        if let Some(b) = &branch {
            let state = if merged { "merged" } else { "NOT merged" };
            queue!(
                tty,
                cursor::MoveTo(0, row),
                style::Print(format!("  branch `{b}` ({state})")),
            )?;
        }

        let keys = match &branch {
            Some(_) => "[y] remove worktree   [b] remove worktree and branch   [n] cancel",
            None => "[y] remove worktree   [n] cancel",
        };
        queue!(
            tty,
            cursor::MoveTo(0, rows.saturating_sub(1)),
            style::Print(keys)
        )?;
        tty.flush()?;

        let Event::Key(key) = event::read()? else {
            continue;
        };
        if key.kind != KeyEventKind::Press {
            continue;
        }
        match key.code {
            KeyCode::Char('y') | KeyCode::Char('Y') => return Ok(Some(false)),
            KeyCode::Char('b') | KeyCode::Char('B') if branch.is_some() => return Ok(Some(true)),
            KeyCode::Char('n') | KeyCode::Char('N') | KeyCode::Esc => return Ok(None),
            KeyCode::Char('c') if key.modifiers.contains(KeyModifiers::CONTROL) => return Ok(None),
            _ => {}
        }
    }
}

#[derive(Debug, PartialEq, Eq)]
enum Action {
    Confirm,
    Cancel,
    Up,
    Down,
    Delete,
    Backspace,
    Insert(char),
    None,
}

/// Maps a key to an action.
///
/// `Delete` is bound to Ctrl-D as well: on Mac keyboards the key labelled
/// "delete" sends Backspace, which the filter needs.
fn key_action(key: &KeyEvent) -> Action {
    let ctrl = key.modifiers.contains(KeyModifiers::CONTROL);
    match key.code {
        KeyCode::Enter => Action::Confirm,
        KeyCode::Esc => Action::Cancel,
        KeyCode::Char('c') | KeyCode::Char('g') if ctrl => Action::Cancel,
        KeyCode::Down => Action::Down,
        KeyCode::Up => Action::Up,
        KeyCode::Char('n') if ctrl => Action::Down,
        KeyCode::Char('p') if ctrl => Action::Up,
        KeyCode::Char('d') if ctrl => Action::Delete,
        KeyCode::Delete => Action::Delete,
        KeyCode::Backspace => Action::Backspace,
        // Terminals told to send 0x08 for the erase key surface it as Ctrl-H.
        KeyCode::Char('h') if ctrl => Action::Backspace,
        KeyCode::Char(c) if !ctrl => Action::Insert(c),
        _ => Action::None,
    }
}

/// Builds the rows without asking git anything slow.
///
/// The status column is left empty here and filled in by [`StatusFeed`]: it
/// costs a `git status` per worktree, which is fast in a small repository and
/// seconds across ten of them. The list has to be on screen before that.
fn load(repo: &Repo) -> Result<Vec<Item>> {
    let worktrees = repo.worktrees()?;
    let Some(main) = worktrees.first().cloned() else {
        return Ok(Vec::new());
    };

    Ok(worktrees
        .into_iter()
        .map(|wt| Item {
            name: repo.display_name(&wt, &main),
            head: wt.short_head(),
            note: String::new(),
            is_main: wt.path == main.path,
            is_current: repo.cwd.starts_with(&wt.path),
            path: wt.path.clone(),
            worktree: wt,
        })
        .collect())
}

/// Fills in the status column without blocking the list.
///
/// One background thread asks which branches are merged, then fans out a
/// thread per worktree for the `git status` calls, so ten worktrees cost about
/// as long as the slowest one rather than the sum of all ten.
struct StatusFeed {
    rx: Option<std::sync::mpsc::Receiver<Vec<(usize, String)>>>,
}

/// Everything a worker needs to describe one worktree, owned so it can move
/// to another thread.
struct Subject {
    index: usize,
    path: PathBuf,
    branch: Option<String>,
    is_main: bool,
    flags: Vec<&'static str>,
}

impl StatusFeed {
    fn spawn(repo: &Repo, items: &[Item]) -> Self {
        let (tx, rx) = std::sync::mpsc::channel();
        let main = repo.main.clone();
        let subjects: Vec<Subject> = items
            .iter()
            .enumerate()
            .map(|(index, item)| Subject {
                index,
                path: item.path.clone(),
                branch: item.worktree.branch.clone(),
                is_main: item.is_main,
                flags: flags(&item.worktree),
            })
            .collect();

        std::thread::spawn(move || {
            let merged = git::merged_branches(&main).unwrap_or_default();
            let mut workers = Vec::new();
            for subject in subjects {
                let merged = merged.clone();
                workers.push(std::thread::spawn(move || {
                    let note = note(
                        &subject.path,
                        subject.branch.as_deref(),
                        &merged,
                        subject.is_main,
                        &subject.flags,
                    );
                    (subject.index, note)
                }));
            }
            let notes = workers.into_iter().filter_map(|w| w.join().ok()).collect();
            // The receiver is gone once the picker closes; nothing to report.
            let _ = tx.send(notes);
        });

        Self { rx: Some(rx) }
    }

    /// Applies whatever has arrived. `true` when the screen needs redrawing.
    fn drain(&mut self, items: &mut [Item]) -> bool {
        use std::sync::mpsc::TryRecvError;
        let Some(rx) = &self.rx else {
            return false;
        };
        match rx.try_recv() {
            Ok(notes) => {
                for (index, note) in notes {
                    if let Some(item) = items.get_mut(index) {
                        item.note = note;
                    }
                }
                self.rx = None;
                true
            }
            Err(TryRecvError::Empty) => false,
            Err(TryRecvError::Disconnected) => {
                self.rx = None;
                false
            }
        }
    }

    /// Once nothing more is coming, the loop can go back to blocking on keys.
    fn is_done(&self) -> bool {
        self.rx.is_none()
    }
}

/// What is worth knowing about a worktree before acting on it.
///
/// The path used to sit here, but it is derived from the branch name for every
/// worktree gwx creates, so it repeated what the name already said. What is
/// missing at a glance is whether removing this one would lose anything.
///
/// "merged" is skipped for the main worktree: a branch is always merged into
/// itself, and the main worktree cannot be removed anyway.
fn note(
    path: &std::path::Path,
    branch: Option<&str>,
    merged: &[String],
    is_main: bool,
    flags: &[&str],
) -> String {
    let mut notes: Vec<String> = Vec::new();

    if git::is_dirty(path).unwrap_or(false) {
        notes.push("dirty".to_string());
    }
    if !is_main {
        if let Some(branch) = branch {
            if merged.iter().any(|b| b == branch) {
                notes.push("merged".to_string());
            }
        }
    }
    notes.extend(flags.iter().map(|f| f.to_string()));
    notes.join(", ")
}

fn flags(wt: &Worktree) -> Vec<&'static str> {
    let mut notes = Vec::new();
    if wt.bare {
        notes.push("bare");
    }
    if wt.detached {
        notes.push("detached");
    }
    if wt.locked {
        notes.push("locked");
    }
    notes
}

/// One entry of the help line: the key, then what it does.
///
/// The key is drawn in reverse video, which gives the boundary between key and
/// label a shape rather than leaving it to whitespace. It reuses the attribute
/// the cursor row already uses, so it holds up on any colour theme.
struct Hint {
    keys: &'static [&'static str],
    label: &'static str,
    /// Dropped first when the terminal is too narrow for the full line.
    optional: bool,
}

/// Keys are spelled out: `bksp` is keycap shorthand, not something a help line
/// can assume its reader knows.
///
/// Everything here is plain ASCII on purpose — arrows and the return symbol
/// land in ranges that terminals disagree about, and U+25B6 in particular has
/// an emoji presentation that renders double width and shifts the whole row.
const HINTS_IDLE: &[Hint] = &[
    Hint {
        keys: &["up/down"],
        label: "move",
        optional: true,
    },
    Hint {
        keys: &["enter"],
        label: "cd",
        optional: false,
    },
    Hint {
        keys: &["ctrl-d", "backspace"],
        label: "delete",
        optional: false,
    },
    Hint {
        keys: &["esc"],
        label: "cancel",
        optional: false,
    },
];

/// While a filter is typed, Backspace edits it instead of deleting.
const HINTS_FILTERING: &[Hint] = &[
    Hint {
        keys: &["up/down"],
        label: "move",
        optional: true,
    },
    Hint {
        keys: &["enter"],
        label: "cd",
        optional: false,
    },
    Hint {
        keys: &["ctrl-d"],
        label: "delete",
        optional: false,
    },
    Hint {
        keys: &["backspace"],
        label: "erase",
        optional: false,
    },
    Hint {
        keys: &["esc"],
        label: "cancel",
        optional: false,
    },
];

/// The hints matching what Backspace does right now.
fn hints_for(picker: &Picker) -> &'static [Hint] {
    if picker.filter.is_empty() {
        HINTS_IDLE
    } else {
        HINTS_FILTERING
    }
}

/// Columns a set of hints needs, badges included.
fn hints_width(hints: &[&Hint]) -> usize {
    hints
        .iter()
        .map(|h| {
            // Each key sits in a badge padded by one space on either side.
            let keys: usize = h.keys.iter().map(|k| k.chars().count() + 2).sum();
            let between_keys = h.keys.len() - 1;
            keys + between_keys + 1 + h.label.chars().count()
        })
        .sum::<usize>()
        + hints.len().saturating_sub(1) * 2
}

/// Drops optional hints until the line fits, rather than truncating mid-word.
fn hints_that_fit(hints: &'static [Hint], width: usize) -> Vec<&'static Hint> {
    let mut kept: Vec<&Hint> = hints.iter().collect();
    while hints_width(&kept) > width && kept.iter().any(|h| h.optional) {
        let index = kept.iter().position(|h| h.optional).unwrap();
        kept.remove(index);
    }
    kept
}

/// Truncates to `width` and pads to it, so a highlighted row spans the line.
fn fit(line: &str, width: usize) -> String {
    let mut out: String = line.chars().take(width).collect();
    let len = out.chars().count();
    if len < width {
        out.extend(std::iter::repeat_n(' ', width - len));
    }
    out
}

const PLACEHOLDER: &str = "type to filter";

/// Column labels. `HEAD` keeps git's own name for the short commit id.
const NAME_HEADER: &str = "WORKTREE";
const HEAD_HEADER: &str = "HEAD   ";
const STATUS_HEADER: &str = "STATUS";

/// What the right of the prompt says about the list below it.
///
/// While filtering, this is the only place the size of the list is stated —
/// without it, typing something that matches nothing just empties the screen
/// with no explanation.
fn count_label(filter: &str, matched: usize, total: usize) -> String {
    if !filter.is_empty() {
        return format!("{matched} of {total}");
    }
    match total {
        1 => "1 worktree".to_string(),
        n => format!("{n} worktrees"),
    }
}

/// Draws the filter line: prompt, what has been typed, a block cursor, and —
/// while nothing is typed — what typing would do.
///
/// The terminal's own cursor is hidden for the whole picker, so the block is
/// drawn by hand as a space in reverse video. Without it, an empty filter line
/// is a lone `>` that gives no sign it accepts input.
fn draw_prompt(tty: &mut File, picker: &Picker, matched: usize, cols: usize) -> Result<()> {
    let placeholder = if picker.filter.is_empty() {
        PLACEHOLDER
    } else {
        ""
    };
    let count = count_label(&picker.filter, matched, picker.items.len());
    let left = 2 + picker.filter.chars().count() + 1 + placeholder.chars().count();

    queue!(tty, style::Print("> "), style::Print(&picker.filter))?;
    queue!(
        tty,
        style::SetAttribute(style::Attribute::Reverse),
        style::Print(" "),
        style::SetAttribute(style::Attribute::Reset),
    )?;
    if !placeholder.is_empty() {
        queue!(
            tty,
            style::SetAttribute(style::Attribute::Dim),
            style::Print(placeholder),
            style::SetAttribute(style::Attribute::Reset),
        )?;
    }

    // Right-aligned, and dropped rather than wrapped when the line is full.
    let gap = cols.saturating_sub(left + count.chars().count());
    if gap > 0 {
        queue!(
            tty,
            style::Print(" ".repeat(gap)),
            style::SetAttribute(style::Attribute::Dim),
            style::Print(count),
            style::SetAttribute(style::Attribute::Reset),
        )?;
    }
    Ok(())
}

fn draw(tty: &mut File, picker: &mut Picker, message: Option<&str>) -> Result<()> {
    let (cols, rows) = terminal::size()?;
    let cols = cols as usize;
    // The prompt, the header, the help line, and any message.
    let reserved = if message.is_some() { 4 } else { 3 };
    let height = (rows as usize).saturating_sub(reserved);
    picker.scroll_into_view(height);

    let matches = picker.matches();
    let name_width = matches
        .iter()
        .map(|&i| picker.items[i].name.chars().count())
        .max()
        .unwrap_or(0)
        // Never narrower than the column it is labelled with.
        .max(NAME_HEADER.len());

    // The table starts at the top, so the header sits against the rows it
    // labels; the filter joins the help line at the bottom, where the things
    // you operate live. Butting the filter against the header, as this used
    // to, made the two read as one block of chrome.
    //
    // Bold and underlined, not dimmed. Dim is what the filter placeholder
    // uses, and a header that shares it reads as another piece of hint text
    // instead of the structure of the table. The underline runs the full width
    // so it doubles as the rule between the header and the rows; carrying both
    // attributes means the header still stands out on a terminal that renders
    // only one of them.
    let header = fit(
        &format!("  {NAME_HEADER:<name_width$}  {HEAD_HEADER}  {STATUS_HEADER}"),
        cols,
    );
    queue!(
        tty,
        terminal::Clear(terminal::ClearType::All),
        cursor::MoveTo(0, 0),
        style::SetAttribute(style::Attribute::Bold),
        style::SetAttribute(style::Attribute::Underlined),
        style::Print(header),
        style::SetAttribute(style::Attribute::Reset),
    )?;

    for (row, &index) in matches.iter().skip(picker.offset).take(height).enumerate() {
        let item = &picker.items[index];
        let is_cursor = picker.offset + row == picker.cursor;
        // The marker column says "you are standing here"; the cursor is the
        // highlight, so the two never compete for the same character.
        let marker = if item.is_current { "*" } else { " " };
        let line = format!(
            "{marker} {:<name_width$}  {}  {}",
            item.name, item.head, item.note
        );
        let line = fit(&line, cols);
        queue!(tty, cursor::MoveTo(0, row as u16 + 1))?;
        if is_cursor {
            // Reverse video rather than a colour: it stays readable on any
            // theme, and highlights the row edge to edge.
            queue!(
                tty,
                style::SetAttribute(style::Attribute::Reverse),
                style::Print(line),
                style::SetAttribute(style::Attribute::Reset),
            )?;
        } else {
            queue!(tty, style::Print(line))?;
        }
    }

    if let Some(message) = message {
        queue!(
            tty,
            cursor::MoveTo(0, rows.saturating_sub(3)),
            style::Print(message.chars().take(cols).collect::<String>()),
        )?;
    }
    queue!(tty, cursor::MoveTo(0, rows.saturating_sub(2)))?;
    draw_prompt(tty, picker, matches.len(), cols)?;

    queue!(tty, cursor::MoveTo(0, rows.saturating_sub(1)))?;
    draw_hints(tty, &hints_that_fit(hints_for(picker), cols))?;
    tty.flush()?;
    Ok(())
}

/// Draws the help line, each key as a reverse-video badge.
fn draw_hints(tty: &mut File, hints: &[&Hint]) -> Result<()> {
    for (i, hint) in hints.iter().enumerate() {
        if i > 0 {
            queue!(tty, style::Print("  "))?;
        }
        for (k, key) in hint.keys.iter().enumerate() {
            if k > 0 {
                queue!(tty, style::Print(" "))?;
            }
            queue!(
                tty,
                style::SetAttribute(style::Attribute::Reverse),
                style::Print(format!(" {key} ")),
                style::SetAttribute(style::Attribute::Reset),
            )?;
        }
        queue!(tty, style::Print(format!(" {}", hint.label)))?;
    }
    Ok(())
}

/// The multi-select screen behind `gwx clean`.
///
/// Deliberately not the picker: that one exists to go somewhere and takes a
/// single choice, this one exists to remove things and takes many. Sharing one
/// widget would mean a mode flag threaded through every keystroke, and a list
/// where Enter sometimes moves you and sometimes deletes.
///
/// Returns the indices to remove, or `None` when the user backed out.
pub fn choose_to_clean(candidates: &[Candidate], with_branch: bool) -> Result<Option<Vec<usize>>> {
    let mut screen = Screen::open()?;
    let mut ticked: Vec<bool> = candidates.iter().map(|c| c.state.preselected()).collect();
    let mut cursor_at = 0usize;
    let last = candidates.len() - 1;

    loop {
        draw_clean(&mut screen.tty, candidates, &ticked, cursor_at, with_branch)?;

        let Event::Key(key) = event::read()? else {
            continue;
        };
        if key.kind != KeyEventKind::Press {
            continue;
        }
        let ctrl = key.modifiers.contains(KeyModifiers::CONTROL);
        match key.code {
            KeyCode::Esc => return Ok(None),
            KeyCode::Char('c') | KeyCode::Char('g') if ctrl => return Ok(None),
            KeyCode::Enter => {
                return Ok(Some(
                    ticked
                        .iter()
                        .enumerate()
                        .filter(|(_, on)| **on)
                        .map(|(i, _)| i)
                        .collect(),
                ))
            }
            KeyCode::Down => cursor_at = if cursor_at == last { 0 } else { cursor_at + 1 },
            KeyCode::Char('n') if ctrl => {
                cursor_at = if cursor_at == last { 0 } else { cursor_at + 1 }
            }
            KeyCode::Up => cursor_at = cursor_at.checked_sub(1).unwrap_or(last),
            KeyCode::Char('p') if ctrl => cursor_at = cursor_at.checked_sub(1).unwrap_or(last),
            KeyCode::Char(' ') => ticked[cursor_at] = !ticked[cursor_at],
            _ => {}
        }
    }
}

/// One `gwx clean` frame: header, rows, and the help line.
fn draw_clean(
    tty: &mut File,
    candidates: &[Candidate],
    ticked: &[bool],
    cursor_at: usize,
    with_branch: bool,
) -> Result<()> {
    let (cols, rows) = terminal::size()?;
    let cols = cols as usize;
    let verdicts: Vec<String> = candidates
        .iter()
        .map(|c| c.state.verdict(with_branch))
        .collect();
    let width = candidates
        .iter()
        .map(|c| c.name.chars().count())
        .max()
        .unwrap_or(0)
        .max(CLEAN_NAME_HEADER.len());
    let verdict_width = verdicts
        .iter()
        .map(|v| v.chars().count())
        .max()
        .unwrap_or(0)
        .max(VERDICT_HEADER.len());

    let header = format!(
        "      {CLEAN_NAME_HEADER:<width$}  {VERDICT_HEADER:<verdict_width$}  {NOTE_HEADER}"
    );
    queue!(
        tty,
        terminal::Clear(terminal::ClearType::All),
        cursor::MoveTo(0, 0),
        style::Print("Select worktrees to remove"),
        cursor::MoveTo(0, 2),
        style::SetAttribute(style::Attribute::Bold),
        style::SetAttribute(style::Attribute::Underlined),
        style::Print(fit(&header, cols)),
        style::SetAttribute(style::Attribute::Reset),
    )?;

    for (i, candidate) in candidates.iter().enumerate() {
        let row = 3 + i as u16;
        if row >= rows.saturating_sub(2) {
            break;
        }
        let line = format!(
            "{} [{}] {:<width$}  {:<verdict_width$}  {}",
            if i == cursor_at { ">" } else { " " },
            if ticked[i] { "x" } else { " " },
            candidate.name,
            verdicts[i],
            candidate.note,
        );
        queue!(tty, cursor::MoveTo(0, row))?;
        if i == cursor_at {
            queue!(
                tty,
                style::SetAttribute(style::Attribute::Reverse),
                style::Print(fit(&format!("{line:<cols$}"), cols)),
                style::SetAttribute(style::Attribute::Reset),
            )?;
        } else {
            queue!(tty, style::Print(fit(&line, cols)))?;
        }
    }

    let count = ticked.iter().filter(|on| **on).count();
    queue!(
        tty,
        cursor::MoveTo(0, rows.saturating_sub(2)),
        style::SetAttribute(style::Attribute::Dim),
        style::Print(fit(
            &format!("{count} of {} selected", candidates.len()),
            cols
        )),
        style::SetAttribute(style::Attribute::Reset),
        cursor::MoveTo(0, rows.saturating_sub(1)),
    )?;
    draw_hints(tty, &hints_that_fit(CLEAN_HINTS, cols))?;
    tty.flush()?;
    Ok(())
}

/// The help line for `gwx clean`.
///
/// Four keys, none of them optional: the line fits an eighty-column terminal
/// whole, so nothing here is a shortcut somebody has to be told about. Bulk
/// selection is deliberately absent — with a handful of worktrees, space is
/// enough, and the key that ticks everything is the one this screen should
/// think hardest before adding.
const CLEAN_HINTS: &[Hint] = &[
    Hint {
        keys: &["up/down"],
        label: "move",
        optional: false,
    },
    Hint {
        keys: &["space"],
        label: "toggle",
        optional: false,
    },
    Hint {
        keys: &["enter"],
        label: "remove",
        optional: false,
    },
    Hint {
        keys: &["esc"],
        label: "cancel",
        optional: false,
    },
];

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

    fn item(name: &str, path: &str) -> Item {
        Item {
            name: name.to_string(),
            path: PathBuf::from(path),
            head: "abc1234".to_string(),
            note: String::new(),
            is_current: false,
            is_main: false,
            worktree: Worktree {
                path: PathBuf::from(path),
                head: None,
                branch: Some(name.to_string()),
                bare: false,
                detached: false,
                locked: false,
            },
        }
    }

    fn picker() -> Picker {
        Picker::new(vec![
            item("@", "/repo"),
            item("feature/auth", "/wt/feature/auth"),
            item("feature/billing", "/wt/feature/billing"),
            item("hotfix", "/wt/hotfix"),
        ])
    }

    #[test]
    fn the_interface_is_ascii_only() {
        // Non-ASCII risks missing glyphs, and emoji-presentation characters
        // render double width and break the column alignment.
        for hint in HINTS_IDLE.iter().chain(HINTS_FILTERING) {
            for key in hint.keys {
                assert!(key.is_ascii(), "{key}");
            }
            assert!(hint.label.is_ascii(), "{}", hint.label);
        }
        for label in [
            "Remove this worktree?",
            "  ! uncommitted changes will be lost",
            "[y] remove worktree   [b] remove worktree and branch   [n] cancel",
        ] {
            assert!(label.is_ascii(), "{label}");
        }
    }

    #[test]
    fn keys_are_spelled_out() {
        // `^d` and `bksp` are shorthand a help line cannot assume its reader
        // knows, so both are written in full.
        let keys: Vec<&str> = HINTS_IDLE
            .iter()
            .chain(HINTS_FILTERING)
            .flat_map(|h| h.keys.iter().copied())
            .collect();
        assert!(keys.contains(&"ctrl-d"), "{keys:?}");
        assert!(keys.contains(&"backspace"), "{keys:?}");
        assert!(!keys.iter().any(|k| k.contains("bksp") || k.contains('^')));
    }

    #[test]
    fn the_help_says_what_backspace_will_do() {
        let mut p = picker();
        let idle = hints_for(&p);
        let delete = idle.iter().find(|h| h.label == "delete").unwrap();
        assert!(delete.keys.contains(&"backspace"), "{:?}", delete.keys);

        p.push_filter('a');
        let filtering = hints_for(&p);
        let erase = filtering.iter().find(|h| h.label == "erase").unwrap();
        assert_eq!(erase.keys, &["backspace"]);
        // While filtering, deleting is ctrl-d only.
        let delete = filtering.iter().find(|h| h.label == "delete").unwrap();
        assert_eq!(delete.keys, &["ctrl-d"]);
    }

    #[test]
    fn a_narrow_terminal_drops_optional_hints_instead_of_cutting_words() {
        let full = hints_width(&HINTS_IDLE.iter().collect::<Vec<_>>());
        assert_eq!(hints_that_fit(HINTS_IDLE, full).len(), HINTS_IDLE.len());

        // One column short: the optional "move" hint goes, the rest survive.
        let narrowed = hints_that_fit(HINTS_IDLE, full - 1);
        assert_eq!(narrowed.len(), HINTS_IDLE.len() - 1);
        assert!(!narrowed.iter().any(|h| h.label == "move"));
        assert!(narrowed.iter().any(|h| h.label == "cancel"));

        // Nothing optional left to drop: the required hints are kept.
        assert!(!hints_that_fit(HINTS_IDLE, 1).is_empty());
    }

    #[test]
    fn the_count_explains_an_empty_list() {
        // Nothing typed: the size of the list.
        assert_eq!(count_label("", 4, 4), "4 worktrees");
        assert_eq!(count_label("", 1, 1), "1 worktree");
        // Typing: how much of it survived, so filtering everything away reads
        // as `0 of 4` instead of an unexplained blank screen.
        assert_eq!(count_label("bill", 1, 4), "1 of 4");
        assert_eq!(count_label("zzz", 0, 4), "0 of 4");
    }

    #[test]
    fn the_column_headers_are_ascii() {
        for header in [NAME_HEADER, HEAD_HEADER, STATUS_HEADER] {
            assert!(header.is_ascii(), "{header}");
        }
    }

    #[test]
    fn rows_start_without_a_status() {
        // The list is drawn before git is asked anything slow, so the status
        // column starts empty and fills in later.
        let p = picker();
        assert!(p.items.iter().all(|i| i.note.is_empty()));
    }

    #[test]
    fn the_placeholder_is_ascii_and_says_what_typing_does() {
        assert!(PLACEHOLDER.is_ascii(), "{PLACEHOLDER}");
        assert!(PLACEHOLDER.contains("filter"), "{PLACEHOLDER}");
    }

    #[test]
    fn backspace_removes_a_worktree_when_nothing_is_typed() {
        let mut p = picker();
        assert_eq!(p.backspace(), Backspace::Delete);
    }

    #[test]
    fn backspace_edits_the_filter_while_there_is_one() {
        let mut p = picker();
        p.push_filter('a');
        p.push_filter('u');
        assert_eq!(p.backspace(), Backspace::ErasedFilter);
        assert_eq!(p.backspace(), Backspace::ErasedFilter);
        assert_eq!(p.filter, "");
    }

    #[test]
    fn holding_backspace_to_clear_cannot_run_into_a_deletion() {
        let mut p = picker();
        for c in "auth".chars() {
            p.push_filter(c);
        }
        // The burst that empties the filter.
        for _ in 0..4 {
            assert_eq!(p.backspace(), Backspace::ErasedFilter);
        }
        // The overshoot from key repeat is swallowed instead of deleting.
        assert_eq!(p.backspace(), Backspace::Absorbed);
        // A deliberate press after that still works.
        assert_eq!(p.backspace(), Backspace::Delete);
    }

    #[test]
    fn any_other_key_ends_the_erasing_streak() {
        let mut p = picker();
        p.push_filter('a');
        assert_eq!(p.backspace(), Backspace::ErasedFilter);
        // Moving the cursor means the next Backspace is a fresh intent.
        p.note_other_key();
        assert_eq!(p.backspace(), Backspace::Delete);
    }

    #[test]
    fn rows_are_padded_so_a_highlight_covers_the_line() {
        assert_eq!(fit("abc", 6), "abc   ");
        assert_eq!(fit("abcdefgh", 4), "abcd");
        assert_eq!(fit("", 3), "   ");
    }

    #[test]
    fn filter_matches_name_and_path_case_insensitively() {
        let mut p = picker();
        p.filter = "AUTH".to_string();
        assert_eq!(p.matches().len(), 1);
        assert_eq!(p.selected().unwrap().name, "feature/auth");

        p.filter = "feature".to_string();
        assert_eq!(p.matches().len(), 2);

        p.filter = "/wt/hot".to_string();
        assert_eq!(p.selected().unwrap().name, "hotfix");
    }

    #[test]
    fn cursor_wraps_around() {
        let mut p = picker();
        p.move_up();
        assert_eq!(p.selected().unwrap().name, "hotfix");
        p.move_down();
        assert_eq!(p.selected().unwrap().name, "@");
    }

    #[test]
    fn cursor_stays_inside_a_shrinking_list() {
        let mut p = picker();
        p.cursor = 3;
        for c in "feature".chars() {
            p.push_filter(c);
        }
        // Only the two `feature/*` rows remain, so the cursor cannot stay at 3.
        assert_eq!(p.matches().len(), 2);
        assert_eq!(p.cursor, 1);
        assert_eq!(p.selected().unwrap().name, "feature/billing");
    }

    #[test]
    fn no_match_leaves_nothing_selected() {
        let mut p = picker();
        p.filter = "zzz".to_string();
        p.clamp();
        assert!(p.selected().is_none());
    }

    #[test]
    fn backspace_restores_matches() {
        let mut p = picker();
        p.push_filter('z');
        assert!(p.selected().is_none());
        p.pop_filter();
        assert_eq!(p.matches().len(), 4);
    }

    #[test]
    fn scrolling_follows_the_cursor() {
        let mut p = picker();
        p.cursor = 3;
        p.scroll_into_view(2);
        assert_eq!(p.offset, 2);
        p.cursor = 0;
        p.scroll_into_view(2);
        assert_eq!(p.offset, 0);
    }

    #[test]
    fn delete_is_bound_to_both_delete_and_ctrl_d() {
        let del = KeyEvent::new(KeyCode::Delete, KeyModifiers::NONE);
        let ctrl_d = KeyEvent::new(KeyCode::Char('d'), KeyModifiers::CONTROL);
        assert!(matches!(key_action(&del), Action::Delete));
        assert!(matches!(key_action(&ctrl_d), Action::Delete));
        // Backspace must stay with the filter, not delete a worktree.
        let backspace = KeyEvent::new(KeyCode::Backspace, KeyModifiers::NONE);
        assert!(matches!(key_action(&backspace), Action::Backspace));
    }

    #[test]
    fn plain_characters_type_into_the_filter() {
        let a = KeyEvent::new(KeyCode::Char('a'), KeyModifiers::NONE);
        assert!(matches!(key_action(&a), Action::Insert('a')));
        // Ctrl-n/p navigate instead of typing.
        let ctrl_n = KeyEvent::new(KeyCode::Char('n'), KeyModifiers::CONTROL);
        assert!(matches!(key_action(&ctrl_n), Action::Down));
    }
}