sidekick 0.6.4

Protects your unsaved Neovim work from Claude Code.
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
//! `sidekick demo` — bundled asciinema playback inside a media-player TUI.
//!
//! Drives an `avt::Vt` virtual terminal from a bundled `.cast` file and
//! copies its visible grid into a ratatui buffer each frame. The whole
//! thing renders inside a bordered "player" widget so it's visually
//! distinct from the user's real terminal.

use std::io;
use std::time::{Duration, Instant};

use anyhow::{Context, anyhow};
use ratatui::Frame;
use ratatui::Terminal;
use ratatui::backend::{Backend, CrosstermBackend};
use ratatui::buffer::Buffer;
use ratatui::crossterm::event::{self, Event, KeyCode, KeyEventKind};
use ratatui::crossterm::execute;
use ratatui::crossterm::terminal::{
    EnterAlternateScreen, LeaveAlternateScreen, disable_raw_mode, enable_raw_mode,
};
use ratatui::layout::{Constraint, Direction, Layout, Position, Rect};
use ratatui::style::{Color, Modifier, Style};
use ratatui::text::Line;
use ratatui::widgets::{Block, BorderType, Borders, LineGauge, Paragraph};
use serde::Deserialize;

const DEMO_CAST: &[u8] = include_bytes!(concat!(env!("OUT_DIR"), "/demo.cast"));
const FRAME_POLL: Duration = Duration::from_millis(33); // ~30fps

#[derive(Deserialize)]
struct CastHeader {
    version: u8,
    // asciicast v2 puts terminal dimensions at the top level...
    width: Option<u16>,
    height: Option<u16>,
    // ...asciicast v3 nests them under `term`.
    term: Option<CastTerm>,
}

#[derive(Deserialize)]
struct CastTerm {
    cols: u16,
    rows: u16,
}

struct Cast {
    width: u16,
    height: u16,
    events: Vec<CastEvent>,
}

struct CastEvent {
    time: f64,
    data: String,
}

fn parse_cast(bytes: &[u8]) -> anyhow::Result<Cast> {
    let text = std::str::from_utf8(bytes).context("data is corrupted")?;
    let mut lines = text.lines();
    let header_line = lines.next().context("demo is empty")?;
    let header: CastHeader = serde_json::from_str(header_line).context("header is malformed")?;
    if header.version != 2 && header.version != 3 {
        return Err(anyhow!(
            "unsupported version {} (need v2 or v3)",
            header.version
        ));
    }
    let (width, height) = match (header.width, header.height, &header.term) {
        (Some(w), Some(h), _) => (w, h),
        (_, _, Some(term)) => (term.cols, term.rows),
        _ => return Err(anyhow!("header missing terminal dimensions")),
    };

    // asciicast v2 event times are absolute; v3 times are intervals since
    // the previous event. Accumulate v3 intervals into an absolute timeline.
    let v3_intervals = header.version == 3;
    let mut clock = 0.0f64;
    let mut events = Vec::new();
    for line in lines {
        if line.trim().is_empty() {
            continue;
        }
        let value: serde_json::Value = serde_json::from_str(line).context("frame is malformed")?;
        let Some(arr) = value.as_array() else {
            continue;
        };
        if arr.len() < 3 {
            continue;
        }
        let Some(time) = arr[0].as_f64() else {
            continue;
        };
        // Accumulate before the "o" filter so skipped events (e.g. a
        // trailing "x" exit marker) don't desync the v3 clock.
        let abs_time = if v3_intervals {
            clock += time;
            clock
        } else {
            time
        };
        if arr[1].as_str() != Some("o") {
            continue;
        }
        let Some(data) = arr[2].as_str() else {
            continue;
        };
        events.push(CastEvent {
            time: abs_time,
            data: data.to_string(),
        });
    }
    Ok(Cast {
        width,
        height,
        events,
    })
}

pub fn run() -> anyhow::Result<()> {
    let cast = parse_cast(DEMO_CAST).context("couldn't load demo")?;

    let (term_cols, term_rows) = ratatui::crossterm::terminal::size()?;

    // Approximate aspect-ratio guard: refuse only when the terminal is
    // genuinely wrong-shaped for a widescreen cast (i.e. tall and narrow).
    // 50% threshold is permissive — same-shape shrinks pass, 4:3-ish
    // terminals pass, only the square / portrait shapes refuse.
    const MIN_RATIO_FRAC: f64 = 0.50;
    let cast_ratio = cast.width as f64 / cast.height as f64;
    let term_ratio = term_cols as f64 / term_rows as f64;
    let threshold = cast_ratio * MIN_RATIO_FRAC;
    if term_ratio < threshold {
        let min_cols = (threshold * term_rows as f64).ceil() as u16;
        let max_rows = (term_cols as f64 / threshold).floor() as u16;
        print_aspect_mismatch_error(
            term_cols,
            term_rows,
            cast.width,
            cast.height,
            min_cols,
            max_rows,
        );
        std::process::exit(2);
    }

    enable_raw_mode()?;
    let mut stdout = io::stdout();
    execute!(stdout, EnterAlternateScreen)?;
    let backend = CrosstermBackend::new(stdout);
    let mut terminal = Terminal::new(backend)?;

    let result = playback_loop(&mut terminal, &cast);

    disable_raw_mode()?;
    execute!(terminal.backend_mut(), LeaveAlternateScreen)?;
    terminal.show_cursor()?;

    match result? {
        ExitReason::UserQuit => Ok(()),
        ExitReason::AspectChanged {
            cols,
            rows,
            min_cols,
            max_rows,
        } => {
            print_aspect_mismatch_error(cols, rows, cast.width, cast.height, min_cols, max_rows);
            std::process::exit(2);
        }
    }
}

enum ExitReason {
    UserQuit,
    AspectChanged {
        cols: u16,
        rows: u16,
        min_cols: u16,
        max_rows: u16,
    },
}

fn playback_loop<B: Backend>(
    terminal: &mut Terminal<B>,
    cast: &Cast,
) -> anyhow::Result<ExitReason> {
    let mut vt = avt::Vt::new(cast.width as usize, cast.height as usize);
    let mut next_event = 0usize;
    let total = cast.events.last().map(|e| e.time).unwrap_or(0.0);

    let cast_ratio = cast.width as f64 / cast.height as f64;
    const MIN_RATIO_FRAC: f64 = 0.50;
    let ratio_threshold = cast_ratio * MIN_RATIO_FRAC;

    let mut start = Instant::now();
    let mut paused = false;
    let mut pause_start: Option<Instant> = None;
    let mut paused_offset = Duration::ZERO;
    let mut last_play_at: Option<Instant> = Some(Instant::now());
    const PLAY_FLASH: Duration = Duration::from_millis(500);

    loop {
        // Bail if the user resized below the aspect-ratio threshold while
        // playback was running. Tear down cleanly via the caller.
        if let Ok((cols, rows)) = ratatui::crossterm::terminal::size() {
            let term_ratio = cols as f64 / rows as f64;
            if term_ratio < ratio_threshold {
                let min_cols = (ratio_threshold * rows as f64).ceil() as u16;
                let max_rows = (cols as f64 / ratio_threshold).floor() as u16;
                return Ok(ExitReason::AspectChanged {
                    cols,
                    rows,
                    min_cols,
                    max_rows,
                });
            }
        }
        let elapsed_dur = if let Some(ps) = pause_start {
            ps.saturating_duration_since(start)
                .saturating_sub(paused_offset)
        } else {
            Instant::now()
                .saturating_duration_since(start)
                .saturating_sub(paused_offset)
        };
        let elapsed = elapsed_dur.as_secs_f64();

        while next_event < cast.events.len() && cast.events[next_event].time <= elapsed {
            vt.feed_str(&cast.events[next_event].data);
            next_event += 1;
        }
        let done = next_event >= cast.events.len() && elapsed >= total;

        let show_play = !paused
            && last_play_at
                .map(|t| t.elapsed() < PLAY_FLASH)
                .unwrap_or(false);

        terminal.draw(|f| draw_player(f, &vt, elapsed, total, paused, done, show_play))?;

        if event::poll(FRAME_POLL)?
            && let Event::Key(k) = event::read()?
            && k.kind != KeyEventKind::Release
        {
            match k.code {
                KeyCode::Char('q') | KeyCode::Esc => return Ok(ExitReason::UserQuit),
                KeyCode::Char(' ') => {
                    if paused {
                        if let Some(ps) = pause_start.take() {
                            paused_offset += ps.elapsed();
                        }
                        paused = false;
                        last_play_at = Some(Instant::now());
                    } else {
                        pause_start = Some(Instant::now());
                        paused = true;
                    }
                }
                KeyCode::Char('r') => {
                    vt = avt::Vt::new(cast.width as usize, cast.height as usize);
                    next_event = 0;
                    start = Instant::now();
                    paused = false;
                    pause_start = None;
                    paused_offset = Duration::ZERO;
                    last_play_at = Some(Instant::now());
                }
                _ => {}
            }
        }
    }
}

fn draw_player(
    f: &mut Frame<'_>,
    vt: &avt::Vt,
    elapsed: f64,
    total: f64,
    paused: bool,
    done: bool,
    show_play: bool,
) {
    let area = f.area();

    let icon = if done {
        ""
    } else if paused {
        ""
    } else {
        ""
    };
    let title = format!(" {icon} sidekick demo ");
    let hints = " [space] pause · [r] restart · [q] quit ";

    let block = Block::default()
        .borders(Borders::ALL)
        .border_type(BorderType::Rounded)
        .border_style(Style::default().fg(Color::Cyan))
        .title(title)
        .title_bottom(Line::from(hints).right_aligned());

    let inner = block.inner(area);
    f.render_widget(block, area);

    let chunks = Layout::default()
        .direction(Direction::Vertical)
        .constraints([Constraint::Min(0), Constraint::Length(1)])
        .split(inner);

    render_vt(f.buffer_mut(), chunks[0], vt);
    if paused {
        render_pause_overlay(f.buffer_mut(), chunks[0]);
    } else if show_play {
        render_play_overlay(f.buffer_mut(), chunks[0]);
    }
    render_status(f, chunks[1], elapsed, total, paused);
}

fn render_play_overlay(buf: &mut Buffer, area: Rect) {
    // Right-pointing triangle: `◣` for the top slope, `◤` for the bottom
    // slope, `█` for the body. Even height means there's no single middle
    // row — instead the two innermost rows both end at the same column, and
    // their slope cells (`◣` above, `◤` below) share a corner point on the
    // cell boundary. That shared point IS the tip. No special tip glyph.
    const ICON_H: u16 = 10;
    let half = ICON_H / 2;
    let icon_w = half;
    if area.width < icon_w || area.height < ICON_H {
        return;
    }
    let x0 = area.x + (area.width - icon_w) / 2;
    let y0 = area.y + (area.height - ICON_H) / 2;
    let style = Style::default()
        .fg(Color::Cyan)
        .add_modifier(Modifier::BOLD);

    for row in 0..ICON_H {
        let (dist, glyph) = if row < half {
            (half - 1 - row, "")
        } else {
            (row - half, "")
        };
        let body_w = half - 1 - dist;

        for col in 0..body_w {
            let pos = Position {
                x: x0 + col,
                y: y0 + row,
            };
            if let Some(c) = buf.cell_mut(pos) {
                c.set_symbol("");
                c.set_style(style);
            }
        }
        let pos = Position {
            x: x0 + body_w,
            y: y0 + row,
        };
        if let Some(c) = buf.cell_mut(pos) {
            c.set_symbol(glyph);
            c.set_style(style);
        }
    }
}

fn render_pause_overlay(buf: &mut Buffer, area: Rect) {
    // Two solid blocks separated by a gap — a classic media-player pause glyph.
    const BAR_W: u16 = 5;
    const GAP_W: u16 = 4;
    const ICON_H: u16 = 8;
    let icon_w = BAR_W * 2 + GAP_W;
    if area.width < icon_w || area.height < ICON_H {
        return;
    }
    let x0 = area.x + (area.width - icon_w) / 2;
    let y0 = area.y + (area.height - ICON_H) / 2;
    let style = Style::default()
        .fg(Color::Cyan)
        .add_modifier(Modifier::BOLD);
    for row in 0..ICON_H {
        for col in 0..icon_w {
            // Left bar: 0..BAR_W. Right bar: BAR_W+GAP_W..icon_w.
            if (BAR_W..BAR_W + GAP_W).contains(&col) {
                continue;
            }
            let pos = Position {
                x: x0 + col,
                y: y0 + row,
            };
            if let Some(c) = buf.cell_mut(pos) {
                c.set_symbol("");
                c.set_style(style);
            }
        }
    }
}

fn render_vt(buf: &mut Buffer, area: Rect, vt: &avt::Vt) {
    for (y, line) in vt.view().enumerate() {
        let row = y as u16;
        if row >= area.height {
            break;
        }
        for (x, cell) in line.cells().iter().enumerate() {
            let col = x as u16;
            if col >= area.width {
                break;
            }
            if cell.width() == 0 {
                // WideTail: ratatui reserves the column itself when we set the
                // wide head; nothing to do here.
                continue;
            }
            let pos = Position {
                x: area.x + col,
                y: area.y + row,
            };
            if let Some(buf_cell) = buf.cell_mut(pos) {
                let mut s = [0u8; 4];
                let symbol = cell.char().encode_utf8(&mut s);
                buf_cell.set_symbol(symbol);
                buf_cell.set_style(pen_to_style(cell.pen()));
            }
        }
    }

    let cursor = vt.cursor();
    if cursor.visible {
        let col = cursor.col as u16;
        let row = cursor.row as u16;
        if col < area.width && row < area.height {
            let pos = Position {
                x: area.x + col,
                y: area.y + row,
            };
            if let Some(buf_cell) = buf.cell_mut(pos) {
                buf_cell.set_style(buf_cell.style().add_modifier(Modifier::REVERSED));
            }
        }
    }
}

fn pen_to_style(pen: &avt::Pen) -> Style {
    let mut style = Style::default();
    if let Some(fg) = pen.foreground() {
        style = style.fg(map_color(fg));
    }
    if let Some(bg) = pen.background() {
        style = style.bg(map_color(bg));
    }
    let mut mods = Modifier::empty();
    if pen.is_bold() {
        mods |= Modifier::BOLD;
    }
    if pen.is_italic() {
        mods |= Modifier::ITALIC;
    }
    if pen.is_underline() {
        mods |= Modifier::UNDERLINED;
    }
    if pen.is_inverse() {
        mods |= Modifier::REVERSED;
    }
    if pen.is_faint() {
        mods |= Modifier::DIM;
    }
    if pen.is_blink() {
        mods |= Modifier::SLOW_BLINK;
    }
    if pen.is_strikethrough() {
        mods |= Modifier::CROSSED_OUT;
    }
    style.add_modifier(mods)
}

fn map_color(c: avt::Color) -> Color {
    match c {
        avt::Color::Indexed(n) => Color::Indexed(n),
        avt::Color::RGB(rgb) => Color::Rgb(rgb.r, rgb.g, rgb.b),
    }
}

fn render_status(f: &mut Frame<'_>, area: Rect, elapsed: f64, total: f64, paused: bool) {
    let clamped = elapsed.clamp(0.0, total.max(0.001));
    let time_text = format!("{} / {}", format_time(clamped), format_time(total));
    let prefix = if paused { "" } else { "" };
    let time_w = time_text.len() as u16 + 2;
    let prefix_w = 4u16;

    let chunks = Layout::default()
        .direction(Direction::Horizontal)
        .constraints([
            Constraint::Length(prefix_w),
            Constraint::Min(1),
            Constraint::Length(time_w),
        ])
        .split(area);

    f.render_widget(
        Paragraph::new(prefix).style(Style::default().fg(Color::Cyan)),
        chunks[0],
    );

    let ratio = if total > 0.0 { clamped / total } else { 0.0 };
    let gauge = LineGauge::default()
        .filled_style(Style::default().fg(Color::Cyan))
        .unfilled_style(Style::default().fg(Color::DarkGray))
        .ratio(ratio.clamp(0.0, 1.0));
    f.render_widget(gauge, chunks[1]);

    f.render_widget(
        Paragraph::new(time_text)
            .style(Style::default().fg(Color::DarkGray))
            .right_aligned(),
        chunks[2],
    );
}

fn format_time(secs: f64) -> String {
    let s = secs.max(0.0) as u64;
    format!("{:02}:{:02}", s / 60, s % 60)
}

fn print_aspect_mismatch_error(
    have_w: u16,
    have_h: u16,
    cast_w: u16,
    cast_h: u16,
    min_cols: u16,
    max_rows: u16,
) {
    let red = "\x1b[31m";
    let cyan = "\x1b[36m";
    let dim = "\x1b[2m";
    let bold = "\x1b[1m";
    let reset = "\x1b[0m";

    eprintln!();
    eprintln!("  {cyan}{reset} {bold}sidekick demo{reset}  {dim}⏹ can't play{reset}");
    eprintln!();
    eprintln!("    {red}{reset}  {bold}Your terminal is the wrong shape for this demo.{reset}");
    eprintln!();
    eprintln!("        {dim}your terminal:{reset}   {bold}{have_w}×{have_h}{reset}");
    eprintln!("        {dim}demo:{reset}            {bold}{cast_w}×{cast_h}{reset}");
    eprintln!();
    eprintln!(
        "    {dim}Widen to at least{reset} {bold}{min_cols}{reset} {dim}cols{reset}{dim}, or shorten to{reset} {bold}{max_rows}{reset} {dim}rows.{reset}"
    );
    eprintln!();
}