Skip to main content

lean_ctx/cli/
common.rs

1pub(crate) fn print_savings(original: usize, sent: usize) {
2    let footer = crate::core::protocol::format_savings(original, sent);
3    if !footer.is_empty() {
4        println!("{footer}");
5    }
6}
7
8/// Strip savings footers from daemon output when the CLI client has footer suppressed.
9#[cfg(unix)]
10pub(crate) fn filter_daemon_output(text: &str) -> String {
11    if crate::core::protocol::savings_footer_visible() {
12        return text.to_string();
13    }
14    text.lines()
15        .filter(|l| {
16            let t = l.trim();
17            !(t.starts_with('[')
18                && t.contains("tok")
19                && t.ends_with(']')
20                && (t.contains("tok saved") || t.contains("lean-ctx:") || t.contains("vs native")))
21        })
22        .collect::<Vec<_>>()
23        .join("\n")
24}
25
26pub fn load_shell_history_pub() -> Vec<String> {
27    load_shell_history()
28}
29
30pub(crate) fn load_shell_history() -> Vec<String> {
31    let shell = std::env::var("SHELL").unwrap_or_default();
32    let Some(home) = dirs::home_dir() else {
33        return Vec::new();
34    };
35
36    let history_file = if shell.contains("zsh") {
37        home.join(".zsh_history")
38    } else if shell.contains("fish") {
39        home.join(".local/share/fish/fish_history")
40    } else if cfg!(windows) && shell.is_empty() {
41        home.join("AppData")
42            .join("Roaming")
43            .join("Microsoft")
44            .join("Windows")
45            .join("PowerShell")
46            .join("PSReadLine")
47            .join("ConsoleHost_history.txt")
48    } else {
49        home.join(".bash_history")
50    };
51
52    // Shell history files (especially zsh's metafied format) frequently contain
53    // non-UTF-8 bytes; `read_to_string` would reject the whole file. Read raw and
54    // decode lossily so a single bad byte never hides 900 lines of real history.
55    match std::fs::read(&history_file) {
56        Ok(bytes) => String::from_utf8_lossy(&bytes)
57            .lines()
58            .filter_map(|l| {
59                let trimmed = l.trim();
60                if trimmed.starts_with(':') {
61                    trimmed
62                        .split(';')
63                        .nth(1)
64                        .map(std::string::ToString::to_string)
65                } else {
66                    Some(trimmed.to_string())
67                }
68            })
69            .filter(|l| !l.is_empty())
70            .collect(),
71        Err(_) => Vec::new(),
72    }
73}
74
75pub(crate) fn daemon_fallback_hint() {
76    use std::sync::Once;
77    static HINT: Once = Once::new();
78    HINT.call_once(|| {
79        if crate::core::protocol::meta_visible() {
80            eprintln!("\x1b[2;33mhint: daemon not running — stats tracked locally (lean-ctx serve -d for full tracking)\x1b[0m");
81        }
82    });
83}
84
85pub(crate) fn format_tokens_cli(tokens: u64) -> String {
86    if tokens >= 1_000_000 {
87        format!("{:.1}M", tokens as f64 / 1_000_000.0)
88    } else if tokens >= 1_000 {
89        format!("{:.1}K", tokens as f64 / 1_000.0)
90    } else {
91        format!("{tokens}")
92    }
93}
94
95pub(crate) fn cli_track_read(path: &str, mode: &str, original_tokens: usize, output_tokens: usize) {
96    crate::core::tool_lifecycle::record_file_read(
97        path,
98        mode,
99        original_tokens,
100        output_tokens,
101        false,
102    );
103}
104
105pub(crate) fn cli_track_read_cached(
106    path: &str,
107    mode: &str,
108    original_tokens: usize,
109    output_tokens: usize,
110) {
111    crate::core::tool_lifecycle::record_file_read(path, mode, original_tokens, output_tokens, true);
112}
113
114pub(crate) fn cli_track_search(original_tokens: usize, output_tokens: usize) {
115    crate::core::tool_lifecycle::record_search(original_tokens, output_tokens);
116}
117
118pub(crate) fn cli_track_tree(original_tokens: usize, output_tokens: usize) {
119    crate::core::tool_lifecycle::record_tree(original_tokens, output_tokens);
120}
121
122pub(crate) fn detect_project_root(args: &[String]) -> String {
123    let mut it = args.iter().peekable();
124    while let Some(a) = it.next() {
125        if let Some(v) = a.strip_prefix("--root=") {
126            if !v.trim().is_empty() {
127                return promote_to_git_root(v);
128            }
129        }
130        if let Some(v) = a.strip_prefix("--project-root=") {
131            if !v.trim().is_empty() {
132                return promote_to_git_root(v);
133            }
134        }
135        if a == "--root" || a == "--project-root" {
136            if let Some(v) = it.peek() {
137                if !v.starts_with("--") && !v.trim().is_empty() {
138                    return promote_to_git_root(v);
139                }
140            }
141        }
142    }
143    let cwd = std::env::current_dir()
144        .ok()
145        .map_or_else(|| ".".to_string(), |p| p.to_string_lossy().to_string());
146    promote_to_git_root(&cwd)
147}
148
149fn promote_to_git_root(path: &str) -> String {
150    let mut p = std::path::Path::new(path);
151    loop {
152        if p.join(".git").exists() {
153            return p.to_string_lossy().to_string();
154        }
155        match p.parent() {
156            Some(parent) => p = parent,
157            None => return path.to_string(),
158        }
159    }
160}