claude_code_toolkit/cli/commands/
timer.rs

1use crate::{ config::credentials::CredentialsManager, error::* };
2use console::{ Term, style };
3use std::time::Duration;
4use tokio::time::sleep;
5
6pub async fn handle_timer() -> Result<()> {
7  let term = Term::stdout();
8  let credentials_manager = CredentialsManager::new()?;
9
10  println!("{}", style("⏱️  Claude Code Session Timer").bold());
11  println!();
12  println!("{}", style("Press Ctrl+C to exit").dim());
13  println!();
14
15  loop {
16    // Clear previous lines if not first iteration
17    term.move_cursor_up(3)?;
18    term.clear_line()?;
19    term.move_cursor_up(1)?;
20    term.clear_line()?;
21    term.move_cursor_up(1)?;
22    term.clear_line()?;
23
24    match display_session_info(&credentials_manager).await {
25      Ok(()) => {}
26      Err(e) => {
27        println!("{}", style(format!("Error reading session: {}", e)).red());
28        println!();
29        println!();
30      }
31    }
32
33    // Wait for 1 second before next update
34    sleep(Duration::from_secs(1)).await;
35  }
36}
37
38async fn display_session_info(credentials_manager: &CredentialsManager) -> Result<()> {
39  let session_info = credentials_manager.get_session_info().await?;
40
41  if session_info.is_expired {
42    println!("Session Status: {}", style("EXPIRED").red().bold());
43    println!("{}", style("Please authenticate with Claude Code").red());
44    println!();
45  } else {
46    let time_str = CredentialsManager::format_time_remaining(session_info.time_remaining);
47
48    // Determine color based on time remaining
49    let time_style = if session_info.time_remaining < 30 * 60 * 1000 {
50      // Less than 30 minutes - red
51      style(time_str).red().bold()
52    } else if session_info.time_remaining < 2 * 60 * 60 * 1000 {
53      // Less than 2 hours - yellow
54      style(time_str).yellow().bold()
55    } else {
56      // More than 2 hours - green
57      style(time_str).green().bold()
58    };
59
60    println!("Session Status: {}", style("ACTIVE").green().bold());
61    println!("Time Remaining: {}", time_style);
62
63    // Progress bar
64    let session_duration = 7 * 24 * 60 * 60 * 1000; // Assume 7-day sessions
65    let percentage = (
66      ((session_info.time_remaining as f64) / (session_duration as f64)) *
67      100.0
68    ).clamp(0.0, 100.0);
69
70    let bar_length = 30;
71    let filled = ((percentage / 100.0) * (bar_length as f64)) as usize;
72    let empty = bar_length - filled;
73
74    let progress_bar = format!(
75      "{}{}",
76      style("█".repeat(filled)).green(),
77      style("░".repeat(empty)).dim()
78    );
79
80    println!("Progress: {} {:.1}%", progress_bar, percentage);
81  }
82
83  Ok(())
84}