scrin 0.1.83

A terminal UI toolkit with panes, widgets, overlays, animations, and Aisling-powered effects/loaders.
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
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
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
use crossterm::{
    cursor,
    event::{self, Event, KeyCode, KeyModifiers},
    execute,
    terminal::{self, EnterAlternateScreen, LeaveAlternateScreen},
};
use scrin::command_palette::{Command, CommandPalette};
use scrin::core::buffer::{Buffer, Cell};
use scrin::core::color::Color;
use scrin::core::rect::Rect;
use scrin::effects::{EffectPlayer, LoaderPlayer};
use scrin::layout::{Constraint, Direction, Layout};
use scrin::overlays::toast::{Toast, ToastKind};
use scrin::overlays::{Modal, Overlay, OverlayPosition, Transition};
use scrin::panes::{Pane, PaneManager, ResizeBehavior};
use scrin::sanitize;
use scrin::status_bar::{StatusBar, StatusBarPosition};
use scrin::widgets::block::{Block, BorderStyle};
use scrin::widgets::code::CodeBlock;
use scrin::widgets::markdown_output::{MarkdownOutput, OutputTheme};
use scrin::widgets::toggle::{Toggle, ToggleStyle};
use scrin::widgets::Widget;
use std::io::{self, Write};
use std::time::{Duration, Instant};

const BG: Color = Color::rgb(4, 5, 12);
const VOID_BG: Color = Color::rgb(8, 10, 20);
const PANEL_BG: Color = Color::rgb(13, 17, 27);
const TEXT: Color = Color::rgb(219, 226, 239);
const DIM: Color = Color::rgb(113, 125, 153);
const BLUE: Color = Color::rgb(86, 193, 255);
const CYAN: Color = Color::rgb(81, 255, 236);
const PINK: Color = Color::rgb(255, 69, 175);
const PURPLE: Color = Color::rgb(169, 112, 255);
const GREEN: Color = Color::rgb(90, 255, 154);
const GOLD: Color = Color::rgb(255, 196, 87);
const RED: Color = Color::rgb(255, 98, 98);

#[derive(Clone)]
struct ToggleSpec {
    label: &'static str,
    description: &'static str,
    key: char,
    style: ToggleStyle,
    active: bool,
    color: Color,
}

struct App {
    panes: PaneManager,
    palette: CommandPalette,
    status: StatusBar,
    help: Modal,
    toasts: Vec<Toast>,
    toggles: Vec<ToggleSpec>,
    selected: usize,
    effect: EffectPlayer,
    effect_index: usize,
    loader_index: usize,
    effect_tick: usize,
    loader_tick: usize,
    progress: f32,
    progress_dir: f32,
    frame: u64,
    fps: f64,
    fps_counter: u64,
    fps_timer: Instant,
    log: Vec<String>,
}

impl App {
    fn new() -> Self {
        let mut panes = PaneManager::new().with_direction(Direction::Horizontal);
        panes.add_named_pane(
            Pane::new(0, "switchboard")
                .with_constraint(Constraint::Length(36))
                .with_min_size(30, 12)
                .with_resize_behavior(ResizeBehavior::AutoCollapse)
                .with_border_color(CYAN)
                .with_toggle_key('s'),
            "switchboard",
        );
        panes.add_named_pane(
            Pane::new(1, "reactor")
                .with_constraint(Constraint::Min(46))
                .with_min_size(34, 12)
                .with_resize_behavior(ResizeBehavior::Fixed)
                .with_border_color(PINK),
            "reactor",
        );
        panes.add_named_pane(
            Pane::new(2, "telemetry")
                .with_constraint(Constraint::Length(38))
                .with_min_size(30, 12)
                .with_resize_behavior(ResizeBehavior::AutoCollapse)
                .with_border_color(GREEN)
                .with_toggle_key('t'),
            "telemetry",
        );

        let toggles = vec![
            ToggleSpec {
                label: "Quantum Panes",
                description: "opens or seals the side panes",
                key: '1',
                style: ToggleStyle::Slider,
                active: true,
                color: CYAN,
            },
            ToggleSpec {
                label: "Aisling Reactor",
                description: "animates effect and loader engines",
                key: '2',
                style: ToggleStyle::Checkbox,
                active: true,
                color: PINK,
            },
            ToggleSpec {
                label: "Glitch Skin",
                description: "warps colors and border pulse",
                key: '3',
                style: ToggleStyle::Radio,
                active: false,
                color: PURPLE,
            },
            ToggleSpec {
                label: "Telemetry Rain",
                description: "streams live signal glyphs",
                key: '4',
                style: ToggleStyle::Text,
                active: true,
                color: GREEN,
            },
            ToggleSpec {
                label: "Auto Cycle",
                description: "rotates effect and loader choices",
                key: '5',
                style: ToggleStyle::Block,
                active: true,
                color: GOLD,
            },
        ];

        let palette = CommandPalette::new().with_commands(vec![
            command("Toggle Panes", "1", "open or close side panes", "toggle_0"),
            command("Toggle Reactor", "2", "pause or run aisling", "toggle_1"),
            command("Toggle Glitch", "3", "enable chromatic skin", "toggle_2"),
            command("Toggle Telemetry", "4", "show signal rain", "toggle_3"),
            command(
                "Toggle Auto Cycle",
                "5",
                "rotate effects/loaders",
                "toggle_4",
            ),
            command("Next Effect", "]", "advance effect kind", "next_effect"),
            command("Previous Effect", "[", "rewind effect kind", "prev_effect"),
            command("Next Loader", "l", "advance loader kind", "next_loader"),
            command("Focus Pane", "Tab", "cycle visible pane focus", "focus"),
            command("Reset Deck", "r", "restore default toggles", "reset"),
            command("Help", "?", "show shortcuts", "help"),
            command("Quit", "q", "exit demo", "quit"),
        ]);

        let help = Modal::new(
            "Exotic Toggle Deck",
            "1-5 flip toggles, j/k select, space/enter flip selected, Ctrl+K commands, Tab focus, [/] effects, l loader, r reset, q quit.",
        )
        .with_border_color(CYAN)
        .with_transition(Transition::Scale);

        let mut app = Self {
            panes,
            palette,
            status: StatusBar::new().with_position(StatusBarPosition::Bottom),
            help,
            toasts: Vec::new(),
            toggles,
            selected: 0,
            effect: Self::make_effect(0, false),
            effect_index: 0,
            loader_index: 0,
            effect_tick: 0,
            loader_tick: 0,
            progress: 0.0,
            progress_dir: 0.018,
            frame: 0,
            fps: 0.0,
            fps_counter: 0,
            fps_timer: Instant::now(),
            log: vec!["deck armed: all toggle styles online".to_string()],
        };
        app.rebuild_effect();
        app.sync_panes();
        app
    }

    fn make_effect(index: usize, glitch: bool) -> EffectPlayer {
        let kinds = EffectPlayer::all_kinds();
        let kind = kinds[index % kinds.len()];
        let text = if glitch {
            "GLITCH SKIN // TOGGLE WORMHOLE\nreactor drift: chroma unstable\nselection cursor: phase locked"
        } else {
            "TOGGLE WORMHOLE ONLINE\npanes, overlays, loaders, effects\nall controlled by live switch state"
        };
        EffectPlayer::new(kind, text)
            .with_size(78, 10)
            .with_seed(1337 + index as u64)
            .with_gradient_colors(vec![CYAN, PINK, PURPLE, GREEN, GOLD], 45.0)
    }

    fn rebuild_effect(&mut self) {
        self.effect = Self::make_effect(self.effect_index, self.toggles[2].active);
    }

    fn sync_panes(&mut self) {
        let side_panes = self.toggles[0].active;
        for name in ["switchboard", "telemetry"] {
            if let Some(pane) = self.panes.get_pane_by_name_mut(name) {
                if side_panes {
                    if !pane.is_open() {
                        pane.open();
                    }
                } else if pane.is_open() {
                    pane.close();
                }
            }
        }
    }

    fn toggle(&mut self, index: usize) {
        let mut event = None;
        if let Some(spec) = self.toggles.get_mut(index) {
            spec.active = !spec.active;
            let state = if spec.active { "enabled" } else { "disabled" };
            let kind = if spec.active {
                ToastKind::Success
            } else {
                ToastKind::Warning
            };
            event = Some((format!("{} {}", spec.label, state), kind));
        }
        if let Some((message, kind)) = event {
            self.push_log(message.clone());
            self.push_toast(message, kind);
        }
        if index == 0 {
            self.sync_panes();
        }
        if index == 2 {
            self.rebuild_effect();
        }
    }

    fn reset(&mut self) {
        for (spec, active) in self.toggles.iter_mut().zip([true, true, false, true, true]) {
            spec.active = active;
        }
        self.selected = 0;
        self.effect_index = 0;
        self.loader_index = 0;
        self.progress = 0.0;
        self.progress_dir = 0.018;
        self.rebuild_effect();
        self.sync_panes();
        self.push_log("deck reset to default state".to_string());
        self.push_toast("deck reset".to_string(), ToastKind::Info);
    }

    fn next_effect(&mut self) {
        self.effect_index = (self.effect_index + 1) % EffectPlayer::all_kinds().len();
        self.rebuild_effect();
        self.push_log(format!("effect -> {}", self.effect.name()));
    }

    fn prev_effect(&mut self) {
        let len = EffectPlayer::all_kinds().len();
        self.effect_index = if self.effect_index == 0 {
            len - 1
        } else {
            self.effect_index - 1
        };
        self.rebuild_effect();
        self.push_log(format!("effect -> {}", self.effect.name()));
    }

    fn next_loader(&mut self) {
        self.loader_index = (self.loader_index + 1) % LoaderPlayer::all_kinds().len();
        let name = LoaderPlayer::all_kinds()[self.loader_index].name();
        self.push_log(format!("loader -> {}", name));
    }

    fn update(&mut self, delta: Duration) {
        self.frame = self.frame.wrapping_add(1);
        self.fps_counter += 1;
        let elapsed = self.fps_timer.elapsed();
        if elapsed >= Duration::from_secs(1) {
            self.fps = self.fps_counter as f64 / elapsed.as_secs_f64();
            self.fps_counter = 0;
            self.fps_timer = Instant::now();
        }

        if self.toggles[1].active {
            self.effect.advance();
            self.effect_tick = self.effect_tick.wrapping_add(1);
            self.loader_tick = self.loader_tick.wrapping_add(1);
            self.progress += self.progress_dir;
            if self.progress >= 1.0 {
                self.progress = 1.0;
                self.progress_dir = -0.012;
            } else if self.progress <= 0.0 {
                self.progress = 0.0;
                self.progress_dir = 0.018;
            }
        }

        if self.toggles[4].active && self.frame % 96 == 0 {
            self.next_effect();
            self.next_loader();
        }

        for toast in &mut self.toasts {
            toast.update(delta);
        }
        self.toasts.retain(|toast| toast.is_visible());
        self.help.update(delta);
    }

    fn push_log(&mut self, line: String) {
        self.log.push(line);
        if self.log.len() > 12 {
            self.log.remove(0);
        }
    }

    fn push_toast(&mut self, message: String, kind: ToastKind) {
        self.toasts.push(
            Toast::new(&message, kind)
                .with_position(OverlayPosition::BottomRight)
                .with_lifetime(Duration::from_secs(2))
                .with_transition(Transition::SlideUp)
                .with_max_width(54),
        );
    }

    fn active_count(&self) -> usize {
        self.toggles.iter().filter(|spec| spec.active).count()
    }
}

fn main() -> io::Result<()> {
    let mut stdout = io::stdout();
    terminal::enable_raw_mode()?;
    execute!(stdout, EnterAlternateScreen, cursor::Hide)?;

    let mut app = App::new();
    let mut last_frame = Instant::now();

    let result = (|| -> io::Result<()> {
        loop {
            let now = Instant::now();
            let delta = now.saturating_duration_since(last_frame);
            last_frame = now;
            app.update(delta);

            let (cols, rows) = terminal::size()?;
            app.panes.handle_resize(cols, rows);
            app.sync_panes();

            let mut buffer = Buffer::with_background(cols as usize, rows as usize, Some(BG));
            let screen = Rect::new(0, 0, cols, rows);
            render_background(&mut buffer, screen, app.frame, app.toggles[2].active);

            let main_h = rows.saturating_sub(1);
            let main = Rect::new(0, 0, cols, main_h);
            let header_h = 4.min(main.height);
            let header = Rect::new(0, 0, cols, header_h);
            let body = Rect::new(0, header_h, cols, main.height.saturating_sub(header_h));
            render_header(&app, &mut buffer, header);
            render_panes(&app, &mut buffer, body);

            if app.help.is_visible() {
                app.help.render(&mut buffer, screen);
            }
            if app.palette.is_open() {
                app.palette.render(&mut buffer, screen);
            }
            for toast in &app.toasts {
                toast.render(&mut buffer, screen);
            }

            render_status(
                &mut app,
                &mut buffer,
                Rect::new(0, rows.saturating_sub(1), cols, 1),
            );

            write!(stdout, "\x1b[H{}", buffer.to_ansi_string())?;
            stdout.flush()?;

            if event::poll(Duration::from_millis(55))? {
                if let Event::Key(key) = event::read()? {
                    if handle_key(&mut app, key.code, key.modifiers) {
                        return Ok(());
                    }
                }
            }
        }
    })();

    execute!(stdout, cursor::Show, LeaveAlternateScreen)?;
    terminal::disable_raw_mode()?;
    result
}

fn handle_key(app: &mut App, code: KeyCode, modifiers: KeyModifiers) -> bool {
    if app.palette.is_open() {
        match code {
            KeyCode::Esc => app.palette.close(),
            KeyCode::Enter => {
                if let Some(action) = app.palette.execute_selected().map(str::to_string) {
                    app.palette.close();
                    return execute_action(app, &action);
                }
            }
            KeyCode::Up => app.palette.select_prev(),
            KeyCode::Down => app.palette.select_next(),
            KeyCode::Backspace => app.palette.backspace(),
            KeyCode::Char(c)
                if modifiers == KeyModifiers::NONE || modifiers == KeyModifiers::SHIFT =>
            {
                app.palette.input_char(c)
            }
            _ => {}
        }
        return false;
    }

    if app.help.is_visible() {
        match code {
            KeyCode::Esc | KeyCode::Char('?') | KeyCode::Enter | KeyCode::Char(' ') => {
                app.help.hide();
            }
            _ => return false,
        }
    }

    match (modifiers, code) {
        (KeyModifiers::CONTROL, KeyCode::Char('c'))
        | (KeyModifiers::CONTROL, KeyCode::Char('q'))
        | (_, KeyCode::Char('q')) => true,
        (KeyModifiers::CONTROL, KeyCode::Char('k')) => {
            app.palette.open();
            false
        }
        (_, KeyCode::Char('?')) => {
            app.help.show();
            false
        }
        (_, KeyCode::Char('j')) | (_, KeyCode::Down) => {
            app.selected = (app.selected + 1) % app.toggles.len();
            false
        }
        (_, KeyCode::Char('k')) | (_, KeyCode::Up) => {
            app.selected = if app.selected == 0 {
                app.toggles.len() - 1
            } else {
                app.selected - 1
            };
            false
        }
        (_, KeyCode::Char(' ')) | (_, KeyCode::Enter) => {
            app.toggle(app.selected);
            false
        }
        (_, KeyCode::Char('1')) => {
            app.toggle(0);
            false
        }
        (_, KeyCode::Char('2')) => {
            app.toggle(1);
            false
        }
        (_, KeyCode::Char('3')) => {
            app.toggle(2);
            false
        }
        (_, KeyCode::Char('4')) => {
            app.toggle(3);
            false
        }
        (_, KeyCode::Char('5')) => {
            app.toggle(4);
            false
        }
        (_, KeyCode::Char(']')) | (_, KeyCode::Right) => {
            app.next_effect();
            false
        }
        (_, KeyCode::Char('[')) | (_, KeyCode::Left) => {
            app.prev_effect();
            false
        }
        (_, KeyCode::Char('l')) => {
            app.next_loader();
            false
        }
        (_, KeyCode::Char('r')) => {
            app.reset();
            false
        }
        (_, KeyCode::Tab) => {
            app.panes.focus_next();
            false
        }
        (_, KeyCode::BackTab) => {
            app.panes.focus_prev();
            false
        }
        _ => false,
    }
}

fn execute_action(app: &mut App, action: &str) -> bool {
    match action {
        "toggle_0" => app.toggle(0),
        "toggle_1" => app.toggle(1),
        "toggle_2" => app.toggle(2),
        "toggle_3" => app.toggle(3),
        "toggle_4" => app.toggle(4),
        "next_effect" => app.next_effect(),
        "prev_effect" => app.prev_effect(),
        "next_loader" => app.next_loader(),
        "focus" => app.panes.focus_next(),
        "reset" => app.reset(),
        "help" => app.help.show(),
        "quit" => return true,
        _ => {}
    }
    false
}

fn render_background(buffer: &mut Buffer, area: Rect, frame: u64, glitch: bool) {
    buffer.fill(area, ' ', TEXT, Some(BG));
    for y in area.y..area.bottom() {
        for x in area.x..area.right() {
            let seed = (x as u64 * 17 + y as u64 * 41 + frame / 2) % 97;
            if seed == 0 || (glitch && seed == 7) {
                let ch = match (x as u64 + y as u64 + frame) % 5 {
                    0 => '·',
                    1 => '˙',
                    2 => '`',
                    3 => '˚',
                    _ => '.',
                };
                let color = if glitch && seed == 7 {
                    PINK
                } else {
                    DIM.dim(0.45)
                };
                buffer.set(x as usize, y as usize, Cell::new(ch, color, Some(BG)));
            }
        }
    }
}

fn render_header(app: &App, buffer: &mut Buffer, area: Rect) {
    if area.height == 0 {
        return;
    }
    let title = " EXOTIC TOGGLE DECK // five switch styles driving panes + aisling ";
    write_bold(
        buffer,
        area.x as usize + 2,
        area.y as usize,
        title,
        CYAN,
        Some(BG),
        area.width.saturating_sub(4),
    );
    if area.height > 1 {
        let line = format!(
            " keys: 1-5 flip  j/k select  Ctrl+K palette  Tab focus  effect:{}  loader:{} ",
            app.effect.name(),
            LoaderPlayer::all_kinds()[app.loader_index].name()
        );
        write_line(
            buffer,
            area.x as usize + 2,
            area.y as usize + 1,
            &line,
            DIM,
            Some(BG),
            area.width.saturating_sub(4),
        );
    }
    if area.height > 2 {
        let width = area.width.saturating_sub(4) as usize;
        let filled = ((app.progress * width as f32) as usize).min(width.saturating_sub(1));
        for x in 0..width {
            let ch = if x <= filled { '' } else { '' };
            let color = if x <= filled {
                palette_color(x + app.frame as usize)
            } else {
                Color::rgb(26, 33, 52)
            };
            buffer.set(
                area.x as usize + 2 + x,
                area.y as usize + 2,
                Cell::new(ch, color, Some(BG)),
            );
        }
    }
    if area.height > 3 {
        let chips = app
            .toggles
            .iter()
            .map(|spec| format!("{}:{}", spec.key, if spec.active { "on" } else { "off" }))
            .collect::<Vec<_>>()
            .join("  ");
        write_line(
            buffer,
            area.x as usize + 2,
            area.y as usize + 3,
            &chips,
            GOLD,
            Some(BG),
            area.width.saturating_sub(4),
        );
    }
}

fn render_panes(app: &App, buffer: &mut Buffer, area: Rect) {
    let ids = app.panes.visible_pane_ids();
    app.panes
        .render_with_content(buffer, area, |idx, inner, buf| {
            let Some(id) = ids.get(idx) else {
                return;
            };
            match id.0 {
                0 => render_switchboard(app, buf, inner),
                1 => render_reactor(app, buf, inner),
                2 => render_telemetry(app, buf, inner),
                _ => {}
            }
        });
}

fn render_switchboard(app: &App, buffer: &mut Buffer, area: Rect) {
    buffer.fill(area, ' ', TEXT, Some(PANEL_BG));
    write_bold(
        buffer,
        area.x as usize,
        area.y as usize,
        "switch matrix",
        CYAN,
        Some(PANEL_BG),
        area.width,
    );
    if area.height > 1 {
        write_line(
            buffer,
            area.x as usize,
            area.y as usize + 1,
            "all five ToggleStyle variants",
            DIM,
            Some(PANEL_BG),
            area.width,
        );
    }

    let mut y = area.y.saturating_add(3);
    for (i, spec) in app.toggles.iter().enumerate() {
        if y >= area.bottom() {
            break;
        }
        let selected = i == app.selected;
        let key = format!("{}{}", if selected { "" } else { " " }, spec.key);
        write_line(
            buffer,
            area.x as usize,
            y as usize,
            &key,
            if selected { Color::WHITE } else { spec.color },
            Some(PANEL_BG),
            3,
        );
        let toggle_area = Rect::new(area.x.saturating_add(3), y, area.width.saturating_sub(3), 1);
        Toggle::new(spec.label, spec.active)
            .with_style(spec.style)
            .with_active_color(spec.color)
            .with_inactive_color(DIM.dim(0.35))
            .with_label_color(if selected { Color::WHITE } else { TEXT })
            .render(buffer, toggle_area);
        if y + 1 < area.bottom() {
            write_line(
                buffer,
                area.x as usize + 3,
                y as usize + 1,
                spec.description,
                DIM,
                Some(PANEL_BG),
                area.width.saturating_sub(3),
            );
        }
        y = y.saturating_add(3);
    }

    if y < area.bottom() {
        write_line(
            buffer,
            area.x as usize,
            y as usize,
            "space/enter flips selected",
            GOLD,
            Some(PANEL_BG),
            area.width,
        );
    }
}

fn render_reactor(app: &App, buffer: &mut Buffer, area: Rect) {
    buffer.fill(area, ' ', TEXT, Some(VOID_BG));
    if area.height == 0 || area.width == 0 {
        return;
    }

    let top_h = (area.height / 2).max(7).min(area.height);
    let layout = Layout::vertical(vec![
        Constraint::Length(top_h),
        Constraint::Length(6),
        Constraint::Min(0),
    ]);
    let rects = layout.split(area);

    let effect_title = format!("aisling effect // {}", app.effect.name());
    let effect_title =
        sanitize::truncate_str(&effect_title, rects[0].width.saturating_sub(4) as usize);
    let effect_block = Block::new(&effect_title)
        .with_borders(BorderStyle::Double)
        .with_border_color(if app.toggles[2].active { PINK } else { BLUE })
        .with_bg(VOID_BG)
        .with_inner_margin(Rect::ZERO);
    effect_block.render(buffer, rects[0]);
    let effect_inner = effect_block.inner(rects[0]);
    if app.toggles[1].active {
        app.effect.render_to_buffer(buffer, effect_inner);
    } else {
        write_bold(
            buffer,
            effect_inner.x as usize,
            effect_inner.y as usize,
            "reactor paused",
            RED,
            Some(VOID_BG),
            effect_inner.width,
        );
        if effect_inner.height > 1 {
            write_line(
                buffer,
                effect_inner.x as usize,
                effect_inner.y as usize + 1,
                "toggle 2 resumes the aisling engine",
                DIM,
                Some(VOID_BG),
                effect_inner.width,
            );
        }
    }

    if rects.len() > 1 && rects[1].height > 0 {
        let kind = LoaderPlayer::all_kinds()[app.loader_index];
        let title = format!("loader runway // {}", kind.name());
        let title = sanitize::truncate_str(&title, rects[1].width.saturating_sub(4) as usize);
        let loader_block = Block::new(&title)
            .with_borders(BorderStyle::Rounded)
            .with_border_color(GREEN)
            .with_bg(PANEL_BG)
            .with_inner_margin(Rect::ZERO);
        loader_block.render(buffer, rects[1]);
        let inner = loader_block.inner(rects[1]);
        let loader = LoaderPlayer::new(kind)
            .with_size(inner.width as usize, inner.height as usize)
            .with_label("toggle-driven progress".to_string())
            .with_gradient_colors(vec![CYAN, PINK, GREEN, GOLD], 45.0);
        loader.render(
            app.loader_tick,
            LoaderPlayer::progress_from_fraction(app.progress),
            buffer,
            inner,
        );
    }

    if rects.len() > 2 && rects[2].height > 0 {
        let code = format!(
            "ToggleDeck {{\n  panes: {},\n  reactor: {},\n  glitch_skin: {},\n  telemetry_rain: {},\n  auto_cycle: {},\n  effect: \"{}\",\n  loader: \"{}\",\n}}",
            state(app.toggles[0].active),
            state(app.toggles[1].active),
            state(app.toggles[2].active),
            state(app.toggles[3].active),
            state(app.toggles[4].active),
            app.effect.name(),
            LoaderPlayer::all_kinds()[app.loader_index].name(),
        );
        CodeBlock::new(&code)
            .with_language("rust")
            .with_current_line((app.selected + 2).min(7))
            .render(buffer, rects[2]);
    }
}

fn render_telemetry(app: &App, buffer: &mut Buffer, area: Rect) {
    buffer.fill(area, ' ', TEXT, Some(PANEL_BG));
    let split = Layout::vertical(vec![Constraint::Length(9), Constraint::Min(0)]).split(area);
    let markdown = format!(
        "# toggle telemetry\n- active switches: **{} / {}**\n- focused pane: `{}`\n- selected switch: `{}`\n> use Ctrl+K for command palette actions\n\n```text\nprogress {:>3}%  fps {:>4.0}\n```",
        app.active_count(),
        app.toggles.len(),
        app.panes
            .active_focus
            .map(|id| id.0.to_string())
            .unwrap_or_else(|| "none".to_string()),
        app.toggles[app.selected].label,
        (app.progress * 100.0) as usize,
        app.fps,
    );
    MarkdownOutput::new(&markdown)
        .with_theme(OutputTheme::SCRIN)
        .with_code_line_numbers(false)
        .render(buffer, split[0]);

    if split.len() > 1 && split[1].height > 0 {
        let block = Block::new("signal log")
            .with_borders(BorderStyle::Rounded)
            .with_border_color(GOLD)
            .with_bg(PANEL_BG);
        block.render(buffer, split[1]);
        let inner = block.inner(split[1]);
        if app.toggles[3].active {
            render_signal_rain(buffer, inner, app.frame);
        }
        let start = app.log.len().saturating_sub(inner.height as usize);
        for (row, line) in app.log.iter().skip(start).enumerate() {
            if row as u16 >= inner.height {
                break;
            }
            write_line(
                buffer,
                inner.x as usize,
                inner.y as usize + row,
                line,
                if row + start + 1 == app.log.len() {
                    GREEN
                } else {
                    DIM
                },
                Some(PANEL_BG),
                inner.width,
            );
        }
    }
}

fn render_signal_rain(buffer: &mut Buffer, area: Rect, frame: u64) {
    for y in area.y..area.bottom() {
        for x in area.x..area.right() {
            let seed = (x as u64 * 11 + y as u64 * 19 + frame) % 37;
            if seed == 0 {
                let ch = match (x as u64 + frame / 3) % 6 {
                    0 => '0',
                    1 => '1',
                    2 => '',
                    3 => '',
                    4 => '',
                    _ => '·',
                };
                buffer.set(
                    x as usize,
                    y as usize,
                    Cell::new(ch, GREEN.dim(0.2), Some(PANEL_BG)),
                );
            }
        }
    }
}

fn render_status(app: &mut App, buffer: &mut Buffer, area: Rect) {
    app.status.clear();
    app.status.set_left(
        &format!(
            "selected {} ({})",
            app.toggles[app.selected].label, app.toggles[app.selected].key
        ),
        app.toggles[app.selected].color,
    );
    app.status.set_center(
        &format!(
            "effects:{} loaders:{}",
            EffectPlayer::all_kinds().len(),
            LoaderPlayer::all_kinds().len()
        ),
        DIM,
    );
    app.status
        .set_right("Ctrl+K commands  ? help  q quit", GOLD);
    app.status.render(buffer, area);
}

fn command(name: &str, shortcut: &str, description: &str, action_id: &str) -> Command {
    Command {
        name: name.to_string(),
        shortcut: Some(shortcut.to_string()),
        description: description.to_string(),
        action_id: action_id.to_string(),
    }
}

fn state(active: bool) -> &'static str {
    if active {
        "online"
    } else {
        "offline"
    }
}

fn palette_color(i: usize) -> Color {
    match i % 6 {
        0 => CYAN,
        1 => PINK,
        2 => PURPLE,
        3 => GREEN,
        4 => GOLD,
        _ => BLUE,
    }
}

fn write_line(
    buffer: &mut Buffer,
    x: usize,
    y: usize,
    text: &str,
    fg: Color,
    bg: Option<Color>,
    width: u16,
) {
    if width == 0 {
        return;
    }
    let display = sanitize::truncate_str(text, width as usize);
    buffer.set_str(x, y, &display, fg, bg);
}

fn write_bold(
    buffer: &mut Buffer,
    x: usize,
    y: usize,
    text: &str,
    fg: Color,
    bg: Option<Color>,
    width: u16,
) {
    if width == 0 {
        return;
    }
    let display = sanitize::truncate_str(text, width as usize);
    buffer.set_str_bold(x, y, &display, fg, bg);
}