use crate::status::{short_duration, short_session, short_tokens, Status};
use eclipse_core::config::ClearOnExit;
use std::io::Write;
pub fn prefix(mode: ClearOnExit) -> &'static str {
match mode {
ClearOnExit::Screen => "\x1b[2J\x1b[H",
ClearOnExit::All => "\x1b[2J\x1b[H\x1b[3J",
ClearOnExit::Off => "",
}
}
pub fn line(status: &Status) -> String {
let mut parts: Vec<String> = vec![format!("eclipse {}", env!("CARGO_PKG_VERSION"))];
if let Some(session) = &status.session {
parts.push(format!("session {}", short_session(session)));
}
parts.push(match status.totals.turns {
0 => "no turns".to_string(),
1 => "1 turn".to_string(),
n => format!("{n} turns"),
});
let tokens = status.totals.prompt_tokens + status.totals.completion_tokens;
if tokens > 0 {
parts.push(format!("{} tokens", short_tokens(tokens)));
}
let elapsed = status.started.elapsed().as_millis() as u64;
if elapsed >= 1_000 {
parts.push(short_duration(elapsed));
}
parts.join(" · ")
}
pub fn print(mode: ClearOnExit, text: &str) {
let mut out = std::io::stdout();
let _ = write!(out, "{}", prefix(mode));
let _ = writeln!(out, "{text}");
let _ = out.flush();
}
#[cfg(test)]
mod tests {
use super::*;
fn status() -> Status {
let mut s = Status::new("m", "host:1", 1000, "/work".to_string());
s.session = Some("sess-0123456789abcdef".to_string());
s
}
#[test]
fn only_the_clearing_modes_carry_escapes() {
assert_eq!(prefix(ClearOnExit::Off), "");
assert!(prefix(ClearOnExit::Screen).contains("\x1b[2J"));
assert!(!prefix(ClearOnExit::Screen).contains("\x1b[3J"));
assert!(
prefix(ClearOnExit::All).contains("\x1b[3J"),
"all is the one that takes the scrollback with it"
);
}
#[test]
fn a_run_with_no_turns_says_so_and_prints_no_figures() {
let text = line(&status());
assert!(text.starts_with(&format!("eclipse {}", env!("CARGO_PKG_VERSION"))));
assert!(text.contains("no turns"), "{text}");
assert!(!text.contains("tokens"), "no turn reported usage: {text}");
assert!(
text.contains("session …"),
"the id's tail, not the whole id: {text}"
);
}
#[test]
fn the_figures_are_the_ones_the_run_recorded() {
let mut s = status();
s.totals.turns = 12;
s.totals.prompt_tokens = 90_000;
s.totals.completion_tokens = 3_300;
let text = line(&s);
assert!(text.contains("12 turns"), "{text}");
assert!(text.contains("93.3K tokens"), "{text}");
}
#[test]
fn one_turn_is_not_one_turns() {
let mut s = status();
s.totals.turns = 1;
assert!(line(&s).contains("1 turn ·") || line(&s).ends_with("1 turn"));
}
}