Skip to content

Open source Grok Bot alternative, Bring your own agent and subscription: message Claude Code, Codex, Gemini CLI, Cursor, Copilot and 40+ AI agents as bots on your own computer, from iPhone, Mac, Windows, Linux or terminal. Agent-agnostic and free. Also a Muse and Dots alternative. Built in Rust.

fix(host): the terminal's usage sheet scrolls when its limits don't fit

leepokai56271f11e41e
diff --git a/host/src/tui/app/analytics.rs b/host/src/tui/app/analytics.rs
index 8d904020..63d21f54 100644
--- a/host/src/tui/app/analytics.rs
+++ b/host/src/tui/app/analytics.rs
@@ -53,7 +53,7 @@ impl App {
/// one, else the full trace.
fn screen_on_top(&self) -> Option<Screen> {
let overlay = self.overlays.iter().rev().find_map(|o| match o {
- Overlay::Usage => Some(Screen::Usage),
+ Overlay::Usage(_) => Some(Screen::Usage),
Overlay::Pair(_) => Some(Screen::Pairing),
Overlay::Memory(_) => Some(Screen::Memory),
Overlay::Library(_) => Some(Screen::Library),
@@ -96,7 +96,7 @@ mod tests {
#[tokio::test]
async fn screens_count_as_they_open_and_chats_as_the_bot_changes() {
let mut app = test_app();
- app.overlays.push(Overlay::Usage);
+ app.overlays.push(Overlay::Usage(0));
app.report_screen();
assert_eq!(app.seen.screen, None, "nothing before the first hello");
app.seen.opened = true;
diff --git a/host/src/tui/app/input.rs b/host/src/tui/app/input.rs
index d67ac8e3..3d1c3529 100644
--- a/host/src/tui/app/input.rs
+++ b/host/src/tui/app/input.rs
@@ -304,7 +304,7 @@ impl App {
self.focus = Focus::Trace;
}
KeyCode::Char('b') => self.compact = !self.compact,
- KeyCode::Char('U') => self.overlays.push(Overlay::Usage),
+ KeyCode::Char('U') => self.overlays.push(Overlay::Usage(0)),
KeyCode::Char('M') => self.open_memory(),
KeyCode::Char('R') => self.open_routines(),
KeyCode::Char('L') => self.open_library(),
diff --git a/host/src/tui/app/overlay_keys.rs b/host/src/tui/app/overlay_keys.rs
index 173c36ac..d32e52e4 100644
--- a/host/src/tui/app/overlay_keys.rs
+++ b/host/src/tui/app/overlay_keys.rs
@@ -53,7 +53,7 @@ impl App {
Action::Library => self.open_library(),
Action::RefreshAgents => self.call("refreshBackends", json!({}), After::Backends),
Action::Pair => self.open_pair(),
- Action::Usage => self.overlays.push(Overlay::Usage),
+ Action::Usage => self.overlays.push(Overlay::Usage(0)),
Action::CheckUpdate => self.call("checkHostUpdate", json!({}), After::Update),
Action::Analytics => self.toggle_analytics(),
Action::AboutAnalytics => self.overlays.push(Overlay::Analytics(AnalyticsSheet::default())),
@@ -133,7 +133,19 @@ impl App {
self.call("pairing", json!({}), After::Pairing);
Some(Overlay::Pair(PairCode::Loading))
}
- o @ (Overlay::Usage | Overlay::Pair(_)) => {
+ Overlay::Usage(scroll) => {
+ let (_, height) = crossterm::terminal::size().unwrap_or((80, 24));
+ let max = super::super::view::usage_scroll_max(self, height);
+ match k.code {
+ KeyCode::Esc | KeyCode::Enter | KeyCode::Char('q' | 'U' | 'P') => None,
+ KeyCode::Down | KeyCode::Char('j') => Some(Overlay::Usage((scroll + 1).min(max))),
+ KeyCode::Up | KeyCode::Char('k') => Some(Overlay::Usage(scroll.saturating_sub(1))),
+ KeyCode::PageDown | KeyCode::Char(' ') => Some(Overlay::Usage((scroll + 10).min(max))),
+ KeyCode::PageUp => Some(Overlay::Usage(scroll.saturating_sub(10))),
+ _ => Some(Overlay::Usage(scroll)),
+ }
+ }
+ o @ Overlay::Pair(_) => {
if matches!(k.code, KeyCode::Esc | KeyCode::Enter | KeyCode::Char('q' | 'U' | 'P')) {
None
} else {
diff --git a/host/src/tui/app/ui.rs b/host/src/tui/app/ui.rs
index db9530ae..8bc1784b 100644
--- a/host/src/tui/app/ui.rs
+++ b/host/src/tui/app/ui.rs
@@ -301,7 +301,8 @@ pub enum Overlay {
Folder(FolderPicker),
Form(Box<Form>),
Group(GroupForm),
- Usage,
+ /// How far its list is scrolled.
+ Usage(usize),
Pair(PairCode),
Memory(MemorySheet),
/// A read-only conversation between two bots.
diff --git a/host/src/tui/view/mod.rs b/host/src/tui/view/mod.rs
index 327e3c87..e3e45f09 100644
--- a/host/src/tui/view/mod.rs
+++ b/host/src/tui/view/mod.rs
@@ -35,7 +35,7 @@ pub(super) use overlay::{field_line, input_line};
pub(super) use style::theme;
pub(super) use time::when;
pub(super) use transcript::{file_size, unfoldable};
-pub(super) use usage::when_future;
+pub(super) use usage::{usage_scroll_max, when_future};
use ratatui::Frame;
use ratatui::buffer::Buffer;
diff --git a/host/src/tui/view/overlay.rs b/host/src/tui/view/overlay.rs
index 73725032..52acd2b6 100644
--- a/host/src/tui/view/overlay.rs
+++ b/host/src/tui/view/overlay.rs
@@ -125,7 +125,7 @@ pub(super) fn overlay(buf: &mut Buffer, area: Rect, app: &mut App, top: &Overlay
}
Overlay::Form(form) => form_view(buf, area, app, form),
Overlay::Group(g) => group_view(buf, area, app, g),
- Overlay::Usage => usage(buf, area, app),
+ Overlay::Usage(scroll) => usage(buf, area, app, *scroll),
Overlay::Pair(code) => pair(buf, area, code),
Overlay::Memory(m) => super::super::sheets::memory(buf, area, app, m),
Overlay::BotChat(c) => super::bot_chat::bot_chat(buf, area, app, c),
diff --git a/host/src/tui/view/usage.rs b/host/src/tui/view/usage.rs
index a69f9a3c..c4c0e9e4 100644
--- a/host/src/tui/view/usage.rs
+++ b/host/src/tui/view/usage.rs
@@ -2,6 +2,7 @@
use ratatui::buffer::Buffer;
use ratatui::layout::Rect;
+use serde_json::Value;
use super::super::app::App;
use super::super::l10n::tr;
@@ -11,60 +12,98 @@ use super::filled;
use super::style::theme;
use super::time::{DAYS, MONTHS, clock, local, month_day, now_ms};
-pub(super) fn usage(buf: &mut Buffer, area: Rect, app: &App) {
+/// One line of the sheet's list.
+enum Line<'a> {
+ Provider(&'a str),
+ Window(&'a Value),
+ Gap,
+}
+
+/// Each provider's name, its windows and a gap.
+fn lines(providers: &[Value]) -> Vec<Line<'_>> {
+ let mut lines = Vec::new();
+ for p in providers {
+ lines.push(Line::Provider(p["name"].as_str().unwrap_or_default()));
+ lines.extend(p["windows"].as_array().into_iter().flatten().map(Line::Window));
+ lines.push(Line::Gap);
+ }
+ lines
+}
+
+/// The sheet on a screen `area`, and how many list lines fit in it.
+fn sheet(area: Rect, rows: usize) -> (Rect, usize) {
+ let r = centered(area, 78, u(rows + 5).max(8));
+ // The frame, a blank line on top and the footer.
+ (r, usize::from(r.height.saturating_sub(4)))
+}
+
+/// How far the list scrolls on a screen `height` rows tall: more providers than fit scroll with ↑↓.
+pub(in crate::tui) fn usage_scroll_max(app: &App, height: u16) -> usize {
+ let providers = app.usage["providers"].as_array().cloned().unwrap_or_default();
+ let rows = lines(&providers).len();
+ let (_, fits) = sheet(Rect::new(0, 0, 80, height), rows);
+ rows.saturating_sub(fits)
+}
+
+pub(super) fn usage(buf: &mut Buffer, area: Rect, app: &App, scroll: usize) {
let t = theme();
let providers = app.usage["providers"].as_array().cloned().unwrap_or_default();
- let rows: usize = providers.iter().map(|p| 2 + p["windows"].as_array().map_or(0, Vec::len)).sum();
- let r = centered(area, 78, u(rows + 5).max(8));
+ let lines = lines(&providers);
+ let (r, fits) = sheet(area, lines.len());
let inner = frame_box(buf, r, t.text, t.panel, Some((tr("Usage").as_ref(), t.text)));
- let mut y = inner.y + 1;
+ let top = inner.y + 1;
if providers.is_empty() {
- put(buf, inner.x, y, inner.width, "No usage numbers yet.", t.secondary.patch(t.panel));
+ put(buf, inner.x, top, inner.width, "No usage numbers yet.", t.secondary.patch(t.panel));
}
- for p in &providers {
- put(buf, inner.x, y, inner.width, p["name"].as_str().unwrap_or_default(), t.bold.patch(t.panel));
- y += 1;
- for win in p["windows"].as_array().into_iter().flatten() {
- let label = truncate(win["label"].as_str().unwrap_or_default(), 18);
- put(buf, inner.x + 2, y, 18, &label, t.secondary.patch(t.panel));
- if let Some(text) = win["text"].as_str().filter(|text| !text.is_empty()) {
- put(buf, inner.x + 21, y, inner.width.saturating_sub(23), &truncate(text, 40), t.text.patch(t.panel));
- } else {
- let pct = win["percent"].as_f64().unwrap_or(0.0).clamp(0.0, 100.0);
- let on = filled(pct, 16);
- let mut x = put(
- buf,
- inner.x + 21,
- y,
- 16,
- &"▰".repeat(on.min(16)),
- if pct >= 80.0 { t.amber } else { t.text }.patch(t.panel),
- );
- x = put(buf, x, y, 16, &"▱".repeat(16 - on.min(16)), t.line.patch(t.panel));
- put(buf, x + 1, y, 5, &format!("{pct:>3.0}%"), t.text.patch(t.panel));
- let reset = win["resetsAt"]
- .as_i64()
- .map(|ms| format!("resets {}", when_future(ms)))
- .or_else(|| {
- // "Sep 26 at 11:59am (Asia/Taipei)": the zone is this computer's own.
- win["resetsText"].as_str().map(|s| truncate(s.split(" (").next().unwrap_or(s), 24))
- })
- .unwrap_or_default();
- let reset = crate::tui::l10n::reset_line(&reset);
- rput(buf, inner.right(), y, &reset, t.dim.patch(t.panel));
+ let scroll = scroll.min(lines.len().saturating_sub(fits));
+ for (y, line) in (top..).zip(lines.iter().skip(scroll).take(fits)) {
+ match line {
+ Line::Provider(name) => {
+ put(buf, inner.x, y, inner.width, name, t.bold.patch(t.panel));
}
- y += 1;
+ Line::Window(win) => window(buf, inner, y, win),
+ Line::Gap => {}
}
- y += 1;
}
- put(
+ let footer = if lines.len() > fits {
+ "Read from the agents on this computer. ↑↓ scroll · esc close"
+ } else {
+ "Read from the agents on this computer. esc close"
+ };
+ put(buf, inner.x, inner.bottom() - 1, inner.width, footer, t.dim.patch(t.panel));
+}
+
+/// One limit: its label, a bar with the percent and when it resets; or its note.
+fn window(buf: &mut Buffer, inner: Rect, y: u16, win: &Value) {
+ let t = theme();
+ let label = truncate(win["label"].as_str().unwrap_or_default(), 18);
+ put(buf, inner.x + 2, y, 18, &label, t.secondary.patch(t.panel));
+ if let Some(text) = win["text"].as_str().filter(|text| !text.is_empty()) {
+ put(buf, inner.x + 21, y, inner.width.saturating_sub(23), &truncate(text, 40), t.text.patch(t.panel));
+ return;
+ }
+ let pct = win["percent"].as_f64().unwrap_or(0.0).clamp(0.0, 100.0);
+ let on = filled(pct, 16);
+ let mut x = put(
buf,
- inner.x,
- inner.bottom() - 1,
- inner.width,
- "Read from the agents on this computer. esc close",
- t.dim.patch(t.panel),
+ inner.x + 21,
+ y,
+ 16,
+ &"▰".repeat(on.min(16)),
+ if pct >= 80.0 { t.amber } else { t.text }.patch(t.panel),
);
+ x = put(buf, x, y, 16, &"▱".repeat(16 - on.min(16)), t.line.patch(t.panel));
+ put(buf, x + 1, y, 5, &format!("{pct:>3.0}%"), t.text.patch(t.panel));
+ let reset = win["resetsAt"]
+ .as_i64()
+ .map(|ms| format!("resets {}", when_future(ms)))
+ .or_else(|| {
+ // "Sep 26 at 11:59am (Asia/Taipei)": the zone is this computer's own.
+ win["resetsText"].as_str().map(|s| truncate(s.split(" (").next().unwrap_or(s), 24))
+ })
+ .unwrap_or_default();
+ let reset = crate::tui::l10n::reset_line(&reset);
+ rput(buf, inner.right(), y, &reset, t.dim.patch(t.panel));
}
pub(in crate::tui) fn when_future(ms: i64) -> String {
@@ -79,3 +118,23 @@ pub(in crate::tui) fn when_future(ms: i64) -> String {
format!("{} {d}", MONTHS[m])
}
}
+
+#[cfg(test)]
+mod tests {
+ use super::super::super::app::App;
+ use super::super::super::net::Client;
+ use super::usage_scroll_max;
+ use serde_json::json;
+
+ #[tokio::test]
+ async fn five_providers_scroll_on_a_small_terminal() {
+ let (tx, _rx) = tokio::sync::mpsc::unbounded_channel();
+ let mut app = App::new(Client::new("http://127.0.0.1:1", None), tx, String::new());
+ let windows = json!([{"label": "5h", "percent": 10}, {"label": "Weekly", "percent": 20}, {"label": "Opus", "percent": 30}]);
+ let providers: Vec<_> = (0..5).map(|i| json!({"name": format!("P{i}"), "windows": windows})).collect();
+ app.usage = json!({ "providers": providers });
+ // 25 lines (each name, three windows, a gap); a 24-row screen shows 18 of them.
+ assert_eq!(usage_scroll_max(&app, 24), 7);
+ assert_eq!(usage_scroll_max(&app, 60), 0);
+ }
+}