Skip to main content

stynx_code_tui/widgets/
cost_display.rs

1use ratatui::{
2    buffer::Buffer,
3    layout::Rect,
4    style::Style,
5    text::{Line, Span},
6    widgets::{Paragraph, Widget},
7};
8
9use crate::theme;
10
11pub struct CostDisplay {
12    pub input_tokens: u64,
13    pub output_tokens: u64,
14    pub cost: f64,
15}
16
17impl CostDisplay {
18    pub fn new(input_tokens: u64, output_tokens: u64, cost: f64) -> Self {
19        Self { input_tokens, output_tokens, cost }
20    }
21
22    fn format_tokens(count: u64) -> String {
23        if count >= 1_000_000 { format!("{:.1}M", count as f64 / 1_000_000.0) }
24        else if count >= 1_000 { format!("{:.1}K", count as f64 / 1_000.0) }
25        else { count.to_string() }
26    }
27}
28
29impl Widget for CostDisplay {
30    fn render(self, area: Rect, buf: &mut Buffer) {
31        let line = Line::from(vec![
32            Span::styled("In: ", Style::default().fg(theme::MUTED())),
33            Span::styled(Self::format_tokens(self.input_tokens), Style::default().fg(theme::FOAM())),
34            Span::styled("  Out: ", Style::default().fg(theme::MUTED())),
35            Span::styled(Self::format_tokens(self.output_tokens), Style::default().fg(theme::FOAM())),
36            Span::styled("  Cost: ", Style::default().fg(theme::MUTED())),
37            Span::styled(format!("${:.4}", self.cost), Style::default().fg(theme::GOLD())),
38        ]);
39        Paragraph::new(line).render(area, buf);
40    }
41}