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
//! The persistent full-screen UI (`swapdex ui` on a real terminal), ccusage-
//! style by user request: the screen clears, the UI stays up, and everything
//! happens inside it. Switching shows its result in the status line and
//! REFRESHES the list in place; landing in a conversation (resume or new) is
//! the one action that leaves - by design, that is the goal of a switch.
//!
//! No second implementation of anything: a switch/restore runs this same
//! binary as a subprocess (`swapdex use/restore`) with its output captured
//! into the status line, and session/launch data comes from the caller
//! through [`TuiCtx`].
use anyhow::Result;
use ratatui::crossterm::event::{self, Event, KeyCode, KeyEvent, KeyEventKind, KeyModifiers};
use ratatui::layout::{Constraint, Layout, Rect};
use ratatui::style::{Color, Modifier, Style};
use ratatui::text::{Line, Span};
use ratatui::widgets::{Block, Borders, List, ListItem, ListState, Paragraph};
use std::path::PathBuf;
const VIOLET: Color = Color::Rgb(157, 107, 255); // the brand accent (#9d6bff)
const DEXGRAY: Color = Color::Rgb(150, 150, 160); // the dimmed "dex" half
const MUTED: Color = Color::Rgb(139, 138, 149); // subtitles / hints
/// Same rounded panel, but with an owned (dynamic) title.
fn list_block_titled(title: &str) -> Block<'static> {
Block::default()
.borders(Borders::ALL)
.border_type(ratatui::widgets::BorderType::Rounded)
.border_style(Style::default().fg(Color::Rgb(96, 94, 116)))
.title(Span::styled(
title.to_string(),
Style::default().fg(VIOLET).add_modifier(Modifier::BOLD),
))
}
/// A rounded, violet-titled panel border - the shared frame for every list.
fn list_block(title: &'static str) -> Block<'static> {
Block::default()
.borders(Borders::ALL)
.border_type(ratatui::widgets::BorderType::Rounded)
.border_style(Style::default().fg(Color::Rgb(96, 94, 116)))
.title(Span::styled(
title,
Style::default().fg(VIOLET).add_modifier(Modifier::BOLD),
))
}
/// The two-tone wordmark as ratatui lines (violet SWAP + dim dex), shared with
/// the CLI banner so the TUI header IS the brand mark. Empty when the terminal
/// is too short to spare the rows.
fn logo_lines() -> Vec<Line<'static>> {
crate::banner::SWAP
.iter()
.zip(crate::banner::DEX.iter())
.map(|(sw, dx)| {
Line::from(vec![
Span::raw(" "),
Span::styled(
*sw,
Style::default().fg(VIOLET).add_modifier(Modifier::BOLD),
),
Span::styled(*dx, Style::default().fg(DEXGRAY)),
])
})
.collect()
}
/// A key-hint footer where the keys are violet and the labels muted, so the
/// eye lands on the keys (lazygit/gitui idiom).
/// Keep a list selection in bounds after the row count changes (a switch,
/// delete, or a concurrent `swapdex rm`), so the highlight never points past
/// the end and no later `rows[i]` can panic.
fn clamp_selection(state: &mut ListState, len: usize) {
match (state.selected(), len) {
(_, 0) => state.select(None),
(Some(i), n) if i >= n => state.select(Some(n - 1)),
(None, n) if n > 0 => state.select(Some(0)),
_ => {}
}
}
/// Map a clicked terminal row to a list index, accounting for the list's scroll
/// `offset`: the first VISIBLE row is `offset`, not `0`. Ignoring the offset
/// meant a click on a scrolled list opened an earlier, hidden entry (potentially
/// another account's session). Caller guarantees `click_row >= top`.
fn click_row_index(offset: usize, click_row: u16, top: u16, per: u16) -> usize {
offset + (click_row.saturating_sub(top) / per.max(1)) as usize
}
fn key_hints(pairs: &[(&'static str, &'static str)]) -> Line<'static> {
let mut spans = vec![Span::raw(" ")];
for (i, (key, label)) in pairs.iter().enumerate() {
if i > 0 {
spans.push(Span::styled(" ", Style::default().fg(MUTED)));
}
spans.push(Span::styled(
*key,
Style::default().fg(VIOLET).add_modifier(Modifier::BOLD),
));
spans.push(Span::raw(" "));
spans.push(Span::styled(*label, Style::default().fg(MUTED)));
}
Line::from(spans)
}
pub struct Row {
pub name: String,
pub ident: String,
pub tools: String,
pub active: bool,
pub warn: Option<&'static str>,
}
/// One line in the post-switch "open" screen (pre-rendered by the caller).
pub struct SessionEntry {
pub line: String,
}
/// Everything the UI needs from the outside world.
pub trait TuiCtx {
fn rows(&mut self) -> Vec<Row>;
/// Perform the switch (subprocess); returns (success, condensed message).
fn switch(&mut self, name: &str) -> (bool, String);
fn restore(&mut self) -> String;
fn delete(&mut self, name: &str) -> String;
/// (label, session entries) for the just-switched profile.
/// (label, session entries, the profile's tools) for the just-switched
/// profile. The tools drive which "open a NEW ..." entries to show.
fn sessions(&mut self, name: &str) -> (String, Vec<SessionEntry>, Vec<&'static str>);
/// Rename a profile (subprocess). Returns (ok, message).
fn rename(&mut self, old: &str, new: &str) -> (bool, String);
/// Save the accounts you're currently logged into as a new profile
/// (subprocess `add <name>` - captures live logins, no sign-out). This is
/// the onboarding action: a fresh machine is usually already logged in.
fn save_current(&mut self, name: &str) -> (bool, String);
/// Run `doctor` and return its output lines for a read-only panel.
fn doctor(&mut self) -> Vec<String>;
/// Run `usage` and return its lines (consumed tokens per account).
fn usage(&mut self) -> Vec<String>;
/// Run `quota` and return its lines (remaining quota per Claude account -
/// the one opt-in network read).
fn quota(&mut self) -> Vec<String>;
/// Is `sessionwiki` installed? When not, the session menu is native and a
/// one-line hint points at what installing it would add.
fn sessionwiki_present(&mut self) -> bool;
/// Display names of the tools you're logged into RIGHT NOW (for the
/// empty-state onboarding: "save these as a profile").
fn live_tools(&mut self) -> Vec<String>;
}
/// What finally leaves the UI. Executed by the caller AFTER the terminal is
/// restored.
pub enum Outcome {
Quit,
/// Open the i-th session from the last `sessions()` call.
OpenSession(usize),
/// Open a fresh conversation in `dir` (None = current directory).
NewConv {
tool: &'static str,
dir: Option<PathBuf>,
},
/// Run the add-a-new-account login flow (needs the real terminal).
AddAccount(&'static str),
}
const NEW_CONV: [(&str, &str); 4] = [
("open a NEW Claude Code conversation", "claude-code"),
("open a NEW Codex conversation", "codex"),
("open a NEW Gemini conversation", "gemini"),
("open a NEW Antigravity conversation", "antigravity"),
];
/// The "open a NEW <tool> conversation" entries for the tools a profile
/// actually has - so a Claude-only account doesn't offer Codex/Gemini/etc.
fn new_conv_for(tools: &[&str]) -> Vec<(&'static str, &'static str)> {
NEW_CONV
.iter()
.filter(|(_, t)| tools.contains(t))
.map(|&(l, t)| (l, t))
.collect()
}
/// What a text-input screen is collecting.
enum InputKind {
Rename(String), // rename this existing profile
SaveCurrent, // save the current live logins as a new profile
}
/// One row in the folder browser.
enum FolderRow {
OpenHere, // launch the conversation in the current dir
Up, // go to the parent dir
Home, // jump to $HOME
Into(PathBuf), // descend into this subdirectory
}
/// The browser rows for `cwd`: "open here", parent (if any), home, then the
/// visible subdirectories (alphabetical, dotfiles hidden, unreadable skipped).
fn folder_rows(cwd: &std::path::Path) -> Vec<FolderRow> {
let mut rows = vec![FolderRow::OpenHere];
if cwd.parent().is_some() {
rows.push(FolderRow::Up);
}
if dirs::home_dir().is_some_and(|h| h != cwd) {
rows.push(FolderRow::Home);
}
let mut subs: Vec<PathBuf> = std::fs::read_dir(cwd)
.into_iter()
.flatten()
.flatten()
.map(|e| e.path())
.filter(|p| {
p.is_dir()
&& !p
.file_name()
.and_then(|n| n.to_str())
.is_some_and(|n| n.starts_with('.'))
})
.collect();
subs.sort();
rows.extend(subs.into_iter().map(FolderRow::Into));
rows
}
/// A path with $HOME collapsed to `~`, for a compact browser title.
fn tildify(p: &std::path::Path) -> String {
if let Some(home) = dirs::home_dir() {
if let Ok(rest) = p.strip_prefix(&home) {
return if rest.as_os_str().is_empty() {
"~".to_string()
} else {
format!("~/{}", rest.display())
};
}
}
p.display().to_string()
}
enum Screen {
Main,
Open {
label: String,
entries: Vec<SessionEntry>,
new_conv: Vec<(&'static str, &'static str)>,
},
/// A folder BROWSER (no typing): navigate into subdirs, `..` to go up,
/// and pick "open here" - conversations are per-directory.
Folder {
tool: &'static str,
cwd: PathBuf,
rows: Vec<FolderRow>,
/// The Open screen to return to on Esc (one step back, not two).
back: (String, Vec<SessionEntry>, Vec<(&'static str, &'static str)>),
},
ToolPick,
/// A single-line text prompt (rename / save-current / new-account name).
Input {
kind: InputKind,
value: String,
},
/// Read-only `doctor` output. `pending` = the (slow, tool-probing) check
/// has not run yet; we draw a "checking..." frame first so the UI never
/// looks frozen.
Doctor {
lines: Vec<String>,
scroll: u16,
pending: bool,
},
/// Read-only `usage` output (consumed tokens per account, local).
Usage {
lines: Vec<String>,
scroll: u16,
pending: bool,
},
/// Read-only `quota` output (remaining quota per Claude account). `pending`
/// draws a "fetching..." frame first because this one hits the network.
Quota {
lines: Vec<String>,
scroll: u16,
pending: bool,
},
}
/// The persistent loop. Enters the alternate screen once and stays there
/// until an [`Outcome`] leaves it.
pub fn run(ctx: &mut dyn TuiCtx) -> Result<Outcome> {
let mut terminal = ratatui::try_init()?;
// Mouse: scroll to move, click to select/switch - the "manage by clicking"
// the picker was asked for. Best-effort; key control is unaffected if the
// terminal refuses.
let _ = ratatui::crossterm::execute!(
std::io::stdout(),
ratatui::crossterm::event::EnableMouseCapture
);
let mut rows = ctx.rows();
let mut state = ListState::default();
state.select(Some(rows.iter().position(|r| r.active).unwrap_or(0)));
let mut open_state = ListState::default();
let mut status = String::new();
let mut confirm_delete: Option<usize> = None;
// Checked once: drives the "install sessionwiki for more" hint in the
// native session menu.
let wiki_present = ctx.sessionwiki_present();
let mut screen = Screen::Main;
// Cached only while the list is empty (onboarding); cheap to recompute.
let mut onboard_live: Vec<String> = if rows.is_empty() {
ctx.live_tools()
} else {
Vec::new()
};
// The list-body Rect from the last draw, so a mouse click can map its row
// to a selection index.
let mut main_area = Rect::default();
let outcome = 'ui: loop {
terminal.draw(|f| {
let [main, foot, help] = Layout::vertical([
Constraint::Min(3),
Constraint::Length(1),
Constraint::Length(1),
])
.areas(f.area());
main_area = main;
match &screen {
Screen::Main => {
// A tall terminal gets the full wordmark header; a short
// one drops it so the list keeps its room.
let show_logo = main.height >= 14;
let head_h = if show_logo { 8 } else { 0 };
let [header, body] =
Layout::vertical([Constraint::Length(head_h), Constraint::Min(3)])
.areas(main);
if show_logo {
let mut lines = logo_lines();
lines.push(Line::from(""));
lines.push(Line::from(Span::styled(
" Claude Code \u{b7} Codex \u{b7} Gemini \u{b7} Antigravity - one command, all local",
Style::default().fg(MUTED),
)));
f.render_widget(Paragraph::new(lines), header);
}
let items: Vec<ListItem> = rows
.iter()
.map(|r| {
// Filled dot = the active profile, hollow = the
// rest - the eye finds the live account fast.
let (glyph, gstyle) = if r.active {
("\u{25cf} ", Style::default().fg(VIOLET))
} else {
("\u{25cb} ", Style::default().fg(Color::DarkGray))
};
let name_style = if r.active {
Style::default().fg(VIOLET).add_modifier(Modifier::BOLD)
} else {
Style::default().add_modifier(Modifier::BOLD)
};
let mut top = vec![
Span::styled(glyph, gstyle),
Span::styled(r.name.clone(), name_style),
Span::raw(" "),
Span::styled(r.ident.clone(), Style::default().fg(DEXGRAY)),
];
if let Some(w) = r.warn {
top.push(Span::styled(
format!(" ({w})"),
Style::default().fg(Color::Rgb(200, 150, 90)),
));
}
ListItem::new(vec![
Line::from(top),
Line::from(Span::styled(
format!(" {}", r.tools),
Style::default().fg(Color::DarkGray),
)),
Line::from(""),
])
})
.collect();
if rows.is_empty() {
// Onboarding. A fresh machine is usually ALREADY logged
// into some tools - the fastest first step is to save
// those, so lead with it when they exist.
let mut lines = vec![
Line::from(""),
Line::from(Span::styled(
" Welcome to swapdex.",
Style::default().fg(VIOLET).add_modifier(Modifier::BOLD),
)),
Line::from(""),
];
if onboard_live.is_empty() {
lines.push(Line::from(Span::styled(
" You're not logged into any tool yet. Sign in to Claude Code,",
Style::default().fg(MUTED),
)));
lines.push(Line::from(Span::styled(
" Codex, Gemini, or Antigravity first, then come back.",
Style::default().fg(MUTED),
)));
lines.push(Line::from(""));
lines.push(key_hints(&[
("a", "log in to a new account"),
("q", "quit"),
]));
} else {
lines.push(Line::from(vec![
Span::styled(" You're logged into ", Style::default().fg(MUTED)),
Span::styled(
onboard_live.join(", "),
Style::default().fg(Color::Reset).add_modifier(Modifier::BOLD),
),
Span::styled(".", Style::default().fg(MUTED)),
]));
lines.push(Line::from(""));
lines.push(key_hints(&[
("s", "save these as your first profile"),
("a", "add a different account"),
("q", "quit"),
]));
}
f.render_widget(
Paragraph::new(lines).block(list_block(" welcome ")),
body,
);
} else {
let list = List::new(items)
.block(list_block(" accounts "))
.highlight_style(
Style::default()
.bg(Color::Rgb(50, 47, 68))
.add_modifier(Modifier::BOLD),
)
.highlight_symbol("\u{2503} ");
f.render_stateful_widget(list, body, &mut state);
}
let foot_line = if let Some(i) = confirm_delete {
Line::from(Span::styled(
format!(
" delete saved profile '{}'? the live login stays. y / N",
rows[i].name
),
Style::default().fg(Color::Rgb(200, 150, 90)),
))
} else {
Line::from(Span::styled(
format!(" {}", status),
Style::default().fg(MUTED),
))
};
f.render_widget(Paragraph::new(foot_line), foot);
let hints: &[(&str, &str)] = if rows.is_empty() {
&[("?", "health"), ("q", "quit")]
} else {
&[
("\u{21b5}", "switch"),
("o", "open"),
("a", "add"),
("n", "rename"),
("u", "usage"),
("%", "quota"),
("r", "restore"),
("d", "delete"),
("?", "health"),
("q", "quit"),
]
};
f.render_widget(Paragraph::new(key_hints(hints)), help);
}
Screen::Open { label, entries, new_conv } => {
let mut items: Vec<ListItem> = entries
.iter()
.map(|e| ListItem::new(Line::from(e.line.clone())))
.collect();
for (nlabel, _) in new_conv {
items.push(ListItem::new(Line::from(Span::styled(
*nlabel,
Style::default().fg(VIOLET),
))));
}
let list = List::new(items)
.block(list_block_titled(&format!(" {label} ")))
.highlight_style(
Style::default()
.bg(Color::Rgb(50, 47, 68))
.add_modifier(Modifier::BOLD),
)
.highlight_symbol("\u{2503} ");
f.render_stateful_widget(list, main, &mut open_state);
// Native session menu (no sessionwiki): a one-line nudge at
// what installing it adds. With sessionwiki, show the switch
// status instead.
let foot_line = if wiki_present {
Span::styled(format!(" {status}"), Style::default().fg(MUTED))
} else {
Span::styled(
" tip: install sessionwiki to search these, trace a file to its \
session, and group by account",
Style::default().fg(MUTED),
)
};
f.render_widget(Paragraph::new(Line::from(foot_line)), foot);
f.render_widget(
Paragraph::new(key_hints(&[("\u{21b5}", "open"), ("esc", "back")])),
help,
);
}
Screen::Folder { tool, cwd, rows: frows, .. } => {
let name = NEW_CONV
.iter()
.find(|(_, t)| t == tool)
.map(|(l, _)| *l)
.unwrap_or("open");
let items: Vec<ListItem> = frows
.iter()
.map(|r| match r {
FolderRow::OpenHere => ListItem::new(Line::from(Span::styled(
"\u{25b8} open here",
Style::default().fg(VIOLET).add_modifier(Modifier::BOLD),
))),
FolderRow::Up => ListItem::new(Line::from(Span::styled(
"\u{2191} ..",
Style::default().fg(DEXGRAY),
))),
FolderRow::Home => ListItem::new(Line::from(Span::styled(
"\u{2302} ~ (home)",
Style::default().fg(DEXGRAY),
))),
FolderRow::Into(p) => {
let leaf = p
.file_name()
.and_then(|n| n.to_str())
.unwrap_or("?");
ListItem::new(Line::from(vec![
Span::styled(" ", Style::default()),
Span::styled(
format!("{leaf}/"),
Style::default().fg(Color::Reset),
),
]))
}
})
.collect();
let list = List::new(items)
.block(list_block_titled(&format!(
" {name} \u{2014} {} ",
tildify(cwd)
)))
.highlight_style(
Style::default()
.bg(Color::Rgb(50, 47, 68))
.add_modifier(Modifier::BOLD),
)
.highlight_symbol("\u{2503} ");
f.render_stateful_widget(list, main, &mut open_state);
f.render_widget(Paragraph::new(""), foot);
f.render_widget(
Paragraph::new(key_hints(&[
("\u{21b5}", "enter / open here"),
("\u{2191}\u{2193}", "move"),
("esc", "back"),
])),
help,
);
}
Screen::ToolPick => {
let items: Vec<ListItem> =
["Claude Code", "Codex", "Gemini CLI", "Antigravity"]
.iter()
.map(|l| ListItem::new(Line::from(*l)))
.collect();
let list = List::new(items)
.block(list_block(" add an account - which tool? "))
.highlight_style(
Style::default()
.bg(Color::Rgb(50, 47, 68))
.add_modifier(Modifier::BOLD),
)
.highlight_symbol("\u{2503} ");
f.render_stateful_widget(list, main, &mut open_state);
f.render_widget(Paragraph::new(""), foot);
f.render_widget(
Paragraph::new(key_hints(&[("\u{21b5}", "choose"), ("esc", "back")])),
help,
);
}
Screen::Input { kind, value } => {
let (title, prompt) = match kind {
InputKind::Rename(old) => (
" rename profile ".to_string(),
format!("new name for '{old}'"),
),
InputKind::SaveCurrent => (
" save current logins ".to_string(),
"name for this profile".to_string(),
),
};
f.render_widget(
Paragraph::new(vec![
Line::from(""),
Line::from(vec![
Span::styled(format!(" {prompt}"), Style::default().fg(MUTED)),
Span::raw(": "),
Span::styled(
format!("{value}\u{2588}"),
Style::default().fg(VIOLET).add_modifier(Modifier::BOLD),
),
]),
])
.block(list_block_titled(&title)),
main,
);
f.render_widget(
Paragraph::new(Line::from(Span::styled(
format!(" {status}"),
Style::default().fg(MUTED),
))),
foot,
);
f.render_widget(
Paragraph::new(key_hints(&[("\u{21b5}", "confirm"), ("esc", "cancel")])),
help,
);
}
Screen::Doctor { lines, scroll, .. } => {
let text: Vec<Line> = lines
.iter()
.map(|l| {
// Colour the verdict word so problems stand out.
let style = if l.contains("problem") {
Style::default().fg(Color::Rgb(210, 140, 90))
} else if l.contains(" ok ") || l.contains("healthy") {
Style::default().fg(Color::Rgb(120, 190, 140))
} else {
Style::default().fg(DEXGRAY)
};
Line::from(Span::styled(format!(" {l}"), style))
})
.collect();
f.render_widget(
Paragraph::new(text)
.scroll((*scroll, 0))
.block(list_block(" doctor - health check ")),
main,
);
f.render_widget(Paragraph::new(""), foot);
f.render_widget(
Paragraph::new(key_hints(&[
("\u{2191}\u{2193}", "scroll"),
("esc", "back"),
])),
help,
);
}
Screen::Usage { lines, scroll, .. } => {
let text: Vec<Line> = lines
.iter()
.map(|l| {
let style = if l.trim_start().starts_with('@') {
Style::default().fg(VIOLET)
} else if l.contains("note:") || l.contains("(") {
Style::default().fg(MUTED)
} else {
Style::default().fg(DEXGRAY)
};
Line::from(Span::styled(format!(" {l}"), style))
})
.collect();
f.render_widget(
Paragraph::new(text)
.scroll((*scroll, 0))
.block(list_block(" usage - tokens used (local, this machine) ")),
main,
);
f.render_widget(Paragraph::new(""), foot);
f.render_widget(
Paragraph::new(key_hints(&[
("\u{2191}\u{2193}", "scroll"),
("esc", "back"),
])),
help,
);
}
Screen::Quota { lines, scroll, .. } => {
let text: Vec<Line> = lines
.iter()
.map(|l| {
let style = if l.contains("% left") {
Style::default().fg(VIOLET)
} else if l.contains("expired")
|| l.contains("rejected")
|| l.contains("unexpected")
|| l.contains("could not reach")
{
Style::default().fg(Color::Rgb(200, 150, 90))
} else if l.starts_with(' ') || l.contains("network") || l.contains("(") {
Style::default().fg(MUTED)
} else {
Style::default().fg(DEXGRAY)
};
Line::from(Span::styled(format!(" {l}"), style))
})
.collect();
f.render_widget(
Paragraph::new(text)
.scroll((*scroll, 0))
.block(list_block(" quota - remaining (live from Anthropic) ")),
main,
);
f.render_widget(Paragraph::new(""), foot);
f.render_widget(
Paragraph::new(key_hints(&[
("\u{2191}\u{2193}", "scroll"),
("esc", "back"),
])),
help,
);
}
}
})?;
// A pending health check runs AFTER its "checking..." frame is drawn,
// so the UI shows feedback instead of freezing on the old screen.
if let Screen::Doctor { pending: true, .. } = &screen {
let lines = ctx.doctor();
screen = Screen::Doctor {
lines,
scroll: 0,
pending: false,
};
continue;
}
if let Screen::Usage { pending: true, .. } = &screen {
let lines = ctx.usage();
screen = Screen::Usage {
lines,
scroll: 0,
pending: false,
};
continue;
}
if let Screen::Quota { pending: true, .. } = &screen {
let lines = ctx.quota();
screen = Screen::Quota {
lines,
scroll: 0,
pending: false,
};
continue;
}
// A left click on a menu item both selects AND activates it; treat
// that as a synthesized Enter so the key handler below does the work.
let mut click_activate = false;
let key = match event::read()? {
Event::Key(k) if k.kind == KeyEventKind::Press => k,
Event::Mouse(m) => {
use ratatui::crossterm::event::{MouseButton, MouseEventKind as MK};
// Text panels (doctor/usage/quota) scroll their content with
// the wheel - the list logic below is for menu screens only.
if let Screen::Doctor { lines, scroll, .. }
| Screen::Usage { lines, scroll, .. }
| Screen::Quota { lines, scroll, .. } = &mut screen
{
let max = (lines.len() as u16).saturating_sub(1);
match m.kind {
MK::ScrollDown => *scroll = (*scroll + 1).min(max),
MK::ScrollUp => *scroll = scroll.saturating_sub(1),
_ => {}
}
continue;
}
let list_len = match &screen {
Screen::Main => rows.len(),
Screen::Open {
entries, new_conv, ..
} => entries.len() + new_conv.len(),
Screen::ToolPick => 4,
Screen::Folder { rows: frows, .. } => frows.len(),
_ => 0,
};
let is_main = matches!(screen, Screen::Main);
let sel = if is_main { &mut state } else { &mut open_state };
match m.kind {
MK::ScrollDown if list_len > 0 => {
let i = sel.selected().unwrap_or(0);
sel.select(Some((i + 1).min(list_len - 1)));
}
MK::ScrollUp if list_len > 0 => {
let i = sel.selected().unwrap_or(0);
sel.select(Some(i.saturating_sub(1)));
}
MK::Down(MouseButton::Left) if list_len > 0 => {
// The list box's first row = main.y + logo-header + border.
let header = if is_main && main_area.height >= 14 {
8u16
} else {
0
};
let per = if is_main { 3 } else { 1 }; // Main rows are 3 lines
let top = main_area.y + header + 1;
// Bottom of the list box's INNER area (above its border).
// A click below it (the foot/help rows) must not map to a
// hidden entry and synthesize an Enter.
let bottom = main_area.y + main_area.height.saturating_sub(1);
if m.row >= top && m.row < bottom {
// offset(): a scrolled list's first visible row is
// sel.offset(), not 0 - without it a click opened a
// hidden earlier entry (maybe another account).
let idx = click_row_index(sel.offset(), m.row, top, per);
if idx < list_len {
sel.select(Some(idx));
// Click activates a MENU item; on Main it only
// selects (Enter switches) so a stray click
// never switches accounts by surprise.
click_activate = !is_main;
}
}
}
_ => {}
}
if click_activate {
KeyEvent::new(KeyCode::Enter, KeyModifiers::NONE)
} else {
continue;
}
}
_ => continue,
};
// Ctrl+C quits from ANY screen - raw mode swallows the signal, and it
// is the first key a user in trouble reaches for.
if key.modifiers.contains(KeyModifiers::CONTROL) && key.code == KeyCode::Char('c') {
break 'ui Outcome::Quit;
}
match &mut screen {
Screen::Main => {
if let Some(i) = confirm_delete {
if matches!(key.code, KeyCode::Char('y') | KeyCode::Char('Y')) {
if let Some(row) = rows.get(i) {
status = ctx.delete(&row.name);
rows = ctx.rows();
}
// The list may now be EMPTY - a dangling Some(0)
// would make the next Enter/o index out of bounds.
clamp_selection(&mut state, rows.len());
// Deleting the last profile drops to the welcome screen,
// which reads onboard_live - recompute it (the live login
// is untouched by a delete) so it does not falsely claim
// you are logged out. Mirrors the save/rename path.
onboard_live = if rows.is_empty() {
ctx.live_tools()
} else {
Vec::new()
};
}
confirm_delete = None;
continue;
}
match key.code {
KeyCode::Char('q') | KeyCode::Esc => break 'ui Outcome::Quit,
KeyCode::Down | KeyCode::Char('j') => {
let i = state.selected().unwrap_or(0);
state.select(Some((i + 1).min(rows.len().saturating_sub(1))));
}
KeyCode::Up | KeyCode::Char('k') => {
let i = state.selected().unwrap_or(0);
state.select(Some(i.saturating_sub(1)));
}
KeyCode::Enter if !rows.is_empty() => {
// rows.get, not rows[i]: the stored selection can point
// past the list if a concurrent `swapdex rm` shrank it.
if let Some(name) = state
.selected()
.and_then(|i| rows.get(i))
.map(|r| r.name.clone())
{
let (ok, msg) = ctx.switch(&name);
status = msg;
rows = ctx.rows();
clamp_selection(&mut state, rows.len());
if ok {
let (label, entries, tools) = ctx.sessions(&name);
let new_conv = new_conv_for(&tools);
open_state.select(Some(0));
// A single-tool profile: skip the menu entirely
// when there are no sessions to pick - go
// straight to that tool's folder browser.
if entries.is_empty() && new_conv.len() == 1 {
let tool = new_conv[0].1;
let cwd = std::env::current_dir()
.ok()
.or_else(dirs::home_dir)
.unwrap_or_else(|| PathBuf::from("/"));
let frows = folder_rows(&cwd);
screen = Screen::Folder {
tool,
cwd,
rows: frows,
back: (label, entries, new_conv),
};
} else {
screen = Screen::Open {
label,
entries,
new_conv,
};
}
}
}
}
KeyCode::Char('o') if !rows.is_empty() => {
if let Some(name) = state
.selected()
.and_then(|i| rows.get(i))
.map(|r| r.name.clone())
{
// Switch FIRST, like Enter: opening a NEW conversation
// (or resuming) launches under whatever account is
// live, so without switching, `o` on a non-active
// profile would open the wrong account. `o` differs
// from Enter only in always showing the full menu
// (Enter shortcuts a single-tool profile to the folder).
let (ok, msg) = ctx.switch(&name);
status = msg;
rows = ctx.rows();
clamp_selection(&mut state, rows.len());
if ok {
let (label, entries, tools) = ctx.sessions(&name);
let new_conv = new_conv_for(&tools);
open_state.select(Some(0));
screen = Screen::Open {
label,
entries,
new_conv,
};
}
}
}
KeyCode::Char('a') => {
open_state.select(Some(0));
screen = Screen::ToolPick;
}
KeyCode::Char('s') if rows.is_empty() && !onboard_live.is_empty() => {
// Onboarding: save the accounts you're already logged
// into as your first profile.
screen = Screen::Input {
kind: InputKind::SaveCurrent,
value: String::new(),
};
}
KeyCode::Char('n') if !rows.is_empty() => {
if let Some(name) = state
.selected()
.and_then(|i| rows.get(i))
.map(|r| r.name.clone())
{
screen = Screen::Input {
kind: InputKind::Rename(name),
value: String::new(),
};
}
}
KeyCode::Char('r') => {
status = ctx.restore();
rows = ctx.rows();
}
KeyCode::Char('d') if !rows.is_empty() => {
confirm_delete = state.selected();
}
KeyCode::Char('?') => {
screen = Screen::Doctor {
lines: vec!["running health check...".into()],
scroll: 0,
pending: true,
};
}
KeyCode::Char('u') if !rows.is_empty() => {
screen = Screen::Usage {
lines: vec!["computing usage...".into()],
scroll: 0,
pending: true,
};
}
// Ungated like doctor's '?': quota also covers a live
// login that is not saved as any profile yet.
KeyCode::Char('%') => {
screen = Screen::Quota {
lines: vec!["fetching remaining quota from Anthropic...".into()],
scroll: 0,
pending: true,
};
}
_ => {}
}
}
Screen::Open {
entries, new_conv, ..
} => match key.code {
KeyCode::Esc | KeyCode::Char('q') => {
rows = ctx.rows();
screen = Screen::Main;
}
KeyCode::Down | KeyCode::Char('j') => {
// saturating: both lists can be empty (a profile whose
// store entry vanished mid-session) - `- 1` would panic.
let max = (entries.len() + new_conv.len()).saturating_sub(1);
let i = open_state.selected().unwrap_or(0);
open_state.select(Some((i + 1).min(max)));
}
KeyCode::Up | KeyCode::Char('k') => {
let i = open_state.selected().unwrap_or(0);
open_state.select(Some(i.saturating_sub(1)));
}
KeyCode::Enter => {
let i = open_state.selected().unwrap_or(0);
if i < entries.len() {
break 'ui Outcome::OpenSession(i);
}
let Some(&(_, tool)) = new_conv.get(i - entries.len()) else {
continue;
};
let cwd = std::env::current_dir()
.ok()
.or_else(dirs::home_dir)
.unwrap_or_else(|| PathBuf::from("/"));
let frows = folder_rows(&cwd);
open_state.select(Some(0));
if let Screen::Open {
label,
entries,
new_conv: nc,
} = std::mem::replace(
&mut screen,
Screen::Folder {
tool,
cwd,
rows: frows,
back: (String::new(), Vec::new(), Vec::new()),
},
) {
if let Screen::Folder { back, .. } = &mut screen {
*back = (label, entries, nc);
}
}
}
_ => {}
},
Screen::Folder {
tool,
cwd,
rows: frows,
back,
} => match key.code {
KeyCode::Esc => {
// One step back to the Open menu, not two.
let (label, entries, new_conv) = std::mem::take(back);
screen = Screen::Open {
label,
entries,
new_conv,
};
}
KeyCode::Down | KeyCode::Char('j') => {
let i = open_state.selected().unwrap_or(0);
open_state.select(Some((i + 1).min(frows.len().saturating_sub(1))));
}
KeyCode::Up | KeyCode::Char('k') => {
let i = open_state.selected().unwrap_or(0);
open_state.select(Some(i.saturating_sub(1)));
}
// Left / Backspace = go up a level (a natural browser gesture).
KeyCode::Left | KeyCode::Backspace => {
if let Some(parent) = cwd.parent() {
*cwd = parent.to_path_buf();
*frows = folder_rows(cwd);
open_state.select(Some(0));
}
}
KeyCode::Enter | KeyCode::Right => {
let i = open_state.selected().unwrap_or(0);
match frows.get(i) {
Some(FolderRow::OpenHere) => {
break 'ui Outcome::NewConv {
tool,
dir: Some(cwd.clone()),
};
}
Some(FolderRow::Up) => {
if let Some(parent) = cwd.parent() {
*cwd = parent.to_path_buf();
*frows = folder_rows(cwd);
open_state.select(Some(0));
}
}
Some(FolderRow::Home) => {
if let Some(h) = dirs::home_dir() {
*cwd = h;
*frows = folder_rows(cwd);
open_state.select(Some(0));
}
}
Some(FolderRow::Into(p)) => {
*cwd = p.clone();
*frows = folder_rows(cwd);
open_state.select(Some(0));
}
None => {}
}
}
_ => {}
},
Screen::ToolPick => match key.code {
KeyCode::Esc | KeyCode::Char('q') => screen = Screen::Main,
KeyCode::Down | KeyCode::Char('j') => {
let i = open_state.selected().unwrap_or(0);
open_state.select(Some((i + 1).min(3)));
}
KeyCode::Up | KeyCode::Char('k') => {
let i = open_state.selected().unwrap_or(0);
open_state.select(Some(i.saturating_sub(1)));
}
KeyCode::Enter => {
let tool = ["claude-code", "codex", "gemini", "antigravity"]
[open_state.selected().unwrap_or(0)];
break 'ui Outcome::AddAccount(tool);
}
_ => {}
},
Screen::Input { kind, value } => match key.code {
KeyCode::Esc => screen = Screen::Main,
KeyCode::Backspace => {
value.pop();
}
KeyCode::Char(c) => value.push(c),
KeyCode::Enter => {
let name = value.trim().to_string();
if name.is_empty() {
screen = Screen::Main;
} else {
let (ok, msg) = match kind {
InputKind::Rename(old) => ctx.rename(old, &name),
InputKind::SaveCurrent => ctx.save_current(&name),
};
status = msg;
rows = ctx.rows();
onboard_live = if rows.is_empty() {
ctx.live_tools()
} else {
Vec::new()
};
if ok {
state.select(rows.iter().position(|r| r.name == name).or(Some(0)));
}
screen = Screen::Main;
}
}
_ => {}
},
Screen::Doctor { lines, scroll, .. } => match key.code {
KeyCode::Esc | KeyCode::Char('q') => screen = Screen::Main,
KeyCode::Down | KeyCode::Char('j') => {
let max = (lines.len() as u16).saturating_sub(1);
*scroll = (*scroll + 1).min(max);
}
KeyCode::Up | KeyCode::Char('k') => *scroll = scroll.saturating_sub(1),
_ => {}
},
Screen::Usage { lines, scroll, .. } => match key.code {
KeyCode::Esc | KeyCode::Char('q') => screen = Screen::Main,
KeyCode::Down | KeyCode::Char('j') => {
let max = (lines.len() as u16).saturating_sub(1);
*scroll = (*scroll + 1).min(max);
}
KeyCode::Up | KeyCode::Char('k') => *scroll = scroll.saturating_sub(1),
_ => {}
},
Screen::Quota { lines, scroll, .. } => match key.code {
KeyCode::Esc | KeyCode::Char('q') => screen = Screen::Main,
KeyCode::Down | KeyCode::Char('j') => {
let max = (lines.len() as u16).saturating_sub(1);
*scroll = (*scroll + 1).min(max);
}
KeyCode::Up | KeyCode::Char('k') => *scroll = scroll.saturating_sub(1),
_ => {}
},
}
};
let _ = ratatui::crossterm::execute!(
std::io::stdout(),
ratatui::crossterm::event::DisableMouseCapture
);
ratatui::restore();
Ok(outcome)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn clamp_selection_keeps_index_in_bounds() {
let mut s = ListState::default();
s.select(Some(3));
clamp_selection(&mut s, 2); // list shrank to 2
assert_eq!(s.selected(), Some(1), "clamped to last");
clamp_selection(&mut s, 0); // now empty
assert_eq!(s.selected(), None, "no selection on empty list");
clamp_selection(&mut s, 3); // grew from empty
assert_eq!(s.selected(), Some(0), "reselect top when non-empty");
s.select(Some(1));
clamp_selection(&mut s, 5); // still in range
assert_eq!(s.selected(), Some(1), "untouched when in range");
}
#[test]
fn click_row_index_accounts_for_scroll_offset() {
// top=5, per=1 (a 1-line menu row). No scroll: clicking the first inner
// row selects item 0; clicking two rows down selects item 2.
assert_eq!(click_row_index(0, 5, 5, 1), 0);
assert_eq!(click_row_index(0, 7, 5, 1), 2);
// Scrolled down by 3: the first VISIBLE row is item 3, not 0 - the old
// math returned 0 here and opened a hidden earlier session.
assert_eq!(click_row_index(3, 5, 5, 1), 3);
assert_eq!(click_row_index(3, 7, 5, 1), 5);
// 3-line Main rows (per=3): two text rows down is still the same entry.
assert_eq!(click_row_index(0, 6, 5, 3), 0);
assert_eq!(click_row_index(0, 8, 5, 3), 1);
}
#[test]
fn new_conv_only_offers_the_profiles_tools() {
// A Claude-only profile offers just Claude, not all four.
let one = super::new_conv_for(&["claude-code"]);
assert_eq!(one.len(), 1);
assert_eq!(one[0].1, "claude-code");
// A two-tool profile offers exactly those two, in canonical order.
let two = super::new_conv_for(&["gemini", "codex"]);
let tools: Vec<&str> = two.iter().map(|(_, t)| *t).collect();
assert_eq!(tools, vec!["codex", "gemini"]);
}
#[test]
fn folder_rows_lead_with_open_here_and_hide_dotfiles() {
let dir = tempfile::tempdir().unwrap();
std::fs::create_dir(dir.path().join("visible")).unwrap();
std::fs::create_dir(dir.path().join(".hidden")).unwrap();
std::fs::write(dir.path().join("afile"), b"x").unwrap();
let rows = folder_rows(dir.path());
assert!(matches!(rows[0], FolderRow::OpenHere), "open-here is first");
assert!(
rows.iter().any(|r| matches!(r, FolderRow::Up)),
"parent exists -> Up row present"
);
let into: Vec<_> = rows
.iter()
.filter_map(|r| match r {
FolderRow::Into(p) => p.file_name().and_then(|n| n.to_str()),
_ => None,
})
.collect();
assert_eq!(into, vec!["visible"], "only non-dot subdirs, no files");
}
#[test]
fn tildify_collapses_home() {
if let Some(home) = dirs::home_dir() {
assert_eq!(tildify(&home), "~");
assert_eq!(tildify(&home.join("proj")), "~/proj");
}
assert_eq!(tildify(std::path::Path::new("/etc")), "/etc");
}
}