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
#![doc(html_favicon_url = "https://raw.githubusercontent.com/zng-ui/zng/main/examples/image/res/zng-logo-icon.png")]
#![doc(html_logo_url = "https://raw.githubusercontent.com/zng-ui/zng/main/examples/image/res/zng-logo.png")]
//!
//! ANSI text widget, properties and nodes.
//!
//! # Crate
//!
#![doc = include_str!(concat!("../", std::env!("CARGO_PKG_README")))]
#![warn(unused_extern_crates)]
#![warn(missing_docs)]

zng_wgt::enable_widget_macros!();

use zng_ext_font::*;
use zng_wgt::{prelude::*, *};
use zng_wgt_fill::*;
use zng_wgt_filter::*;
use zng_wgt_scroll::{lazy, LazyMode};
use zng_wgt_stack::{Stack, StackDirection};
use zng_wgt_text::*;

#[doc(hidden)]
pub use zng_wgt_text::__formatx;

/// Render text styled using ANSI escape sequences.
///
/// Supports color, weight, italic and more, see [`AnsiStyle`] for the full style supported.
#[widget($crate::AnsiText {
    ($txt:literal) => {
        txt = $crate::__formatx!($txt);
    };
    ($txt:expr) => {
        txt = $txt;
    };
    ($txt:tt, $($format:tt)*) => {
        txt = $crate::__formatx!($txt, $($format)*);
    };
})]
#[rustfmt::skip]
pub struct AnsiText(
    FontMix<
    TextSpacingMix<
    ParagraphMix<
    LangMix<
    WidgetBase
    >>>>
);
impl AnsiText {
    fn widget_intrinsic(&mut self) {
        widget_set! {
            self;
            font_family = ["JetBrains Mono", "Consolas", "monospace"];
        };

        self.widget_builder().push_build_action(|wgt| {
            let txt = wgt.capture_var_or_default(property_id!(txt));
            let child = ansi_node(txt);
            wgt.set_child(child.boxed());
        });
    }

    widget_impl! {
        /// ANSI text.
        pub txt(text: impl IntoVar<Txt>);
    }
}

pub use ansi_parse::*;
mod ansi_parse {

    use super::*;

    /// Represents a segment of ANSI styled text that shares the same style.
    #[derive(Debug)]
    pub struct AnsiTxt<'a> {
        /// Text run.
        pub txt: &'a str,
        /// Text style.
        pub style: AnsiStyle,
    }

    /// Represents the ANSI style of a text run.
    ///
    /// See [`AnsiText`](struct@super::AnsiText) for more details.
    #[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
    pub struct AnsiStyle {
        /// Background color.
        pub background_color: AnsiColor,
        /// Font color.
        pub color: AnsiColor,
        /// Font weight.
        pub weight: AnsiWeight,
        /// Font italic.
        pub italic: bool,
        /// Underline.
        pub underline: bool,
        /// Strikethrough.
        pub strikethrough: bool,
        /// Negative color.
        pub invert_color: bool,
        /// Visibility.
        pub hidden: bool,
        /// Blink animation.
        pub blink: bool,
    }
    impl Default for AnsiStyle {
        fn default() -> Self {
            Self {
                background_color: AnsiColor::Black,
                color: AnsiColor::White,
                weight: Default::default(),
                italic: false,
                underline: false,
                strikethrough: false,
                invert_color: false,
                hidden: false,
                blink: false,
            }
        }
    }

    /// Named ANSI color.
    ///
    /// See [`AnsiStyle`] for more details.
    #[allow(missing_docs)]
    #[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
    pub enum AnsiColor {
        Black,
        Red,
        Green,
        Yellow,
        Blue,
        Magenta,
        Cyan,
        White,
        /// Gray
        BrightBlack,
        BrightRed,
        BrightGreen,
        BrightYellow,
        BrightBlue,
        BrightMagenta,
        BrightCyan,
        BrightWhite,
        /// 8-bit lookup.
        Ansi256(u8),
        /// RGB
        TrueColor(u8, u8, u8),
    }
    impl_from_and_into_var! {
        fn from(color: AnsiColor) -> Rgba {
            match color {
                AnsiColor::Black => rgb(0, 0, 0),
                AnsiColor::Red => rgb(205, 49, 49),
                AnsiColor::Green => rgb(13, 188, 121),
                AnsiColor::Yellow => rgb(229, 229, 16),
                AnsiColor::Blue => rgb(36, 114, 200),
                AnsiColor::Magenta => rgb(188, 63, 188),
                AnsiColor::Cyan => rgb(17, 168, 205),
                AnsiColor::White => rgb(229, 229, 229),
                AnsiColor::BrightBlack => rgb(102, 102, 102),
                AnsiColor::BrightRed => rgb(241, 76, 76),
                AnsiColor::BrightGreen => rgb(35, 209, 139),
                AnsiColor::BrightYellow => rgb(245, 245, 67),
                AnsiColor::BrightBlue => rgb(59, 142, 234),
                AnsiColor::BrightMagenta => rgb(214, 112, 214),
                AnsiColor::BrightCyan => rgb(41, 184, 219),
                AnsiColor::BrightWhite => rgb(229, 229, 229),
                AnsiColor::Ansi256(c) => {
                    let (r, g, b) = X_TERM_256[c as usize];
                    rgb(r, g, b)
                }
                AnsiColor::TrueColor(r, g, b) => rgb(r, g, b),
            }
        }
    }

    /// Font weight defined by ANSI escape codes.
    ///
    /// See [`AnsiStyle`] for more details.
    #[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
    pub enum AnsiWeight {
        /// Normal.
        Normal,
        /// Bold.
        Bold,
        /// Light.
        Faint,
    }
    impl Default for AnsiWeight {
        fn default() -> Self {
            Self::Normal
        }
    }
    impl_from_and_into_var! {
        fn from(weight: AnsiWeight) -> FontWeight {
            match weight {
                AnsiWeight::Normal => FontWeight::NORMAL,
                AnsiWeight::Bold => FontWeight::BOLD,
                AnsiWeight::Faint => FontWeight::LIGHT,
            }
        }
    }

    /// Iterator that parses ANSI escape codes.
    ///
    /// This is the pull style parser used internally by the [`AnsiText!`] widget.
    ///
    /// [`AnsiText!`]: struct@crate::AnsiText
    pub struct AnsiTextParser<'a> {
        source: &'a str,
        /// Current style.
        pub style: AnsiStyle,
    }
    impl<'a> AnsiTextParser<'a> {
        /// New parsing iterator.
        pub fn new(source: &'a str) -> Self {
            Self {
                source,
                style: AnsiStyle::default(),
            }
        }
    }
    impl<'a> Iterator for AnsiTextParser<'a> {
        type Item = AnsiTxt<'a>;

        fn next(&mut self) -> Option<Self::Item> {
            const CSI: &str = "\x1b[";

            fn is_esc_end(byte: u8) -> bool {
                (0x40..=0x7e).contains(&byte)
            }

            loop {
                if self.source.is_empty() {
                    return None;
                } else if let Some(source) = self.source.strip_prefix(CSI) {
                    let mut esc_end = 0;
                    while esc_end < source.len() && !is_esc_end(source.as_bytes()[esc_end]) {
                        esc_end += 1;
                    }
                    esc_end += 1;

                    let (esc, source) = source.split_at(esc_end);

                    let esc = &esc[..(esc.len() - 1)];
                    self.style.set(esc);

                    self.source = source;
                    continue;
                } else if let Some(i) = self.source.find(CSI) {
                    let (txt, source) = self.source.split_at(i);
                    self.source = source;
                    return Some(AnsiTxt {
                        txt,
                        style: self.style.clone(),
                    });
                } else {
                    return Some(AnsiTxt {
                        txt: std::mem::take(&mut self.source),
                        style: self.style.clone(),
                    });
                }
            }
        }
    }

    impl AnsiStyle {
        fn set(&mut self, esc_codes: &str) {
            let mut esc_codes = esc_codes.split(';');
            while let Some(code) = esc_codes.next() {
                match code {
                    "0" => *self = Self::default(),
                    "1" => self.weight = AnsiWeight::Bold,
                    "2" => self.weight = AnsiWeight::Faint,
                    "3" => self.italic = true,
                    "4" => self.underline = true,
                    "5" => self.blink = true,
                    "7" => self.invert_color = true,
                    "8" => self.hidden = true,
                    "9" => self.strikethrough = true,
                    "22" => self.weight = AnsiWeight::Normal,
                    "23" => self.italic = false,
                    "24" => self.underline = false,
                    "25" => self.blink = false,
                    "27" => self.invert_color = false,
                    "28" => self.hidden = false,
                    "29" => self.strikethrough = false,
                    "30" => self.color = AnsiColor::Black,
                    "31" => self.color = AnsiColor::Red,
                    "32" => self.color = AnsiColor::Green,
                    "33" => self.color = AnsiColor::Yellow,
                    "34" => self.color = AnsiColor::Blue,
                    "35" => self.color = AnsiColor::Magenta,
                    "36" => self.color = AnsiColor::Cyan,
                    "37" => self.color = AnsiColor::White,
                    "40" => self.color = AnsiColor::Black,
                    "41" => self.color = AnsiColor::Red,
                    "42" => self.color = AnsiColor::Green,
                    "43" => self.color = AnsiColor::Yellow,
                    "44" => self.color = AnsiColor::Blue,
                    "45" => self.color = AnsiColor::Magenta,
                    "46" => self.color = AnsiColor::Cyan,
                    "47" => self.color = AnsiColor::White,
                    "90" => self.color = AnsiColor::BrightBlack,
                    "91" => self.color = AnsiColor::BrightRed,
                    "92" => self.color = AnsiColor::BrightGreen,
                    "93" => self.color = AnsiColor::BrightYellow,
                    "94" => self.color = AnsiColor::BrightBlue,
                    "95" => self.color = AnsiColor::BrightMagenta,
                    "96" => self.color = AnsiColor::BrightCyan,
                    "97" => self.color = AnsiColor::BrightWhite,
                    "100" => self.background_color = AnsiColor::BrightBlack,
                    "101" => self.background_color = AnsiColor::BrightRed,
                    "102" => self.background_color = AnsiColor::BrightGreen,
                    "103" => self.background_color = AnsiColor::BrightYellow,
                    "104" => self.background_color = AnsiColor::BrightBlue,
                    "105" => self.background_color = AnsiColor::BrightMagenta,
                    "106" => self.background_color = AnsiColor::BrightCyan,
                    "107" => self.background_color = AnsiColor::BrightWhite,
                    "38" | "48" => {
                        let target = if code == "38" {
                            &mut self.color
                        } else {
                            &mut self.background_color
                        };
                        match esc_codes.next() {
                            Some("5") => {
                                let c = esc_codes.next().and_then(|c| c.parse().ok()).unwrap_or(0);
                                *target = AnsiColor::Ansi256(c)
                            }
                            Some("2") => {
                                let r = esc_codes.next().and_then(|c| c.parse().ok()).unwrap_or(0);
                                let g = esc_codes.next().and_then(|c| c.parse().ok()).unwrap_or(0);
                                let b = esc_codes.next().and_then(|c| c.parse().ok()).unwrap_or(0);

                                *target = AnsiColor::TrueColor(r, g, b);
                            }
                            _ => {}
                        }
                    }
                    _ => (),
                }
            }
        }
    }
}

pub use ansi_fn::*;
mod ansi_fn {
    use std::time::Duration;

    use super::{AnsiColor, AnsiStyle, AnsiWeight};

    use super::*;

    /// Arguments for a widget function for an ANSI styled text fragment.
    ///
    /// See [`TEXT_FN_VAR`] for more details.
    pub struct TextFnArgs {
        /// The text.
        pub txt: Txt,
        /// The ANSI style.
        pub style: AnsiStyle,
    }

    /// Arguments for a widget function for a text line.
    ///
    /// See [`LINE_FN_VAR`] for more details.
    pub struct LineFnArgs {
        /// Zero-counted global index of this line.
        pub index: u32,
        /// Zero-counted index of this line in the parent page.
        pub page_index: u32,
        /// Text segment widgets, generated by [`TEXT_FN_VAR`].
        pub text: UiVec,
    }

    /// Arguments for a widget function for a stack of lines.
    ///
    /// See [`PAGE_FN_VAR`] for more details.
    pub struct PageFnArgs {
        /// Zero-counted index of this page.
        pub index: u32,

        /// Line widgets, generated by [`LINE_FN_VAR`].
        pub lines: UiVec,
    }

    /// Arguments for a widget function for a stack of pages.
    ///
    /// See [`PANEL_FN_VAR`] for more details.
    pub struct PanelFnArgs {
        /// Page widgets, generated by [`PAGE_FN_VAR`].
        pub pages: UiVec,
    }

    context_var! {
        /// Widget function for [`TextFnArgs`].
        ///
        /// The returned widgets are layout by the [`LINE_FN_VAR`]. The default view is [`default_text_fn`].
        pub static TEXT_FN_VAR: WidgetFn<TextFnArgs> = wgt_fn!(|args: TextFnArgs| { default_text_fn(args) });

        /// Widget function for [`LineFnArgs`].
        ///
        /// The returned widgets are layout by the [`PAGE_FN_VAR`]. The default view is [`default_line_fn`].
        pub static LINE_FN_VAR: WidgetFn<LineFnArgs> = wgt_fn!(|args: LineFnArgs| { default_line_fn(args) });

        /// Widget function for [`PageFnArgs`].
        ///
        /// The returned widgets are layout by the [`PANEL_FN_VAR`] widget. The default view is [`default_page_fn`].
        pub static PAGE_FN_VAR: WidgetFn<PageFnArgs> = wgt_fn!(|args: PageFnArgs| { default_page_fn(args) });

        /// Widget function for [`PanelFnArgs`].
        ///
        /// The returned view is the [`AnsiText!`] child. The default is [`default_panel_fn`].
        ///
        /// [`AnsiText!`]: struct@super::AnsiText
        pub static PANEL_FN_VAR: WidgetFn<PanelFnArgs> = wgt_fn!(|args: PanelFnArgs| { default_panel_fn(args) });

        /// Duration the ANSI blink animation keeps the text visible for.
        ///
        /// Set to `ZERO` or `MAX` to disable animation.
        pub static BLINK_INTERVAL_VAR: Duration = Duration::ZERO;

        /// Maximum number of lines per [`PAGE_FN_VAR`].
        ///
        /// Is `200` by default.
        pub static LINES_PER_PAGE_VAR: u32 = 200;
    }

    /// Default [`TEXT_FN_VAR`].
    ///
    /// This view is configured by contextual variables like [`BLINK_INTERVAL_VAR`] and all text variables that are
    /// not overridden by the ANSI style, like the font.
    ///
    /// Returns a `Text!` with the text and style.
    pub fn default_text_fn(args: TextFnArgs) -> impl UiNode {
        let mut text = Text::widget_new();

        widget_set! {
            &mut text;
            txt = args.txt;
        }

        if args.style.background_color != AnsiColor::Black {
            widget_set! {
                &mut text;
                background_color = args.style.background_color;
            }
        }
        if args.style.color != AnsiColor::White {
            widget_set! {
                &mut text;
                font_color = args.style.color;
            }
        }

        if args.style.weight != AnsiWeight::Normal {
            widget_set! {
                &mut text;
                font_weight = args.style.weight;
            }
        }
        if args.style.italic {
            widget_set! {
                &mut text;
                font_style = FontStyle::Italic;
            }
        }

        if args.style.underline {
            widget_set! {
                &mut text;
                underline = 1, LineStyle::Solid;
            }
        }
        if args.style.strikethrough {
            widget_set! {
                &mut text;
                strikethrough = 1, LineStyle::Solid;
            }
        }

        if args.style.invert_color {
            widget_set! {
                &mut text;
                invert_color = true;
            }
        }

        if args.style.hidden {
            widget_set! {
                &mut text;
                visibility = Visibility::Hidden;
            }
        }
        if args.style.blink && !args.style.hidden {
            let opacity = var(1.fct());

            let interval = BLINK_INTERVAL_VAR.get();
            if interval != Duration::ZERO && interval != Duration::MAX {
                opacity.step_oci(0.fct(), interval).perm();

                widget_set! {
                    &mut text;
                    opacity;
                }
            }
        }

        text.widget_build()
    }

    /// Default [`LINE_FN_VAR`].
    ///
    /// Returns a `Wrap!` for text with multiple segments, or returns the single segment, or an empty text.
    pub fn default_line_fn(mut args: LineFnArgs) -> impl UiNode {
        use crate::prelude::*;

        if args.text.is_empty() {
            Text!("").boxed()
        } else if args.text.len() == 1 {
            args.text.remove(0)
        } else {
            Stack! {
                direction = StackDirection::start_to_end();
                children = args.text;
            }
            .boxed()
        }
    }

    /// Default [`PAGE_FN_VAR`].
    ///
    /// Returns a `Stack!` for multiple lines, or return the single line, or a nil node.
    pub fn default_page_fn(mut args: PageFnArgs) -> impl UiNode {
        use crate::prelude::*;

        if args.lines.is_empty() {
            NilUiNode.boxed()
        } else if args.lines.len() == 1 {
            args.lines.remove(0)
        } else {
            let len = args.lines.len();
            Stack! {
                direction = StackDirection::top_to_bottom();
                children = args.lines;
                lazy = LazyMode::lazy_vertical(wgt_fn!(|_| {
                    let height_sample = zng_wgt_text::node::line_placeholder(50);
                    zng_wgt_stack::lazy_sample(len, StackDirection::top_to_bottom(), 0, height_sample)
                }));
            }
            .boxed()
        }
    }

    /// Default [`PANEL_FN_VAR`].
    ///
    /// Returns a `Stack!` for multiple pages, or returns the single page, or a nil node.
    pub fn default_panel_fn(mut args: PanelFnArgs) -> impl UiNode {
        use crate::prelude::*;

        if args.pages.is_empty() {
            NilUiNode.boxed()
        } else if args.pages.len() == 1 {
            args.pages.remove(0)
        } else {
            Stack! {
                direction = StackDirection::top_to_bottom();
                children = args.pages;
            }
            .boxed()
        }
    }

    /// ANSI blink animation interval.
    ///
    /// Set to `ZERO` to disable the blink animation.
    ///
    /// Sets the [`BLINK_INTERVAL_VAR`].
    #[property(CONTEXT, default(BLINK_INTERVAL_VAR), widget_impl(AnsiText))]
    pub fn blink_interval(child: impl UiNode, interval: impl IntoVar<Duration>) -> impl UiNode {
        with_context_var(child, BLINK_INTERVAL_VAR, interval)
    }

    /// Widget function that converts [`TextFnArgs`] to widgets.
    ///
    /// Sets the [`TEXT_FN_VAR`].
    #[property(CONTEXT, default(TEXT_FN_VAR), widget_impl(AnsiText))]
    pub fn text_fn(child: impl UiNode, wgt_fn: impl IntoVar<WidgetFn<TextFnArgs>>) -> impl UiNode {
        with_context_var(child, TEXT_FN_VAR, wgt_fn)
    }

    /// Widget function that converts [`LineFnArgs`] to widgets.
    ///
    /// Sets the [`LINE_FN_VAR`].
    #[property(CONTEXT, default(LINE_FN_VAR), widget_impl(AnsiText))]
    pub fn line_fn(child: impl UiNode, wgt_fn: impl IntoVar<WidgetFn<LineFnArgs>>) -> impl UiNode {
        with_context_var(child, LINE_FN_VAR, wgt_fn)
    }

    /// Widget function that converts [`PageFnArgs`] to widgets.
    ///
    /// A *page* is a stack of a maximum of [`lines_per_page`], the text is split in pages mostly for performance reasons.
    ///
    /// Sets the [`PAGE_FN_VAR`].
    ///
    /// [`lines_per_page`]: fn@lines_per_page
    #[property(CONTEXT, default(PAGE_FN_VAR), widget_impl(AnsiText))]
    pub fn page_fn(child: impl UiNode, wgt_fn: impl IntoVar<WidgetFn<PageFnArgs>>) -> impl UiNode {
        with_context_var(child, PAGE_FN_VAR, wgt_fn)
    }

    /// Widget function that converts [`PanelFnArgs`] to widgets.
    #[property(CONTEXT, default(PANEL_FN_VAR), widget_impl(AnsiText))]
    pub fn panel_fn(child: impl UiNode, wgt_fn: impl IntoVar<WidgetFn<PanelFnArgs>>) -> impl UiNode {
        with_context_var(child, PANEL_FN_VAR, wgt_fn)
    }

    /// Maximum number of lines per page view.
    ///
    /// Sets the [`LINES_PER_PAGE_VAR`].
    #[property(CONTEXT, default(LINES_PER_PAGE_VAR), widget_impl(AnsiText))]
    pub fn lines_per_page(child: impl UiNode, count: impl IntoVar<u32>) -> impl UiNode {
        with_context_var(child, LINES_PER_PAGE_VAR, count)
    }
}

fn generate_ansi(txt: &impl Var<Txt>) -> BoxedUiNode {
    use ansi_fn::*;
    use std::mem;

    txt.with(|txt| {
        let text_fn = TEXT_FN_VAR.get();
        let line_fn = LINE_FN_VAR.get();
        let page_fn = PAGE_FN_VAR.get();
        let panel_fn = PANEL_FN_VAR.get();
        let lines_per_page = LINES_PER_PAGE_VAR.get() as usize;

        let mut pages = Vec::with_capacity(4);
        let mut lines = Vec::with_capacity(50);

        for (i, line) in txt.lines().enumerate() {
            let text = ansi_parse::AnsiTextParser::new(line)
                .filter_map(|txt| {
                    text_fn.call_checked(TextFnArgs {
                        txt: txt.txt.to_txt(),
                        style: txt.style,
                    })
                })
                .collect();

            lines.push(line_fn(LineFnArgs {
                index: i as u32,
                page_index: lines.len() as u32,
                text,
            }));

            if lines.len() == lines_per_page {
                let lines = mem::replace(&mut lines, Vec::with_capacity(50));
                pages.push(page_fn(PageFnArgs {
                    index: pages.len() as u32,
                    lines: lines.into(),
                }));
            }
        }

        if !lines.is_empty() {
            pages.push(page_fn(PageFnArgs {
                index: pages.len() as u32,
                lines: lines.into(),
            }));
        }

        panel_fn(PanelFnArgs { pages: pages.into() })
    })
}

/// Implements the ANSI parsing and view generation, configured by contextual properties.
pub fn ansi_node(txt: impl IntoVar<Txt>) -> impl UiNode {
    let txt = txt.into_var();
    match_node(NilUiNode.boxed(), move |c, op| match op {
        UiNodeOp::Init => {
            WIDGET
                .sub_var(&txt)
                .sub_var(&TEXT_FN_VAR)
                .sub_var(&LINE_FN_VAR)
                .sub_var(&PAGE_FN_VAR)
                .sub_var(&PANEL_FN_VAR)
                .sub_var(&LINES_PER_PAGE_VAR)
                .sub_var(&BLINK_INTERVAL_VAR);

            *c.child() = generate_ansi(&txt);
        }
        UiNodeOp::Deinit => {
            c.deinit();
            *c.child() = NilUiNode.boxed();
        }
        UiNodeOp::Update { .. } => {
            use ansi_fn::*;

            if txt.is_new()
                || TEXT_FN_VAR.is_new()
                || LINE_FN_VAR.is_new()
                || PAGE_FN_VAR.is_new()
                || PANEL_FN_VAR.is_new()
                || LINES_PER_PAGE_VAR.is_new()
                || BLINK_INTERVAL_VAR.is_new()
            {
                c.child().deinit();
                *c.child() = generate_ansi(&txt);
                c.child().init();
                WIDGET.update_info().layout().render();
            }
        }
        _ => {}
    })
}

static X_TERM_256: [(u8, u8, u8); 256] = [
    (0, 0, 0),
    (128, 0, 0),
    (0, 128, 0),
    (128, 128, 0),
    (0, 0, 128),
    (128, 0, 128),
    (0, 128, 128),
    (192, 192, 192),
    (128, 128, 128),
    (255, 0, 0),
    (0, 255, 0),
    (255, 255, 0),
    (0, 0, 255),
    (255, 0, 255),
    (0, 255, 255),
    (255, 255, 255),
    (0, 0, 0),
    (0, 0, 95),
    (0, 0, 135),
    (0, 0, 175),
    (0, 0, 215),
    (0, 0, 255),
    (0, 95, 0),
    (0, 95, 95),
    (0, 95, 135),
    (0, 95, 175),
    (0, 95, 215),
    (0, 95, 255),
    (0, 135, 0),
    (0, 135, 95),
    (0, 135, 135),
    (0, 135, 175),
    (0, 135, 215),
    (0, 135, 255),
    (0, 175, 0),
    (0, 175, 95),
    (0, 175, 135),
    (0, 175, 175),
    (0, 175, 215),
    (0, 175, 255),
    (0, 215, 0),
    (0, 215, 95),
    (0, 215, 135),
    (0, 215, 175),
    (0, 215, 215),
    (0, 215, 255),
    (0, 255, 0),
    (0, 255, 95),
    (0, 255, 135),
    (0, 255, 175),
    (0, 255, 215),
    (0, 255, 255),
    (95, 0, 0),
    (95, 0, 95),
    (95, 0, 135),
    (95, 0, 175),
    (95, 0, 215),
    (95, 0, 255),
    (95, 95, 0),
    (95, 95, 95),
    (95, 95, 135),
    (95, 95, 175),
    (95, 95, 215),
    (95, 95, 255),
    (95, 135, 0),
    (95, 135, 95),
    (95, 135, 135),
    (95, 135, 175),
    (95, 135, 215),
    (95, 135, 255),
    (95, 175, 0),
    (95, 175, 95),
    (95, 175, 135),
    (95, 175, 175),
    (95, 175, 215),
    (95, 175, 255),
    (95, 215, 0),
    (95, 215, 95),
    (95, 215, 135),
    (95, 215, 175),
    (95, 215, 215),
    (95, 215, 255),
    (95, 255, 0),
    (95, 255, 95),
    (95, 255, 135),
    (95, 255, 175),
    (95, 255, 215),
    (95, 255, 255),
    (135, 0, 0),
    (135, 0, 95),
    (135, 0, 135),
    (135, 0, 175),
    (135, 0, 215),
    (135, 0, 255),
    (135, 95, 0),
    (135, 95, 95),
    (135, 95, 135),
    (135, 95, 175),
    (135, 95, 215),
    (135, 95, 255),
    (135, 135, 0),
    (135, 135, 95),
    (135, 135, 135),
    (135, 135, 175),
    (135, 135, 215),
    (135, 135, 255),
    (135, 175, 0),
    (135, 175, 95),
    (135, 175, 135),
    (135, 175, 175),
    (135, 175, 215),
    (135, 175, 255),
    (135, 215, 0),
    (135, 215, 95),
    (135, 215, 135),
    (135, 215, 175),
    (135, 215, 215),
    (135, 215, 255),
    (135, 255, 0),
    (135, 255, 95),
    (135, 255, 135),
    (135, 255, 175),
    (135, 255, 215),
    (135, 255, 255),
    (175, 0, 0),
    (175, 0, 95),
    (175, 0, 135),
    (175, 0, 175),
    (175, 0, 215),
    (175, 0, 255),
    (175, 95, 0),
    (175, 95, 95),
    (175, 95, 135),
    (175, 95, 175),
    (175, 95, 215),
    (175, 95, 255),
    (175, 135, 0),
    (175, 135, 95),
    (175, 135, 135),
    (175, 135, 175),
    (175, 135, 215),
    (175, 135, 255),
    (175, 175, 0),
    (175, 175, 95),
    (175, 175, 135),
    (175, 175, 175),
    (175, 175, 215),
    (175, 175, 255),
    (175, 215, 0),
    (175, 215, 95),
    (175, 215, 135),
    (175, 215, 175),
    (175, 215, 215),
    (175, 215, 255),
    (175, 255, 0),
    (175, 255, 95),
    (175, 255, 135),
    (175, 255, 175),
    (175, 255, 215),
    (175, 255, 255),
    (215, 0, 0),
    (215, 0, 95),
    (215, 0, 135),
    (215, 0, 175),
    (215, 0, 215),
    (215, 0, 255),
    (215, 95, 0),
    (215, 95, 95),
    (215, 95, 135),
    (215, 95, 175),
    (215, 95, 215),
    (215, 95, 255),
    (215, 135, 0),
    (215, 135, 95),
    (215, 135, 135),
    (215, 135, 175),
    (215, 135, 215),
    (215, 135, 255),
    (215, 175, 0),
    (215, 175, 95),
    (215, 175, 135),
    (215, 175, 175),
    (215, 175, 215),
    (215, 175, 255),
    (215, 215, 0),
    (215, 215, 95),
    (215, 215, 135),
    (215, 215, 175),
    (215, 215, 215),
    (215, 215, 255),
    (215, 255, 0),
    (215, 255, 95),
    (215, 255, 135),
    (215, 255, 175),
    (215, 255, 215),
    (215, 255, 255),
    (255, 0, 0),
    (255, 0, 95),
    (255, 0, 135),
    (255, 0, 175),
    (255, 0, 215),
    (255, 0, 255),
    (255, 95, 0),
    (255, 95, 95),
    (255, 95, 135),
    (255, 95, 175),
    (255, 95, 215),
    (255, 95, 255),
    (255, 135, 0),
    (255, 135, 95),
    (255, 135, 135),
    (255, 135, 175),
    (255, 135, 215),
    (255, 135, 255),
    (255, 175, 0),
    (255, 175, 95),
    (255, 175, 135),
    (255, 175, 175),
    (255, 175, 215),
    (255, 175, 255),
    (255, 215, 0),
    (255, 215, 95),
    (255, 215, 135),
    (255, 215, 175),
    (255, 215, 215),
    (255, 215, 255),
    (255, 255, 0),
    (255, 255, 95),
    (255, 255, 135),
    (255, 255, 175),
    (255, 255, 215),
    (255, 255, 255),
    (8, 8, 8),
    (18, 18, 18),
    (28, 28, 28),
    (38, 38, 38),
    (48, 48, 48),
    (58, 58, 58),
    (68, 68, 68),
    (78, 78, 78),
    (88, 88, 88),
    (98, 98, 98),
    (108, 108, 108),
    (118, 118, 118),
    (128, 128, 128),
    (138, 138, 138),
    (148, 148, 148),
    (158, 158, 158),
    (168, 168, 168),
    (178, 178, 178),
    (188, 188, 188),
    (198, 198, 198),
    (208, 208, 208),
    (218, 218, 218),
    (228, 228, 228),
    (238, 238, 238),
];