use super::{centered, fg, spinner, INK3, LIVE, NUM, SEED, WARN};
use crate::session::app::{AgentState, App, Tab};
use ratatui::layout::{Alignment, Rect};
use ratatui::style::{Modifier, Style};
use ratatui::text::{Line, Span};
use ratatui::widgets::{Paragraph, Tabs};
use ratatui::Frame;
pub fn too_small(f: &mut Frame, area: Rect) {
let msg = "terminal too small (70×18 needed)";
let r = centered(area, msg.chars().count() as u16, 1);
f.render_widget(Paragraph::new(msg).style(fg(WARN)), r);
}
fn thousands(v: f64) -> String {
let s = format!("{v:.2}");
let (int, frac) = s.split_once('.').unwrap_or((&s, "00"));
let mut out = String::new();
for (i, c) in int.chars().enumerate() {
if i > 0 && (int.len() - i) % 3 == 0 {
out.push(',');
}
out.push(c);
}
format!("{out}.{frac}")
}
fn age(app: &App) -> Option<u64> {
[app.mesh_at, app.agent_at]
.into_iter()
.flatten()
.map(|t| t.elapsed().as_secs())
.max()
}
pub fn top_bar(f: &mut Frame, area: Rect, app: &App) {
let mut spans = vec![
Span::styled(" ● ", fg(SEED)),
Span::styled(
format!("zc {}", env!("CARGO_PKG_VERSION")),
Style::default().add_modifier(Modifier::BOLD),
),
Span::styled(" ─ ", fg(INK3)),
];
let (pip, color) = match (app.summary(), &app.mesh) {
(Some(s), _) if s.mesh.reachable => (format!("● connected · {}", s.mesh.route), LIVE),
(Some(_), _) => ("● offline".to_string(), INK3),
(None, Some(m)) if m.local_only.is_some() => ("● local only".to_string(), WARN),
(None, Some(_)) => ("● connected".to_string(), LIVE),
(None, None) => (format!("{} connecting", spinner(app)), INK3),
};
spans.push(Span::styled(pip, fg(color)));
if let Some(s) = app.summary() {
if let Some(acc) = &s.account {
spans.push(Span::styled(" ─ ", fg(INK3)));
spans.push(Span::styled(
format!("{} zkcr", thousands(acc.credits_balance)),
fg(NUM),
));
}
spans.push(Span::styled(" ─ ", fg(INK3)));
spans.push(Span::styled(s.env.clone(), fg(INK3)));
}
if app.agent_failures > 0 {
spans.push(Span::styled(
format!(" · agent ✗{}", app.agent_failures),
fg(WARN),
));
}
f.render_widget(Paragraph::new(Line::from(spans)), area);
if let Some(a) = age(app) {
f.render_widget(
Paragraph::new(format!("updated {a}s "))
.style(fg(INK3))
.alignment(Alignment::Right),
area,
);
}
}
pub fn tabs(f: &mut Frame, area: Rect, app: &App) {
let titles: Vec<String> = Tab::ALL
.iter()
.enumerate()
.map(|(i, t)| format!("{} {}", i + 1, t.title()))
.collect();
f.render_widget(
Tabs::new(titles)
.select(app.tab.index())
.style(fg(INK3))
.highlight_style(fg(SEED).add_modifier(Modifier::BOLD))
.divider(" "),
area,
);
}
pub fn status_line(f: &mut Frame, area: Rect, app: &App) {
let line: Line = if let Some(bar) = &app.bar {
Line::from(vec![
Span::styled(" > ", fg(SEED)),
Span::raw(bar.input.clone()),
Span::styled("█", fg(SEED)),
])
} else if let Some((msg, _)) = &app.toast {
Line::from(Span::styled(format!(" {msg}"), fg(WARN)))
} else if let Some(p) = app.summary().and_then(|s| s.problems.first()) {
let more = app.summary().map(|s| s.problems.len()).unwrap_or(1) - 1;
let mut text = format!(" ⚠ {}", p.message);
if let Some(h) = &p.hint {
text.push_str(&format!(" — {h}"));
}
if more > 0 {
text.push_str(&format!(" (+{more} more)"));
}
Line::from(Span::styled(text, fg(WARN)))
} else if let Some(AgentState::NotRunning(Some(e))) = &app.agent {
Line::from(Span::styled(format!(" zc agent: {e}"), fg(INK3)))
} else {
Line::default()
};
f.render_widget(Paragraph::new(line), area);
}
pub fn footer(f: &mut Frame, area: Rect, app: &App) {
let keys: String = if let Some(bar) = &app.bar {
match bar.matches.is_empty() {
true => "tab complete · ↑↓ history · ⏎ run · esc close".into(),
false => bar.matches.join(" "),
}
} else if app.price_edit.is_some() {
"←/→ step · digits type · ⏎ apply · esc cancel".into()
} else if app.filtering {
"type to filter · ⏎ keep · esc clear".into()
} else {
let start = if app.summary().is_none() {
"a start agent "
} else {
""
};
match app.tab {
Tab::Mesh => "↑↓ move ⏎ expand c copy zc:// y copy SDK line / filter s sort : command r refresh ? help q quit".into(),
Tab::Account => format!("{start}↑↓ move ⏎ open device : command r refresh ? help q quit"),
Tab::Share => format!("{start}space sharing +/- workers p price : command r refresh ? help q quit"),
}
};
f.render_widget(Paragraph::new(format!(" {keys}")).style(fg(INK3)), area);
}