heatsync-tui 0.2.0

heat-sorted live multichat in the terminal — twitch, kick + youtube (read-only), real emotes, vim keys
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
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
use crate::app::{App, Feed, InputMode};
use crate::config::TabPos;
use crate::emote::render::EMOTE_H;
use crate::input::slot_of;
use crate::layout::{fit, layout_message, layout_text, EmoteMode, Look, Place, RowPlan, Rows};
use crate::palette;
use heatsync_core::edit;
use heatsync_core::emote::{segments, EmoteSet, Segment};
use heatsync_core::heat::Tier;
use heatsync_core::scroll;
use heatsync_core::TabKind;
use heatsync_core::{Channel, Platform};
use ratatui::layout::{Constraint, Layout, Rect};
use ratatui::style::Modifier;
use ratatui::text::{Line, Span};
use ratatui::widgets::Paragraph;
use ratatui::Frame;
use ratatui_image::Image;

/// width of the tab column when the bar is vertical (left/right).
const TAB_COL_W: u16 = 16;

pub(crate) fn emote_mode(app: &App) -> EmoteMode<'_> {
    match (&app.store, &app.fb) {
        // a tmux session outlives any one client: reattached over mosh or from
        // termux, the graphics escapes probed at startup would just vanish and
        // every emote would be a blank hole. usable() re-checks the attached
        // client on a TTL, so those frames draw emote names instead — and the
        // images come straight back when a graphics terminal reattaches.
        (Some(s), _) if s.usable() => EmoteMode::Term(s),
        (None, Some(f)) => EmoteMode::Fb(f),
        _ => EmoteMode::Text,
    }
}

pub(crate) fn ui(f: &mut Frame, app: &App) {
    app.chat_body.set(Rect::default());
    let mode = emote_mode(app);
    // wysiwyg strip: while composing, a draft that resolves an emote gets a
    // live preview row above the footer — the message as it will actually
    // render, images included. zero rows (and zero cost) otherwise.
    let preview_h = preview_height(app, mode);
    let [main, preview, footer] = Layout::vertical([
        Constraint::Min(0),
        Constraint::Length(preview_h),
        Constraint::Length(1),
    ])
    .areas(f.area());
    if preview_h > 0 {
        draw_preview(f, preview, app, mode);
    }

    if let (InputMode::Pick, Some(p)) = (app.mode, &app.picker) {
        f.render_widget(Paragraph::new(p.lines(main.width, main.height)), main);
        draw_footer(f, footer, app, app.channels.len());
        return;
    }

    if matches!(
        app.mode,
        InputMode::Manage | InputMode::SlotPick | InputMode::SlotEdit(_)
    ) {
        draw_manage(f, main, app);
        draw_footer(f, footer, app, app.channels.len());
        return;
    }

    if app.channels.is_empty() {
        f.render_widget(
            Paragraph::new(Line::from(vec![
                Span::styled("  no channels — press ", palette::DIM),
                Span::styled("o", palette::HEAD),
                Span::styled(" to join one", palette::DIM),
            ])),
            main,
        );
        draw_footer(f, footer, app, 0);
        return;
    }

    // carve the tab bar out of `main` according to the configured position.
    let (tabs, chat) = match app.tab_pos {
        TabPos::Top => {
            let a = Layout::vertical([Constraint::Length(1), Constraint::Min(0)]).split(main);
            (a[0], a[1])
        }
        TabPos::Bottom => {
            let a = Layout::vertical([Constraint::Min(0), Constraint::Length(1)]).split(main);
            (a[1], a[0])
        }
        TabPos::Left => {
            let a =
                Layout::horizontal([Constraint::Length(TAB_COL_W), Constraint::Min(0)]).split(main);
            (a[0], a[1])
        }
        TabPos::Right => {
            let a =
                Layout::horizontal([Constraint::Min(0), Constraint::Length(TAB_COL_W)]).split(main);
            (a[1], a[0])
        }
    };

    draw_tabs(f, tabs, app);
    draw_active(f, chat, app, mode);
    draw_footer(f, footer, app, app.channels.len());
}

/// rows the composer preview needs: EMOTE_H when the draft resolves at least
/// one emote on a graphics tier, else 0 (plain text needs no preview — the
/// composer already IS the text).
fn preview_height(app: &App, mode: EmoteMode) -> u16 {
    if app.mode != InputMode::Insert || app.channels.is_empty() || mode.square_cells().is_none() {
        return 0;
    }
    let text = app.line.text();
    if text.is_empty() {
        return 0;
    }
    let set = &app.emotes[app.focus];
    if segments(&text, set)
        .iter()
        .any(|s| matches!(s, Segment::Stack(_)))
    {
        EMOTE_H
    } else {
        0
    }
}

/// the draft as it will render — same layout path as a real chat row (first
/// visual row; the preview strip is one emote-row tall).
fn draw_preview(f: &mut Frame, area: Rect, app: &App, mode: EmoteMode) {
    let set = &app.emotes[app.focus];
    let mut b = Rows::new(area.width);
    b.prefix(Span::styled(" ❯ ", palette::TEXT), 3);
    let text = app.line.text();
    layout_text(&mut b, &text, set, EmoteSet::none(), mode);
    if let Some(row) = b.finish().into_iter().next() {
        draw_planned(f, mode, area, area.y, row.line, &row.places, EMOTE_H);
    }
}

/// the channel tab bar — horizontal row (top/bottom) or vertical list (left/right).
pub(crate) fn draw_tabs(f: &mut Frame, area: Rect, app: &App) {
    let tab_style = |i: usize, ch: &Channel| {
        let base = if i == app.focus {
            palette::TAG
        } else {
            palette::tab(ch.unread())
        };
        // the temp tab is underlined: a look, not a save.
        if ch.temp {
            base.add_modifier(Modifier::UNDERLINED)
        } else {
            base
        }
    };

    // merged tabs read `name+2` — the sources are one channel to the eye.
    let plus = |ch: &Channel| {
        if ch.merged() {
            format!("+{}", ch.extra.len())
        } else {
            String::new()
        }
    };
    if app.tab_pos.is_vertical() {
        for (i, ch) in app.channels.iter().enumerate() {
            if i as u16 >= area.height {
                break;
            }
            let label = fit(
                if ch.kind.is_special() {
                    format!(" {} ", ch.name)
                } else {
                    format!(" {}{} {:.0} ", ch.name, plus(ch), ch.heat)
                },
                area.width,
            );
            let row = Rect {
                x: area.x,
                y: area.y + i as u16,
                width: area.width,
                height: 1,
            };
            f.render_widget(
                Paragraph::new(Line::from(Span::styled(label, tab_style(i, ch)))),
                row,
            );
        }
    } else {
        let mut spans = Vec::new();
        for (i, ch) in app.channels.iter().enumerate() {
            let label = if ch.kind.is_special() {
                format!(" {} ", ch.name)
            } else {
                format!(
                    " {}·{}{} {:.0} ",
                    ch.name,
                    ch.platform.tag(),
                    plus(ch),
                    ch.heat
                )
            };
            spans.push(Span::styled(label, tab_style(i, ch)));
            spans.push(Span::raw(" "));
        }
        f.render_widget(Paragraph::new(Line::from(spans)), area);
    }
}

/// the active channel: a heat bar, then its chat. messages with a ready emote
/// occupy EMOTE_H rows (text on the bottom row, emote painted across the block),
/// so emotes render big without colliding with neighbouring lines. bottom-anchored.
fn draw_active(f: &mut Frame, area: Rect, app: &App, mode: EmoteMode) {
    let ch = &app.channels[app.focus];
    let set = &app.emotes[app.focus];
    let pin = ch.visible_pin();
    let [barrow, pinrow, body] = Layout::vertical([
        Constraint::Length(1),
        Constraint::Length(u16::from(pin.is_some())),
        Constraint::Min(0),
    ])
    .areas(area);
    f.render_widget(
        Paragraph::new(heat_bar(ch.heat, barrow.width as usize)),
        barrow,
    );
    if let Some(p) = pin {
        let line = fit(format!("◉ {}: {}", p.user, p.text), pinrow.width);
        f.render_widget(
            Paragraph::new(Line::from(Span::styled(line, palette::DIM_TAG))),
            pinrow,
        );
    }
    if body.height == 0 || body.width == 0 {
        return;
    }

    let cap = body.height as usize;
    // plan visible messages newest-first, honouring per-message height (wrapped
    // rows + emote rows). chat always follows live — no scrollback, no message
    // cursor. layout stops the moment the pane is full: nothing outside the
    // viewport is ever laid out or drawn.
    let mut plan: Vec<Vec<RowPlan>> = Vec::new();
    let mut used = 0usize;
    // live: the newest line sits at the bottom. with the message cursor up, the
    // cursor's line does — only the lines from there up are ever laid out.
    app.view_h.set(body.height);
    app.chat_body.set(body);
    let cursor_on = app.mode == InputMode::Cursor;
    let end = if cursor_on {
        scroll::index_of(ch.seq, ch.messages.len(), app.cursor).map_or(0, |i| i + 1)
    } else {
        ch.messages.len()
    };
    for (n, m) in ch.messages.range(..end).rev().enumerate() {
        // a stream row names the streamer: colour + paint resolved here, every
        // frame, so a source that lands later restyles it.
        let user = if crate::streamrow::is_row(m) {
            Some(std::borrow::Cow::Owned(crate::streamrow::look(
                m,
                &app.paints,
                &app.special.follow,
                &app.seen,
            )))
        } else {
            app.paints
                .look_for(m.uid.as_deref(), &m.user, m.color.is_some())
        };
        let mut rows = layout_message(
            m,
            set,
            mode,
            body.width,
            &Look {
                me: &app.me,
                tag_platform: ch.merged(),
                stamps: app.stamps,
                user: user.as_deref(),
            },
        );
        if cursor_on && n == 0 {
            for r in rows.iter_mut() {
                for sp in r.line.spans.iter_mut() {
                    sp.style = sp.style.add_modifier(Modifier::REVERSED);
                }
            }
        }
        let h: usize = rows.iter().map(|r| r.h as usize).sum();
        if used + h > cap {
            // the newest message alone can exceed the pane — keep its tail.
            if used == 0 {
                let mut acc = 0usize;
                let keep_from = rows
                    .iter()
                    .rposition(|r| {
                        acc += r.h as usize;
                        acc > cap
                    })
                    .map(|i| i + 1)
                    .unwrap_or(0);
                rows.drain(..keep_from);
                used = rows.iter().map(|r| r.h as usize).sum();
                plan.push(rows);
            }
            break;
        }
        used += h;
        plan.push(rows);
    }
    plan.reverse();

    let mut y = body.y + (cap - used) as u16; // bottom-anchor
    for rows in plan {
        for row in rows {
            draw_planned(f, mode, body, y, row.line, &row.places, row.h);
            y += row.h;
        }
    }
}

/// draw one planned row: the text line on the bottom row of its block, then the
/// reserved emote cells (image inline, or NBSP + framebuffer queue). shared by
/// the chat body and the composer preview.
fn draw_planned(
    f: &mut Frame,
    mode: EmoteMode,
    area: Rect,
    y: u16,
    line: Line,
    places: &[Place],
    h: u16,
) {
    f.render_widget(
        Paragraph::new(line),
        Rect {
            x: area.x,
            y: y + h - 1,
            width: area.width,
            height: 1,
        },
    );
    for p in places {
        let x = area.x + p.col;
        match mode {
            EmoteMode::Term(store) => {
                if let Some(proto) = store.frame(&p.key) {
                    let rect = Rect {
                        x,
                        y,
                        width: p.w,
                        height: h,
                    };
                    if store.is_sixel(&p.key) {
                        // NBSP cells diff like text; the sixel itself is queued
                        // and written once, after the text (see emote/blit.rs).
                        fill_nbsp(f, rect);
                        store.queue_blit(rect, &p.key);
                    } else {
                        f.render_widget(Image::new(proto), rect);
                    }
                }
            }
            EmoteMode::Fb(fb) => {
                // fill the reserved cells with NBSP — renders blank like a
                // space, but DIFFERS from the space that scrolls in behind
                // a departing emote. ratatui's diff then re-emits exactly
                // those cells and the console itself erases the stale
                // pixels before we blit. plain spaces diff as "unchanged",
                // the console never repaints them, and old emote pixels
                // smear across the screen during fast scroll.
                fill_nbsp(
                    f,
                    Rect {
                        x,
                        y,
                        width: p.w,
                        height: h,
                    },
                );
                fb.push(x, y, &p.key);
            }
            EmoteMode::Text => {}
        }
    }
}

/// fill `rect` with NBSP: blank like a space, but different from the space that
/// scrolls in behind a departing emote, so ratatui's diff rewrites exactly those
/// cells and the terminal erases the stale pixels.
fn fill_nbsp(f: &mut Frame, rect: Rect) {
    let pad = "\u{a0}".repeat(rect.width as usize);
    let lines: Vec<Line> = (0..rect.height).map(|_| Line::raw(pad.clone())).collect();
    f.render_widget(Paragraph::new(lines), rect);
}

/// rover-style channel manager: a single-pane list, cursor row highlighted, the
/// active channel marked. reorder/leave/open from here.
fn draw_manage(f: &mut Frame, area: Rect, app: &App) {
    let [head, list] = Layout::vertical([Constraint::Length(1), Constraint::Min(0)]).areas(area);
    f.render_widget(
        Paragraph::new(Line::from(vec![
            Span::styled(" channels ", palette::TAG),
            Span::styled(format!("  {} open", app.channels.len()), palette::DIM),
        ])),
        head,
    );

    if app.channels.is_empty() {
        f.render_widget(
            Paragraph::new(Line::from(Span::styled(
                "  empty — press a to add a channel",
                palette::DIM,
            ))),
            list,
        );
        return;
    }

    for (i, ch) in app.channels.iter().enumerate() {
        if i as u16 >= list.height {
            break;
        }
        let sel = i == app.manage_cursor;
        let style = if sel {
            palette::TAG
        } else {
            palette::heat(Tier::of(ch.heat))
        };
        let style = if ch.temp {
            style.add_modifier(Modifier::UNDERLINED)
        } else {
            style
        };
        let cursor = if sel { "❯" } else { " " };
        let active = if i == app.focus { "●" } else { " " };
        let tags = ch
            .subs()
            .map(|(p, _)| p.tag())
            .collect::<Vec<_>>()
            .join("+");
        let label = format!(
            " {cursor} {active} {}·{}   {:>6.0}   {} msg",
            ch.name,
            tags,
            ch.heat,
            ch.messages.len(),
        );
        let row = Rect {
            x: list.x,
            y: list.y + i as u16,
            width: list.width,
            height: 1,
        };
        f.render_widget(
            Paragraph::new(Line::from(Span::styled(fit(label, list.width), style))),
            row,
        );
    }
}

fn heat_bar(heat: f64, width: usize) -> Line<'static> {
    let width = width.max(1);
    let frac = (heat / heatsync_core::heat::MYTHIC).clamp(0.0, 1.0);
    let filled = (frac * width as f64).round() as usize;
    let hue = palette::heat_fill(Tier::of(heat));
    Line::from(vec![
        Span::styled("\u{2588}".repeat(filled), hue),
        Span::styled("\u{2591}".repeat(width - filled), palette::TRACK),
    ])
}

/// one `key label` footer hint: brand-colored key, dim label. every hint bar
/// shows exactly ONE key per action — aliases stay out of the footer.
fn hint(k: &'static str, d: &'static str) -> [Span<'static>; 2] {
    [
        Span::styled(k, palette::HEAD),
        Span::styled(format!(" {d}  "), palette::DIM),
    ]
}

/// live feed, no heatsync login: sends and mod tools won't work. the demo feed
/// never needs one.
fn logged_out_live(app: &App) -> bool {
    app.hs.is_none() && matches!(app.feed, Feed::Live { .. })
}

fn draw_footer(f: &mut Frame, area: Rect, app: &App, n: usize) {
    let tag = palette::TAG;
    // Manage mode → rover-style key hints.
    if app.mode == InputMode::Manage {
        let mut spans = vec![Span::styled(" manage ", tag), Span::raw("  ")];
        for pair in [
            hint("jk", "move"),
            hint("enter", "open"),
            hint("a", "add"),
            hint("e", "edit"),
            hint("x", "leave"),
            hint("JK", "reorder"),
            hint("esc", "back"),
        ] {
            spans.extend(pair);
        }
        if let Some(msg) = &app.status {
            spans.push(Span::styled(format!(" {msg}"), palette::WARN));
        }
        f.render_widget(Paragraph::new(Line::from(spans)), area);
        return;
    }
    // SlotPick → show the cursor row's three slots, pick one with t/k/y.
    if app.mode == InputMode::SlotPick {
        let mut spans = vec![Span::styled(" edit ", tag), Span::raw("  ")];
        if let Some(ch) = app.channels.get(app.manage_cursor) {
            for (key, p) in [
                ("t", Platform::Twitch),
                ("k", Platform::Kick),
                ("y", Platform::Youtube),
            ] {
                spans.push(Span::styled(key, palette::HEAD));
                spans.push(Span::styled(
                    format!(
                        " {}:{}  ",
                        p.tag(),
                        slot_of(ch, p).unwrap_or_else(|| "—".to_string())
                    ),
                    palette::DIM,
                ));
            }
        }
        spans.extend(hint("esc", "back"));
        f.render_widget(Paragraph::new(Line::from(spans)), area);
        return;
    }
    // SlotEdit → type the slot's new name, join-prompt style.
    if let InputMode::SlotEdit(p) = app.mode {
        let spans = vec![
            Span::styled(format!(" edit {} ", p.tag()), tag),
            Span::styled(" ❯ ", palette::TEXT),
            Span::styled(app.input.clone(), palette::TEXT),
            Span::styled("\u{2588}", palette::TEXT),
            Span::styled("   enter apply · empty clears · esc back", palette::DIM),
        ];
        f.render_widget(Paragraph::new(Line::from(spans)), area);
        return;
    }
    if app.mode == InputMode::Pick {
        let mut spans = vec![Span::styled(" pick ", tag), Span::raw("  ")];
        for pair in [
            hint("type", "filter"),
            hint("↑↓", "move"),
            hint("enter", "join"),
            hint("+", "merge"),
            hint("esc", "close"),
        ] {
            spans.extend(pair);
        }
        f.render_widget(Paragraph::new(Line::from(spans)), area);
        return;
    }
    // Insert mode → the message composer for the focused channel.
    if app.mode == InputMode::Insert && !app.channels.is_empty() {
        let ch = &app.channels[app.focus];
        // read-only only when there's genuinely no send path — the same order
        // send_line tries: heatsync login, then the user's own token, then the
        // ws relay. the mock feed echoes locally, so it always composes.
        let hs = app.hs.is_some();
        let readonly = match (&app.feed, ch.kind, ch.platform) {
            (Feed::Mock(_), ..) => false,
            // mentions still takes slash commands.
            (_, TabKind::Mentions, _) => false,
            (_, TabKind::Feed | TabKind::Whispers, _) => !hs,
            (_, _, Platform::Twitch) => !hs && app.twitch_tx.is_none(),
            (_, _, Platform::Kick) => !hs && app.kick_tx.is_none() && app.out.is_none(),
            (_, _, Platform::Youtube) => app.out.is_none() || app.special.ws_cli,
        };
        // the tag names the mode as well as the target, so `esc` never leaves
        // you guessing whether a keystroke will type or command.
        let normal = app.line.mode() == edit::Mode::Normal;
        // a virtual tab has no platform, so it carries no ·tw.
        let target = if ch.kind.is_special() {
            ch.name.clone()
        } else {
            format!("{}·{}", ch.name, ch.platform.tag())
        };
        let prompt = if normal {
            format!(" {target} NORMAL ")
        } else {
            format!(" {target} ")
        };
        let tag = palette::TAG;
        let mut spans = vec![
            Span::styled(prompt, tag),
            Span::styled(" ❯ ", palette::TEXT),
        ];
        // replying: a dim chip names who, and goes with the message.
        if let Some((_, user)) = &app.reply {
            spans.push(Span::styled(format!("↪@{user} "), palette::DIM_TAG));
        }
        // the warning stands in for an empty line only — whatever is typed
        // (a /join, a /w) always stays visible.
        if readonly && app.line.text().is_empty() {
            spans.push(Span::styled(
                "read-only — no send token · esc",
                palette::WARN,
            ));
        } else {
            // draw the line with a block cursor sitting ON a character in normal
            // mode and between characters in insert — the same shape vi has, so
            // the mode is readable without looking at the tag.
            let chars: Vec<char> = app.line.text().chars().collect();
            let at = app.line.cursor();
            let body = palette::TEXT;
            let block = palette::SEL;
            let take = |r: std::ops::Range<usize>| -> String { chars[r].iter().collect() };
            spans.push(Span::styled(take(0..at.min(chars.len())), body));
            if at < chars.len() {
                spans.push(Span::styled(take(at..at + 1), block));
                spans.push(Span::styled(take(at + 1..chars.len()), body));
            } else {
                spans.push(Span::styled(" ", block));
            }
            if !app.line.pending().is_empty() {
                spans.push(Span::styled(
                    format!("  {}", app.line.pending()),
                    palette::HEAD,
                ));
            }
            // a command's reply (usage, "not open: x") has to be visible from
            // the composer — that is where the command was typed.
            if let Some(msg) = &app.status {
                spans.push(Span::styled(format!("   {msg}"), palette::WARN));
            } else if app.line.is_empty() && !normal {
                let hint = match ch.kind {
                    TabKind::Feed => "   text posts to the feed  ·  /op /re  ·  r on a row replies",
                    TabKind::Whispers => {
                        "   /w <user> <msg>  ·  /dm <user> <msg>  ·  r on a row replies"
                    }
                    TabKind::Mentions => {
                        "   commands only  ·  /join /w /dm  ·  esc, enter jumps to the source"
                    }
                    TabKind::Channel => {
                        "   tab completes emotes/@users  ·  /join /open /part /quit  ·  text goes to chat"
                    }
                };
                spans.push(Span::styled(hint, palette::DIM));
            } else if normal {
                spans.push(Span::styled("   kj history  esc leave", palette::DIM));
            }
        }
        f.render_widget(Paragraph::new(Line::from(spans)), area);
        return;
    }

    let (dot, dot_color, state) = match &app.feed {
        Feed::Mock(_) => ("\u{25cb} ", palette::DIM, "mock".to_string()),
        Feed::Live {
            connected: true, ..
        } => ("\u{25cf} ", palette::LIVE, "live".to_string()),
        Feed::Live {
            connected: false, ..
        } => ("\u{25cf} ", palette::WARN, "connecting".to_string()),
    };
    let mut spans = vec![Span::styled(" heatsync ", palette::TAG)];
    // message cursor: how much arrived while parked, and the keys that work here.
    if app.mode == InputMode::Cursor {
        if let Some(ch) = app.channels.get(app.focus) {
            let newer = scroll::newer(ch.seq, ch.messages.len(), app.cursor);
            spans.push(Span::styled(
                format!(" -- {newer} new --  "),
                palette::DIM_TAG,
            ));
        }
        for pair in [
            hint("jk", "move"),
            hint("r", "reply"),
            hint("y", "yank"),
            hint("d", "delete"),
            hint("G", "live"),
        ] {
            spans.extend(pair);
        }
        if let Some(msg) = &app.status {
            spans.push(Span::styled(format!("  {msg}"), palette::WARN));
        }
        f.render_widget(Paragraph::new(Line::from(spans)), area);
        return;
    }
    // a message (search miss, send error) takes the front of the line the way
    // vi's command line does — appended after the key hints it was simply
    // truncated away on anything narrower than ~110 columns.
    if let Some(msg) = &app.status {
        spans.push(Span::styled(format!("  {msg}"), palette::WARN));
    } else if logged_out_live(app) {
        spans.push(Span::styled(
            "  not logged in — q then `hs login`",
            palette::DIM,
        ));
    }
    // essentials only, one key per action — the full set fits a phone-width
    // terminal.
    spans.push(Span::raw("  "));
    for pair in [
        hint("jk", "chan"),
        hint("i", "say"),
        hint("o", "pick"),
        hint("m", "manage"),
    ] {
        spans.extend(pair);
    }
    if app.channels.get(app.focus).is_some_and(|c| c.temp) {
        spans.extend(hint("s", "keep"));
    }
    spans.extend(hint("q", "quit"));
    if app.paused {
        spans.push(Span::styled("PAUSED  ", palette::WARN_BOLD));
    }
    spans.push(Span::styled(dot, dot_color));
    spans.push(Span::styled(format!("{state} · {n} ch"), palette::DIM));
    f.render_widget(Paragraph::new(Line::from(spans)), area);
}

#[cfg(test)]
mod fb_reserve_tests {
    use ratatui::buffer::Buffer;
    use ratatui::layout::Rect;

    /// the framebuffer tier's stale-pixel defense: reserved emote cells hold
    /// NBSP, so when an emote departs and ordinary spaces scroll in, ratatui's
    /// diff re-emits those cells and the console erases the old pixels. if the
    /// reservation were plain spaces the diff would be empty and pixels would
    /// smear during fast scroll — this pins both halves of that contract.
    #[test]
    fn nbsp_reservation_diffs_against_space() {
        let area = Rect::new(0, 0, 4, 1);
        let reserved = Buffer::with_lines(["\u{a0}\u{a0}\u{a0} "]);
        let scrolled = Buffer::filled(area, ratatui::buffer::Cell::EMPTY);
        // emote leaves → every reserved cell re-emits (console erases pixels)
        assert_eq!(reserved.diff(&scrolled).len(), 3);
        // plain spaces would be invisible to the diff (the smear bug)
        assert!(scrolled.diff(&scrolled).is_empty());
    }
}