2026-07-12 11:28:39 +07:00
|
|
|
//! Status bar rendering for the TUI.
|
|
|
|
|
//!
|
|
|
|
|
//! Flow: `draw_status_bar` reads live connection/turn state off
|
|
|
|
|
//! `AppStateRest` every frame and paints a single-line bar at the top
|
|
|
|
|
//! (or bottom, per layout) of the screen showing agent status, provider,
|
|
|
|
|
//! and model.
|
|
|
|
|
//!
|
|
|
|
|
//! Why: kept as one small, self-contained render function rather than a
|
|
|
|
|
//! widget struct, matching the other `view/*` modules' functional style.
|
|
|
|
|
|
2026-07-11 13:16:10 +07:00
|
|
|
use ratatui::layout::Rect;
|
|
|
|
|
use ratatui::style::{Style, Modifier};
|
|
|
|
|
use ratatui::text::{Line, Span};
|
|
|
|
|
use ratatui::widgets::Block;
|
|
|
|
|
use ratatui::Frame;
|
|
|
|
|
use super::theme::Theme;
|
|
|
|
|
|
2026-07-12 11:28:39 +07:00
|
|
|
/// Render the single-line status bar showing connection state, provider, and model.
|
|
|
|
|
///
|
|
|
|
|
/// Flow: derive an agent status label/color from turn-in-flight and API
|
|
|
|
|
/// connection state → build left ([zesdex] STATUS) and right
|
|
|
|
|
/// (provider · model) span groups → render as one styled Line.
|
|
|
|
|
///
|
|
|
|
|
/// Return: nothing; draws directly into `frame` at `area`.
|
2026-07-11 13:16:10 +07:00
|
|
|
pub fn draw_status_bar(frame: &mut Frame, area: Rect, state: &crate::app::state::rest::AppStateRest) {
|
2026-07-12 03:14:52 +07:00
|
|
|
// Connection status — reflects actual agent readiness:
|
|
|
|
|
// PROG → turn is in flight
|
|
|
|
|
// READY → connected and ready
|
|
|
|
|
// NOAPI → disconnected
|
2026-07-12 03:19:25 +07:00
|
|
|
let spinner_frames = ["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"];
|
2026-07-12 03:14:52 +07:00
|
|
|
let (agent_status, conn_color) = if state.turn_in_flight() {
|
2026-07-12 03:19:25 +07:00
|
|
|
let frame = spinner_frames[(state.misc.tick_count as usize / 2) % spinner_frames.len()];
|
|
|
|
|
(format!("{} PROG", frame), Theme::MODE_YOLO)
|
2026-07-12 03:14:52 +07:00
|
|
|
} else if state.misc.api_connected {
|
2026-07-12 03:19:25 +07:00
|
|
|
("READY".to_string(), Theme::MODE_AUTO)
|
2026-07-12 03:14:52 +07:00
|
|
|
} else {
|
2026-07-12 03:19:25 +07:00
|
|
|
("NOAPI".to_string(), Theme::DIM)
|
2026-07-12 03:14:52 +07:00
|
|
|
};
|
|
|
|
|
let status = Span::styled(
|
|
|
|
|
format!(" {} ", agent_status),
|
2026-07-11 13:16:10 +07:00
|
|
|
Style::default()
|
2026-07-12 03:14:52 +07:00
|
|
|
.fg(if agent_status == "NOAPI" { Theme::DIM } else { Theme::BG })
|
|
|
|
|
.bg(conn_color)
|
2026-07-11 13:16:10 +07:00
|
|
|
.add_modifier(Modifier::BOLD),
|
|
|
|
|
);
|
|
|
|
|
|
2026-07-12 03:14:52 +07:00
|
|
|
// Left chunk: [zesdex] STATUS
|
|
|
|
|
let mut spans = vec![
|
|
|
|
|
Span::styled(" [zesdex] ", Style::default().fg(Theme::TEXT).add_modifier(Modifier::BOLD)),
|
|
|
|
|
status,
|
|
|
|
|
];
|
2026-07-11 13:16:10 +07:00
|
|
|
|
2026-07-12 13:40:58 +07:00
|
|
|
// Right chunk: token usage, provider, model
|
|
|
|
|
let right_str = if let Some(ref rt) = state.session_runtime {
|
|
|
|
|
let max_tokens = state.app_config.model_roles.values()
|
|
|
|
|
.find(|role| role.provider == state.settings.provider && role.model == state.settings.model)
|
|
|
|
|
.and_then(|role| role.context_window);
|
|
|
|
|
|
|
|
|
|
let total_chars: usize = rt.messages.iter()
|
|
|
|
|
.filter_map(|m| m.content.as_deref())
|
|
|
|
|
.map(|c| c.len())
|
|
|
|
|
.sum();
|
|
|
|
|
let current_tokens = total_chars / 4;
|
|
|
|
|
|
|
|
|
|
let mut parts = Vec::new();
|
|
|
|
|
if rt.usage.last_tokens_in > 0 || rt.usage.last_tokens_out > 0 {
|
|
|
|
|
parts.push(format!("↑{} ↓{}", rt.usage.last_tokens_in, rt.usage.last_tokens_out));
|
|
|
|
|
}
|
|
|
|
|
let max_str = max_tokens.map(|v| v.to_string()).unwrap_or_else(|| "?".to_string());
|
|
|
|
|
parts.push(format!("{}/{}", current_tokens, max_str));
|
|
|
|
|
parts.push(state.settings.provider.clone());
|
|
|
|
|
parts.push(state.settings.model.clone());
|
|
|
|
|
|
|
|
|
|
format!(" {} ", parts.join(" · "))
|
|
|
|
|
} else {
|
|
|
|
|
let max_tokens = state.app_config.model_roles.values()
|
|
|
|
|
.find(|role| role.provider == state.settings.provider && role.model == state.settings.model)
|
|
|
|
|
.and_then(|role| role.context_window);
|
|
|
|
|
let max_str = max_tokens.map(|v| v.to_string()).unwrap_or_else(|| "?".to_string());
|
|
|
|
|
format!(" 0/{} · {} · {} ", max_str, state.settings.provider, state.settings.model)
|
|
|
|
|
};
|
|
|
|
|
|
2026-07-12 03:14:52 +07:00
|
|
|
spans.push(Span::styled(
|
2026-07-12 13:40:58 +07:00
|
|
|
right_str,
|
2026-07-11 13:16:10 +07:00
|
|
|
Style::default().fg(Theme::DIM),
|
2026-07-12 03:14:52 +07:00
|
|
|
));
|
2026-07-11 13:16:10 +07:00
|
|
|
|
2026-07-12 03:14:52 +07:00
|
|
|
let line = Line::from(spans);
|
2026-07-11 13:16:10 +07:00
|
|
|
|
|
|
|
|
let block = Block::default()
|
|
|
|
|
.style(
|
|
|
|
|
Style::default()
|
|
|
|
|
.bg(Theme::STATUS_BAR_BG)
|
|
|
|
|
.fg(Theme::TEXT),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
let paragraph = ratatui::widgets::Paragraph::new(line).block(block);
|
|
|
|
|
frame.render_widget(paragraph, area);
|
|
|
|
|
}
|