blaeck 0.4.0

A component-based terminal UI framework for Rust
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
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
//! Example Viewer - Interactive tool for reviewing and cleaning up blaeck examples
//!
//! Run with: cargo run --example example_viewer
//!
//! Three-panel layout: file list | source code | live output
//!
//! Controls:
//!   ↑/k, ↓/j     - Navigate example list
//!   PageUp/K      - Scroll code up
//!   PageDown/J    - Scroll code down
//!   [, ]          - Resize preview panel (shrink/grow)
//!   Enter         - Enter live interactive preview (focus mode)
//!   Esc           - Exit focus mode / Quit
//!   r             - Run selected example (output appears in right panel)
//!   s             - Stop running example
//!   b             - Build selected example
//!   d             - Toggle mark for deletion
//!   x             - Execute deletions
//!   q/Esc         - Quit

#[path = "previews/mod.rs"]
mod previews;

use std::boxed::Box as StdBox;

use blaeck::input::Key;
use blaeck::prelude::*;
use crossterm::event::{poll, read, Event, KeyCode, KeyModifiers};
use previews::live::{self, LivePreview};
use std::io::{BufRead, BufReader};
use std::path::PathBuf;
use std::process::{Command, Stdio};
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Arc, Mutex};
use std::time::{Duration, Instant};

const ASYNC_EXAMPLES: &[&str] = &["cube3d", "cube3d_braille"];
const LIST_WIDTH: f32 = 26.0;
const RUN_TIMEOUT_SECS: u64 = 10;
const MIN_PREVIEW_RATIO: f32 = 0.15;
const MAX_PREVIEW_RATIO: f32 = 0.70;
const DEFAULT_PREVIEW_RATIO: f32 = 0.35;

struct ExampleInfo {
    name: String,
    path: PathBuf,
    requires_async: bool,
    marked: bool,
}

enum Action {
    None,
    Quit,
}

// Background process capture shared between threads
struct Capture {
    output: Mutex<String>,
    done: AtomicBool,
    pid: Mutex<Option<u32>>,
}

impl Capture {
    fn new() -> Arc<Self> {
        Arc::new(Self {
            output: Mutex::new(String::new()),
            done: AtomicBool::new(true),
            pid: Mutex::new(None),
        })
    }

    fn append(&self, text: &str) {
        self.output.lock().unwrap().push_str(text);
    }

    fn get_output(&self) -> String {
        self.output.lock().unwrap().clone()
    }

    fn is_done(&self) -> bool {
        self.done.load(Ordering::SeqCst)
    }

    fn is_running(&self) -> bool {
        !self.is_done()
    }

    fn reset(&self) {
        *self.output.lock().unwrap() = String::new();
        self.done.store(false, Ordering::SeqCst);
        *self.pid.lock().unwrap() = None;
    }

    fn kill(&self) {
        if let Some(pid) = *self.pid.lock().unwrap() {
            unsafe {
                libc::kill(pid as i32, libc::SIGTERM);
            }
        }
    }
}

/// Strip ANSI escape sequences from text
fn strip_ansi(text: &str) -> String {
    let mut result = String::with_capacity(text.len());
    let mut chars = text.chars().peekable();
    while let Some(ch) = chars.next() {
        if ch == '\x1b' {
            // Skip ESC[...X sequences
            if chars.peek() == Some(&'[') {
                chars.next(); // consume '['
                              // Consume until we hit a letter (the final byte)
                while let Some(&c) = chars.peek() {
                    chars.next();
                    if c.is_ascii_alphabetic() || c == '~' || c == 'H' || c == 'J' {
                        break;
                    }
                }
            } else if chars.peek() == Some(&']') {
                // OSC sequence: ESC]...ST or ESC]...BEL
                chars.next();
                while let Some(&c) = chars.peek() {
                    chars.next();
                    if c == '\x07' {
                        break;
                    }
                    if c == '\x1b' {
                        chars.next(); // consume '\'
                        break;
                    }
                }
            }
        } else if ch == '\r' {
            // Skip carriage returns
        } else {
            result.push(ch);
        }
    }
    result
}

fn spawn_example(capture: &Arc<Capture>, name: &str, is_async: bool) {
    capture.reset();
    capture.append(&format!("Building {}...\n", name));

    let cap = capture.clone();
    let name = name.to_string();

    std::thread::spawn(move || {
        // Build first
        let mut build_cmd = Command::new("cargo");
        build_cmd
            .arg("build")
            .arg("--example")
            .arg(&name)
            .stdout(Stdio::piped())
            .stderr(Stdio::piped());
        if is_async {
            build_cmd.arg("--features").arg("async");
        }

        match build_cmd.output() {
            Ok(output) if !output.status.success() => {
                let stderr = String::from_utf8_lossy(&output.stderr);
                cap.append(&format!("Build failed:\n{}", strip_ansi(&stderr)));
                cap.done.store(true, Ordering::SeqCst);
                return;
            }
            Err(e) => {
                cap.append(&format!("Build error: {}\n", e));
                cap.done.store(true, Ordering::SeqCst);
                return;
            }
            _ => {}
        }

        cap.append("Running...\n\n");

        // Run the binary directly (skip cargo overhead)
        let binary = format!("./target/debug/examples/{}", name);
        let mut child = match Command::new(&binary)
            .stdout(Stdio::piped())
            .stderr(Stdio::piped())
            .spawn()
        {
            Ok(c) => c,
            Err(e) => {
                cap.append(&format!("Run error: {}\n", e));
                cap.done.store(true, Ordering::SeqCst);
                return;
            }
        };

        *cap.pid.lock().unwrap() = Some(child.id());

        // Read stdout in a separate thread
        let stdout = child.stdout.take().unwrap();
        let stderr = child.stderr.take().unwrap();

        let cap_out = cap.clone();
        let out_thread = std::thread::spawn(move || {
            let reader = BufReader::new(stdout);
            for line in reader.lines().map_while(Result::ok) {
                let clean = strip_ansi(&line);
                if !clean.trim().is_empty() {
                    cap_out.append(&clean);
                    cap_out.append("\n");
                }
            }
        });

        let cap_err = cap.clone();
        let err_thread = std::thread::spawn(move || {
            let reader = BufReader::new(stderr);
            for line in reader.lines().map_while(Result::ok) {
                let clean = strip_ansi(&line);
                if !clean.trim().is_empty() {
                    cap_err.append(&clean);
                    cap_err.append("\n");
                }
            }
        });

        // Wait with timeout
        let start = std::time::Instant::now();
        loop {
            match child.try_wait() {
                Ok(Some(status)) => {
                    cap.append(&format!(
                        "\n--- Exited ({}) ---\n",
                        if status.success() { "OK" } else { "error" }
                    ));
                    break;
                }
                Ok(None) => {
                    if start.elapsed() > Duration::from_secs(RUN_TIMEOUT_SECS) {
                        let _ = child.kill();
                        cap.append(&format!("\n--- Killed after {}s ---\n", RUN_TIMEOUT_SECS));
                        break;
                    }
                    std::thread::sleep(Duration::from_millis(100));
                }
                Err(e) => {
                    cap.append(&format!("\nWait error: {}\n", e));
                    break;
                }
            }
        }

        let _ = out_thread.join();
        let _ = err_thread.join();
        cap.done.store(true, Ordering::SeqCst);
    });
}

struct ViewerState {
    examples: Vec<ExampleInfo>,
    selected: usize,
    code_scroll: usize,
    current_code: String,
    status_msg: String,
    term_width: u16,
    term_height: u16,
    capture: Arc<Capture>,
    output_scroll: usize,
    show_preview: bool,
    animation_timer: AnimationTimer,
    // Focus mode: live interactive preview
    focused_preview: Option<StdBox<dyn LivePreview>>,
    focus_mode: bool,
    last_frame_time: Instant,
    preview_ratio: f32,
}

impl ViewerState {
    fn new() -> Self {
        let (w, h) = crossterm::terminal::size().unwrap_or((120, 30));
        let mut state = Self {
            examples: Vec::new(),
            selected: 0,
            code_scroll: 0,
            current_code: String::new(),
            status_msg: String::new(),
            term_width: w,
            term_height: h,
            capture: Capture::new(),
            output_scroll: 0,
            show_preview: true,
            animation_timer: AnimationTimer::new(),
            focused_preview: None,
            focus_mode: false,
            last_frame_time: Instant::now(),
            preview_ratio: DEFAULT_PREVIEW_RATIO,
        };
        state.discover_examples();
        state.load_code();
        state
    }

    fn enter_focus_mode(&mut self) {
        if let Some(ex) = self.examples.get(self.selected) {
            if let Some(preview) = live::create_live_preview(&ex.name) {
                self.focused_preview = Some(preview);
                self.focus_mode = true;
                self.status_msg = format!("LIVE: {}", ex.name);
            } else {
                self.status_msg = format!("No live preview for {}", ex.name);
            }
        }
    }

    fn exit_focus_mode(&mut self) {
        self.focused_preview = None;
        self.focus_mode = false;
        self.status_msg.clear();
    }

    fn discover_examples(&mut self) {
        let examples_dir = PathBuf::from("blaeck/examples");
        if let Ok(entries) = std::fs::read_dir(&examples_dir) {
            let mut examples: Vec<ExampleInfo> = entries
                .filter_map(|e| e.ok())
                .filter(|e| {
                    let name = e.file_name().to_string_lossy().to_string();
                    name.ends_with(".rs") && name != "example_viewer.rs"
                })
                .map(|e| {
                    let filename = e.file_name().to_string_lossy().to_string();
                    let name = filename.trim_end_matches(".rs").to_string();
                    let requires_async = ASYNC_EXAMPLES.contains(&name.as_str());
                    ExampleInfo {
                        name,
                        path: e.path(),
                        requires_async,
                        marked: false,
                    }
                })
                .collect();
            examples.sort_by(|a, b| a.name.cmp(&b.name));
            self.examples = examples;
        }
    }

    fn load_code(&mut self) {
        if let Some(ex) = self.examples.get(self.selected) {
            // If there's a preview module with the real UI code, show that
            let preview_path = ex
                .path
                .parent()
                .unwrap()
                .join("previews")
                .join(format!("{}.rs", ex.name));
            if preview_path.exists() {
                let main_code = std::fs::read_to_string(&ex.path).unwrap_or_default();
                let preview_code = std::fs::read_to_string(&preview_path).unwrap_or_default();
                self.current_code = format!(
                    "// === {} (main) ===\n{}\n\n// === previews/{}.rs (UI code) ===\n{}",
                    ex.name,
                    main_code.trim(),
                    ex.name,
                    preview_code.trim()
                );
            } else {
                self.current_code = std::fs::read_to_string(&ex.path)
                    .unwrap_or_else(|_| "Error reading file".into());
            }
        }
        self.code_scroll = 0;
    }

    fn visible_code(&self) -> (String, usize) {
        let max_lines = (self.term_height as usize).saturating_sub(4);
        let lines: Vec<&str> = self.current_code.lines().collect();
        let start = self.code_scroll.min(lines.len().saturating_sub(1));
        let end = (start + max_lines).min(lines.len());

        // Calculate available width for code panel (accounting for borders, padding, line numbers)
        let remaining = self.term_width as f32 - LIST_WIDTH;
        let code_width = remaining - (remaining * self.preview_ratio).round();
        // Account for: border (2), padding (1), line number column (~6), separator (~2)
        let max_chars = (code_width as usize).saturating_sub(11);

        // Truncate each line to fit within the panel (character-aware)
        let truncated_lines: Vec<String> = lines[start..end]
            .iter()
            .map(|line| {
                let char_count = line.chars().count();
                if char_count > max_chars {
                    let truncated: String =
                        line.chars().take(max_chars.saturating_sub(1)).collect();
                    format!("{}", truncated)
                } else {
                    line.to_string()
                }
            })
            .collect();

        let visible = truncated_lines.join("\n");
        (visible, start + 1)
    }

    fn code_line_count(&self) -> usize {
        self.current_code.lines().count()
    }

    fn max_scroll(&self) -> usize {
        let max_lines = (self.term_height as usize).saturating_sub(4);
        self.code_line_count().saturating_sub(max_lines)
    }

    fn visible_output(&self) -> String {
        let output = self.capture.get_output();
        let max_lines = (self.term_height as usize).saturating_sub(4);
        let lines: Vec<&str> = output.lines().collect();

        if lines.len() <= max_lines {
            output
        } else {
            // Auto-scroll to bottom, or use manual scroll
            let start = if self.output_scroll == 0 {
                // Auto: show last N lines
                lines.len().saturating_sub(max_lines)
            } else {
                self.output_scroll
                    .min(lines.len().saturating_sub(max_lines))
            };
            let end = (start + max_lines).min(lines.len());
            lines[start..end].join("\n")
        }
    }

    fn handle_input(&mut self, key: crossterm::event::KeyEvent) -> Action {
        // Resize keys work in both normal and focus mode
        match key.code {
            KeyCode::Char(']') => {
                self.preview_ratio = (self.preview_ratio + 0.05).min(MAX_PREVIEW_RATIO);
                return Action::None;
            }
            KeyCode::Char('[') => {
                self.preview_ratio = (self.preview_ratio - 0.05).max(MIN_PREVIEW_RATIO);
                return Action::None;
            }
            _ => {}
        }

        // Focus mode: forward keys to the live preview
        if self.focus_mode {
            return self.handle_focus_input(key);
        }

        match key.code {
            KeyCode::Char('q') | KeyCode::Esc => {
                if self.capture.is_running() {
                    self.capture.kill();
                }
                return Action::Quit;
            }
            KeyCode::Char('c') if key.modifiers.contains(KeyModifiers::CONTROL) => {
                if self.capture.is_running() {
                    self.capture.kill();
                }
                return Action::Quit;
            }

            // Enter focus mode with live preview
            KeyCode::Enter => {
                self.enter_focus_mode();
            }

            // Navigate list
            KeyCode::Up | KeyCode::Char('k') => {
                if self.selected > 0 {
                    self.selected -= 1;
                    self.load_code();
                }
            }
            KeyCode::Down | KeyCode::Char('j') => {
                if self.selected + 1 < self.examples.len() {
                    self.selected += 1;
                    self.load_code();
                }
            }

            // Scroll code
            KeyCode::PageUp | KeyCode::Char('K') => {
                self.code_scroll = self.code_scroll.saturating_sub(10);
            }
            KeyCode::PageDown | KeyCode::Char('J') => {
                self.code_scroll = (self.code_scroll + 10).min(self.max_scroll());
            }

            // Run example in background
            KeyCode::Char('r') => {
                if let Some(ex) = self.examples.get(self.selected) {
                    if self.capture.is_running() {
                        self.capture.kill();
                        // Small delay for cleanup
                        std::thread::sleep(Duration::from_millis(200));
                    }
                    self.output_scroll = 0;
                    spawn_example(&self.capture, &ex.name, ex.requires_async);
                    self.status_msg = format!("Running {}...", ex.name);
                }
            }

            // Toggle preview
            KeyCode::Char('p') => {
                self.show_preview = !self.show_preview;
                self.status_msg = if self.show_preview {
                    "Preview mode".into()
                } else {
                    "Output mode".into()
                };
            }

            // Stop running example
            KeyCode::Char('s') => {
                if self.capture.is_running() {
                    self.capture.kill();
                    self.status_msg = "Stopped".into();
                }
            }

            // Build
            KeyCode::Char('b') => {
                if let Some(ex) = self.examples.get(self.selected) {
                    let name = ex.name.clone();
                    let is_async = ex.requires_async;
                    self.status_msg = format!("Building {}...", name);
                    let mut cmd = Command::new("cargo");
                    cmd.arg("build").arg("--example").arg(&name);
                    if is_async {
                        cmd.arg("--features").arg("async");
                    }
                    match cmd.output() {
                        Ok(output) if output.status.success() => {
                            self.status_msg = format!("{} built OK", name);
                        }
                        Ok(output) => {
                            let stderr = String::from_utf8_lossy(&output.stderr);
                            let last = stderr.lines().last().unwrap_or("unknown error");
                            self.status_msg = format!("{}: {}", name, last);
                        }
                        Err(e) => {
                            self.status_msg = format!("{}: {}", name, e);
                        }
                    }
                }
            }

            // Mark for deletion
            KeyCode::Char('d') => {
                if let Some(ex) = self.examples.get_mut(self.selected) {
                    ex.marked = !ex.marked;
                    let label = if ex.marked { "marked" } else { "unmarked" };
                    self.status_msg = format!("{} {} for deletion", label, ex.name);
                }
            }

            // Execute deletions
            KeyCode::Char('x') => {
                let count = self.examples.iter().filter(|e| e.marked).count();
                if count == 0 {
                    self.status_msg = "No files marked for deletion".into();
                } else {
                    self.status_msg = format!("Delete {} files? Press 'y' to confirm", count);
                }
            }
            KeyCode::Char('y') => {
                if self.status_msg.contains("Press 'y' to confirm") {
                    let mut deleted = 0;
                    let to_delete: Vec<PathBuf> = self
                        .examples
                        .iter()
                        .filter(|e| e.marked)
                        .map(|e| e.path.clone())
                        .collect();
                    for path in &to_delete {
                        if std::fs::remove_file(path).is_ok() {
                            deleted += 1;
                        }
                    }
                    self.examples.retain(|e| !e.marked);
                    if self.selected >= self.examples.len() && !self.examples.is_empty() {
                        self.selected = self.examples.len() - 1;
                    }
                    self.load_code();
                    self.status_msg = format!("Deleted {} files", deleted);
                }
            }

            _ => {}
        }
        Action::None
    }

    fn handle_focus_input(&mut self, key: crossterm::event::KeyEvent) -> Action {
        // Ctrl+C always quits
        if key.code == KeyCode::Char('c') && key.modifiers.contains(KeyModifiers::CONTROL) {
            return Action::Quit;
        }

        // Esc exits focus mode
        if key.code == KeyCode::Esc {
            self.exit_focus_mode();
            return Action::None;
        }

        // Forward all other keys to the live preview
        if let Some(ref mut preview) = self.focused_preview {
            let blaeck_key = Key::from(key);
            let should_exit = preview.handle_key(&blaeck_key);
            if should_exit {
                self.exit_focus_mode();
            }
        }

        Action::None
    }
}

fn render_list(state: &ViewerState) -> Element {
    let max_visible = (state.term_height as usize).saturating_sub(5);
    let list_scroll = if state.selected >= max_visible {
        state.selected - max_visible + 1
    } else {
        0
    };

    let mut items: Vec<Element> = Vec::new();

    items.push(element! {
        Text(
            content: format!(" Examples ({}) ", state.examples.len()),
            bold: true,
            color: Color::Cyan
        )
    });

    for (i, ex) in state.examples.iter().enumerate().skip(list_scroll) {
        if items.len() >= max_visible {
            break;
        }

        let is_selected = i == state.selected;
        let prefix = if ex.marked {
            ""
        } else if is_selected {
            ""
        } else {
            "  "
        };

        let suffix = if ex.requires_async { "" } else { "" };

        let color = if ex.marked {
            Color::Red
        } else if is_selected {
            Color::Yellow
        } else {
            Color::White
        };

        let name = format!("{}{}{}", prefix, ex.name, suffix);
        let max_name_len = (LIST_WIDTH as usize).saturating_sub(3);
        let display = if name.len() > max_name_len {
            format!("{}", &name[..max_name_len - 1])
        } else {
            name
        };

        items.push(element! {
            Text(content: display, color: color, bold: is_selected)
        });
    }

    Element::column(items)
}

fn render_code(state: &ViewerState) -> Element {
    let (visible_code, start_line) = state.visible_code();

    if visible_code.is_empty() {
        return element! {
            Text(content: "  (empty file)", dim: true)
        };
    }

    Element::node::<SyntaxHighlight>(
        SyntaxHighlightProps::new(&visible_code)
            .language("rust")
            .theme(SyntaxTheme::OceanDark)
            .line_numbers(LineNumberStyle::WithSeparator)
            .start_line(start_line),
        vec![],
    )
}

fn try_render_preview(name: &str, timer: &AnimationTimer) -> Option<Element> {
    match name {
        "animation" => Some(previews::animation::build_ui_with_timer(timer)),
        "banner" => Some(previews::banner::build_ui()),
        "barchart" => Some(previews::barchart::build_ui()),
        "borders" => Some(previews::borders::build_ui()),
        "breadcrumbs" => Some(previews::breadcrumbs::build_ui()),
        "diff" => Some(previews::diff::build_ui()),
        "gradient" => Some(previews::gradient::build_ui()),
        "hello" => Some(previews::hello::build_ui()),
        "keyhints" => Some(previews::keyhints::build_ui()),
        "markdown" => Some(previews::markdown::build_ui()),
        "modal" => Some(previews::modal::build_ui()),
        "statusbar" => Some(previews::statusbar::build_ui()),
        "syntax" => Some(previews::syntax::build_ui()),
        "table" => Some(previews::table::build_ui()),
        "tree" => Some(previews::tree::build_ui()),
        "demo_inline" => Some(previews::demo_inline::build_ui_with_timer(timer)),
        "logbox" => Some(previews::logbox::build_ui_with_timer(timer)),
        "logbox_command" => Some(previews::logbox_command::build_ui_with_timer(timer)),
        "task_runner" => Some(previews::task_runner::build_ui_with_timer(timer)),
        "reactive_counter" => Some(previews::reactive_counter::build_ui()),
        "reactive_list" => Some(previews::reactive_list::build_ui()),
        "quickstart_interactive" => Some(previews::quickstart_interactive::build_ui()),
        "spinner_demo" => Some(previews::spinner_demo::build_ui_with_timer(timer)),
        "timer" => Some(previews::timer::build_ui_with_timer(timer)),
        "preview" => Some(previews::preview::build_ui_with_timer(timer)),
        "reactive_timeline" => Some(previews::reactive_timeline::build_ui()),
        "stagger_demo" => Some(previews::stagger_demo::build_ui()),
        "timeline_debug" => Some(previews::timeline_debug::build_ui()),
        "timeline_demo" => Some(previews::timeline_demo::build_ui_with_timer(timer)),
        "focus_demo" => Some(previews::focus_demo::build_ui()),
        "form_demo" => Some(previews::form_demo::build_ui()),
        "select_demo" => Some(previews::select_demo::build_ui()),
        "interactive" => Some(previews::interactive::build_ui()),
        "menu" => Some(previews::menu::build_ui()),
        "multiselect" => Some(previews::multiselect::build_ui()),
        "autocomplete" => Some(previews::autocomplete::build_ui()),
        "tabs" => Some(previews::tabs::build_ui()),
        "sparkline" => Some(previews::sparkline::build_ui()),
        "polish_demo" => Some(previews::polish_demo::build_ui()),
        "async_app" => Some(previews::async_app::build_ui()),
        "cube3d_braille" => Some(previews::cube3d_braille::build_ui()),
        "dashboard" => Some(previews::dashboard::build_ui()),
        "plasma" => Some(previews::plasma::build_ui()),
        "showcase" => Some(previews::showcase::build_ui()),
        _ => None,
    }
}

fn render_output(state: &mut ViewerState) -> Element {
    let name = state
        .examples
        .get(state.selected)
        .map(|e| e.name.as_str())
        .unwrap_or("");

    // Focus mode: render the live interactive preview
    if state.focus_mode {
        if let Some(ref mut preview) = state.focused_preview {
            let preview_element = preview.render();
            return element! {
                Box(flex_direction: FlexDirection::Column) {
                    Text(content: " LIVE ", bold: true, color: Color::Yellow)
                    #(preview_element)
                }
            };
        }
    }

    // Try direct element render first
    if state.show_preview {
        if let Some(preview) = try_render_preview(name, &state.animation_timer) {
            return element! {
                Box(flex_direction: FlexDirection::Column) {
                    Text(content: " Preview ", bold: true, color: Color::Green)
                    #(preview)
                }
            };
        }
    }

    // Fall back to captured subprocess output
    let output = state.visible_output();

    if output.is_empty() && !state.show_preview {
        return element! {
            Box(flex_direction: FlexDirection::Column) {
                Text(content: " Output ", bold: true, color: Color::Cyan)
                Text(content: "", dim: true)
                Text(content: " Press 'r' to run", dim: true)
                Text(content: " Press 'p' to preview", dim: true)
            }
        };
    }

    if output.is_empty() {
        return element! {
            Box(flex_direction: FlexDirection::Column) {
                Text(content: " Output ", bold: true, color: Color::Cyan)
                Text(content: "", dim: true)
                Text(content: " No preview available", dim: true)
                Text(content: " Press 'r' to run", dim: true)
            }
        };
    }

    let running = state.capture.is_running();
    let header_text = if running {
        " Output (running) "
    } else {
        " Output "
    };
    let header_color = if running { Color::Green } else { Color::Cyan };

    let lines: Vec<Element> = output
        .lines()
        .map(|line| {
            element! {
                Text(content: line.to_string(), color: Color::White)
            }
        })
        .collect();

    let mut children = vec![element! {
        Text(content: header_text.to_string(), bold: true, color: header_color)
    }];
    children.extend(lines);

    Element::column(children)
}

fn render_status(state: &ViewerState) -> Element {
    if state.focus_mode {
        let name = state
            .examples
            .get(state.selected)
            .map(|e| e.name.as_str())
            .unwrap_or("");
        let status = format!(
            " LIVE: {}  │  [Esc] Exit  [Keys] Interact  [/] Resize",
            name
        );
        return element! {
            Text(content: status, color: Color::Black, bg_color: Color::Yellow)
        };
    }

    let marked_count = state.examples.iter().filter(|e| e.marked).count();
    let scroll_info = format!(" L{}/{}", state.code_scroll + 1, state.code_line_count());

    let marked_info = if marked_count > 0 {
        format!(" | {}", marked_count)
    } else {
        String::new()
    };

    let running_info = if state.capture.is_running() {
        " | [s] Stop"
    } else {
        ""
    };

    let controls = format!(
        " [↑↓] Nav  [Enter] Live  [PgUp/Dn] Scroll  [/] Resize  [r] Run{}  [b] Build  [d] Mark  [x] Del  [q] Quit{}{}",
        running_info, marked_info, scroll_info
    );

    let status = if state.status_msg.is_empty() {
        controls
    } else {
        format!(" {}{}", state.status_msg, controls)
    };

    element! {
        Text(content: status, color: Color::Black, bg_color: Color::White)
    }
}

fn build_ui(state: &mut ViewerState) -> Element {
    let list = render_list(state);
    let code = render_code(state);
    let output = render_output(state);
    let status = render_status(state);

    let right_border_color = if state.focus_mode {
        Color::Yellow
    } else {
        Color::DarkGray
    };
    let right_border_style = if state.focus_mode {
        BorderStyle::Double
    } else {
        BorderStyle::Single
    };

    // Compute fixed widths based on preview ratio
    let remaining = state.term_width as f32 - LIST_WIDTH;
    let preview_width = (remaining * state.preview_ratio).round();
    let code_width = remaining - preview_width;

    element! {
        Box(flex_direction: FlexDirection::Column, width: state.term_width as f32, height: state.term_height as f32) {
            Box(flex_direction: FlexDirection::Row, flex_grow: 1.0) {
                // Left: example list
                Box(
                    width: LIST_WIDTH,
                    flex_direction: FlexDirection::Column,
                    border_style: BorderStyle::Single,
                    border_color: Color::DarkGray,
                    padding_left: 1.0,
                ) {
                    #(list)
                }
                // Center: code view
                Box(
                    width: code_width,
                    flex_direction: FlexDirection::Column,
                    border_style: BorderStyle::Single,
                    border_color: Color::DarkGray,
                    padding_left: 1.0,
                    overflow_x: Overflow::Hidden,
                    overflow_y: Overflow::Hidden,
                ) {
                    #(code)
                }
                // Right: output panel
                Box(
                    width: preview_width,
                    flex_direction: FlexDirection::Column,
                    border_style: right_border_style,
                    border_color: right_border_color,
                    padding_left: 1.0,
                    overflow_x: Overflow::Hidden,
                    overflow_y: Overflow::Hidden,
                ) {
                    #(output)
                }
            }
            // Status bar
            #(status)
        }
    }
}

// libc for process kill
mod libc {
    extern "C" {
        pub fn kill(pid: i32, sig: i32) -> i32;
    }
    pub const SIGTERM: i32 = 15;
}

fn main() -> std::io::Result<()> {
    let mut blaeck = Blaeck::new(std::io::stdout())?;
    let mut state = ViewerState::new();

    crossterm::terminal::enable_raw_mode()?;
    blaeck.set_cursor_visible(false);

    loop {
        // Calculate delta time
        let now = Instant::now();
        let dt = now.duration_since(state.last_frame_time).as_secs_f64();
        state.last_frame_time = now;

        // Tick the focused preview if any
        let needs_continuous = if let Some(ref mut preview) = state.focused_preview {
            preview.poll_events();
            preview.tick(dt);
            preview.needs_tick()
        } else {
            false
        };

        let ui = build_ui(&mut state);
        blaeck.render(ui)?;

        // Update status when capture finishes
        if state.capture.is_done() && state.status_msg.contains("Running") {
            let name = state.status_msg.replace("Running ", "").replace("...", "");
            state.status_msg = format!("{} done", name.trim());
        }

        // Use shorter poll timeout when in continuous rendering mode (animations)
        let poll_timeout = if needs_continuous {
            Duration::from_millis(16) // ~60fps
        } else {
            Duration::from_millis(50) // Normal mode
        };

        if poll(poll_timeout)? {
            match read()? {
                Event::Key(key) => {
                    if let Action::Quit = state.handle_input(key) {
                        break;
                    }
                }
                Event::Resize(w, h) => {
                    state.term_width = w;
                    state.term_height = h;
                    let _ = blaeck.handle_resize(w, h);
                }
                _ => {}
            }
        }
    }

    blaeck.set_cursor_visible(true);
    crossterm::terminal::disable_raw_mode()?;
    blaeck.unmount()?;

    let marked: Vec<&str> = state
        .examples
        .iter()
        .filter(|e| e.marked)
        .map(|e| e.name.as_str())
        .collect();
    if !marked.is_empty() {
        println!("Warning: {} files still marked for deletion:", marked.len());
        for name in &marked {
            println!("  - {}", name);
        }
    }

    Ok(())
}