use crate::tui::cell::{Cell, Color, Style};
use crate::tui::layout::Rect;
use crate::tui::screen::Screen;
use crate::tui::widget::{Widget, styles};
pub struct StatusBarWidget {
pub mode_label: String,
pub mode_color: Color,
pub model_name: String,
pub token_count: Option<(u32, u32)>, pub message_count: usize,
pub pinned_file_count: usize,
pub session_name: String,
pub is_streaming: bool,
pub tool_call_count: u64,
pub total_tokens_used: u64,
pub focus_label: String,
}
impl StatusBarWidget {
pub fn new(mode_label: &str, model_name: &str) -> Self {
let mode_color = match mode_label {
"casual" => styles::MODE_CASUAL_FG,
"planning" => styles::MODE_PLANNING_FG,
"agent" => styles::MODE_AGENT_FG,
"research" => styles::MODE_RESEARCH_FG,
_ => Color::WHITE,
};
Self {
mode_label: mode_label.to_string(),
mode_color,
model_name: model_name.to_string(),
token_count: None,
message_count: 0,
pinned_file_count: 0,
session_name: String::from("unnamed"),
is_streaming: false,
tool_call_count: 0,
total_tokens_used: 0,
focus_label: String::from("input"),
}
}
pub fn update_labels(&mut self, mode_label: &str, model_name: &str) {
self.mode_label = mode_label.to_string();
self.mode_color = match mode_label {
"casual" => styles::MODE_CASUAL_FG,
"planning" => styles::MODE_PLANNING_FG,
"agent" => styles::MODE_AGENT_FG,
"research" => styles::MODE_RESEARCH_FG,
_ => Color::WHITE,
};
self.model_name = model_name.to_string();
}
pub fn set_session_name(&mut self, name: &str) {
self.session_name = name.to_string();
}
pub fn set_message_count(&mut self, count: usize) {
self.message_count = count;
}
pub fn set_token_count(&mut self, used: u64, total: Option<u64>) {
self.token_count = total.map(|t| (used as u32, t as u32));
}
pub fn set_tool_call_count(&mut self, count: u64) {
self.tool_call_count = count;
}
pub fn set_total_tokens_used(&mut self, count: u64) {
self.total_tokens_used = count;
}
pub fn set_streaming(&mut self, streaming: bool) {
self.is_streaming = streaming;
}
pub fn set_focus_label(&mut self, label: &str) {
self.focus_label = label.to_string();
}
fn format_tokens(&self) -> String {
match self.token_count {
Some((used, total)) => {
let used_str = if used >= 1000 {
format!("{:.1}k", used as f64 / 1000.0)
} else {
used.to_string()
};
let total_str = if total >= 1000 {
format!("{:.0}k", total as f64 / 1000.0)
} else {
total.to_string()
};
let pct = if total > 0 {
format!("{:.0}%", (used as f64 / total as f64) * 100.0)
} else {
"?%".to_string()
};
format!("{used_str}/{total_str} ({pct})")
}
None => "? tokens".to_string(),
}
}
}
impl Widget for StatusBarWidget {
fn render(&mut self, area: Rect, screen: &mut Screen) {
if area.is_empty() || area.height < 1 {
return;
}
screen.fill_rect(
area,
Cell {
char: ' ',
fg: styles::STATUS_BAR_FG,
bg: styles::STATUS_BAR_BG,
style: Style::default(),
wide: false,
},
);
let row = area.y;
let mode_text = format!(" {} ", self.mode_label);
screen.write_str(
row,
area.x,
&mode_text,
self.mode_color,
styles::STATUS_BAR_BG,
Style::bold(),
);
let mut col = area.x + mode_text.len() as u16;
screen.write_str(
row,
col,
" │ ",
Color::Ansi(240),
styles::STATUS_BAR_BG,
Style::default(),
);
col += 3;
let model_text = &self.model_name;
screen.write_str(
row,
col,
model_text,
Color::WHITE,
styles::STATUS_BAR_BG,
Style::default(),
);
col += model_text.len() as u16;
screen.write_str(
row,
col,
" │ ",
Color::Ansi(240),
styles::STATUS_BAR_BG,
Style::default(),
);
col += 3;
let token_text = self.format_tokens();
let token_color = match self.token_count {
Some((used, total)) if total > 0 => {
let pct = used as f64 / total as f64;
if pct > 0.9 {
Color::RED
} else if pct > 0.7 {
Color::YELLOW
} else {
Color::GREEN
}
}
_ => Color::GRAY,
};
screen.write_str(
row,
col,
&token_text,
token_color,
styles::STATUS_BAR_BG,
Style::default(),
);
col += token_text.len() as u16;
screen.write_str(
row,
col,
" │ ",
Color::Ansi(240),
styles::STATUS_BAR_BG,
Style::default(),
);
col += 3;
let msg_text = format!("{} msgs", self.message_count);
screen.write_str(
row,
col,
&msg_text,
Color::WHITE,
styles::STATUS_BAR_BG,
Style::dim(),
);
col += msg_text.len() as u16;
if self.pinned_file_count > 0 {
screen.write_str(
row,
col,
" │ ",
Color::Ansi(240),
styles::STATUS_BAR_BG,
Style::default(),
);
col += 3;
let file_text = format!("{} files", self.pinned_file_count);
screen.write_str(
row,
col,
&file_text,
Color::WHITE,
styles::STATUS_BAR_BG,
Style::dim(),
);
col += file_text.len() as u16;
}
if self.tool_call_count > 0 {
screen.write_str(
row,
col,
" │ ",
Color::Ansi(240),
styles::STATUS_BAR_BG,
Style::default(),
);
col += 3;
let tool_text = format!("{} tools", self.tool_call_count);
screen.write_str(
row,
col,
&tool_text,
Color::WHITE,
styles::STATUS_BAR_BG,
Style::dim(),
);
col += tool_text.len() as u16;
}
if self.total_tokens_used > 0 {
screen.write_str(
row,
col,
" │ ",
Color::Ansi(240),
styles::STATUS_BAR_BG,
Style::default(),
);
col += 3;
let total_text = format!(
"{} total",
if self.total_tokens_used >= 1000 {
format!("{:.1}k", self.total_tokens_used as f64 / 1000.0)
} else {
self.total_tokens_used.to_string()
}
);
screen.write_str(
row,
col,
&total_text,
Color::WHITE,
styles::STATUS_BAR_BG,
Style::dim(),
);
col += total_text.len() as u16;
}
let focus_text = format!(" ▸ {} ", self.focus_label);
let focus_color = Color::Ansi(178); let session_text = format!(" {} ", self.session_name);
let session_start = area.x + area.width.saturating_sub(session_text.len() as u16);
let focus_start = session_start.saturating_sub(focus_text.len() as u16);
if focus_start > col {
screen.write_str(
row,
focus_start,
&focus_text,
focus_color,
styles::STATUS_BAR_BG,
Style::bold(),
);
}
if session_start > col {
screen.write_str(
row,
session_start,
&session_text,
Color::Ansi(240),
styles::STATUS_BAR_BG,
Style::default(),
);
}
if self.is_streaming && col + 4 < area.x + area.width {
screen.write_str(
row,
col,
" ⋯",
Color::ORANGE,
styles::STATUS_BAR_BG,
Style::default(),
);
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_status_bar_render() {
let mut screen = Screen::new(80, 24);
let mut widget = StatusBarWidget::new("agent", "llama3.1:8b");
let area = Rect::new(0, 0, 80, 1);
widget.render(area, &mut screen);
assert_ne!(screen.get(0, 0).unwrap().char, '\0');
}
#[test]
fn test_format_tokens() {
let widget = StatusBarWidget::new("agent", "llama3.1:8b");
assert_eq!(widget.format_tokens(), "? tokens");
let mut widget = StatusBarWidget::new("agent", "llama3.1:8b");
widget.token_count = Some((12000, 128000));
let tokens = widget.format_tokens();
assert!(tokens.contains("%") && tokens.contains("12.0k") && tokens.contains("128k"));
}
#[test]
fn test_status_bar_with_tokens() {
let mut screen = Screen::new(80, 24);
let mut widget = StatusBarWidget::new("agent", "llama3.1:8b");
widget.token_count = Some((5000, 128000));
widget.message_count = 42;
let area = Rect::new(0, 0, 80, 1);
widget.render(area, &mut screen);
assert_ne!(screen.get(0, 0).unwrap().char, '\0');
}
}