clauth 0.2.0

A simple Claude Code account switcher - swap OAuth and API profiles in an instant
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
use std::io::{self, Write};
use std::time::Duration;

use anyhow::Result;
use crossterm::cursor::{Hide, MoveTo, Show};
use crossterm::event::{self, Event, KeyCode, KeyEventKind, KeyModifiers};
use crossterm::style::Print;
use crossterm::terminal::{self, Clear, ClearType};
use crossterm::{execute, queue};
use inquire::{CustomType, InquireError, Select};

use crate::actions::{delete_profile, edit_profile, is_cancelled, rename_profile, switch_profile};
use crate::fallback::{DEFAULT_THRESHOLD, threshold_for};
use crate::profile::{AppConfig, save_app_state, save_profile};
use crate::ui::{
    C_ACCENT, C_BOLD, C_DANGER, C_DIM, C_FAINT, C_FG_OFF, C_NOBOLD, C_ORANGE, C_RESET,
    endpoint_visible_width, format_profile_entry, format_submenu_title, visible_width,
    weekly_bar_visible_width,
};

// ── Profile submenu ───────────────────────────────────────────────────────────

#[derive(Clone, Copy)]
enum SubmenuAction {
    Switch,
    Edit,
    Rename,
    Delete,
    Back,
}

impl SubmenuAction {
    fn label(self, is_active: bool) -> String {
        match self {
            Self::Switch if is_active => {
                format!("Switch to this profile{C_NOBOLD}  {C_FAINT}(already active){C_RESET}")
            }
            Self::Switch => "Switch to this profile".to_string(),
            Self::Edit => format!("Edit{C_NOBOLD}  {C_DIM}(URL / API key){C_RESET}"),
            Self::Rename => "Rename".to_string(),
            Self::Delete => format!("{C_DANGER}Delete{C_RESET}"),
            Self::Back => format!("{C_FAINT}← Back{C_RESET}"),
        }
    }
}

pub(crate) fn profile_submenu(config: &mut AppConfig, profile_name: &str) -> Result<()> {
    use SubmenuAction::*;
    const ACTIONS: [SubmenuAction; 5] = [Switch, Edit, Rename, Delete, Back];

    let start_row = crossterm::cursor::position().map(|(_, r)| r).ok();

    loop {
        if let Some(row) = start_row {
            let _ = execute!(
                io::stdout(),
                MoveTo(0, row),
                Clear(ClearType::FromCursorDown)
            );
        }

        let (title, is_active) = match config.find(profile_name) {
            Some(p) => (format_submenu_title(p), config.is_active(profile_name)),
            None => return Ok(()),
        };

        let labels: Vec<String> = ACTIONS.iter().map(|a| a.label(is_active)).collect();

        let idx = match Select::new(&title, labels)
            .without_filtering()
            .without_help_message()
            .raw_prompt()
        {
            Ok(opt) => opt.index,
            Err(InquireError::OperationCanceled | InquireError::OperationInterrupted) => {
                return Ok(());
            }
            Err(e) => return Err(e.into()),
        };

        let result: Result<bool> = match ACTIONS[idx] {
            Switch => {
                // Refresh every profile's OAuth token before linking the
                // target — the rotated access token then ends up in
                // ~/.claude/.credentials.json via the symlink, and the
                // menu's usage column shows fresh data for the others.
                let _ = crate::oauth::refresh_all(config);
                switch_profile(config, profile_name).map(|_| true)
            }
            Edit => edit_profile(config, profile_name).map(|_| false),
            Rename => rename_profile(config, profile_name),
            Delete => delete_profile(config, profile_name),
            Back => return Ok(()),
        };

        match result {
            Ok(true) => return Ok(()),
            Ok(false) => {}
            Err(e) if is_cancelled(&e) => {}
            Err(e) => return Err(e),
        }
    }
}

// ── Main menu ─────────────────────────────────────────────────────────────────

#[derive(Clone, Copy)]
pub(crate) enum MainAction {
    Profile(usize),
    NewBlank,
    Capture,
    FallbackChain,
    Quit,
}

pub(crate) enum MainMenuResult {
    Selected(usize),
    Cancelled,
}

struct RawModeGuard {
    row: u16,
}

impl RawModeGuard {
    fn new(lines_needed: u16) -> Result<Self> {
        let mut out = io::stdout();
        let (_, rows) = terminal::size()?;
        let (_, mut row) = crossterm::cursor::position()?;

        let shift = (row + lines_needed).saturating_sub(rows);
        if shift > 0 {
            // `\n` only scrolls when the cursor is on the last row; otherwise
            // it just moves the cursor down and we end up rendering over
            // existing history. Park on the bottom row first so each newline
            // pushes a line into scrollback.
            execute!(out, MoveTo(0, rows.saturating_sub(1)))?;
            write!(out, "{}", "\n".repeat(shift as usize))?;
            out.flush()?;
            row = row.saturating_sub(shift);
        }

        terminal::enable_raw_mode()?;
        execute!(out, MoveTo(0, row), Hide)?;
        Ok(Self { row })
    }
}

impl Drop for RawModeGuard {
    fn drop(&mut self) {
        let mut out = io::stdout();
        let _ = execute!(
            out,
            MoveTo(0, self.row),
            Clear(ClearType::FromCursorDown),
            Show
        );
        let _ = terminal::disable_raw_mode();
    }
}

fn render_main_menu(labels: &[String], selected: usize, row: u16) -> Result<()> {
    let mut out = io::stdout();
    queue!(out, MoveTo(0, row), Clear(ClearType::FromCursorDown))?;
    queue!(out, Print(format!("{C_ACCENT}?{C_RESET} clauth")))?;

    for (i, label) in labels.iter().enumerate() {
        queue!(out, MoveTo(0, row + i as u16 + 1))?;
        if i == selected {
            queue!(
                out,
                Print(format!("{C_ORANGE}{C_RESET} {C_BOLD}{label}{C_RESET}"))
            )?;
        } else {
            queue!(out, Print(format!("  {label}")))?;
        }
    }

    out.flush()?;
    Ok(())
}

pub(crate) fn main_menu_prompt(
    mut labels: Vec<String>,
    mut refresh: impl FnMut() -> Vec<String>,
) -> Result<MainMenuResult> {
    if labels.is_empty() {
        return Ok(MainMenuResult::Cancelled);
    }

    let lines_needed = labels.len() as u16 + 1;
    let raw_mode = RawModeGuard::new(lines_needed)?;

    let mut selected = 0;
    render_main_menu(&labels, selected, raw_mode.row)?;

    loop {
        if !event::poll(Duration::from_millis(500))? {
            let refreshed = refresh();
            if !refreshed.is_empty() {
                labels = refreshed;
                selected = selected.min(labels.len() - 1);
                render_main_menu(&labels, selected, raw_mode.row)?;
            }
            continue;
        }

        let Event::Key(key) = event::read()? else {
            continue;
        };
        if key.kind != KeyEventKind::Press {
            continue;
        }

        match key.code {
            KeyCode::Up | KeyCode::Char('k') => {
                selected = if selected == 0 {
                    labels.len() - 1
                } else {
                    selected - 1
                };
                render_main_menu(&labels, selected, raw_mode.row)?;
            }
            KeyCode::Down | KeyCode::Char('j') => {
                selected = (selected + 1) % labels.len();
                render_main_menu(&labels, selected, raw_mode.row)?;
            }
            KeyCode::Home => {
                selected = 0;
                render_main_menu(&labels, selected, raw_mode.row)?;
            }
            KeyCode::End => {
                selected = labels.len() - 1;
                render_main_menu(&labels, selected, raw_mode.row)?;
            }
            KeyCode::Enter => return Ok(MainMenuResult::Selected(selected)),
            KeyCode::Esc | KeyCode::Char('q' | 'Q') => {
                return Ok(MainMenuResult::Cancelled);
            }
            KeyCode::Char('c') if key.modifiers.contains(KeyModifiers::CONTROL) => {
                return Ok(MainMenuResult::Cancelled);
            }
            _ => {}
        }
    }
}

pub(crate) fn build_main_menu(config: &AppConfig) -> Vec<(String, MainAction)> {
    let name_width = config
        .profiles
        .iter()
        .map(|p| p.name.len())
        .max()
        .unwrap_or(0)
        .max(4);

    // Align bars to the widest endpoint among profiles that actually render
    // one. Profiles without a fetched 5-hour window don't push the column.
    let endpoint_width = config
        .profiles
        .iter()
        .filter(|p| {
            p.usage
                .as_ref()
                .and_then(|u| u.five_hour.as_ref())
                .is_some()
        })
        .map(endpoint_visible_width)
        .max()
        .unwrap_or(0);

    let cols = terminal::size().map(|(c, _)| c as usize).unwrap_or(80);
    let max_base_width = config
        .profiles
        .iter()
        .map(|p| {
            visible_width(&format_profile_entry(
                p,
                config.is_active(&p.name),
                name_width,
                endpoint_width,
                false,
            ))
        })
        .max()
        .unwrap_or(0);
    let max_weekly_width = config
        .profiles
        .iter()
        .map(weekly_bar_visible_width)
        .max()
        .unwrap_or(0);
    let show_weekly = max_weekly_width > 0 && max_base_width + max_weekly_width <= cols;

    let mut items = Vec::with_capacity(config.profiles.len() + 3);

    for (i, p) in config.profiles.iter().enumerate() {
        items.push((
            format_profile_entry(
                p,
                config.is_active(&p.name),
                name_width,
                endpoint_width,
                show_weekly,
            ),
            MainAction::Profile(i),
        ));
    }
    items.push((
        format!("{C_ORANGE}+{C_FG_OFF} New profile"),
        MainAction::NewBlank,
    ));
    items.push((
        format!("{C_ORANGE}+{C_FG_OFF} New from current profile"),
        MainAction::Capture,
    ));
    let chain_len = config.state.fallback_chain.len();
    let chain_hint = if chain_len == 0 {
        format!("{C_FAINT}empty{C_RESET}")
    } else {
        format!(
            "{C_DIM}{chain_len} profile{}{C_RESET}",
            if chain_len == 1 { "" } else { "s" }
        )
    };
    items.push((
        format!("{C_ACCENT}{C_FG_OFF} Fallback chain  {chain_hint}"),
        MainAction::FallbackChain,
    ));
    items.push((format!("{C_FAINT}Quit{C_RESET}"), MainAction::Quit));

    items
}

// ── Fallback chain editor ─────────────────────────────────────────────────────

fn five_hour_pct(config: &AppConfig, name: &str) -> Option<f64> {
    config
        .find(name)?
        .usage
        .as_ref()?
        .five_hour
        .as_ref()
        .map(|w| w.utilization)
}

fn chain_member_label(config: &AppConfig, position: usize, name: &str) -> String {
    let threshold = config
        .find(name)
        .map(threshold_for)
        .unwrap_or(DEFAULT_THRESHOLD);
    let usage = match five_hour_pct(config, name) {
        Some(pct) => {
            let color = if pct >= threshold {
                C_DANGER
            } else if pct >= threshold * 0.8 {
                C_ORANGE
            } else {
                C_DIM
            };
            format!("{color}5h {pct:.0}%{C_RESET}")
        }
        None => format!("{C_FAINT}5h n/a{C_RESET}"),
    };
    let active = if config.is_active(name) {
        format!("{C_ACCENT}{C_RESET}")
    } else {
        "  ".to_string()
    };
    format!(
        "{C_FAINT}{position:>2}.{C_RESET} {active}{C_BOLD}{name}{C_RESET}  {C_FAINT}@ {threshold:.0}%{C_RESET}  {usage}"
    )
}

#[derive(Clone, Copy)]
enum ChainItemAction {
    Threshold,
    MoveUp,
    MoveDown,
    Remove,
    Back,
}

fn chain_item_submenu(config: &mut AppConfig, name: &str) -> Result<()> {
    use ChainItemAction::*;

    loop {
        let position = match config.state.fallback_chain.iter().position(|n| n == name) {
            Some(p) => p,
            None => return Ok(()),
        };
        let chain_len = config.state.fallback_chain.len();
        let current = config
            .find(name)
            .map(threshold_for)
            .unwrap_or(DEFAULT_THRESHOLD);

        let mut actions: Vec<(String, ChainItemAction)> = Vec::new();
        actions.push((
            format!("Set threshold  {C_FAINT}(current: {current:.0}%){C_RESET}"),
            Threshold,
        ));
        if position > 0 {
            actions.push(("Move up".to_string(), MoveUp));
        }
        if position + 1 < chain_len {
            actions.push(("Move down".to_string(), MoveDown));
        }
        actions.push((format!("{C_DANGER}Remove from chain{C_RESET}"), Remove));
        actions.push((format!("{C_FAINT}← Back{C_RESET}"), Back));

        let title = format!("{C_BOLD}{name}{C_RESET}{C_FAINT} · fallback chain{C_RESET}");
        let labels: Vec<String> = actions.iter().map(|(l, _)| l.clone()).collect();
        let idx = match Select::new(&title, labels)
            .without_filtering()
            .without_help_message()
            .raw_prompt()
        {
            Ok(opt) => opt.index,
            Err(InquireError::OperationCanceled | InquireError::OperationInterrupted) => {
                return Ok(());
            }
            Err(e) => return Err(e.into()),
        };

        match actions[idx].1 {
            Threshold => {
                let entered: Result<f64, _> = CustomType::<f64>::new("Threshold percentage:")
                    .with_default(current)
                    .with_help_message(
                        "Auto-switch off this profile at this 5h utilization (0..=100).",
                    )
                    .with_error_message("Enter a number between 0 and 100.")
                    .with_validator(|v: &f64| {
                        if (0.0..=100.0).contains(v) {
                            Ok(inquire::validator::Validation::Valid)
                        } else {
                            Ok(inquire::validator::Validation::Invalid(
                                "Threshold must be between 0 and 100.".into(),
                            ))
                        }
                    })
                    .prompt()
                    .map_err(anyhow::Error::from);
                let value = match entered {
                    Ok(v) => v,
                    Err(e) if is_cancelled(&e) => continue,
                    Err(e) => return Err(e),
                };
                if let Some(profile) = config.find_mut(name) {
                    profile.fallback_threshold = Some(value);
                    save_profile(profile)?;
                }
            }
            MoveUp => {
                config.state.fallback_chain.swap(position - 1, position);
                save_app_state(&config.state)?;
            }
            MoveDown => {
                config.state.fallback_chain.swap(position, position + 1);
                save_app_state(&config.state)?;
            }
            Remove => {
                config.state.fallback_chain.retain(|n| n != name);
                save_app_state(&config.state)?;
                return Ok(());
            }
            Back => return Ok(()),
        }
    }
}

fn add_to_chain_prompt(config: &mut AppConfig) -> Result<()> {
    let candidates: Vec<String> = config
        .profiles
        .iter()
        .map(|p| p.name.clone())
        .filter(|n| !config.state.fallback_chain.iter().any(|c| c == n))
        .collect();
    if candidates.is_empty() {
        return Ok(());
    }

    let title = format!("{C_BOLD}Add profile to chain{C_RESET}");
    let selection = Select::new(&title, candidates.clone())
        .without_filtering()
        .without_help_message()
        .raw_prompt();
    let idx = match selection {
        Ok(opt) => opt.index,
        Err(InquireError::OperationCanceled | InquireError::OperationInterrupted) => return Ok(()),
        Err(e) => return Err(e.into()),
    };
    let name = candidates[idx].clone();

    if let Some(profile) = config.find_mut(&name)
        && profile.fallback_threshold.is_none()
    {
        profile.fallback_threshold = Some(DEFAULT_THRESHOLD);
        save_profile(profile)?;
    }
    config.state.fallback_chain.push(name);
    save_app_state(&config.state)?;
    Ok(())
}

pub(crate) fn fallback_chain_menu(config: &mut AppConfig) -> Result<()> {
    let start_row = crossterm::cursor::position().map(|(_, r)| r).ok();

    loop {
        if let Some(row) = start_row {
            let _ = execute!(
                io::stdout(),
                MoveTo(0, row),
                Clear(ClearType::FromCursorDown)
            );
        }

        let chain = config.state.fallback_chain.clone();
        let mut entries: Vec<(String, Option<String>)> = Vec::new();
        for (i, name) in chain.iter().enumerate() {
            entries.push((chain_member_label(config, i + 1, name), Some(name.clone())));
        }

        let any_unchained = config
            .profiles
            .iter()
            .any(|p| !chain.iter().any(|c| c == &p.name));
        if any_unchained {
            entries.push((format!("{C_ORANGE}+{C_FG_OFF} Add profile to chain"), None));
        }
        entries.push((format!("{C_FAINT}← Back{C_RESET}"), Some(String::new())));

        let title = if chain.is_empty() {
            format!(
                "{C_BOLD}Fallback chain{C_RESET}{C_FAINT} · empty — add a profile to enable auto-switch{C_RESET}"
            )
        } else {
            format!(
                "{C_BOLD}Fallback chain{C_RESET}{C_FAINT} · {} profile{}{C_RESET}",
                chain.len(),
                if chain.len() == 1 { "" } else { "s" }
            )
        };
        let labels: Vec<String> = entries.iter().map(|(l, _)| l.clone()).collect();
        let idx = match Select::new(&title, labels)
            .without_filtering()
            .without_help_message()
            .raw_prompt()
        {
            Ok(opt) => opt.index,
            Err(InquireError::OperationCanceled | InquireError::OperationInterrupted) => {
                return Ok(());
            }
            Err(e) => return Err(e.into()),
        };

        match entries[idx].1.clone() {
            None => add_to_chain_prompt(config)?,
            Some(s) if s.is_empty() => return Ok(()),
            Some(name) => {
                if let Err(e) = chain_item_submenu(config, &name)
                    && !is_cancelled(&e)
                {
                    return Err(e);
                }
            }
        }
    }
}