rollcli 0.3.3

A command-line dice roller for tabletop RPGs with advantage/disadvantage and probability estimation
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
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
use ratatui::Frame;
use ratatui::layout::{Alignment, Constraint, Direction, Layout, Rect};
use ratatui::style::{Modifier, Style};
use ratatui::text::{Line, Span};
use ratatui::widgets::{
    Bar, BarChart, BarGroup, Block, Borders, Clear, List, ListItem, Paragraph, Wrap,
};

use super::app::{App, RollerFocus, Screen};
use super::theme;

/// Main draw dispatch.
pub fn draw(f: &mut Frame, app: &App) {
    match app.screen {
        Screen::Roller => draw_roller(f, app),
        Screen::History => draw_history_screen(f, app),
        Screen::Help => {
            // Draw the previous screen underneath, then overlay help
            match app.prev_screen {
                Screen::Roller => draw_roller(f, app),
                Screen::History => draw_history_screen(f, app),
                Screen::Help => draw_roller(f, app),
            }
            draw_help_overlay(f, app);
        }
    }
}

// ── Tab bar ─────────────────────────────────────────────────────────────────

fn draw_tab_bar(f: &mut Frame, app: &App, area: Rect) {
    let roller_style = if app.screen == Screen::Roller {
        Style::default()
            .fg(theme::ACCENT)
            .add_modifier(Modifier::BOLD)
    } else {
        Style::default().fg(ratatui::style::Color::DarkGray)
    };
    let history_style = if app.screen == Screen::History {
        Style::default()
            .fg(theme::ACCENT)
            .add_modifier(Modifier::BOLD)
    } else {
        Style::default().fg(ratatui::style::Color::DarkGray)
    };

    let tabs = Line::from(vec![
        Span::styled(" [", Style::default().fg(ratatui::style::Color::DarkGray)),
        Span::styled("Roller", roller_style),
        Span::styled("] [", Style::default().fg(ratatui::style::Color::DarkGray)),
        Span::styled("History", history_style),
        Span::styled("]", Style::default().fg(ratatui::style::Color::DarkGray)),
        Span::styled(
            "  roll - dice roller",
            Style::default().fg(ratatui::style::Color::DarkGray),
        ),
    ]);
    let paragraph = Paragraph::new(tabs);
    f.render_widget(paragraph, area);
}

// ── Roller screen ───────────────────────────────────────────────────────────

fn draw_roller(f: &mut Frame, app: &App) {
    let area = f.area();

    let chunks = Layout::default()
        .direction(Direction::Vertical)
        .constraints([
            Constraint::Length(1), // tab bar
            Constraint::Length(7), // input + presets row
            Constraint::Length(5), // result
            Constraint::Min(5),    // distribution
            Constraint::Length(1), // footer
        ])
        .split(area);

    draw_tab_bar(f, app, chunks[0]);
    draw_input_presets_row(f, app, chunks[1]);
    draw_result(f, app, chunks[2]);
    draw_inline_distribution(f, app, chunks[3]);
    draw_roller_footer(f, app, chunks[4]);
}

fn draw_input_presets_row(f: &mut Frame, app: &App, area: Rect) {
    let cols = Layout::default()
        .direction(Direction::Horizontal)
        .constraints([
            Constraint::Percentage(60), // input
            Constraint::Percentage(40), // presets
        ])
        .split(area);

    draw_input(f, app, cols[0]);
    draw_presets_sidebar(f, app, cols[1]);
}

fn draw_input(f: &mut Frame, app: &App, area: Rect) {
    let display = if app.input.is_empty() {
        vec![
            Span::styled("> ", theme::input()),
            Span::styled(
                "type expression (e.g. 2d20+4)...",
                Style::default().fg(ratatui::style::Color::DarkGray),
            ),
        ]
    } else {
        vec![
            Span::styled("> ", theme::input()),
            Span::styled(&app.input, theme::input()),
        ]
    };

    let focus_border = if app.roller_focus == RollerFocus::Input {
        Style::default().fg(theme::ACCENT)
    } else {
        Style::default().fg(theme::ACCENT_DIM)
    };

    let block = Block::default()
        .title(Span::styled(" Expression ", theme::title()))
        .borders(Borders::ALL)
        .border_style(focus_border);

    let paragraph = Paragraph::new(Line::from(display)).block(block);
    f.render_widget(paragraph, area);

    // Place cursor only when input is focused
    if app.roller_focus == RollerFocus::Input {
        let cursor_x = area.x + 2 + app.cursor_pos as u16;
        let cursor_y = area.y + 1;
        if cursor_x < area.x + area.width - 1 {
            f.set_cursor_position((cursor_x, cursor_y));
        }
    }
}

fn draw_presets_sidebar(f: &mut Frame, app: &App, area: Rect) {
    let focus_border = if app.roller_focus == RollerFocus::Presets {
        Style::default().fg(theme::ACCENT)
    } else {
        Style::default().fg(theme::ACCENT_DIM)
    };

    let block = Block::default()
        .title(Span::styled(" Presets (Tab) ", theme::title()))
        .borders(Borders::ALL)
        .border_style(focus_border);

    if app.presets.is_empty() {
        let empty = Paragraph::new(Line::from(Span::styled(
            " No presets",
            Style::default().fg(ratatui::style::Color::DarkGray),
        )))
        .block(block);
        f.render_widget(empty, area);
        return;
    }

    let items: Vec<ListItem> = app
        .presets
        .iter()
        .enumerate()
        .map(|(i, preset)| {
            let is_selected = app.roller_focus == RollerFocus::Presets && i == app.preset_selected;
            let marker = if is_selected { "> " } else { "  " };
            let style = if is_selected {
                theme::selected()
            } else {
                Style::default()
            };
            let line = Line::from(vec![
                Span::styled(marker, style),
                Span::styled(&preset.name, theme::preset_name()),
                Span::styled(" ", Style::default()),
                Span::styled(&preset.expression, theme::preset_expr()),
            ]);
            ListItem::new(line)
        })
        .collect();

    let list = List::new(items).block(block);
    f.render_widget(list, area);
}

fn draw_result(f: &mut Frame, app: &App, area: Rect) {
    let age_ms = app.latest_roll_age_ms().unwrap_or(1000);

    let border_style = if app.error_msg.is_some() {
        Style::default().fg(theme::DANGER)
    } else {
        theme::flash_border(age_ms)
    };

    let block = Block::default()
        .title(Span::styled(" Result ", theme::title()))
        .borders(Borders::ALL)
        .border_style(border_style);

    let content = if let Some(ref err) = app.error_msg {
        vec![Line::from(Span::styled(
            format!("Error: {err}"),
            theme::error(),
        ))]
    } else if let Some(entry) = app.latest_roll() {
        // Choose total style: nat max (green), nat min (red), flash, or normal
        let total_style = if entry.is_nat_max {
            theme::nat_max()
        } else if entry.is_nat_min {
            theme::nat_min()
        } else {
            theme::flash_result(age_ms)
        };

        // Build the total display with optional nat label
        let total_str = if entry.is_nat_max {
            format!("{} NAT MAX!", entry.total)
        } else if entry.is_nat_min {
            format!("{} NAT MIN", entry.total)
        } else {
            entry.total.to_string()
        };

        let mut lines = vec![Line::from(vec![
            Span::styled(&entry.expression, theme::history_expr()),
            Span::styled(" => ", Style::default().fg(ratatui::style::Color::DarkGray)),
            Span::styled(&entry.breakdown, theme::result_detail()),
            Span::styled(" = ", Style::default().fg(ratatui::style::Color::DarkGray)),
            Span::styled(total_str, total_style),
        ])];
        lines.push(Line::from(Span::styled(
            format!(
                "min={}  max={}  mean={:.2}",
                entry.stats.min, entry.stats.max, entry.stats.mean
            ),
            theme::stats(),
        )));
        lines
    } else {
        vec![Line::from(Span::styled(
            "Press Enter to roll",
            Style::default().fg(ratatui::style::Color::DarkGray),
        ))]
    };

    let paragraph = Paragraph::new(content).block(block);
    f.render_widget(paragraph, area);
}

fn draw_inline_distribution(f: &mut Frame, app: &App, area: Rect) {
    let Some(ref dist) = app.dist else {
        let block = Block::default()
            .title(Span::styled(" Distribution ", theme::title()))
            .borders(Borders::ALL)
            .border_style(Style::default().fg(theme::ACCENT_DIM));
        let empty = Paragraph::new(Line::from(Span::styled(
            "Roll an expression to see distribution",
            Style::default().fg(ratatui::style::Color::DarkGray),
        )))
        .block(block);
        f.render_widget(empty, area);
        return;
    };

    // Split off a 1-line footer for the probability readout when active
    let has_target = dist.target.is_some();
    let chunks = if has_target {
        Layout::default()
            .direction(Direction::Vertical)
            .constraints([Constraint::Min(4), Constraint::Length(1)])
            .split(area)
    } else {
        Layout::default()
            .direction(Direction::Vertical)
            .constraints([Constraint::Min(4)])
            .split(area)
    };
    let chart_area = chunks[0];

    // Build title with prob hint or result
    let title = if let (Some(target), Some(prob)) = (dist.target, dist.target_prob) {
        Line::from(vec![
            Span::styled(" Distribution ", theme::title()),
            Span::styled(
                format!("P(>= {target}) = {:.1}%", prob * 100.0),
                Style::default()
                    .fg(ratatui::style::Color::Yellow)
                    .add_modifier(ratatui::style::Modifier::BOLD),
            ),
            Span::styled(" ", Style::default()),
        ])
    } else {
        Line::from(vec![
            Span::styled(" Distribution ", theme::title()),
            Span::styled(
                " [/] navigate ",
                Style::default().fg(ratatui::style::Color::DarkGray),
            ),
        ])
    };

    let block = Block::default()
        .title(title)
        .borders(Borders::ALL)
        .border_style(Style::default().fg(theme::ACCENT_DIM));

    let total: u64 = dist.counts.values().sum();
    let bars: Vec<Bar> = dist
        .counts
        .iter()
        .map(|(&value, &count)| {
            let pct = if total > 0 {
                (count as f64 / total as f64 * 100.0) as u64
            } else {
                0
            };
            let style = if dist.target == Some(value) {
                theme::bar_highlight()
            } else {
                theme::bar_chart()
            };
            Bar::default()
                .value(pct)
                .label(Line::from(value.to_string()))
                .style(style)
                .text_value(format!("{:.1}%", count as f64 / total as f64 * 100.0))
        })
        .collect();

    let bar_count = bars.len() as u16;
    let chart = BarChart::default()
        .block(block)
        .data(BarGroup::default().bars(&bars))
        .bar_width(((chart_area.width.saturating_sub(2)) / bar_count.max(1)).clamp(1, 5))
        .bar_gap(1);

    f.render_widget(chart, chart_area);

    // Probability detail line
    if has_target {
        if let (Some(target), Some(prob)) = (dist.target, dist.target_prob) {
            let prob_line = Line::from(vec![
                Span::styled(
                    format!("  P(result >= {target})  =  {:.2}%", prob * 100.0),
                    Style::default().fg(ratatui::style::Color::Yellow),
                ),
                Span::styled(
                    "   [/] move target",
                    Style::default().fg(ratatui::style::Color::DarkGray),
                ),
            ]);
            f.render_widget(Paragraph::new(prob_line), chunks[1]);
        }
    }
}

fn draw_roller_footer(f: &mut Frame, app: &App, area: Rect) {
    let keys = if app.roller_focus == RollerFocus::Presets {
        Line::from(vec![
            Span::styled("[Enter]", theme::keybinding_key()),
            Span::styled(" Roll  ", theme::keybinding_desc()),
            Span::styled("[j/k]", theme::keybinding_key()),
            Span::styled(" Navigate  ", theme::keybinding_desc()),
            Span::styled("[d]", theme::keybinding_key()),
            Span::styled(" Delete  ", theme::keybinding_desc()),
            Span::styled("[Tab/Esc]", theme::keybinding_key()),
            Span::styled(" Back  ", theme::keybinding_desc()),
            Span::styled("[F2]", theme::keybinding_key()),
            Span::styled(" History", theme::keybinding_desc()),
        ])
    } else {
        Line::from(vec![
            Span::styled("[Enter]", theme::keybinding_key()),
            Span::styled(" Roll  ", theme::keybinding_desc()),
            Span::styled("[Tab]", theme::keybinding_key()),
            Span::styled(" Presets  ", theme::keybinding_desc()),
            Span::styled("[ ]", theme::keybinding_key()),
            Span::styled(" Dist target  ", theme::keybinding_desc()),
            Span::styled("[F2]", theme::keybinding_key()),
            Span::styled(" History  ", theme::keybinding_desc()),
            Span::styled("[F1]", theme::keybinding_key()),
            Span::styled(" Help  ", theme::keybinding_desc()),
            Span::styled("[Esc]", theme::keybinding_key()),
            Span::styled(" Quit", theme::keybinding_desc()),
        ])
    };
    let footer = Paragraph::new(keys).alignment(Alignment::Center);
    f.render_widget(footer, area);
}

// ── History screen ──────────────────────────────────────────────────────────

fn draw_history_screen(f: &mut Frame, app: &App) {
    let area = f.area();

    let chunks = Layout::default()
        .direction(Direction::Vertical)
        .constraints([
            Constraint::Length(1), // tab bar
            Constraint::Min(5),    // history list
            Constraint::Length(1), // footer
        ])
        .split(area);

    draw_tab_bar(f, app, chunks[0]);
    draw_history_list(f, app, chunks[1]);
    draw_history_footer(f, chunks[2]);
}

fn draw_history_list(f: &mut Frame, app: &App, area: Rect) {
    let block = Block::default()
        .title(Span::styled(
            format!(" History ({}) ", app.roll_history.len()),
            theme::title(),
        ))
        .borders(Borders::ALL)
        .border_style(Style::default().fg(theme::ACCENT_DIM));

    if app.roll_history.is_empty() {
        let empty = Paragraph::new(Line::from(Span::styled(
            "No rolls yet",
            Style::default().fg(ratatui::style::Color::DarkGray),
        )))
        .block(block);
        f.render_widget(empty, area);
        return;
    }

    // Show history in reverse (newest first)
    let items: Vec<ListItem> = app
        .roll_history
        .iter()
        .rev()
        .skip(app.history_scroll)
        .map(|entry| {
            let total_style = if entry.is_nat_max {
                theme::nat_max()
            } else if entry.is_nat_min {
                theme::nat_min()
            } else {
                theme::history_result()
            };
            let line = Line::from(vec![
                Span::styled(format!("{:<16}", entry.expression), theme::history_expr()),
                Span::styled(" => ", Style::default().fg(ratatui::style::Color::DarkGray)),
                Span::styled(format!("{:<6}", entry.total), total_style),
                Span::styled(&entry.breakdown, theme::result_detail()),
            ]);
            ListItem::new(line)
        })
        .collect();

    let list = List::new(items).block(block);
    f.render_widget(list, area);
}

fn draw_history_footer(f: &mut Frame, area: Rect) {
    let keys = Line::from(vec![
        Span::styled("[F2/Tab/Esc]", theme::keybinding_key()),
        Span::styled(" Roller  ", theme::keybinding_desc()),
        Span::styled("[PgUp/PgDn]", theme::keybinding_key()),
        Span::styled(" Scroll  ", theme::keybinding_desc()),
        Span::styled("[F1]", theme::keybinding_key()),
        Span::styled(" Help  ", theme::keybinding_desc()),
    ]);
    let footer = Paragraph::new(keys).alignment(Alignment::Center);
    f.render_widget(footer, area);
}

// ── Help overlay ────────────────────────────────────────────────────────────

fn draw_help_overlay(f: &mut Frame, _app: &App) {
    let area = f.area();

    // Center a box
    let width = 54.min(area.width.saturating_sub(4));
    let height = 26.min(area.height.saturating_sub(4));
    let x = (area.width.saturating_sub(width)) / 2;
    let y = (area.height.saturating_sub(height)) / 2;
    let popup = Rect::new(x, y, width, height);

    f.render_widget(Clear, popup);

    let help_text = vec![
        Line::from(""),
        Line::from(vec![Span::styled(
            "  Roller Tab",
            Style::default()
                .add_modifier(Modifier::BOLD)
                .fg(theme::ACCENT),
        )]),
        Line::from(vec![
            Span::styled("  Enter      ", theme::keybinding_key()),
            Span::styled("Roll the expression", theme::keybinding_desc()),
        ]),
        Line::from(vec![
            Span::styled("  Tab        ", theme::keybinding_key()),
            Span::styled("Focus presets sidebar", theme::keybinding_desc()),
        ]),
        Line::from(vec![
            Span::styled("  F2         ", theme::keybinding_key()),
            Span::styled("Switch to History tab", theme::keybinding_desc()),
        ]),
        Line::from(vec![
            Span::styled("  Up/Down    ", theme::keybinding_key()),
            Span::styled("Browse input history", theme::keybinding_desc()),
        ]),
        Line::from(vec![
            Span::styled("  Ctrl+U     ", theme::keybinding_key()),
            Span::styled("Clear input", theme::keybinding_desc()),
        ]),
        Line::from(vec![
            Span::styled("  [ / ]      ", theme::keybinding_key()),
            Span::styled("Move distribution target (P >= N)", theme::keybinding_desc()),
        ]),
        Line::from(""),
        Line::from(vec![Span::styled(
            "  Presets (Tab)",
            Style::default()
                .add_modifier(Modifier::BOLD)
                .fg(theme::ACCENT),
        )]),
        Line::from(vec![
            Span::styled("  j/k        ", theme::keybinding_key()),
            Span::styled("Navigate presets", theme::keybinding_desc()),
        ]),
        Line::from(vec![
            Span::styled("  Enter      ", theme::keybinding_key()),
            Span::styled("Roll selected preset", theme::keybinding_desc()),
        ]),
        Line::from(vec![
            Span::styled("  d          ", theme::keybinding_key()),
            Span::styled("Delete selected preset", theme::keybinding_desc()),
        ]),
        Line::from(""),
        Line::from(vec![Span::styled(
            "  History Tab",
            Style::default()
                .add_modifier(Modifier::BOLD)
                .fg(theme::ACCENT),
        )]),
        Line::from(vec![
            Span::styled("  PgUp/PgDn  ", theme::keybinding_key()),
            Span::styled("Scroll roll history", theme::keybinding_desc()),
        ]),
        Line::from(vec![
            Span::styled("  F2/Tab/Esc ", theme::keybinding_key()),
            Span::styled("Back to Roller", theme::keybinding_desc()),
        ]),
        Line::from(""),
        Line::from(vec![Span::styled(
            "  Expressions",
            Style::default()
                .add_modifier(Modifier::BOLD)
                .fg(theme::ACCENT),
        )]),
        Line::from(vec![
            Span::styled("  2d6+3      ", theme::keybinding_key()),
            Span::styled("Roll 2 six-sided dice + 3", theme::keybinding_desc()),
        ]),
        Line::from(vec![
            Span::styled("  adv d20+5  ", theme::keybinding_key()),
            Span::styled("Roll with advantage", theme::keybinding_desc()),
        ]),
        Line::from(vec![
            Span::styled("  4d6kh3     ", theme::keybinding_key()),
            Span::styled("Roll 4d6, keep highest 3", theme::keybinding_desc()),
        ]),
        Line::from(""),
    ];

    let block = Block::default()
        .title(Span::styled(" Help (F1 to close) ", theme::title()))
        .borders(Borders::ALL)
        .border_style(Style::default().fg(theme::ACCENT));

    let paragraph = Paragraph::new(help_text)
        .block(block)
        .wrap(Wrap { trim: false });
    f.render_widget(paragraph, popup);
}