ditto-cli 0.4.2

Switch between Claude Code, Codex, opencode, OMP, Prime Agent, Pi, and 29 more coding-agent accounts
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
use std::{
    collections::HashMap,
    env,
    path::Path,
    sync::mpsc::{self, Receiver, Sender},
    thread,
    time::Duration,
};

use anyhow::Result;
use crossterm::event::{self, Event, KeyCode, KeyEvent, KeyEventKind, KeyModifiers};
use ratatui::{
    DefaultTerminal, Frame,
    layout::{Alignment, Constraint, Flex, Layout, Rect},
    style::{Color, Modifier, Style},
    text::{Line, Span, Text},
    widgets::{Block, Borders, Clear, List, ListItem, ListState, Paragraph, Wrap},
};

use crate::{
    launch::{self, AuthOperation, AuthStatus, Tool},
    profile::{Profile, Store},
    settings, shared,
};

const DITTO_PURPLE: Color = Color::Rgb(190, 134, 255);
const CLAUDE_ORANGE: Color = Color::Rgb(222, 133, 93);
const CODEX_GREEN: Color = Color::Rgb(104, 201, 154);
const FX_MAGENTA: Color = Color::Rgb(236, 72, 153);
const OPENCODE_CYAN: Color = Color::Rgb(103, 199, 209);
const OMP_BLUE: Color = Color::Rgb(96, 165, 250);
const PRIME_PURPLE: Color = Color::Rgb(168, 85, 247);

/// Width reserved for the tool name so the status and path columns line up.
const TOOL_COLUMN: usize = 13;
const SPINNER: [&str; 8] = ["", "", "", "", "", "", "", ""];
/// How long the loop waits for input before looking for finished probes.
const TICK: Duration = Duration::from_millis(110);
/// Below this the panes cannot show a usable amount of the profile.
const MINIMUM_WIDTH: u16 = 68;
const MINIMUM_HEIGHT: u16 = 26;
/// The width at which every profile shortcut fits on one footer row.
const WIDE_FOOTER_WIDTH: u16 = if cfg!(target_os = "macos") { 101 } else { 88 };
/// Marks the profile that commands use when they omit a profile name.
const DEFAULT_MARK: &str = "";

/// A footer entry: the key, what it does, and the colour of the key.
type Shortcut = (&'static str, &'static str, Color);

const SELECT: Shortcut = ("↑↓", "select", DITTO_PURPLE);
const NEW: Shortcut = ("n", "new", Color::Gray);
const RENAME: Shortcut = ("e", "rename", Color::Gray);
const DEFAULT: Shortcut = ("d", "default", Color::Gray);
const SIGN_IN: Shortcut = ("l", "sign in", Color::Gray);
const SIGN_OUT: Shortcut = ("L", "sign out", Color::Gray);
const REFRESH: Shortcut = ("r", "refresh", Color::Gray);
const QUIT: Shortcut = ("q", "quit", Color::Gray);

const LAUNCH_CLAUDE: Shortcut = ("c", "Claude Code", CLAUDE_ORANGE);
const LAUNCH_CODEX: Shortcut = ("x", "Codex", CODEX_GREEN);
#[cfg(target_os = "macos")]
const LAUNCH_CODEX_DESKTOP: Shortcut = ("X", "Codex Desktop", CODEX_GREEN);
const LAUNCH_FX: Shortcut = ("f", "fx", FX_MAGENTA);
const LAUNCH_OPENCODE: Shortcut = ("o", "opencode", OPENCODE_CYAN);
const LAUNCH_OMP: Shortcut = ("p", "OMP", OMP_BLUE);
const LAUNCH_PRIME: Shortcut = ("a", "Prime", PRIME_PURPLE);
const LAUNCH_PI: Shortcut = ("i", "Pi", DITTO_PURPLE);
const LAUNCH_MORE: Shortcut = ("", "any tool", DITTO_PURPLE);
const TOOL_SHORTCUTS: &[Shortcut] = &[
    LAUNCH_CLAUDE,
    LAUNCH_CODEX,
    LAUNCH_FX,
    LAUNCH_OPENCODE,
    LAUNCH_OMP,
    LAUNCH_PRIME,
    LAUNCH_PI,
    #[cfg(target_os = "macos")]
    LAUNCH_CODEX_DESKTOP,
    LAUNCH_MORE,
];
/// Split over two rows for a terminal too narrow to show every launch shortcut.
const NARROW_TOOL_ROWS: [&[Shortcut]; 2] = [
    &[LAUNCH_CLAUDE, LAUNCH_CODEX, LAUNCH_FX, LAUNCH_OPENCODE],
    &[
        LAUNCH_OMP,
        LAUNCH_PRIME,
        LAUNCH_PI,
        #[cfg(target_os = "macos")]
        LAUNCH_CODEX_DESKTOP,
        LAUNCH_MORE,
    ],
];
const AUTH_SHORTCUTS: [Shortcut; 5] = [
    ("c", "Claude Code", CLAUDE_ORANGE),
    ("x", "Codex", CODEX_GREEN),
    ("f", "fx", FX_MAGENTA),
    ("o", "opencode", OPENCODE_CYAN),
    ("a", "Prime Agent", PRIME_PURPLE),
];
const WIDE_SHORTCUT_ROW: [Shortcut; 8] = [
    SELECT, NEW, RENAME, DEFAULT, SIGN_IN, SIGN_OUT, REFRESH, QUIT,
];
const NARROW_SHORTCUT_ROWS: [&[Shortcut]; 2] = [
    &[SELECT, NEW, RENAME, DEFAULT],
    &[SIGN_IN, SIGN_OUT, REFRESH, QUIT],
];

pub enum UiAction {
    Launch {
        tool: Tool,
        profile: Profile,
    },
    #[cfg(target_os = "macos")]
    LaunchCodexDesktop {
        profile: Profile,
    },
    Authenticate {
        operation: AuthOperation,
        tool: Tool,
        profile: Profile,
    },
}

enum Mode {
    Browsing,
    Creating {
        input: String,
        error: Option<String>,
    },
    Renaming {
        original: String,
        input: String,
        error: Option<String>,
    },
    Notice {
        title: &'static str,
        message: String,
    },
    ChoosingTool {
        operation: AuthOperation,
    },
    /// Picking any installed tool to launch: too many for a letter each, so
    /// the list is typed into instead.
    Launching {
        filter: String,
        selected: usize,
    },
    ConfirmingLogout {
        tool: Tool,
    },
}

/// Sign-in state for one profile. `None` means the probe is still running, so
/// the interface can say "checking" instead of guessing.
#[derive(Clone, Copy)]
struct ProfileAuth {
    generation: u64,
    /// Indexed by [`Tool::index`]: a field per tool stopped being possible
    /// when tools became a table.
    statuses: [Option<AuthStatus>; Tool::ALL.len()],
}

impl Default for ProfileAuth {
    fn default() -> Self {
        Self {
            generation: 0,
            statuses: [None; Tool::ALL.len()],
        }
    }
}

impl ProfileAuth {
    fn get(&self, tool: Tool) -> Option<AuthStatus> {
        self.statuses[tool.index()]
    }

    fn set(&mut self, tool: Tool, status: AuthStatus) {
        self.statuses[tool.index()] = Some(status);
    }

    fn pending(&self) -> bool {
        Tool::ALL.iter().any(|tool| self.get(*tool).is_none())
    }
}

/// A finished sign-in probe. The generation lets a refresh discard answers
/// that were already in flight when it started.
struct Probe {
    profile: String,
    generation: u64,
    tool: Tool,
    status: AuthStatus,
}

/// Desktop and CLI launches use different authentication surfaces. Keeping the
/// target separate prevents a CLI login probe from labeling Desktop signed in.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
enum LaunchTarget {
    Cli(Tool),
    #[cfg(target_os = "macos")]
    CodexDesktop,
}

struct App<'a> {
    store: &'a Store,
    profiles: Vec<Profile>,
    selected: usize,
    mode: Mode,
    auth: HashMap<String, ProfileAuth>,
    generation: u64,
    sender: Sender<Probe>,
    receiver: Receiver<Probe>,
    spinner: usize,
    has_auth_environment: bool,
    default_profile: Option<String>,
    /// Discover launch targets once rather than probing the filesystem on redraw.
    installed: Vec<LaunchTarget>,
}

impl<'a> App<'a> {
    fn new(
        store: &'a Store,
        profiles: Vec<Profile>,
        initial_profile: Option<&str>,
        default_profile: Option<String>,
    ) -> Self {
        let selected = initial_profile
            .and_then(|name| profiles.iter().position(|profile| profile.name == name))
            .unwrap_or(0);
        let (sender, receiver) = mpsc::channel();
        let mut installed = Vec::new();
        for tool in Tool::ALL {
            if tool.installed() {
                installed.push(LaunchTarget::Cli(tool));
            }
            #[cfg(target_os = "macos")]
            if tool == Tool::Codex && launch::codex_desktop_app(store.user_home()).is_ok() {
                installed.push(LaunchTarget::CodexDesktop);
            }
        }
        let mut app = Self {
            store,
            profiles,
            selected,
            mode: Mode::Browsing,
            auth: HashMap::new(),
            generation: 0,
            sender,
            receiver,
            spinner: 0,
            has_auth_environment: auth_environment_is_set(),
            default_profile,
            installed,
        };
        app.probe_selected();
        app
    }

    fn selected_profile(&self) -> &Profile {
        &self.profiles[self.selected]
    }

    fn selected_auth(&self) -> ProfileAuth {
        self.auth
            .get(&self.selected_profile().name)
            .copied()
            .unwrap_or_default()
    }

    fn move_to(&mut self, index: usize) {
        let last = self.profiles.len().saturating_sub(1);
        self.selected = index.min(last);
        if !self.auth.contains_key(&self.selected_profile().name) {
            self.probe_selected();
        }
    }

    /// Asks each CLI about the selected profile on its own thread. The probes
    /// spawn other programs, so running them inline would freeze the list
    /// every time the cursor moves.
    fn probe_selected(&mut self) {
        let profile = self.selected_profile().clone();
        self.generation += 1;
        let generation = self.generation;
        self.auth.insert(
            profile.name.clone(),
            ProfileAuth {
                generation,
                ..ProfileAuth::default()
            },
        );

        for tool in Tool::ALL {
            let sender = self.sender.clone();
            let profile = profile.clone();
            thread::spawn(move || {
                let status = launch::auth_status(tool, &profile);
                let _ = sender.send(Probe {
                    profile: profile.name,
                    generation,
                    tool,
                    status,
                });
            });
        }
    }

    /// Collects finished probes. Returns whether anything on screen changed.
    fn collect_probes(&mut self) -> bool {
        let mut changed = false;
        while let Ok(probe) = self.receiver.try_recv() {
            if let Some(auth) = self.auth.get_mut(&probe.profile)
                && auth.generation == probe.generation
            {
                auth.set(probe.tool, probe.status);
                changed = true;
            }
        }
        changed
    }

    fn waiting_on_probes(&self) -> bool {
        self.selected_auth().pending()
    }

    fn handle_key(&mut self, key: KeyEvent) -> Result<Action> {
        if key.kind != KeyEventKind::Press {
            return Ok(Action::Continue);
        }

        if key.modifiers.contains(KeyModifiers::CONTROL) && key.code == KeyCode::Char('c') {
            return Ok(Action::Quit);
        }
        // Held modifiers arrive as ordinary characters, so without this Ctrl-H
        // would type an "h" into a name and Ctrl-C would launch Claude Code.
        // Shift is exempt: it is how the sign-out shortcut is typed.
        if key
            .modifiers
            .intersects(KeyModifiers::CONTROL | KeyModifiers::ALT)
        {
            return Ok(Action::Continue);
        }

        match &mut self.mode {
            Mode::Browsing => Ok(match key.code {
                KeyCode::Char('q') | KeyCode::Esc => Action::Quit,
                KeyCode::Up | KeyCode::Char('k') => {
                    self.move_to(self.selected.saturating_sub(1));
                    Action::Continue
                }
                KeyCode::Down | KeyCode::Char('j') => {
                    self.move_to(self.selected + 1);
                    Action::Continue
                }
                KeyCode::Home => {
                    self.move_to(0);
                    Action::Continue
                }
                KeyCode::End => {
                    self.move_to(usize::MAX);
                    Action::Continue
                }
                KeyCode::Char('n') => {
                    self.mode = Mode::Creating {
                        input: String::new(),
                        error: None,
                    };
                    Action::Continue
                }
                KeyCode::Char('e') => {
                    if self.selected_profile().managed {
                        self.mode = Mode::Renaming {
                            original: self.selected_profile().name.clone(),
                            input: String::new(),
                            error: None,
                        };
                    } else {
                        self.mode = Mode::Notice {
                            title: " Cannot rename ",
                            message: "The default profile represents your existing setup and cannot be renamed."
                                .to_owned(),
                        };
                    }
                    Action::Continue
                }
                KeyCode::Char('l') => {
                    self.mode = Mode::ChoosingTool {
                        operation: AuthOperation::Login,
                    };
                    Action::Continue
                }
                KeyCode::Char('L') => {
                    self.mode = Mode::ChoosingTool {
                        operation: AuthOperation::Logout,
                    };
                    Action::Continue
                }
                KeyCode::Char('d') => {
                    self.toggle_default();
                    Action::Continue
                }
                KeyCode::Char('r') => {
                    self.probe_selected();
                    Action::Continue
                }
                KeyCode::Char('c') => Action::Launch(Tool::Claude),
                KeyCode::Char('x') => Action::Launch(Tool::Codex),
                #[cfg(target_os = "macos")]
                KeyCode::Char('X') => Action::LaunchCodexDesktop,
                KeyCode::Char('f') => Action::Launch(Tool::Fx),
                KeyCode::Char('o') => Action::Launch(Tool::Opencode),
                KeyCode::Char('p') => Action::Launch(Tool::Omp),
                KeyCode::Char('a') => Action::Launch(Tool::PrimeAgent),
                KeyCode::Char('i') => Action::Launch(Tool::Pi),
                KeyCode::Enter | KeyCode::Char('t') => {
                    self.mode = Mode::Launching {
                        filter: String::new(),
                        selected: 0,
                    };
                    Action::Continue
                }
                _ => Action::Continue,
            }),
            Mode::Creating { input, error } => match key.code {
                KeyCode::Esc => {
                    self.mode = Mode::Browsing;
                    Ok(Action::Continue)
                }
                KeyCode::Enter => match self.store.create_profile(input) {
                    Ok(profile) => {
                        settings::seed(self.store, &profile);
                        shared::seed(self.store, &profile);
                        self.select_after_change(&profile.name)?;
                        Ok(Action::Continue)
                    }
                    Err(create_error) => {
                        *error = Some(create_error.to_string());
                        Ok(Action::Continue)
                    }
                },
                KeyCode::Backspace => {
                    input.pop();
                    *error = None;
                    Ok(Action::Continue)
                }
                KeyCode::Char(character) if input.len() < 32 => {
                    input.push(character);
                    *error = None;
                    Ok(Action::Continue)
                }
                _ => Ok(Action::Continue),
            },
            Mode::Renaming {
                original,
                input,
                error,
            } => match key.code {
                KeyCode::Esc => {
                    self.mode = Mode::Browsing;
                    Ok(Action::Continue)
                }
                KeyCode::Enter => {
                    let original = original.clone();
                    let signs_out = rename_signs_out(&self.auth, &original);
                    match self.store.rename_profile(&original, input) {
                        Ok(profile) => {
                            self.auth.remove(&original);
                            self.select_after_change(&profile.name)?;
                            if signs_out {
                                self.mode = Mode::Notice {
                                    title: " Claude Code signed out ",
                                    message: format!(
                                        "Claude Code ties its credentials to the profile \
                                         directory, which the rename moved. Press l to sign \
                                         '{}' back in.",
                                        profile.name
                                    ),
                                };
                            }
                            Ok(Action::Continue)
                        }
                        Err(rename_error) => {
                            if let Mode::Renaming { error, .. } = &mut self.mode {
                                *error = Some(rename_error.to_string());
                            }
                            Ok(Action::Continue)
                        }
                    }
                }
                KeyCode::Backspace => {
                    input.pop();
                    *error = None;
                    Ok(Action::Continue)
                }
                KeyCode::Char(character) if input.len() < 32 => {
                    input.push(character);
                    *error = None;
                    Ok(Action::Continue)
                }
                _ => Ok(Action::Continue),
            },
            Mode::Notice { .. } => match key.code {
                KeyCode::Enter | KeyCode::Esc | KeyCode::Char('q') => {
                    self.mode = Mode::Browsing;
                    Ok(Action::Continue)
                }
                _ => Ok(Action::Continue),
            },
            Mode::Launching { filter, selected } => {
                let candidates = launchable(&self.installed, filter);
                match key.code {
                    KeyCode::Esc => self.mode = Mode::Browsing,
                    KeyCode::Enter => {
                        if let Some(target) = candidates.get(*selected) {
                            return Ok(match target {
                                LaunchTarget::Cli(tool) => Action::Launch(*tool),
                                #[cfg(target_os = "macos")]
                                LaunchTarget::CodexDesktop => Action::LaunchCodexDesktop,
                            });
                        }
                    }
                    KeyCode::Up => *selected = selected.saturating_sub(1),
                    KeyCode::Down => {
                        *selected = (*selected + 1).min(candidates.len().saturating_sub(1));
                    }
                    KeyCode::Backspace => {
                        filter.pop();
                        *selected = 0;
                    }
                    // Letters narrow the list rather than launching, so the
                    // profile shortcuts cannot fire from inside it.
                    KeyCode::Char(character) if filter.len() < 32 => {
                        filter.push(character);
                        *selected = 0;
                    }
                    _ => {}
                }
                Ok(Action::Continue)
            }
            Mode::ChoosingTool { operation } => {
                let operation = *operation;
                let tool = match key.code {
                    KeyCode::Esc => {
                        self.mode = Mode::Browsing;
                        return Ok(Action::Continue);
                    }
                    KeyCode::Char('c') => Tool::Claude,
                    KeyCode::Char('x') => Tool::Codex,
                    KeyCode::Char('f') => Tool::Fx,
                    KeyCode::Char('o') => Tool::Opencode,
                    KeyCode::Char('a') => Tool::PrimeAgent,
                    _ => return Ok(Action::Continue),
                };
                if operation == AuthOperation::Logout {
                    self.mode = Mode::ConfirmingLogout { tool };
                    Ok(Action::Continue)
                } else {
                    Ok(Action::Authenticate { operation, tool })
                }
            }
            Mode::ConfirmingLogout { tool } => match key.code {
                KeyCode::Char('y') | KeyCode::Enter => Ok(Action::Authenticate {
                    operation: AuthOperation::Logout,
                    tool: *tool,
                }),
                KeyCode::Char('n') | KeyCode::Esc => {
                    self.mode = Mode::Browsing;
                    Ok(Action::Continue)
                }
                _ => Ok(Action::Continue),
            },
        }
    }

    fn is_default(&self, name: &str) -> bool {
        self.default_profile.as_deref() == Some(name)
    }

    /// Pins the selected profile so commands that omit a name use it, or
    /// releases it when it is already pinned. The state file is written now
    /// rather than on exit, so the pin survives a crash or a Ctrl-C.
    fn toggle_default(&mut self) {
        let name = self.selected_profile().name.clone();
        let pinned = (!self.is_default(&name)).then_some(name);

        match self.store.set_default_profile_name(pinned.as_deref()) {
            Ok(()) => self.default_profile = pinned,
            Err(error) => {
                self.mode = Mode::Notice {
                    title: " Cannot set default ",
                    message: format!("{error:#}"),
                };
            }
        }
    }

    /// Reloads the list after a create or rename and puts the cursor on the
    /// profile the change produced.
    fn select_after_change(&mut self, name: &str) -> Result<()> {
        self.profiles = self.store.list_profiles()?;
        // A rename rewrites the pin, so it is re-read rather than assumed.
        self.default_profile = self.store.default_profile_name()?;
        self.selected = self
            .profiles
            .iter()
            .position(|candidate| candidate.name == name)
            .unwrap_or(0);
        self.probe_selected();
        self.mode = Mode::Browsing;
        Ok(())
    }

    fn draw(&mut self, frame: &mut Frame) {
        let area = frame.area();
        if area.width < MINIMUM_WIDTH || area.height < MINIMUM_HEIGHT {
            self.draw_too_small(frame, area);
            return;
        }

        // The profile shortcuts need a second row before they would be cut off.
        let narrow = area.width < WIDE_FOOTER_WIDTH;
        // Narrow mode splits both the tools and the actions over two rows.
        let footer_height = 4 + 2 * u16::from(narrow) + u16::from(self.has_auth_environment);
        let sections = Layout::vertical([
            Constraint::Length(3),
            Constraint::Min(1),
            Constraint::Length(footer_height),
        ])
        .split(area);

        self.draw_header(frame, sections[0]);
        self.draw_profiles(frame, sections[1]);
        self.draw_footer(frame, sections[2], narrow);
        self.draw_modal(frame, area);
    }

    fn draw_too_small(&self, frame: &mut Frame, area: Rect) {
        let message = Paragraph::new(vec![
            Line::styled("Ditto CLI", Style::new().fg(DITTO_PURPLE).bold()),
            Line::default(),
            Line::raw(format!(
                "Resize to at least {MINIMUM_WIDTH}×{MINIMUM_HEIGHT}."
            )),
            Line::styled(
                format!("This terminal is {}×{}.", area.width, area.height),
                Style::new().fg(Color::DarkGray),
            ),
        ])
        .alignment(Alignment::Center)
        .wrap(Wrap { trim: true });
        frame.render_widget(Clear, area);
        frame.render_widget(message, centered_rect(90, 4.min(area.height), area));
    }

    fn draw_header(&self, frame: &mut Frame, area: Rect) {
        let header = Paragraph::new(Line::from(vec![
            Span::styled("Ditto CLI", Style::new().fg(DITTO_PURPLE).bold()),
            Span::styled(
                "  choose a profile, then Enter to pick a tool",
                Style::new().fg(Color::Gray),
            ),
        ]))
        .alignment(Alignment::Center)
        .block(Block::bordered().border_style(Style::new().fg(DITTO_PURPLE)));
        frame.render_widget(header, area);
    }

    fn draw_profiles(&self, frame: &mut Frame, area: Rect) {
        let columns = Layout::horizontal([Constraint::Length(26), Constraint::Min(30)]).split(area);
        let items = self.profiles.iter().map(|profile| {
            let suffix = if profile.managed { "" } else { "  existing" };
            let mark = if self.is_default(&profile.name) {
                format!("  {DEFAULT_MARK}")
            } else {
                String::new()
            };
            ListItem::new(Line::from(vec![
                Span::raw(&profile.name),
                Span::styled(suffix, Style::new().fg(Color::DarkGray)),
                Span::styled(mark, Style::new().fg(Color::Yellow)),
            ]))
        });
        let profile_list = List::new(items)
            .block(Block::new().title(" Profiles ").borders(Borders::ALL))
            .highlight_symbol("")
            .highlight_style(
                Style::new()
                    .fg(Color::Black)
                    .bg(DITTO_PURPLE)
                    .add_modifier(Modifier::BOLD),
            );
        let mut list_state = ListState::default().with_selected(Some(self.selected));
        frame.render_stateful_widget(profile_list, columns[0], &mut list_state);

        // Deliberately unwrapped: a directory reflowed across three lines is
        // harder to read than one that is shortened to fit.
        let details = self.profile_details(columns[1].width.saturating_sub(2) as usize);
        frame.render_widget(
            Paragraph::new(details).block(
                Block::new()
                    .title(" Selected profile ")
                    .borders(Borders::ALL),
            ),
            columns[1],
        );
    }

    fn profile_details(&self, width: usize) -> Text<'static> {
        let profile = self.selected_profile();
        let auth = self.selected_auth();
        let home = self.store.user_home();
        let kind = if profile.managed {
            "Isolated profile"
        } else {
            "Your existing setup"
        };

        let mut lines = vec![
            Line::from(vec![
                Span::styled(profile.name.clone(), Style::new().fg(DITTO_PURPLE).bold()),
                Span::styled(format!("  {kind}"), Style::new().fg(Color::DarkGray)),
            ]),
            Line::default(),
        ];
        if self.is_default(&profile.name) {
            lines.push(Line::styled(
                format!("{DEFAULT_MARK} Used when no profile is named"),
                Style::new().fg(Color::Yellow),
            ));
            lines.push(Line::default());
        }
        // The table's tools appear only when installed: the pane has room for
        // the handful a person has, not for every agent Ditto knows.
        let shown = Tool::ALL
            .into_iter()
            .filter(|tool| {
                !matches!(tool, Tool::Generic(_))
                    || self.installed.contains(&LaunchTarget::Cli(*tool))
            })
            .collect::<Vec<_>>();
        lines.push(Line::styled(
            "Tools and sign-in status",
            Style::new().bold(),
        ));
        for &tool in &shown {
            lines.push(status_row(tool, auth.get(tool), self.spinner));
            #[cfg(target_os = "macos")]
            if tool == Tool::Codex && self.installed.contains(&LaunchTarget::CodexDesktop) {
                lines.push(desktop_row());
            }
        }

        lines.push(Line::default());
        lines.push(Line::styled("Profile directories", Style::new().bold()));
        lines.extend(shown.iter().map(|&tool| {
            let path = match tool {
                Tool::Claude => profile.claude_home.clone(),
                Tool::Codex => profile.codex_home.clone(),
                Tool::Fx => profile.fx_dir(),
                Tool::Opencode => profile.opencode.data_dir(),
                Tool::Omp => profile.omp_home.clone(),
                Tool::PrimeAgent => profile.prime_agent_home.clone(),
                Tool::Pi => profile.pi_home.clone(),
                Tool::Generic(spec) => profile.tool_root(spec),
            };
            let path = shorten_home(&path, home);
            Line::from(vec![
                Span::styled(
                    format!("{:<TOOL_COLUMN$}", tool.label()),
                    Style::new().fg(tool_color(tool)),
                ),
                Span::styled(
                    truncate_start(&path, width.saturating_sub(TOOL_COLUMN)),
                    Style::new().fg(Color::DarkGray),
                ),
            ])
        }));

        if !profile.managed {
            lines.push(Line::default());
            lines.push(Line::styled(
                "Press n to create an isolated profile.",
                Style::new().fg(Color::DarkGray),
            ));
        }

        Text::from(lines)
    }

    fn draw_footer(&self, frame: &mut Frame, area: Rect, narrow: bool) {
        let mut lines = Vec::new();
        if narrow {
            lines.extend(NARROW_TOOL_ROWS.iter().map(|row| shortcut_line(row)));
            lines.extend(NARROW_SHORTCUT_ROWS.iter().map(|row| shortcut_line(row)));
        } else {
            lines.push(shortcut_line(TOOL_SHORTCUTS));
            lines.push(shortcut_line(&WIDE_SHORTCUT_ROW));
        }
        if self.has_auth_environment {
            lines.push(Line::styled(
                "An API-key environment variable is set and may override the saved login.",
                Style::new().fg(Color::Yellow),
            ));
        }
        frame.render_widget(
            Paragraph::new(lines)
                .alignment(Alignment::Center)
                .block(Block::bordered().border_style(Style::new().fg(Color::DarkGray))),
            area,
        );
    }

    fn draw_modal(&self, frame: &mut Frame, area: Rect) {
        match &self.mode {
            Mode::Browsing => {}
            Mode::Creating { input, error } => {
                let mut lines = vec![
                    Line::raw("Use lowercase letters, numbers, '.', '-' or '_'."),
                    Line::styled(format!("> {input}"), Style::new().fg(DITTO_PURPLE).bold()),
                    Line::default(),
                    Line::styled(
                        "Enter create  ·  Esc cancel",
                        Style::new().fg(Color::DarkGray),
                    ),
                ];
                if let Some(error) = error {
                    lines[2] = Line::styled(error.clone(), Style::new().fg(Color::Red));
                }
                render_popup(frame, centered_rect(64, 8, area), " New profile ", lines);
            }
            Mode::Renaming {
                original,
                input,
                error,
            } => {
                let mut lines = vec![
                    Line::raw(format!("New name for '{original}':")),
                    Line::styled(format!("> {input}"), Style::new().fg(DITTO_PURPLE).bold()),
                    Line::default(),
                    Line::styled(
                        "Enter rename  ·  Esc cancel",
                        Style::new().fg(Color::DarkGray),
                    ),
                ];
                if let Some(error) = error {
                    lines[2] = Line::styled(error.clone(), Style::new().fg(Color::Red));
                } else if rename_signs_out(&self.auth, original) {
                    lines[2] = Line::styled(
                        "Claude Code will need a fresh sign-in afterwards.",
                        Style::new().fg(Color::Yellow),
                    );
                }
                render_popup(frame, centered_rect(64, 8, area), " Rename profile ", lines);
            }
            Mode::Notice { title, message } => {
                let lines = vec![
                    Line::raw(message.clone()),
                    Line::default(),
                    Line::styled("Enter or Esc close", Style::new().fg(Color::DarkGray)),
                ];
                render_popup(
                    frame,
                    centered_rect(64, notice_height(message, area), area),
                    title,
                    lines,
                );
            }
            Mode::ChoosingTool { operation } => {
                let lines = vec![
                    Line::raw(format!(
                        "{} to '{}' with:",
                        operation.label(),
                        self.selected_profile().name
                    )),
                    Line::default(),
                    shortcut_line(&AUTH_SHORTCUTS),
                    Line::default(),
                    // OMP and Pi are missing above on purpose: Ditto CLI can
                    // read their sign-in state but cannot open their commands.
                    Line::styled(
                        "OMP, Pi, and the other agents sign in and out from inside themselves.",
                        Style::new().fg(Color::DarkGray),
                    ),
                    Line::default(),
                    Line::styled("Esc cancel", Style::new().fg(Color::DarkGray)),
                ];
                render_popup(
                    frame,
                    centered_rect(90, 10, area),
                    &format!(" {} ", operation.label()),
                    lines,
                );
            }
            Mode::Launching { filter, selected } => {
                let auth = self.selected_auth();
                let candidates = launchable(&self.installed, filter);
                // Room for the prompt, the hint, the borders, and a blank line
                // either side of the list; the list scrolls inside what is left.
                let rows = usize::from(area.height.saturating_sub(8)).max(1);
                let first = selected
                    .saturating_sub(rows - 1)
                    .min(candidates.len().saturating_sub(rows));
                let mut lines = vec![
                    Line::from(vec![
                        Span::styled("", Style::new().fg(DITTO_PURPLE)),
                        Span::raw(filter.clone()),
                        Span::styled("", Style::new().fg(Color::DarkGray)),
                    ]),
                    Line::default(),
                ];
                if candidates.is_empty() {
                    lines.push(Line::styled(
                        if self.installed.is_empty() {
                            "No agent Ditto knows is installed."
                        } else {
                            "Nothing matches."
                        },
                        Style::new().fg(Color::DarkGray),
                    ));
                }
                lines.extend(candidates.iter().enumerate().skip(first).take(rows).map(
                    |(index, target)| {
                        let row = match target {
                            LaunchTarget::Cli(tool) => {
                                status_row(*tool, auth.get(*tool), self.spinner)
                            }
                            #[cfg(target_os = "macos")]
                            LaunchTarget::CodexDesktop => desktop_row(),
                        };
                        if index == *selected {
                            row.style(Style::new().reversed())
                        } else {
                            row
                        }
                    },
                ));
                lines.push(Line::default());
                lines.push(Line::styled(
                    "type to filter · ↑↓ · Enter launches · Esc",
                    Style::new().fg(Color::DarkGray),
                ));
                let height = (lines.len() as u16 + 2).min(area.height);
                render_popup(
                    frame,
                    centered_rect(80, height, area),
                    &format!(" Launch in '{}' ", self.selected_profile().name),
                    lines,
                );
            }
            Mode::ConfirmingLogout { tool } => {
                let lines = vec![
                    Line::raw(format!(
                        "Sign out of {} for '{}'?",
                        tool.label(),
                        self.selected_profile().name
                    )),
                    Line::default(),
                    Line::styled(
                        "Enter or y confirm  ·  n cancel",
                        Style::new().fg(Color::Yellow),
                    ),
                ];
                render_popup(
                    frame,
                    centered_rect(62, 7, area),
                    " Confirm sign out ",
                    lines,
                );
            }
        }
    }
}

enum Action {
    Continue,
    Quit,
    Launch(Tool),
    #[cfg(target_os = "macos")]
    LaunchCodexDesktop,
    Authenticate {
        operation: AuthOperation,
        tool: Tool,
    },
}

pub fn run(
    store: &Store,
    profiles: Vec<Profile>,
    initial_profile: Option<&str>,
    default_profile: Option<String>,
) -> Result<Option<UiAction>> {
    let app = App::new(store, profiles, initial_profile, default_profile);
    let mut terminal = ratatui::init();
    let guard = TerminalGuard;
    let result = run_loop(&mut terminal, app);
    drop(guard);
    result
}

fn run_loop(terminal: &mut DefaultTerminal, mut app: App<'_>) -> Result<Option<UiAction>> {
    let mut dirty = true;
    loop {
        if dirty {
            terminal.draw(|frame| app.draw(frame))?;
            dirty = false;
        }

        if event::poll(TICK)? {
            match event::read()? {
                Event::Key(key) => {
                    match app.handle_key(key)? {
                        Action::Continue => {}
                        Action::Quit => return Ok(None),
                        Action::Launch(tool) => {
                            return Ok(Some(UiAction::Launch {
                                tool,
                                profile: app.selected_profile().clone(),
                            }));
                        }
                        #[cfg(target_os = "macos")]
                        Action::LaunchCodexDesktop => {
                            return Ok(Some(UiAction::LaunchCodexDesktop {
                                profile: app.selected_profile().clone(),
                            }));
                        }
                        Action::Authenticate { operation, tool } => {
                            return Ok(Some(UiAction::Authenticate {
                                operation,
                                tool,
                                profile: app.selected_profile().clone(),
                            }));
                        }
                    }
                    dirty = true;
                }
                Event::Resize(..) => dirty = true,
                _ => {}
            }
        } else if app.waiting_on_probes() {
            // Only animate while something is actually being waited on.
            app.spinner = app.spinner.wrapping_add(1);
            dirty = true;
        }

        if app.collect_probes() {
            dirty = true;
        }
    }
}

struct TerminalGuard;

impl Drop for TerminalGuard {
    fn drop(&mut self) {
        ratatui::restore();
    }
}

fn tool_color(tool: Tool) -> Color {
    match tool {
        Tool::Claude => CLAUDE_ORANGE,
        Tool::Codex => CODEX_GREEN,
        Tool::Fx => FX_MAGENTA,
        Tool::Opencode => OPENCODE_CYAN,
        Tool::Omp => OMP_BLUE,
        Tool::PrimeAgent => PRIME_PURPLE,
        Tool::Pi => DITTO_PURPLE,
        Tool::Generic(_) => Color::Gray,
    }
}

/// One row of the tool table: the tool in its own colour, then its state.
fn tool_row(tool: Tool, symbol: &str, label: &str, state_color: Color) -> Line<'static> {
    Line::from(vec![
        Span::styled(
            format!("{:<TOOL_COLUMN$}", tool.label()),
            Style::new().fg(tool_color(tool)),
        ),
        Span::styled(format!("{symbol} {label}"), Style::new().fg(state_color)),
    ])
}

fn status_row(tool: Tool, status: Option<AuthStatus>, spinner: usize) -> Line<'static> {
    let (symbol, label, color) = match status {
        None => (
            SPINNER[spinner % SPINNER.len()],
            "Checking",
            Color::DarkGray,
        ),
        Some(AuthStatus::SignedIn) => ("", "Signed in", Color::Green),
        Some(AuthStatus::SignedOut) => ("", "Sign in required", Color::Yellow),
        // A CLI that is simply not installed is not an error worth alarming
        // about, so this stays quiet rather than red.
        Some(AuthStatus::Unavailable) => ("", "Not available", Color::DarkGray),
    };
    tool_row(tool, symbol, label, color)
}

#[cfg(target_os = "macos")]
fn desktop_row() -> Line<'static> {
    Line::from(vec![
        Span::styled(LAUNCH_CODEX_DESKTOP.1, Style::new().fg(CODEX_GREEN)),
        Span::styled("  Open app", Style::new().fg(Color::DarkGray)),
    ])
}

/// Paths are long enough to wrap the detail pane, so the home directory is
/// abbreviated the way a shell prompt would. The separator is the platform's
/// own, so what is shown reads as one path rather than two conventions spliced
/// together.
fn shorten_home(path: &Path, user_home: &Path) -> String {
    match path.strip_prefix(user_home) {
        Ok(relative) => format!("~{}{}", std::path::MAIN_SEPARATOR, relative.display()),
        Err(_) => path.display().to_string(),
    }
}

/// Drops characters from the front of a path. The tail names the profile and
/// the tool, which is the part worth keeping; the head repeats on every row.
fn truncate_start(text: &str, budget: usize) -> String {
    let length = text.chars().count();
    if length <= budget {
        return text.to_owned();
    }
    if budget <= 1 {
        return "".repeat(budget);
    }
    let mut truncated = String::from("");
    truncated.extend(text.chars().skip(length - budget + 1));
    truncated
}

fn shortcut_line(shortcuts: &[(&str, &str, Color)]) -> Line<'static> {
    let mut spans = Vec::with_capacity(shortcuts.len() * 3);
    for (index, (key, label, color)) in shortcuts.iter().enumerate() {
        if index > 0 {
            spans.push(Span::styled(" · ", Style::new().fg(Color::DarkGray)));
        }
        spans.push(Span::styled(
            (*key).to_owned(),
            Style::new().fg(*color).bold(),
        ));
        spans.push(Span::raw(format!(" {label}")));
    }
    Line::from(spans)
}

fn render_popup(frame: &mut Frame, area: Rect, title: &str, lines: Vec<Line<'static>>) {
    frame.render_widget(Clear, area);
    frame.render_widget(
        Paragraph::new(lines).wrap(Wrap { trim: false }).block(
            Block::new()
                .title(title.to_owned())
                .borders(Borders::ALL)
                .border_style(Style::new().fg(DITTO_PURPLE)),
        ),
        area,
    );
}

/// The installed tools whose name contains what was typed, in the order they
/// are listed everywhere else. Case does not matter, and the key counts as
/// well as the label, so `cli` finds Gemini CLI and `kiro` finds `kiro-cli`.
fn launchable(installed: &[LaunchTarget], filter: &str) -> Vec<LaunchTarget> {
    let filter = filter.trim().to_lowercase();
    installed
        .iter()
        .copied()
        .filter(|target| {
            let (label, key) = match target {
                LaunchTarget::Cli(tool) => (tool.label(), tool.key()),
                #[cfg(target_os = "macos")]
                LaunchTarget::CodexDesktop => (LAUNCH_CODEX_DESKTOP.1, "codex-app"),
            };
            filter.is_empty() || label.to_lowercase().contains(&filter) || key.contains(&filter)
        })
        .collect()
}

/// Whether renaming a profile costs it its Claude Code sign-in. Claude Code
/// stores credentials against the directory it was pointed at, and a rename
/// moves that directory, so a signed-in profile does not stay one. A probe
/// that has not answered yet is nothing to warn about rather than guessed at.
fn rename_signs_out(auth: &HashMap<String, ProfileAuth>, name: &str) -> bool {
    auth.get(name)
        .and_then(|auth| auth.get(Tool::Claude))
        .is_some_and(|status| status == AuthStatus::SignedIn)
}

/// Grows a notice to fit the message it carries. The popup wraps its text, so
/// a fixed height would clip anything longer than one line.
fn notice_height(message: &str, area: Rect) -> u16 {
    // The multiplication is done in `usize`: `area.width` is a `u16`, and a
    // terminal past 1023 columns would overflow one before the division.
    let inner = (usize::from(area.width) * 64 / 100)
        .saturating_sub(2)
        .max(1);
    let wrapped = u16::try_from(message.chars().count().div_ceil(inner)).unwrap_or(u16::MAX);
    // Two borders, a blank line and the closing hint sit around the message.
    wrapped.saturating_add(4).clamp(7, area.height)
}

fn centered_rect(percent_x: u16, height: u16, area: Rect) -> Rect {
    let vertical = Layout::vertical([Constraint::Length(height.min(area.height))])
        .flex(Flex::Center)
        .split(area)[0];
    Layout::horizontal([Constraint::Percentage(percent_x)])
        .flex(Flex::Center)
        .split(vertical)[0]
}

fn auth_environment_is_set() -> bool {
    env::vars_os().any(|(name, _)| {
        let name = name.to_string_lossy();
        name.ends_with("_API_KEY") || matches!(name.as_ref(), "ANTHROPIC_AUTH_TOKEN" | "HF_TOKEN")
    })
}

#[cfg(test)]
mod tests {
    use std::path::PathBuf;

    use super::*;

    /// A terminal wide enough to overflow the `u16` the width is measured in
    /// used to size the notice from a wrapped-around number.
    #[test]
    fn sizes_a_notice_on_a_terminal_too_wide_for_a_u16_multiplication() {
        let narrow = Rect::new(0, 0, 100, 40);
        let wide = Rect::new(0, 0, 2000, 40);
        let message = "x".repeat(400);

        assert!(notice_height(&message, narrow) > notice_height(&message, wide));
        assert_eq!(notice_height(&message, wide), 7);
    }

    #[test]
    fn the_launcher_filters_by_label_and_key_without_caring_about_case() {
        let installed = [
            Tool::Claude,
            Tool::by_key("gemini").unwrap(),
            Tool::by_key("kiro-cli").unwrap(),
        ]
        .map(LaunchTarget::Cli);
        assert_eq!(launchable(&installed, ""), installed.to_vec());
        assert_eq!(launchable(&installed, "GEM"), vec![installed[1]]);
        assert_eq!(launchable(&installed, "kiro"), vec![installed[2]]);
        assert_eq!(
            launchable(&installed, "cli"),
            vec![installed[1], installed[2]]
        );
        assert!(launchable(&installed, "zzz").is_empty());
    }

    #[cfg(target_os = "macos")]
    #[test]
    fn desktop_is_selectable_separately_from_codex_cli() {
        let installed = [LaunchTarget::Cli(Tool::Codex), LaunchTarget::CodexDesktop];
        assert_eq!(launchable(&installed, "codex"), installed);
        assert_eq!(
            launchable(&installed, "DESKTOP"),
            vec![LaunchTarget::CodexDesktop]
        );
        assert_eq!(
            launchable(&installed, "codex-app"),
            vec![LaunchTarget::CodexDesktop]
        );
    }

    #[test]
    fn footer_rows_fit_the_widths_that_select_them() {
        // Wide mode shows the tools and the actions as one row each, so the
        // width that selects it is whichever of the two rows is longer.
        let wide = [
            shortcut_line(&WIDE_SHORTCUT_ROW),
            shortcut_line(TOOL_SHORTCUTS),
        ]
        .iter()
        .map(|row| row.width() as u16 + 2)
        .max()
        .unwrap();
        assert!(wide <= WIDE_FOOTER_WIDTH, "{wide} exceeds the wide footer");
        assert!(
            wide > WIDE_FOOTER_WIDTH - 1,
            "the wide footer is {} wider than it needs to be, so terminals \
             that could show one row are given two",
            WIDE_FOOTER_WIDTH - wide
        );

        for row in NARROW_SHORTCUT_ROWS.iter().chain(NARROW_TOOL_ROWS.iter()) {
            let width = shortcut_line(row).width() as u16 + 2;
            assert!(width <= MINIMUM_WIDTH, "{width} exceeds the narrow footer");
        }

        let auth = shortcut_line(&AUTH_SHORTCUTS).width() as u16 + 2;
        let auth_popup = MINIMUM_WIDTH * 90 / 100;
        assert!(auth <= auth_popup, "{auth} exceeds the sign-in dialog");
    }

    #[test]
    fn abbreviates_paths_inside_the_home_directory() {
        let separator = std::path::MAIN_SEPARATOR;
        let home = PathBuf::from("/Users/example");
        let inside = home
            .join(".ditto")
            .join("profiles")
            .join("work")
            .join("claude");

        assert_eq!(
            shorten_home(&inside, &home),
            format!("~{separator}.ditto{separator}profiles{separator}work{separator}claude")
        );
        assert_eq!(
            shorten_home(Path::new("/opt/shared/claude"), &home),
            "/opt/shared/claude"
        );
    }

    #[test]
    fn keeps_the_tail_of_a_path_that_does_not_fit() {
        assert_eq!(
            truncate_start("~/.ditto/work/claude", 40),
            "~/.ditto/work/claude"
        );
        assert_eq!(truncate_start("~/.ditto/work/claude", 12), "…work/claude");
        assert_eq!(truncate_start("abc", 1), "");
        assert_eq!(truncate_start("abc", 0), "");
        // Multi-byte characters must not be split mid-character.
        assert_eq!(truncate_start("→→→→", 3), "…→→");
    }

    #[test]
    fn reports_probes_as_pending_until_every_tool_answers() {
        let mut auth = ProfileAuth::default();
        assert!(auth.pending());

        auth.set(Tool::Claude, AuthStatus::SignedIn);
        auth.set(Tool::Codex, AuthStatus::SignedOut);
        assert!(auth.pending());

        auth.set(Tool::Fx, AuthStatus::SignedIn);
        assert!(auth.pending());

        auth.set(Tool::Opencode, AuthStatus::SignedIn);
        // OMP reports like the rest, so it holds the spinner open until it
        // answers.
        assert!(auth.pending());

        auth.set(Tool::Omp, AuthStatus::SignedOut);
        assert!(auth.pending());

        auth.set(Tool::PrimeAgent, AuthStatus::SignedIn);
        assert!(auth.pending());

        auth.set(Tool::Pi, AuthStatus::SignedOut);
        // The table's tools answer too, instantly, but they are answers all
        // the same.
        assert!(auth.pending());
        for tool in Tool::ALL {
            if matches!(tool, Tool::Generic(_)) {
                auth.set(tool, AuthStatus::Unavailable);
            }
        }
        assert!(!auth.pending());
        assert_eq!(auth.get(Tool::Opencode), Some(AuthStatus::SignedIn));
        assert_eq!(auth.get(Tool::Omp), Some(AuthStatus::SignedOut));
        assert_eq!(auth.get(Tool::PrimeAgent), Some(AuthStatus::SignedIn));
        assert_eq!(auth.get(Tool::Pi), Some(AuthStatus::SignedOut));
    }
}