diff --git a/crates/luxe-core/src/provider/plan_usage.rs b/crates/luxe-core/src/provider/plan_usage.rs index 8415f1e..38079a1 100644 --- a/crates/luxe-core/src/provider/plan_usage.rs +++ b/crates/luxe-core/src/provider/plan_usage.rs @@ -48,6 +48,16 @@ impl PlanWindow { }; Some(label) } + + /// A fixed-width meter such as `▓▓▓░░░░░░░`, filled in proportion to the share used. + /// + /// Rounds to the nearest cell so a nearly-full window never reads as empty, and clamps so a + /// provider reporting over 100% can't draw past the edge. + pub fn bar(&self, cells: usize) -> String { + let share = (self.used_percent / 100.0).clamp(0.0, 1.0); + let filled = ((share * cells as f32).round() as usize).min(cells); + format!("{}{}", "▓".repeat(filled), "░".repeat(cells - filled)) + } } /// A subscription's remaining headroom, as reported alongside a response. @@ -146,6 +156,18 @@ mod plan_tests { assert!(window(0.0, 60, None).reset_label().is_none()); } + #[test] + fn a_bar_fills_in_proportion_and_never_overflows() { + assert_eq!(window(0.0, 60, None).bar(10), "░░░░░░░░░░"); + assert_eq!(window(50.0, 60, None).bar(10), "▓▓▓▓▓░░░░░"); + assert_eq!(window(100.0, 60, None).bar(10), "▓▓▓▓▓▓▓▓▓▓"); + // over-reported and negative values clamp instead of panicking on the subtraction + assert_eq!(window(250.0, 60, None).bar(4), "▓▓▓▓"); + assert_eq!(window(-5.0, 60, None).bar(4), "░░░░"); + // a window that is nearly spent must not round down to an empty-looking bar + assert_eq!(window(96.0, 60, None).bar(10), "▓▓▓▓▓▓▓▓▓▓"); + } + /// The status line has room for one window, and it should be whichever is about to bite. #[test] fn the_tightest_window_wins_the_status_line() { diff --git a/crates/luxe-tui/src/view/dock.rs b/crates/luxe-tui/src/view/dock.rs index 1748511..8ed5a2a 100644 --- a/crates/luxe-tui/src/view/dock.rs +++ b/crates/luxe-tui/src/view/dock.rs @@ -10,6 +10,7 @@ mod files; mod plan; mod telemetry; mod tree; +mod usage; use crate::app::App; use crate::workspace_dock::{DockPresentation, DockSectionModel}; diff --git a/crates/luxe-tui/src/view/dock/chrome.rs b/crates/luxe-tui/src/view/dock/chrome.rs index ef2ad10..65a8aac 100644 --- a/crates/luxe-tui/src/view/dock/chrome.rs +++ b/crates/luxe-tui/src/view/dock/chrome.rs @@ -65,6 +65,7 @@ pub(super) enum Section { Files, Elsewhere, Model, + Usage, Session, } @@ -77,6 +78,7 @@ impl Section { Self::Files => "FILES", Self::Elsewhere => "ELSEWHERE", Self::Model => "MODEL", + Self::Usage => "USAGE", Self::Session => "SESSION", } } @@ -94,7 +96,7 @@ impl Section { match self { Self::Project | Self::Model => Depth::Hero, Self::Plan | Self::Files => Depth::Raised, - Self::Elsewhere | Self::Session => Depth::Recessed, + Self::Elsewhere | Self::Usage | Self::Session => Depth::Recessed, } } } diff --git a/crates/luxe-tui/src/view/dock/telemetry.rs b/crates/luxe-tui/src/view/dock/telemetry.rs index dc3c926..4348acb 100644 --- a/crates/luxe-tui/src/view/dock/telemetry.rs +++ b/crates/luxe-tui/src/view/dock/telemetry.rs @@ -192,6 +192,7 @@ pub(super) fn telemetry_lines(app: &App) -> Vec> { let row = row_width(app); let column = telemetry_label_width(row, WIDEST_TELEMETRY_LABEL); let mut lines = model_telemetry(app, row, column); + lines.extend(super::usage::usage_telemetry(app, column)); lines.extend(session_telemetry(app, row, column)); lines } @@ -386,7 +387,7 @@ fn review_spending(app: &App) -> bool { /// /// `ink` is body text for a measurement, dim for a share of one above it, and the severity hue for /// the window, which is the one value that is also a warning. -fn telemetry_row( +pub(super) fn telemetry_row( label: &str, value: String, ink: ratatui::style::Style, diff --git a/crates/luxe-tui/src/view/dock/tests/rail.rs b/crates/luxe-tui/src/view/dock/tests/rail.rs index f60c911..4376ed6 100644 --- a/crates/luxe-tui/src/view/dock/tests/rail.rs +++ b/crates/luxe-tui/src/view/dock/tests/rail.rs @@ -38,6 +38,42 @@ fn the_rail_reports_a_session_that_is_approving_its_own_asks() { assert!(row.contains("auto-approve"), "{row}"); } +/// An API-key provider reports no allowance, and a USAGE heading over nothing would be noise. +#[test] +fn the_rail_has_no_usage_block_without_a_plan() { + let mut app = app(); + app.status.model = "claude-sonnet-5".into(); + let rendered: Vec = telemetry_lines(&app).iter().map(text).collect(); + assert!(!rendered.iter().any(|row| row.contains("USAGE")), "{rendered:?}"); +} + +/// The point of the block: the share spent and the time left, one row per window the plan has. +#[test] +fn the_rail_shows_each_plan_window_with_its_meter_and_reset() { + use luxe_core::provider::{PlanUsage, PlanWindow}; + let mut app = app(); + app.status.model = "gpt-5".into(); + app.status.plan_usage = Some(PlanUsage { + primary: Some(PlanWindow { + used_percent: 30.0, + window_minutes: 10_080, + resets_in_secs: Some(274_000), + }), + secondary: Some(PlanWindow { + used_percent: 80.0, + window_minutes: 300, + resets_in_secs: Some(1_800), + }), + ..PlanUsage::default() + }); + let rendered: Vec = telemetry_lines(&app).iter().map(text).collect(); + assert!(rendered.iter().any(|row| row.contains("USAGE")), "{rendered:?}"); + let weekly = rendered.iter().find(|row| row.starts_with("weekly")).expect("weekly row"); + assert!(weekly.contains("▓▓▓░░░░░░░ 30% · 3d 4h"), "{weekly}"); + let short = rendered.iter().find(|row| row.starts_with("5h")).expect("5h row"); + assert!(short.contains("▓▓▓▓▓▓▓▓░░ 80% · 30m"), "{short}"); +} + /// The surface behind the span carrying `needle` — the slab the badge is cut from. pub(super) fn surface_behind(line: &Line<'_>, needle: &str) -> Color { line.spans @@ -65,7 +101,7 @@ fn the_rail_is_anchored_at_both_ends_and_recedes_between_them() { for live in [Section::Plan, Section::Files] { assert_eq!(surface(live), shades.at(Depth::Raised), "{live:?} is one step back"); } - for ambient in [Section::Elsewhere, Section::Session] { + for ambient in [Section::Elsewhere, Section::Usage, Section::Session] { assert_eq!(surface(ambient), shades.at(Depth::Recessed), "{ambient:?} is two"); } // Or every assertion above passes on a theme with no ramp at all. diff --git a/crates/luxe-tui/src/view/dock/usage.rs b/crates/luxe-tui/src/view/dock/usage.rs new file mode 100644 index 0000000..a9d2d11 --- /dev/null +++ b/crates/luxe-tui/src/view/dock/usage.rs @@ -0,0 +1,63 @@ +//! Dock usage — the USAGE block: how much of a subscription plan is spent, and when it resets. + +use super::chrome::{section_heading_line, Section}; +use super::telemetry::telemetry_row; +use crate::app::App; +use luxe_core::provider::PlanWindow; +use ratatui::text::Line; + +/// Cells in the meter: ten reads in tenths, and leaves the percentage and countdown room beside it +/// on the default rail. +const BAR_CELLS: usize = 10; + +/// The USAGE block, drawn only while the provider reports a plan window. +/// +/// An API-key provider reports no allowance, so it gets no block at all rather than an empty one. +/// Each window gets its own row: a plan with a 5h and a weekly window is limited by whichever +/// fills first, and the reader can't steer by one they can't see. +pub(super) fn usage_telemetry(app: &App, column: usize) -> Vec> { + let Some(plan) = app.status.plan_usage.as_ref() else { + return Vec::new(); + }; + let windows: Vec<&PlanWindow> = + [&plan.primary, &plan.secondary].into_iter().flatten().collect(); + if windows.is_empty() { + return Vec::new(); + } + let styles = &app.theme_styles; + let mut lines = vec![Line::default(), section_heading_line(Section::Usage, app)]; + for window in windows { + let ink = styles.context_fill(window.used_percent.clamp(0.0, 100.0)); + lines.push(telemetry_row(&window.window_label(), window_value(window), ink, column, app)); + } + lines +} + +/// `▓▓▓░░░░░░░ 32% · 3d 4h`: the meter, the share, and the time left when the provider says. +fn window_value(window: &PlanWindow) -> String { + let mut value = format!("{} {:.0}%", window.bar(BAR_CELLS), window.used_percent); + if let Some(reset) = window.reset_label() { + value.push_str(&format!(" · {reset}")); + } + value +} + +#[cfg(test)] +mod tests { + use super::*; + + fn window(used: f32, resets: Option) -> PlanWindow { + PlanWindow { used_percent: used, window_minutes: 10_080, resets_in_secs: resets } + } + + #[test] + fn a_row_shows_meter_share_and_countdown() { + assert_eq!(window_value(&window(30.0, Some(274_000))), "▓▓▓░░░░░░░ 30% · 3d 4h"); + } + + /// Copilot reports no reset countdown, and the row must not invent one. + #[test] + fn a_window_without_a_reset_time_omits_the_countdown() { + assert_eq!(window_value(&window(30.0, None)), "▓▓▓░░░░░░░ 30%"); + } +}