presenterm 0.16.1

A terminal slideshow presentation tool
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
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
use super::registry::LoadThemeError;
use crate::markdown::text_style::{Color, Colors, UndefinedPaletteColorError};
use hex::{FromHex, FromHexError};
use serde::{Deserialize, Serialize, de::Visitor};
use std::{
    collections::BTreeMap,
    fmt, fs,
    path::{Path, PathBuf},
    str::FromStr,
};

pub(crate) type RawColors = Colors<RawColor>;

/// A presentation theme.
#[derive(Default, Clone, Debug, Deserialize, Serialize)]
#[serde(deny_unknown_fields)]
pub struct PresentationTheme {
    /// The theme this theme extends from.
    #[serde(default)]
    pub(crate) extends: Option<String>,

    /// The style for a slide's title.
    #[serde(default)]
    pub(crate) slide_title: SlideTitleStyle,

    /// The style for a block of code.
    #[serde(default)]
    pub(crate) code: CodeBlockStyle,

    /// The style for the execution output of a piece of code.
    #[serde(default)]
    pub(crate) execution_output: ExecutionOutputBlockStyle,

    /// The style for the pty output of a piece of code.
    #[serde(default)]
    pub(crate) pty_output: PtyOutputBlockStyle,

    /// The style for inline code.
    #[serde(default)]
    pub(crate) inline_code: ModifierStyle,

    /// The style for bold text.
    #[serde(default)]
    pub(crate) bold: ModifierStyle,

    /// The style for italics.
    #[serde(default, alias = "italic")]
    pub(crate) italics: ModifierStyle,

    /// The style for a table.
    #[serde(default)]
    pub(crate) table: Option<Alignment>,

    /// The style for a block quote.
    #[serde(default)]
    pub(crate) block_quote: BlockQuoteStyle,

    /// The style for an alert.
    #[serde(default)]
    pub(crate) alert: AlertStyle,

    /// The default style.
    #[serde(rename = "default", default)]
    pub(crate) default_style: DefaultStyle,

    //// The style of all headings.
    #[serde(default)]
    pub(crate) headings: HeadingStyles,

    /// The style of the introduction slide.
    #[serde(default)]
    pub(crate) intro_slide: IntroSlideStyle,

    /// The style of the presentation footer.
    #[serde(default)]
    pub(crate) footer: Option<FooterStyle>,

    /// The style for typst auto-rendered code blocks.
    #[serde(default)]
    pub(crate) typst: TypstStyle,

    /// The style for mermaid auto-rendered code blocks.
    #[serde(default)]
    pub(crate) mermaid: MermaidStyle,

    /// The style for d2 auto-rendered code blocks.
    #[serde(default)]
    pub(crate) d2: D2Style,

    /// The style for modals.
    #[serde(default)]
    pub(crate) modals: ModalStyle,

    /// The style for layouts.
    #[serde(default)]
    pub(crate) layout_grid: LayoutGridStyle,

    /// The color palette.
    #[serde(default)]
    pub(crate) palette: ColorPalette,
}

impl PresentationTheme {
    /// Construct a presentation from a path.
    pub(crate) fn from_path<P: AsRef<Path>>(path: P) -> Result<Self, LoadThemeError> {
        let contents = fs::read_to_string(&path).map_err(|e| LoadThemeError::Reading(path.as_ref().into(), e))?;
        let theme = serde_yaml::from_str(&contents)
            .map_err(|e| LoadThemeError::Corrupted(path.as_ref().display().to_string(), e.into()))?;
        Ok(theme)
    }
}

/// The style of a slide title.
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
pub(crate) struct SlideTitleStyle {
    /// The alignment.
    #[serde(flatten, default)]
    pub(crate) alignment: Option<Alignment>,

    /// Whether to use a separator line.
    #[serde(default)]
    pub(crate) separator: bool,

    /// The padding that should be added before the text.
    #[serde(default)]
    pub(crate) padding_top: Option<u8>,

    /// The padding that should be added after the text.
    #[serde(default)]
    pub(crate) padding_bottom: Option<u8>,

    /// The colors to be used.
    #[serde(default)]
    pub(crate) colors: RawColors,

    /// The prefix to be added to the slide title.
    #[serde(default)]
    pub(crate) prefix: Option<String>,

    /// Whether to use bold font for slide titles.
    #[serde(default)]
    pub(crate) bold: Option<bool>,

    /// Whether to use italics font for slide titles.
    #[serde(default, alias = "italic")]
    pub(crate) italics: Option<bool>,

    /// Whether to use underlined font for slide titles.
    #[serde(default)]
    pub(crate) underlined: Option<bool>,

    /// The font size to be used if the terminal supports it.
    #[serde(default)]
    pub(crate) font_size: Option<u8>,
}

/// The style for all headings.
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
pub(crate) struct HeadingStyles {
    /// H1 style.
    #[serde(default)]
    pub(crate) h1: HeadingStyle,

    /// H2 style.
    #[serde(default)]
    pub(crate) h2: HeadingStyle,

    /// H3 style.
    #[serde(default)]
    pub(crate) h3: HeadingStyle,

    /// H4 style.
    #[serde(default)]
    pub(crate) h4: HeadingStyle,

    /// H5 style.
    #[serde(default)]
    pub(crate) h5: HeadingStyle,

    /// H6 style.
    #[serde(default)]
    pub(crate) h6: HeadingStyle,
}

/// The style for a heading.
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
pub(crate) struct HeadingStyle {
    /// The alignment.
    #[serde(flatten, default)]
    pub(crate) alignment: Option<Alignment>,

    /// The prefix to be added to this heading.
    ///
    /// This allows adding text like "->" to every heading.
    #[serde(default)]
    pub(crate) prefix: Option<String>,

    /// The colors to be used.
    #[serde(default)]
    pub(crate) colors: RawColors,

    /// The font size to be used if the terminal supports it.
    #[serde(default)]
    pub(crate) font_size: Option<u8>,

    /// Whether the heading is bold.
    #[serde(default)]
    pub(crate) bold: Option<bool>,

    /// Whether the heading is underlined.
    #[serde(default)]
    pub(crate) underlined: Option<bool>,

    /// Whether the heading uses italics.
    #[serde(default, alias = "italic")]
    pub(crate) italics: Option<bool>,
}

/// The style of a block quote.
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
pub(crate) struct BlockQuoteStyle {
    /// The alignment.
    #[serde(flatten, default)]
    pub(crate) alignment: Option<Alignment>,

    /// The prefix to be added to this block quote.
    ///
    /// This allows adding something like a vertical bar before the text.
    #[serde(default)]
    pub(crate) prefix: Option<String>,

    /// The colors to be used.
    #[serde(default)]
    pub(crate) colors: BlockQuoteColors,
}

/// The colors of a block quote.
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
pub(crate) struct BlockQuoteColors {
    /// The foreground/background colors.
    #[serde(flatten)]
    pub(crate) base: RawColors,

    /// The color of the vertical bar that prefixes each line in the quote.
    #[serde(default)]
    pub(crate) prefix: Option<RawColor>,
}

/// The style of an alert.
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
pub(crate) struct AlertStyle {
    /// The alignment.
    #[serde(flatten, default)]
    pub(crate) alignment: Option<Alignment>,

    /// The base colors.
    #[serde(default)]
    pub(crate) base_colors: RawColors,

    /// The prefix to be added to this block quote.
    ///
    /// This allows adding something like a vertical bar before the text.
    #[serde(default)]
    pub(crate) prefix: Option<String>,

    /// The style for each alert type.
    #[serde(default)]
    pub(crate) styles: AlertTypeStyles,
}

/// The style for each alert type.
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
pub(crate) struct AlertTypeStyles {
    /// The style for note alert types.
    #[serde(default)]
    pub(crate) note: AlertTypeStyle,

    /// The style for tip alert types.
    #[serde(default)]
    pub(crate) tip: AlertTypeStyle,

    /// The style for important alert types.
    #[serde(default)]
    pub(crate) important: AlertTypeStyle,

    /// The style for warning alert types.
    #[serde(default)]
    pub(crate) warning: AlertTypeStyle,

    /// The style for caution alert types.
    #[serde(default)]
    pub(crate) caution: AlertTypeStyle,
}

/// The style for an alert type.
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
pub(crate) struct AlertTypeStyle {
    /// The color to be used.
    #[serde(default)]
    pub(crate) color: Option<RawColor>,

    /// The title to be used.
    #[serde(default)]
    pub(crate) title: Option<String>,

    /// The icon to be used.
    #[serde(default)]
    pub(crate) icon: Option<String>,
}

/// The style for the presentation introduction slide.
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
pub(crate) struct IntroSlideStyle {
    /// The style of the title line.
    #[serde(default)]
    pub(crate) title: IntroSlideTitleStyle,

    /// The style of the subtitle line.
    #[serde(default)]
    pub(crate) subtitle: BasicStyle,

    /// The style of the event line.
    #[serde(default)]
    pub(crate) event: BasicStyle,

    /// The style of the location line.
    #[serde(default)]
    pub(crate) location: BasicStyle,

    /// The style of the date line.
    #[serde(default)]
    pub(crate) date: BasicStyle,

    /// The style of the author line.
    #[serde(default)]
    pub(crate) author: AuthorStyle,

    /// Whether we want a footer in the intro slide.
    #[serde(default)]
    pub(crate) footer: Option<bool>,
}

/// A simple style.
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
pub(crate) struct DefaultStyle {
    /// The margin on the left/right of the screen.
    #[serde(default, with = "serde_yaml::with::singleton_map")]
    pub(crate) margin: Option<Margin>,

    /// The colors to be used.
    #[serde(default)]
    pub(crate) colors: RawColors,

    /// The alignment for all elements.
    #[serde(flatten, default)]
    pub(crate) alignment: Option<Alignment>,
}

/// A simple style.
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
pub(crate) struct BasicStyle {
    /// The alignment.
    #[serde(flatten, default)]
    pub(crate) alignment: Option<Alignment>,

    /// The colors to be used.
    #[serde(default)]
    pub(crate) colors: RawColors,
}

/// The intro slide title's style.
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
pub(crate) struct IntroSlideTitleStyle {
    /// The alignment.
    #[serde(flatten, default)]
    pub(crate) alignment: Option<Alignment>,

    /// The colors to be used.
    #[serde(default)]
    pub(crate) colors: RawColors,

    /// The font size to be used if the terminal supports it.
    #[serde(default)]
    pub(crate) font_size: Option<u8>,
}

/// Text alignment.
///
/// This allows anchoring presentation elements to the left, center, or right of the screen.
#[derive(Clone, Debug, Deserialize, Serialize, PartialEq)]
#[serde(tag = "alignment", rename_all = "snake_case")]
pub(crate) enum Alignment {
    /// Left alignment.
    Left {
        /// The margin before any text.
        #[serde(default)]
        margin: Margin,
    },

    /// Right alignment.
    Right {
        /// The margin after any text.
        #[serde(default)]
        margin: Margin,
    },

    /// Center alignment.
    Center {
        /// The minimum margin expected.
        #[serde(default)]
        minimum_margin: Margin,

        /// The minimum size of this element, in columns.
        #[serde(default)]
        minimum_size: u16,
    },
}

impl Default for Alignment {
    fn default() -> Self {
        Self::Left { margin: Margin::Fixed(0) }
    }
}

/// The style for the author line in the presentation intro slide.
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
pub(crate) struct AuthorStyle {
    /// The alignment.
    #[serde(flatten, default)]
    pub(crate) alignment: Option<Alignment>,

    /// The colors to be used.
    #[serde(default)]
    pub(crate) colors: RawColors,

    /// The positioning of the author's name.
    #[serde(default)]
    pub(crate) positioning: AuthorPositioning,
}

/// The style of the footer that's shown in every slide.
#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(tag = "style", rename_all = "snake_case")]
pub(crate) enum FooterStyle {
    /// Use a template to generate the footer.
    Template {
        /// The content to be put on the left.
        left: Option<FooterContent>,

        /// The content to be put on the center.
        center: Option<FooterContent>,

        /// The content to be put on the right.
        right: Option<FooterContent>,

        /// The colors to be used.
        #[serde(default)]
        colors: RawColors,

        /// The height of the footer area.
        height: Option<u16>,
    },

    /// Use a progress bar.
    ProgressBar {
        /// The character that will be used for the progress bar.
        character: Option<char>,

        /// The colors to be used.
        #[serde(default)]
        colors: RawColors,
    },

    /// No footer.
    Empty,
}

impl Default for FooterStyle {
    fn default() -> Self {
        Self::Template { left: None, center: None, right: None, colors: RawColors::default(), height: None }
    }
}

#[derive(Clone, Debug, Deserialize, Serialize, Eq, PartialEq)]
pub(crate) enum FooterTemplateChunk {
    Literal(String),
    OpenBrace,
    ClosedBrace,
    CurrentSlide,
    TotalSlides,
    Author,
    Title,
    SubTitle,
    Event,
    Location,
    Date,
}

#[derive(Clone, Debug, Serialize)]
#[serde(untagged)]
pub(crate) enum FooterContent {
    Template(FooterTemplate),
    Image {
        #[serde(rename = "image")]
        path: PathBuf,
    },
}

struct FooterContentVisitor;

impl<'de> Visitor<'de> for FooterContentVisitor {
    type Value = FooterContent;

    fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
        formatter.write_str("a valid footer")
    }

    fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
    where
        E: serde::de::Error,
    {
        let template = FooterTemplate::from_str(v).map_err(|e| E::custom(e.to_string()))?;
        Ok(FooterContent::Template(template))
    }

    fn visit_map<A>(self, mut map: A) -> Result<Self::Value, A::Error>
    where
        A: serde::de::MapAccess<'de>,
    {
        let Some((key, value)): Option<(String, PathBuf)> = map.next_entry()? else {
            return Err(serde::de::Error::custom("invalid footer"));
        };

        match key.as_str() {
            "image" => Ok(FooterContent::Image { path: value }),
            _ => Err(serde::de::Error::invalid_value(serde::de::Unexpected::Str(&key), &self)),
        }
    }
}

impl<'de> Deserialize<'de> for FooterContent {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        deserializer.deserialize_any(FooterContentVisitor)
    }
}

#[derive(Clone, Debug)]
pub(crate) struct FooterTemplate(pub(crate) Vec<FooterTemplateChunk>);

crate::utils::impl_deserialize_from_str!(FooterTemplate);
crate::utils::impl_serialize_from_display!(FooterTemplate);

impl FromStr for FooterTemplate {
    type Err = ParseFooterTemplateError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let mut chunks = Vec::new();
        let mut chunk_start = 0;
        let mut in_variable = false;
        let mut iter = s.char_indices().peekable();
        while let Some((index, c)) = iter.next() {
            if c == '{' {
                if in_variable {
                    return Err(ParseFooterTemplateError::NestedOpenBrace);
                }
                let double_brace = iter.peek() == Some(&(index + 1, '{'));
                if double_brace {
                    iter.next();
                    if chunk_start != index {
                        chunks.push(FooterTemplateChunk::Literal(s[chunk_start..index].to_string()));
                    }
                    chunks.push(FooterTemplateChunk::OpenBrace);
                    chunk_start = index + 2;
                } else {
                    in_variable = true;
                    if chunk_start != index {
                        chunks.push(FooterTemplateChunk::Literal(s[chunk_start..index].to_string()));
                    }
                    chunk_start = index + 1;
                }
            } else if c == '}' {
                if !in_variable {
                    let double_brace = iter.peek() == Some(&(index + 1, '}'));
                    if double_brace {
                        iter.next();
                        chunks.push(FooterTemplateChunk::Literal(s[chunk_start..index].to_string()));
                        chunks.push(FooterTemplateChunk::ClosedBrace);
                        in_variable = false;
                        chunk_start = index + 2;
                        continue;
                    }
                    return Err(ParseFooterTemplateError::ClosedBraceWithoutOpen);
                }
                let variable = &s[chunk_start..index];
                let chunk = match variable {
                    "current_slide" => FooterTemplateChunk::CurrentSlide,
                    "total_slides" => FooterTemplateChunk::TotalSlides,
                    "author" => FooterTemplateChunk::Author,
                    "title" => FooterTemplateChunk::Title,
                    "sub_title" => FooterTemplateChunk::SubTitle,
                    "event" => FooterTemplateChunk::Event,
                    "location" => FooterTemplateChunk::Location,
                    "date" => FooterTemplateChunk::Date,
                    _ => return Err(ParseFooterTemplateError::UnsupportedVariable(variable.to_string())),
                };
                chunks.push(chunk);
                in_variable = false;
                chunk_start = index + 1;
            }
        }
        if in_variable {
            return Err(ParseFooterTemplateError::TrailingBrace);
        } else if chunk_start != s.len() {
            chunks.push(FooterTemplateChunk::Literal(s[chunk_start..].to_string()));
        }
        Ok(Self(chunks))
    }
}

impl fmt::Display for FooterTemplate {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        use FooterTemplateChunk::*;
        for c in &self.0 {
            match c {
                Literal(l) => write!(f, "{l}"),
                OpenBrace => write!(f, "{{{{"),
                ClosedBrace => write!(f, "}}}}"),
                CurrentSlide => write!(f, "{{current_slide}}"),
                TotalSlides => write!(f, "{{total_slides}}"),
                Author => write!(f, "{{author}}"),
                Title => write!(f, "{{title}}"),
                SubTitle => write!(f, "{{sub_title}}"),
                Event => write!(f, "{{event}}"),
                Location => write!(f, "{{location}}"),
                Date => write!(f, "{{date}}"),
            }?;
        }
        Ok(())
    }
}

#[derive(Debug, thiserror::Error)]
pub(crate) enum ParseFooterTemplateError {
    #[error("found '{{' while already inside '{{' scope")]
    NestedOpenBrace,

    #[error("open '{{' was not closed")]
    TrailingBrace,

    #[error("found '}}' but no '{{' was found")]
    ClosedBraceWithoutOpen,

    #[error("unsupported variable: '{0}'")]
    UnsupportedVariable(String),
}

/// The style for a piece of code.
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
pub(crate) struct CodeBlockStyle {
    /// The alignment.
    #[serde(flatten)]
    pub(crate) alignment: Option<Alignment>,

    /// The padding.
    #[serde(default)]
    pub(crate) padding: PaddingRect,

    /// The syntect theme name to use.
    #[serde(default)]
    pub(crate) theme_name: Option<String>,

    /// Whether to use the theme's background color.
    pub(crate) background: Option<bool>,

    /// Whether to show line numbers in all code blocks.
    #[serde(default)]
    pub(crate) line_numbers: Option<bool>,
}

/// The style for the output of a code execution block.
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
pub(crate) struct ExecutionOutputBlockStyle {
    /// The colors to be used for the output pane.
    #[serde(default)]
    pub(crate) colors: RawColors,

    /// The colors to be used for the text that represents the status of the execution block.
    #[serde(default)]
    pub(crate) status: ExecutionStatusBlockStyle,

    /// The padding.
    #[serde(default)]
    pub(crate) padding: PaddingRect,
}

/// The style for the output of a code execution block running in pty mode.
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
pub(crate) struct PtyOutputBlockStyle {
    /// The colors to be used for the output pane.
    #[serde(default)]
    pub(crate) colors: RawColors,

    /// The style for the standby state.
    #[serde(default)]
    pub(crate) standby: Option<PtyStandbyStyle>,

    #[serde(default)]
    pub(crate) cursor: PtyCursorStyle,
}

/// The style for a PTY's cursor.
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
pub(crate) struct PtyCursorStyle {
    /// The symbol to use on the cursor.
    #[serde(default)]
    pub(crate) symbol: Option<char>,

    /// The colors used when the cursor is on top of non empty cells.
    #[serde(default)]
    pub(crate) highlight_colors: RawColors,
}

/// The style for the standby state.
#[derive(Clone, Debug, Deserialize, Serialize)]
pub(crate) enum PtyStandbyStyle {
    /// Show a play icon.
    LargePlay,
}

/// The style for the status of a code execution block.
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
pub(crate) struct ExecutionStatusBlockStyle {
    /// The colors for the "running" status.
    #[serde(default)]
    pub(crate) running: RawColors,

    /// The colors for the "finished" status.
    #[serde(default)]
    pub(crate) success: RawColors,

    /// The colors for the "finished with error" status.
    #[serde(default)]
    pub(crate) failure: RawColors,

    /// The colors for the "not started" status.
    #[serde(default)]
    pub(crate) not_started: RawColors,
}

#[derive(Clone, Debug, Default, Deserialize, Serialize)]
pub(crate) struct ModifierStyle {
    /// The colors to be used.
    #[serde(default)]
    pub(crate) colors: RawColors,
}

/// Vertical/horizontal padding.
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
pub(crate) struct PaddingRect {
    /// The number of columns to use as horizontal padding.
    #[serde(default)]
    pub(crate) horizontal: Option<u8>,

    /// The number of rows to use as vertical padding.
    #[serde(default)]
    pub(crate) vertical: Option<u8>,
}

/// A margin.
#[derive(Copy, Clone, Debug, Deserialize, Serialize, PartialEq)]
#[serde(rename_all = "snake_case")]
pub(crate) enum Margin {
    /// A fixed number of characters.
    Fixed(u16),

    /// A percent of the screen size.
    Percent(u16),
}

impl Margin {
    pub(crate) fn as_characters(&self, screen_size: u16) -> u16 {
        match *self {
            Self::Fixed(value) => value,
            Self::Percent(percent) => {
                let ratio = percent as f64 / 100.0;
                (screen_size as f64 * ratio).ceil() as u16
            }
        }
    }

    pub(crate) fn is_empty(&self) -> bool {
        matches!(self, Self::Fixed(0) | Self::Percent(0))
    }
}

impl Default for Margin {
    fn default() -> Self {
        Self::Fixed(0)
    }
}

/// Where to position the author's name in the intro slide.
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
#[serde(rename_all = "snake_case")]
pub(crate) enum AuthorPositioning {
    /// Right below the title.
    BelowTitle,

    /// At the bottom of the page.
    #[default]
    PageBottom,
}

/// Typst styles.
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
pub(crate) struct TypstStyle {
    /// The horizontal margin on the generated images.
    pub(crate) horizontal_margin: Option<u16>,

    /// The vertical margin on the generated images.
    pub(crate) vertical_margin: Option<u16>,

    /// The colors to be used.
    #[serde(default)]
    pub(crate) colors: RawColors,
}

/// Mermaid styles.
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
pub(crate) struct MermaidStyle {
    /// The mermaidjs theme to use.
    pub(crate) theme: Option<String>,

    /// The background color to use.
    pub(crate) background: Option<String>,
}

/// D2 styles.
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
pub(crate) struct D2Style {
    /// The d2 theme id to use.
    pub(crate) theme: Option<u32>,
}

/// Modals style.
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
pub(crate) struct ModalStyle {
    /// The default colors to use for everything in the modal.
    #[serde(default)]
    pub(crate) colors: RawColors,

    /// The colors to use for selected lines.
    #[serde(default)]
    pub(crate) selection_colors: RawColors,
}

/// Layout grid style.
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
pub(crate) struct LayoutGridStyle {
    /// The color for layout grids.
    #[serde(default)]
    pub(crate) color: Option<RawColor>,
}

/// The color palette.
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
pub(crate) struct ColorPalette {
    #[serde(default)]
    pub(crate) colors: BTreeMap<String, RawColor>,

    #[serde(default)]
    pub(crate) classes: BTreeMap<String, RawColors>,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) enum RawColor {
    Color(Color),
    Palette(String),
    ForegroundClass(String),
    BackgroundClass(String),
}

crate::utils::impl_deserialize_from_str!(RawColor);
crate::utils::impl_serialize_from_display!(RawColor);

impl RawColor {
    fn new_palette(name: &str) -> Result<Self, ParseColorError> {
        if name.is_empty() { Err(ParseColorError::PaletteColorEmpty) } else { Ok(Self::Palette(name.into())) }
    }

    pub(crate) fn resolve(
        &self,
        palette: &crate::theme::clean::ColorPalette,
    ) -> Result<Option<Color>, UndefinedPaletteColorError> {
        let color = match self {
            Self::Color(c) => Some(*c),
            Self::Palette(name) => {
                Some(palette.colors.get(name).copied().ok_or(UndefinedPaletteColorError(name.clone()))?)
            }
            Self::ForegroundClass(name) => {
                palette.classes.get(name).ok_or(UndefinedPaletteColorError(name.clone()))?.foreground
            }
            Self::BackgroundClass(name) => {
                palette.classes.get(name).ok_or(UndefinedPaletteColorError(name.clone()))?.background
            }
        };
        Ok(color)
    }
}

impl From<Color> for RawColor {
    fn from(color: Color) -> Self {
        Self::Color(color)
    }
}

impl FromStr for RawColor {
    type Err = ParseColorError;

    fn from_str(input: &str) -> Result<Self, Self::Err> {
        let output = match input {
            "black" => Color::Black.into(),
            "white" => Color::White.into(),
            "grey" => Color::Grey.into(),
            "dark_grey" => Color::DarkGrey.into(),
            "red" => Color::Red.into(),
            "dark_red" => Color::DarkRed.into(),
            "green" => Color::Green.into(),
            "dark_green" => Color::DarkGreen.into(),
            "blue" => Color::Blue.into(),
            "dark_blue" => Color::DarkBlue.into(),
            "yellow" => Color::Yellow.into(),
            "dark_yellow" => Color::DarkYellow.into(),
            "magenta" => Color::Magenta.into(),
            "dark_magenta" => Color::DarkMagenta.into(),
            "cyan" => Color::Cyan.into(),
            "dark_cyan" => Color::DarkCyan.into(),
            other if other.starts_with("palette:") => Self::new_palette(other.trim_start_matches("palette:"))?,
            other if other.starts_with("p:") => Self::new_palette(other.trim_start_matches("p:"))?,
            // Fallback to hex-encoded rgb
            _ => {
                let hex = match input.len() {
                    6 => input.to_string(),
                    3 => input.chars().flat_map(|c| [c, c]).collect::<String>(),
                    len => return Err(ParseColorError::InvalidHexLength(len)),
                };
                let values = <[u8; 3]>::from_hex(hex)?;
                Color::Rgb { r: values[0], g: values[1], b: values[2] }.into()
            }
        };
        Ok(output)
    }
}

impl fmt::Display for RawColor {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        use Color::*;
        match self {
            Self::Color(Rgb { r, g, b }) => write!(f, "{}", hex::encode([*r, *g, *b])),
            Self::Color(Black) => write!(f, "black"),
            Self::Color(White) => write!(f, "white"),
            Self::Color(Grey) => write!(f, "grey"),
            Self::Color(DarkGrey) => write!(f, "dark_grey"),
            Self::Color(Red) => write!(f, "red"),
            Self::Color(DarkRed) => write!(f, "dark_red"),
            Self::Color(Green) => write!(f, "green"),
            Self::Color(DarkGreen) => write!(f, "dark_green"),
            Self::Color(Blue) => write!(f, "blue"),
            Self::Color(DarkBlue) => write!(f, "dark_blue"),
            Self::Color(Yellow) => write!(f, "yellow"),
            Self::Color(DarkYellow) => write!(f, "dark_yellow"),
            Self::Color(Magenta) => write!(f, "magenta"),
            Self::Color(DarkMagenta) => write!(f, "dark_magenta"),
            Self::Color(Cyan) => write!(f, "cyan"),
            Self::Color(DarkCyan) => write!(f, "dark_cyan"),
            Self::Palette(name) => write!(f, "palette:{name}"),
            Self::ForegroundClass(_) => Err(fmt::Error),
            Self::BackgroundClass(_) => Err(fmt::Error),
        }
    }
}

#[derive(thiserror::Error, Debug)]
pub(crate) enum ParseColorError {
    #[error("invalid hex color: {0}")]
    Hex(#[from] FromHexError),

    #[error("hex color should only be 3 or 6 long, got hex string of length {0}")]
    InvalidHexLength(usize),

    #[error("palette color name is empty")]
    PaletteColorEmpty,
}

#[cfg(test)]
mod test {
    use super::*;
    use rstest::rstest;

    #[test]
    fn parse_all_footer_template_variables() {
        use FooterTemplateChunk::*;
        let raw = "hi {current_slide} {total_slides} {author} {title} {sub_title} {event} {location} {event}";
        let t: FooterTemplate = raw.parse().expect("invalid input");
        let expected = vec![
            Literal("hi ".into()),
            CurrentSlide,
            Literal(" ".into()),
            TotalSlides,
            Literal(" ".into()),
            Author,
            Literal(" ".into()),
            Title,
            Literal(" ".into()),
            SubTitle,
            Literal(" ".into()),
            Event,
            Literal(" ".into()),
            Location,
            Literal(" ".into()),
            Event,
        ];
        assert_eq!(t.0, expected);
        assert_eq!(t.to_string(), raw);
    }

    #[test]
    fn parse_double_braces() {
        use FooterTemplateChunk::*;
        let raw = "hi {{beep}} {{author}} {{{{}}}}";
        let t: FooterTemplate = raw.parse().expect("invalid input");
        let merged: String =
            t.0.into_iter()
                .map(|l| match l {
                    Literal(s) => s,
                    OpenBrace => "{".to_string(),
                    ClosedBrace => "}".to_string(),
                    _ => panic!("not a literal"),
                })
                .collect();
        assert_eq!(merged, "hi {beep} {author} {{}}");
    }

    #[rstest]
    #[case::trailing("{author")]
    #[case::close_without_open2("author}")]
    fn invalid_footer_templates(#[case] input: &str) {
        FooterTemplate::from_str(input).expect_err("parse succeeded");
    }

    #[test]
    fn color_serde() {
        let color: RawColor = "beef42".parse().unwrap();
        assert_eq!(color.to_string(), "beef42");

        let short_color: RawColor = "ded".parse().unwrap();
        assert_eq!(short_color.to_string(), "ddeedd");
    }

    #[rstest]
    #[case::empty1("p:")]
    #[case::empty2("palette:")]
    fn invalid_palette_color_names(#[case] input: &str) {
        RawColor::from_str(input).expect_err("not an error");
    }

    #[rstest]
    #[case::short("p:hi", "hi")]
    #[case::long("palette:bye", "bye")]
    fn valid_palette_color_names(#[case] input: &str, #[case] expected: &str) {
        let color = RawColor::from_str(input).expect("failed to parse");
        let RawColor::Palette(name) = color else { panic!("not a palette color") };
        assert_eq!(name, expected);
    }
}