opencrabs 0.3.9

The autonomous, self-improving AI agent. Single Rust binary. Every channel. Install with: cargo install opencrabs
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
//! Usage dashboard card renderers
//!
//! Each card is a self-contained render function that takes data + area and draws into a Frame.

use super::data::{
    ActivityStats, DailyStats, DashboardData, ModelStats, ProjectStats, ToolStats, fmt_cost,
    fmt_tokens,
};
use ratatui::{
    Frame,
    layout::{Alignment, Rect},
    style::{Color, Modifier, Style},
    text::{Line, Span},
    widgets::{Block, Borders, Paragraph, Wrap},
};

const LABEL: Style = Style::new().fg(Color::DarkGray);
const BOLD: Style = Style::new().fg(Color::Cyan).add_modifier(Modifier::BOLD);
const DIM: Style = Style::new().fg(Color::DarkGray);
const ACCENT: Style = Style::new().fg(Color::Rgb(215, 100, 20));

fn card_block(title: &str, focused: bool) -> Block<'_> {
    let border_color = if focused {
        Color::Rgb(215, 100, 20)
    } else {
        Color::DarkGray
    };
    Block::default()
        .borders(Borders::ALL)
        .border_style(Style::default().fg(border_color))
        .title(Span::styled(
            format!(" {} ", title),
            if focused { ACCENT } else { LABEL },
        ))
}

// ── Summary Bar ──────────────────────────────────────────────────────────────

pub fn render_summary(f: &mut Frame, data: &DashboardData, area: Rect, period_label: &str) {
    let s = &data.summary;
    let version = crate::VERSION;
    let line = Line::from(vec![
        Span::styled(format!("v{version}  "), ACCENT),
        Span::styled("Tokens: ", LABEL),
        Span::styled(fmt_tokens(s.total_tokens), BOLD),
        Span::styled("  Cost: ", LABEL),
        Span::styled(fmt_cost(s.total_cost), BOLD),
        Span::styled("  Sessions: ", LABEL),
        Span::styled(format!("{}", s.session_count), BOLD),
        Span::styled("  Calls: ", LABEL),
        Span::styled(format!("{}", s.call_count), BOLD),
        Span::styled(format!("  [{}]", period_label), ACCENT),
    ]);
    let block = Block::default()
        .borders(Borders::BOTTOM)
        .border_style(Style::default().fg(Color::DarkGray));
    let paragraph = Paragraph::new(vec![line])
        .block(block)
        .alignment(Alignment::Center);
    f.render_widget(paragraph, area);
}

// ── Daily Activity ───────────────────────────────────────────────────────────

pub fn render_daily(f: &mut Frame, daily: &[DailyStats], area: Rect, focused: bool) {
    let block = card_block("Daily Activity", focused);
    let inner = block.inner(area);
    f.render_widget(block, area);

    if daily.is_empty() {
        let p = Paragraph::new(" No data").style(DIM);
        f.render_widget(p, inner);
        return;
    }

    let max_tokens = daily.iter().map(|d| d.tokens).max().unwrap_or(1);

    // Compute actual cost column width from data
    let max_cost_len = daily
        .iter()
        .map(|d| fmt_tokens(d.tokens).len())
        .max()
        .unwrap_or(8);
    let date_width = 6usize; // " 04-15 "
    let data_cols = max_cost_len + 2; // " {cost}"
    let bar_width = inner.width.saturating_sub((date_width + data_cols) as u16) as usize;

    let mut lines: Vec<Line> = Vec::new();
    // Show most recent days first (reversed), N that fit
    let visible = (inner.height as usize).min(daily.len());
    let start = daily.len().saturating_sub(visible);

    for day in daily[start..].iter().rev() {
        let bar_len = if max_tokens > 0 && bar_width > 0 {
            ((day.tokens as f64 / max_tokens as f64) * bar_width as f64).ceil() as usize
        } else {
            0
        };
        let bar_len = bar_len.max(1).min(bar_width);
        let bar: String = "\u{2584}".repeat(bar_len); // ▄ lower-half block for visual separation
        let pad: String = " ".repeat(bar_width.saturating_sub(bar_len));
        // Show short date (MM-DD)
        let short_date = if day.date.len() >= 10 {
            &day.date[5..10]
        } else {
            &day.date
        };
        lines.push(Line::from(vec![
            Span::styled(format!(" {:>5} ", short_date), DIM),
            Span::styled(bar, ACCENT),
            Span::raw(pad),
            Span::styled(
                format!(" {:>width$}", fmt_tokens(day.tokens), width = max_cost_len),
                LABEL,
            ),
        ]));
    }

    let p = Paragraph::new(lines);
    f.render_widget(p, inner);
}

// ── By Project ───────────────────────────────────────────────────────────────

pub fn render_projects(f: &mut Frame, projects: &[ProjectStats], area: Rect, focused: bool) {
    let block = card_block("By Project", focused);
    let inner = block.inner(area);
    f.render_widget(block, area);

    if projects.is_empty() {
        let p = Paragraph::new(" No data").style(DIM);
        f.render_widget(p, inner);
        return;
    }

    // Compute column widths from actual data
    let max_cost_len = projects
        .iter()
        .map(|p| fmt_cost(p.cost).len())
        .max()
        .unwrap_or(6);
    let max_tok_len = projects
        .iter()
        .map(|p| fmt_tokens(p.tokens).len())
        .max()
        .unwrap_or(6);
    let max_sess_len = projects
        .iter()
        .map(|p| p.sessions.to_string().len())
        .max()
        .unwrap_or(1);

    // Data columns get guaranteed space; name column fills whatever is left
    let spacing = 2;
    let cost_width = max_cost_len;
    let tok_width = max_tok_len;
    let sess_width = max_sess_len;
    // +1 for leading space on name, +1 for trailing 's' on sessions
    let fixed = cost_width + tok_width + sess_width + spacing * 3 + 2;
    let name_width = (inner.width as usize).saturating_sub(fixed).max(4);

    let mut lines: Vec<Line> = Vec::new();
    let visible = (inner.height as usize).min(projects.len());
    for proj in projects.iter().take(visible) {
        let name = if proj.project.len() > name_width {
            format!(
                "{}...",
                proj.project
                    .chars()
                    .take(name_width.saturating_sub(3))
                    .collect::<String>()
            )
        } else {
            proj.project.clone()
        };
        lines.push(Line::from(vec![
            Span::styled(format!(" {:<width$}", name, width = name_width), BOLD),
            Span::raw("  "),
            Span::styled(
                format!("{:>width$}", fmt_cost(proj.cost), width = cost_width),
                LABEL,
            ),
            Span::raw("  "),
            Span::styled(
                format!("{:>width$}", fmt_tokens(proj.tokens), width = tok_width),
                DIM,
            ),
            Span::raw("  "),
            Span::styled(
                format!("{:>width$}s", proj.sessions, width = sess_width),
                DIM,
            ),
        ]));
    }

    let p = Paragraph::new(lines);
    f.render_widget(p, inner);
}

// ── By Model ─────────────────────────────────────────────────────────────────

pub fn render_models(f: &mut Frame, models: &[ModelStats], area: Rect, focused: bool) {
    let block = card_block("By Model", focused);
    let inner = block.inner(area);
    f.render_widget(block, area);

    if models.is_empty() {
        let p = Paragraph::new(" No data").style(DIM);
        f.render_widget(p, inner);
        return;
    }

    // Compute column widths from actual data
    let visible = (inner.height as usize).min(models.len());
    let max_cost_len = models
        .iter()
        .take(visible)
        .map(|m| {
            let c = fmt_cost(m.cost);
            if m.estimated { c.len() + 1 } else { c.len() }
        })
        .max()
        .unwrap_or(6);
    let max_tok_len = models
        .iter()
        .take(visible)
        .map(|m| fmt_tokens(m.tokens).len())
        .max()
        .unwrap_or(6);

    // Data columns get guaranteed space; name column fills whatever is left
    let spacing = 2;
    let cost_width = max_cost_len;
    let tok_width = max_tok_len;
    // +1 for leading space on name
    let fixed = cost_width + tok_width + spacing * 2 + 1;
    let name_width = (inner.width as usize).saturating_sub(fixed).max(4);

    let mut lines: Vec<Line> = Vec::new();
    for m in models.iter().take(visible) {
        let display = crate::tui::provider_selector::model_display_label(&m.model).to_string();
        let name = if display.len() > name_width {
            format!(
                "{}...",
                display
                    .chars()
                    .take(name_width.saturating_sub(3))
                    .collect::<String>()
            )
        } else {
            display
        };
        let cost_style = if m.estimated { ACCENT } else { LABEL };
        let cost_str = if m.estimated {
            format!("~{}", fmt_cost(m.cost))
        } else {
            fmt_cost(m.cost)
        };
        lines.push(Line::from(vec![
            Span::styled(format!(" {:<width$}", name, width = name_width), BOLD),
            Span::raw("  "),
            Span::styled(
                format!("{:>width$}", cost_str, width = cost_width),
                cost_style,
            ),
            Span::raw("  "),
            Span::styled(
                format!("{:>width$}", fmt_tokens(m.tokens), width = tok_width),
                DIM,
            ),
        ]));
    }

    let p = Paragraph::new(lines);
    f.render_widget(p, inner);
}

// ── Core Tools ───────────────────────────────────────────────────────────────

pub fn render_tools(f: &mut Frame, tools: &[ToolStats], area: Rect, focused: bool) {
    let block = card_block("Core Tools", focused);
    let inner = block.inner(area);
    f.render_widget(block, area);

    if tools.is_empty() {
        let p = Paragraph::new(" No data").style(DIM);
        f.render_widget(p, inner);
        return;
    }

    let visible = (inner.height as usize).min(tools.len());

    // Compute widths from actual data
    let max_name_len = tools
        .iter()
        .take(visible)
        .map(|t| t.tool_name.len())
        .max()
        .unwrap_or(8);
    let max_count_len = tools
        .iter()
        .take(visible)
        .map(|t| t.call_count.to_string().len())
        .max()
        .unwrap_or(1);
    let max_count = tools.first().map(|t| t.call_count).unwrap_or(1);

    // Reserve space for actual name and count, bar gets the rest
    let data_cols = max_name_len + max_count_len + 3; // 3 spacer chars
    let total_needed = (inner.width as usize).min(data_cols);
    let name_width = total_needed
        .saturating_sub(max_count_len + 2)
        .max(max_name_len.min(4));
    let count_width = total_needed
        .saturating_sub(name_width + 2)
        .max(max_count_len);
    let bar_width = inner
        .width
        .saturating_sub((name_width + count_width + 3) as u16) as usize;

    let mut lines: Vec<Line> = Vec::new();
    for tool in tools.iter().take(visible) {
        let bar_len = if max_count > 0 && bar_width > 0 {
            ((tool.call_count as f64 / max_count as f64) * bar_width as f64).ceil() as usize
        } else {
            0
        };
        let bar_len = bar_len.max(1).min(bar_width);
        let bar: String = "\u{2584}".repeat(bar_len);
        let pad: String = " ".repeat(bar_width.saturating_sub(bar_len));
        let name = if tool.tool_name.len() > name_width {
            format!(
                "{}...",
                tool.tool_name
                    .chars()
                    .take(name_width.saturating_sub(3))
                    .collect::<String>()
            )
        } else {
            tool.tool_name.clone()
        };
        lines.push(Line::from(vec![
            Span::styled(format!(" {:<width$}", name, width = name_width), BOLD),
            Span::styled(bar, ACCENT),
            Span::raw(pad),
            Span::styled(
                format!(" {:>width$}", tool.call_count, width = count_width),
                DIM,
            ),
        ]));
    }

    let p = Paragraph::new(lines);
    f.render_widget(p, inner);
}

// ── By Activity ──────────────────────────────────────────────────────────────

pub fn render_activities(f: &mut Frame, activities: &[ActivityStats], area: Rect, focused: bool) {
    let block = card_block("By Activity", focused);
    let inner = block.inner(area);
    f.render_widget(block, area);

    if activities.is_empty() {
        let p = Paragraph::new(" No data").style(DIM);
        f.render_widget(p, inner);
        return;
    }

    // Compute column widths from actual data
    let visible = (inner.height.saturating_sub(1) as usize).min(activities.len());
    let max_cat_len = activities
        .iter()
        .take(visible)
        .map(|a| a.category.len())
        .max()
        .unwrap_or(8);
    let max_cost_len = activities
        .iter()
        .take(visible)
        .map(|a| fmt_cost(a.cost).len())
        .max()
        .unwrap_or(6);
    let max_turns_len = activities
        .iter()
        .take(visible)
        .map(|a| a.turns.to_string().len())
        .max()
        .unwrap_or(1);
    let max_pct_len = activities
        .iter()
        .take(visible)
        .map(|a| a.one_shot_pct.to_string().len())
        .max()
        .unwrap_or(1);

    // Data columns get guaranteed space; category+bar fill whatever is left
    let spacing = 1; // single space between data cols
    let pct_width = max_pct_len + 2; // e.g. "45%" needs digit + %
    let fixed_data = max_cost_len + max_turns_len + pct_width + spacing * 3;
    let cat_bar_width = (inner.width as usize).saturating_sub(fixed_data + 1); // +1 leading space
    let cat_width = max_cat_len.min(cat_bar_width / 3).max(4);
    let bar_width = cat_bar_width.saturating_sub(cat_width);

    let header_line = Line::from(vec![
        Span::styled(format!(" {:<width$}", "Category", width = cat_width), LABEL),
        Span::raw(" ".repeat(bar_width)),
        Span::styled(format!(" {:>width$}", "Cost", width = max_cost_len), LABEL),
        Span::styled(
            format!(" {:>width$}", "Turns", width = max_turns_len),
            LABEL,
        ),
        Span::styled(format!(" {:>width$}", "1-shot", width = pct_width), LABEL),
    ]);
    let mut lines: Vec<Line> = vec![header_line];

    let max_cost = activities.iter().map(|a| a.cost).fold(0.0_f64, f64::max);

    for act in activities.iter().take(visible) {
        let bar_len = if max_cost > 0.0 && bar_width > 0 {
            ((act.cost / max_cost) * bar_width as f64).ceil() as usize
        } else {
            0
        };
        let bar_len = bar_len
            .max(if act.cost > 0.0 { 1 } else { 0 })
            .min(bar_width);
        let bar: String = "\u{2584}".repeat(bar_len);
        let pad: String = " ".repeat(bar_width.saturating_sub(bar_len));
        let category = if act.category.len() > cat_width {
            format!(
                "{}...",
                act.category
                    .chars()
                    .take(cat_width.saturating_sub(3))
                    .collect::<String>()
            )
        } else {
            act.category.clone()
        };
        let one_shot = format!("{}%", act.one_shot_pct as u32);
        lines.push(Line::from(vec![
            Span::styled(format!(" {:<width$}", category, width = cat_width), BOLD),
            Span::styled(bar, ACCENT),
            Span::raw(pad),
            Span::styled(
                format!(" {:>width$}", fmt_cost(act.cost), width = max_cost_len),
                LABEL,
            ),
            Span::styled(
                format!(" {:>width$}", act.turns, width = max_turns_len),
                DIM,
            ),
            Span::styled(format!(" {:>width$}", one_shot, width = pct_width), DIM),
        ]));
    }

    let p = Paragraph::new(lines);
    f.render_widget(p, inner);
}

// ── Footer ───────────────────────────────────────────────────────────────────

pub fn render_footer(f: &mut Frame, area: Rect) {
    let line = Line::from(vec![
        Span::styled("Tab", ACCENT),
        Span::styled(" navigate  ", DIM),
        Span::styled("Enter", ACCENT),
        Span::styled(" details  ", DIM),
        Span::styled("T", ACCENT),
        Span::styled(" today  ", DIM),
        Span::styled("W", ACCENT),
        Span::styled(" week  ", DIM),
        Span::styled("M", ACCENT),
        Span::styled(" month  ", DIM),
        Span::styled("A", ACCENT),
        Span::styled(" all  ", DIM),
        Span::styled("Esc", ACCENT),
        Span::styled(" close", DIM),
    ]);
    let p = Paragraph::new(vec![line])
        .alignment(Alignment::Center)
        .wrap(Wrap { trim: false });
    f.render_widget(p, area);
}