use ratatui::{
style::{Modifier, Style},
text::{Line, Span},
};
use super::style_tokens;
use crate::widgets::spinner::{COMPLETED_CHAR, FAILURE_CHAR};
pub enum ToolLineStyle {
Primary,
Nested,
}
pub fn format_elapsed(secs: u64) -> String {
if secs >= 60 {
format!("{}m {}s", secs / 60, secs % 60)
} else {
format!("{secs}s")
}
}
pub fn tool_line_active(
prefix_spans: Vec<Span<'static>>,
spinner_char: char,
verb: String,
arg: String,
elapsed: Option<String>,
style: ToolLineStyle,
) -> Line<'static> {
let mut spans = prefix_spans;
spans.push(Span::styled(
format!("{spinner_char} "),
Style::default().fg(style_tokens::BLUE_BRIGHT),
));
let (verb_style, arg_style, elapsed_style) = match style {
ToolLineStyle::Primary => (
Style::default()
.fg(style_tokens::PRIMARY)
.add_modifier(Modifier::BOLD),
Style::default().fg(style_tokens::SUBTLE),
Style::default().fg(style_tokens::GREY),
),
ToolLineStyle::Nested => (
Style::default().fg(style_tokens::SUBTLE),
Style::default().fg(style_tokens::GREY),
Style::default().fg(style_tokens::SUBTLE),
),
};
spans.push(Span::styled(verb, verb_style));
spans.push(Span::styled(format!(" {arg}"), arg_style));
if let Some(el) = elapsed {
spans.push(Span::styled(format!(" {el}"), elapsed_style));
}
Line::from(spans)
}
pub fn tool_line_completed(
prefix_spans: Vec<Span<'static>>,
success: bool,
verb: String,
arg: String,
elapsed: Option<String>,
style: ToolLineStyle,
) -> Line<'static> {
let mut spans = prefix_spans;
let (icon, icon_color) = if success {
(COMPLETED_CHAR, style_tokens::GREEN_BRIGHT)
} else {
(FAILURE_CHAR, style_tokens::ERROR)
};
spans.push(Span::styled(
format!("{icon} "),
Style::default().fg(icon_color),
));
let (verb_style, arg_style, elapsed_style) = match style {
ToolLineStyle::Primary => (
Style::default()
.fg(style_tokens::PRIMARY)
.add_modifier(Modifier::BOLD),
Style::default().fg(style_tokens::SUBTLE),
Style::default().fg(style_tokens::GREY),
),
ToolLineStyle::Nested => (
Style::default().fg(style_tokens::SUBTLE),
Style::default().fg(style_tokens::GREY),
Style::default().fg(style_tokens::SUBTLE),
),
};
spans.push(Span::styled(verb, verb_style));
spans.push(Span::styled(format!(" {arg}"), arg_style));
if let Some(el) = elapsed {
spans.push(Span::styled(format!(" {el}"), elapsed_style));
}
Line::from(spans)
}
#[cfg(test)]
#[path = "tool_line_tests.rs"]
mod tests;