1pub(crate) fn print_savings(original: usize, sent: usize) {
2 let saved = original.saturating_sub(sent);
3 if original > 0 && saved > 0 {
4 let pct = (saved as f64 / original as f64 * 100.0).round() as usize;
5 println!("[{saved} tok saved ({pct}%)]");
6 }
7}
8
9pub fn load_shell_history_pub() -> Vec<String> {
10 load_shell_history()
11}
12
13pub(crate) fn load_shell_history() -> Vec<String> {
14 let shell = std::env::var("SHELL").unwrap_or_default();
15 let Some(home) = dirs::home_dir() else {
16 return Vec::new();
17 };
18
19 let history_file = if shell.contains("zsh") {
20 home.join(".zsh_history")
21 } else if shell.contains("fish") {
22 home.join(".local/share/fish/fish_history")
23 } else if cfg!(windows) && shell.is_empty() {
24 home.join("AppData")
25 .join("Roaming")
26 .join("Microsoft")
27 .join("Windows")
28 .join("PowerShell")
29 .join("PSReadLine")
30 .join("ConsoleHost_history.txt")
31 } else {
32 home.join(".bash_history")
33 };
34
35 match std::fs::read_to_string(&history_file) {
36 Ok(content) => content
37 .lines()
38 .filter_map(|l| {
39 let trimmed = l.trim();
40 if trimmed.starts_with(':') {
41 trimmed
42 .split(';')
43 .nth(1)
44 .map(std::string::ToString::to_string)
45 } else {
46 Some(trimmed.to_string())
47 }
48 })
49 .filter(|l| !l.is_empty())
50 .collect(),
51 Err(_) => Vec::new(),
52 }
53}
54
55pub(crate) fn format_tokens_cli(tokens: u64) -> String {
56 if tokens >= 1_000_000 {
57 format!("{:.1}M", tokens as f64 / 1_000_000.0)
58 } else if tokens >= 1_000 {
59 format!("{:.1}K", tokens as f64 / 1_000.0)
60 } else {
61 format!("{tokens}")
62 }
63}