pub const MARKER: char = '│';
pub const PAST_MARKER: char = '▓';
pub fn short_tokens(n: u64) -> String {
match n {
0..=999 => n.to_string(),
1_000..=999_999 => {
let v = n as f64 / 1_000.0;
if v < 100.0 {
format!("{v:.1}K")
} else {
format!("{v:.0}K")
}
}
_ => {
let v = n as f64 / 1_000_000.0;
if v < 100.0 {
format!("{v:.1}M")
} else {
format!("{v:.0}M")
}
}
}
}
pub fn bar(fraction: f64, width: usize) -> String {
let fraction = fraction.clamp(0.0, 1.0);
let filled = ((fraction * width as f64).round() as usize).min(width);
if filled == 0 && fraction > 0.0 {
return format!("▏{}", "░".repeat(width - 1));
}
format!("{}{}", "█".repeat(filled), "░".repeat(width - filled))
}
pub fn bar_marked(share: f64, marker: f64, width: usize) -> String {
if width == 0 {
return String::new();
}
let show = !marker.is_nan() && marker > 0.0 && marker < 1.0;
if !show {
return bar(share, width);
}
let column = ((marker * width as f64) as usize).min(width - 1);
let share = share.clamp(0.0, 1.0);
let filled = ((share * width as f64).round() as usize).min(width);
let fill = if share >= marker { PAST_MARKER } else { '█' };
let mut cells: Vec<char> = (0..width)
.map(|i| {
if i == column {
MARKER
} else if i < filled {
fill
} else {
'░'
}
})
.collect();
if filled == 0 && share > 0.0 && column != 0 {
cells[0] = '▏';
}
cells.into_iter().collect()
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Reading {
pub used: Option<u64>,
pub denominator: usize,
pub marker: Option<f64>,
}
impl Reading {
pub fn new(used: Option<u64>, denominator: usize, marker: Option<f64>) -> Self {
Self {
used,
denominator,
marker,
}
}
pub fn share(&self) -> Option<f64> {
let used = self.used?;
(self.denominator > 0).then(|| used as f64 / self.denominator as f64)
}
pub fn passed(&self) -> bool {
match (self.share(), self.marker) {
(Some(share), Some(marker)) => share >= marker,
_ => false,
}
}
pub fn bar(&self, width: usize) -> String {
bar_marked(
self.share().unwrap_or(0.0),
self.marker.unwrap_or(0.0),
width,
)
}
pub fn pieces(&self, width: usize) -> Pieces {
if self.denominator == 0 {
return Pieces {
numbers: match self.used {
Some(used) => format!("{} tokens", short_tokens(used)),
None => "--".to_string(),
},
bar: String::new(),
percent: None,
note: Some("no budget"),
};
}
let denom = short_tokens(self.denominator as u64);
Pieces {
numbers: match self.used {
Some(used) => format!("{}/{}", short_tokens(used), denom),
None => format!("--/{denom}"),
},
bar: self.bar(width),
percent: self.share().map(|s| format!("{:.0}%", s * 100.0)),
note: None,
}
}
pub fn text(&self, width: usize) -> String {
self.pieces(width).text()
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct Pieces {
pub numbers: String,
pub bar: String,
pub percent: Option<String>,
pub note: Option<&'static str>,
}
impl Pieces {
pub fn text(&self) -> String {
let mut out = format!("ctx {}", self.numbers);
if !self.bar.is_empty() {
out.push(' ');
out.push_str(&self.bar);
}
if let Some(percent) = &self.percent {
out.push(' ');
out.push_str(percent);
}
if let Some(note) = self.note {
out.push_str(" · ");
out.push_str(note);
}
out
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn the_bar_is_the_same_shape_when_the_denominator_is_the_window() {
let r = Reading::new(Some(30_000), 100_000, Some(0.8));
assert_eq!(r.text(10), "ctx 30.0K/100K ███░░░░░│░ 30%");
assert!(!r.passed());
let past = Reading::new(Some(90_000), 100_000, Some(0.8));
assert!(past.passed());
assert!(past.bar(10).contains(PAST_MARKER), "{}", past.bar(10));
}
#[test]
fn no_turn_yet_is_not_zero_percent() {
let r = Reading::new(None, 328_000, Some(0.85));
assert_eq!(r.text(10), "ctx --/328K ░░░░░░░░│░");
assert_eq!(r.share(), None);
assert!(!r.passed());
}
#[test]
fn a_window_nobody_stated_says_so_instead_of_dividing_by_it() {
let r = Reading::new(Some(5_000), 0, None);
assert_eq!(r.text(10), "ctx 5.0K tokens · no budget");
assert_eq!(Reading::new(None, 0, None).text(10), "ctx -- · no budget");
assert_eq!(r.bar(10), "░░░░░░░░░░");
}
#[test]
fn token_counts_stay_short_and_stable_width() {
assert_eq!(short_tokens(0), "0");
assert_eq!(short_tokens(999), "999");
assert_eq!(short_tokens(1_234), "1.2K");
assert_eq!(short_tokens(23_100), "23.1K");
assert_eq!(short_tokens(327_680), "328K");
assert_eq!(short_tokens(1_234_567), "1.2M");
assert_eq!(short_tokens(12_000_000), "12.0M");
}
#[test]
fn a_tiny_fill_still_shows_a_sliver() {
assert_eq!(bar(0.002, 4), "▏░░░");
assert_eq!(bar(0.0, 4), "░░░░", "zero really is empty");
}
#[test]
fn the_bar_clamps_instead_of_overflowing() {
assert_eq!(bar(0.0, 4), "░░░░");
assert_eq!(bar(0.5, 4), "██░░");
assert_eq!(bar(1.0, 4), "████");
assert_eq!(bar(4.0, 4), "████", "an over-budget prompt fills the bar");
assert_eq!(
bar(-1.0, 4),
"░░░░",
"and a nonsense value cannot underflow"
);
}
#[test]
fn a_marked_bar_puts_the_tripwire_where_the_policy_says_and_never_hides_it() {
assert_eq!(bar_marked(0.0, 0.85, 10), "░░░░░░░░│░", "visible at zero");
assert_eq!(bar_marked(0.5, 0.85, 10), "█████░░░│░");
assert_eq!(
bar_marked(1.0, 0.85, 10),
"▓▓▓▓▓▓▓▓│▓",
"a full window is past the tripwire, and the marker still owns its cell"
);
}
#[test]
fn a_marked_bar_marks_the_edges_of_the_scale_honestly() {
assert_eq!(
bar_marked(0.82, 0.85, 10),
"████████│░",
"just under is not passed"
);
assert_eq!(
bar_marked(0.85, 0.85, 10),
"▓▓▓▓▓▓▓▓│░",
"at the threshold the fill itself changes state"
);
assert_eq!(bar_marked(0.0, 0.05, 10), "│░░░░░░░░░");
assert_eq!(
bar_marked(0.0, 0.85, 1),
"│",
"a one-cell bar has no room but the marker"
);
assert_eq!(bar_marked(0.5, 0.85, 0), "", "no width, no bar");
}
#[test]
fn a_marker_outside_the_scale_is_not_drawn_at_all() {
assert_eq!(
bar_marked(0.5, 0.0, 10),
bar(0.5, 10),
"compact_at = 0 is off: an unmarked bar"
);
assert_eq!(
bar_marked(0.5, 1.0, 10),
bar(0.5, 10),
"the hard limit is the bar's own end"
);
assert_eq!(bar_marked(0.5, f64::NAN, 10), bar(0.5, 10));
}
}