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
use crate::{
    code::execute::SnippetExecutor,
    commands::{
        listener::{Command, CommandListener},
        speaker_notes::{SpeakerNotesEvent, SpeakerNotesEventPublisher},
    },
    config::{KeyBindingsConfig, SlideTransitionConfig, SlideTransitionStyleConfig},
    markdown::parse::MarkdownParser,
    presentation::{
        Presentation, Slide,
        builder::{PresentationBuilder, PresentationBuilderOptions, Themes, error::BuildError},
        diff::PresentationDiffer,
        poller::{PollableEffect, Poller, PollerCommand},
    },
    render::{
        ErrorSource, RenderError, RenderResult, TerminalDrawer, TerminalDrawerOptions,
        ascii_scaler::AsciiScaler,
        engine::{MaxSize, RenderEngine, RenderEngineOptions},
        operation::{Pollable, RenderAsyncStartPolicy, RenderOperation},
        properties::WindowSize,
        validate::OverflowValidator,
    },
    resource::Resources,
    terminal::{
        image::printer::{ImagePrinter, ImageRegistry},
        printer::{TerminalCommand, TerminalIo},
        virt::{ImageBehavior, TerminalGrid, VirtualTerminal},
    },
    theme::{ProcessingThemeError, raw::PresentationTheme},
    third_party::ThirdPartyRender,
    transitions::{
        AnimateTransition, AnimationFrame, LinesFrame, TransitionDirection,
        collapse_horizontal::CollapseHorizontalAnimation, fade::FadeAnimation,
        slide_horizontal::SlideHorizontalAnimation,
    },
};
use std::{
    fmt::Display,
    io::{self},
    mem,
    ops::Deref,
    path::Path,
    sync::Arc,
    time::{Duration, Instant},
};

pub struct PresenterOptions {
    pub mode: PresentMode,
    pub builder_options: PresentationBuilderOptions,
    pub font_size_fallback: u8,
    pub bindings: KeyBindingsConfig,
    pub validate_overflows: bool,
    pub max_size: MaxSize,
    pub transition: Option<SlideTransitionConfig>,
}

/// A slideshow presenter.
///
/// This type puts everything else together.
pub struct Presenter<'a> {
    default_theme: &'a PresentationTheme,
    listener: CommandListener,
    parser: MarkdownParser<'a>,
    resources: Resources,
    third_party: ThirdPartyRender,
    code_executor: Arc<SnippetExecutor>,
    state: PresenterState,
    image_printer: Arc<ImagePrinter>,
    themes: Themes,
    options: PresenterOptions,
    speaker_notes_event_publisher: Option<SpeakerNotesEventPublisher>,
    poller: Poller,
}

impl<'a> Presenter<'a> {
    /// Construct a new presenter.
    #[allow(clippy::too_many_arguments)]
    pub fn new(
        default_theme: &'a PresentationTheme,
        listener: CommandListener,
        parser: MarkdownParser<'a>,
        resources: Resources,
        third_party: ThirdPartyRender,
        code_executor: Arc<SnippetExecutor>,
        themes: Themes,
        image_printer: Arc<ImagePrinter>,
        options: PresenterOptions,
        speaker_notes_event_publisher: Option<SpeakerNotesEventPublisher>,
    ) -> Self {
        Self {
            default_theme,
            listener,
            parser,
            resources,
            third_party,
            code_executor,
            state: PresenterState::Empty,
            image_printer,
            themes,
            options,
            speaker_notes_event_publisher,
            poller: Poller::launch(),
        }
    }

    /// Run a presentation.
    pub fn present(mut self, path: &Path) -> Result<(), PresentationError> {
        if matches!(self.options.mode, PresentMode::Development) {
            self.resources.watch_presentation_file(path.to_path_buf());
        }
        self.state = PresenterState::Presenting(Presentation::from(vec![]));
        self.try_reload(path, true)?;

        let drawer_options = TerminalDrawerOptions {
            font_size_fallback: self.options.font_size_fallback,
            max_size: self.options.max_size.clone(),
        };
        let mut drawer = TerminalDrawer::new(self.image_printer.clone(), drawer_options)?;
        loop {
            // Poll async renders once before we draw just in case.
            self.render(&mut drawer)?;

            loop {
                if self.process_poller_effects()? {
                    self.render(&mut drawer)?;
                }

                let command = match self.listener.try_next_command()? {
                    Some(command) => command,
                    _ => match self.resources.resources_modified() {
                        true => Command::Reload,
                        false => {
                            if self.check_async_error() {
                                break;
                            }
                            continue;
                        }
                    },
                };
                match self.apply_command(command) {
                    CommandSideEffect::Exit => {
                        self.publish_event(SpeakerNotesEvent::Exit)?;
                        return Ok(());
                    }
                    CommandSideEffect::Suspend => {
                        self.suspend(&mut drawer);
                        break;
                    }
                    CommandSideEffect::Reload => {
                        self.try_reload(path, false)?;
                        break;
                    }
                    CommandSideEffect::Redraw => {
                        self.try_scale_transition_images()?;
                        break;
                    }
                    CommandSideEffect::AnimateNextSlide => {
                        self.animate_next_slide(&mut drawer)?;
                        break;
                    }
                    CommandSideEffect::AnimatePreviousSlide => {
                        self.animate_previous_slide(&mut drawer)?;
                        break;
                    }
                    CommandSideEffect::None => (),
                };
            }

            if !matches!(self.state, PresenterState::Failure { .. }) {
                let slide_index = self.state.presentation().current_slide_index() as u32 + 1;
                let slide = self.state.presentation().current_slide();
                self.publish_event(SpeakerNotesEvent::GoTo {
                    slide: slide_index,
                    chunk: slide.current_chunk_index() as u32,
                })?;
            }
        }
    }

    fn process_poller_effects(&mut self) -> Result<bool, PresentationError> {
        let current_slide = match &self.state {
            PresenterState::Presenting(presentation)
            | PresenterState::SlideIndex(presentation)
            | PresenterState::KeyBindings(presentation)
            | PresenterState::Failure { presentation, .. } => presentation.current_slide_index(),
            PresenterState::Empty => usize::MAX,
        };
        let mut refreshed = false;
        let mut needs_render = false;
        while let Some(effect) = self.poller.next_effect() {
            match effect {
                PollableEffect::RefreshSlide(index) => {
                    needs_render = needs_render || index == current_slide;
                    refreshed = true;
                }
                PollableEffect::DisplayError { slide, error } => {
                    let presentation = mem::take(&mut self.state).into_presentation();
                    self.state =
                        PresenterState::failure(error, presentation, ErrorSource::Slide(slide + 1), FailureMode::Other);
                    needs_render = true;
                }
            }
        }
        if refreshed {
            self.try_scale_transition_images()?;
        }
        Ok(needs_render)
    }

    fn publish_event(&self, event: SpeakerNotesEvent) -> io::Result<()> {
        if let Some(publisher) = &self.speaker_notes_event_publisher {
            publisher.send(event)?;
        }
        Ok(())
    }

    fn check_async_error(&mut self) -> bool {
        let error_holder = self.state.presentation().state.async_error_holder();
        let error_holder = error_holder.lock().unwrap();
        match error_holder.deref() {
            Some(error) => {
                let presentation = mem::take(&mut self.state).into_presentation();
                self.state = PresenterState::failure(
                    &error.error,
                    presentation,
                    ErrorSource::Slide(error.slide),
                    FailureMode::Other,
                );
                true
            }
            None => false,
        }
    }

    fn render(&mut self, drawer: &mut TerminalDrawer) -> RenderResult {
        let result = match &self.state {
            PresenterState::Presenting(presentation) => {
                drawer.render_operations(presentation.current_slide().iter_visible_operations())
            }
            PresenterState::SlideIndex(presentation) => {
                drawer.render_operations(presentation.current_slide().iter_visible_operations())?;
                drawer.render_operations(presentation.iter_slide_index_operations())
            }
            PresenterState::KeyBindings(presentation) => {
                drawer.render_operations(presentation.current_slide().iter_visible_operations())?;
                drawer.render_operations(presentation.iter_bindings_operations())
            }
            PresenterState::Failure { error, source, .. } => drawer.render_error(error, source),
            PresenterState::Empty => panic!("cannot render without state"),
        };
        // If the screen is too small, simply ignore this. Eventually the user will resize the
        // screen.
        if matches!(result, Err(RenderError::TerminalTooSmall)) { Ok(()) } else { result }
    }

    fn apply_command(&mut self, command: Command) -> CommandSideEffect {
        // These ones always happens no matter our state.
        match command {
            Command::Reload => {
                return CommandSideEffect::Reload;
            }
            Command::HardReload => {
                if matches!(self.options.mode, PresentMode::Development) {
                    self.resources.clear();
                }
                return CommandSideEffect::Reload;
            }
            Command::ToggleLayoutGrid => {
                self.options.builder_options.layout_grid = !self.options.builder_options.layout_grid;
                return CommandSideEffect::Reload;
            }
            Command::Exit => return CommandSideEffect::Exit,
            Command::Suspend => return CommandSideEffect::Suspend,
            _ => (),
        };
        if matches!(command, Command::Redraw) {
            if !self.is_displaying_other_error() {
                let presentation = mem::take(&mut self.state).into_presentation();
                self.state = self.validate_overflows(presentation);
            }
            return CommandSideEffect::Redraw;
        }

        // Now apply the commands that require a presentation.
        let presentation = match &mut self.state {
            PresenterState::Presenting(presentation)
            | PresenterState::SlideIndex(presentation)
            | PresenterState::KeyBindings(presentation) => presentation,
            _ => {
                return CommandSideEffect::None;
            }
        };
        let needs_redraw = match command {
            Command::Next => {
                let current_slide = presentation.current_slide_index();
                if !presentation.jump_next() {
                    false
                } else if presentation.current_slide_index() != current_slide {
                    return CommandSideEffect::AnimateNextSlide;
                } else {
                    true
                }
            }
            Command::NextFast => presentation.jump_next_fast(),
            Command::Previous => {
                let current_slide = presentation.current_slide_index();
                if !presentation.jump_previous() {
                    false
                } else if presentation.current_slide_index() != current_slide {
                    return CommandSideEffect::AnimatePreviousSlide;
                } else {
                    true
                }
            }
            Command::PreviousFast => presentation.jump_previous_fast(),
            Command::FirstSlide => presentation.jump_first_slide(),
            Command::LastSlide => presentation.jump_last_slide(),
            Command::GoToSlide(number) => presentation.go_to_slide(number.saturating_sub(1) as usize),
            Command::GoToSlideChunk { slide, chunk } => {
                presentation.go_to_slide(slide.saturating_sub(1) as usize);
                presentation.current_slide_mut().jump_chunk(chunk as usize);
                true
            }
            Command::RenderAsyncOperations => {
                let pollables = Self::trigger_slide_async_renders(presentation);
                if !pollables.is_empty() {
                    for pollable in pollables {
                        self.poller.send(PollerCommand::Poll { pollable, slide: presentation.current_slide_index() });
                    }
                    return CommandSideEffect::Redraw;
                } else {
                    return CommandSideEffect::None;
                }
            }
            Command::ToggleSlideIndex => {
                self.toggle_slide_index();
                true
            }
            Command::ToggleKeyBindingsConfig => {
                self.toggle_key_bindings();
                true
            }
            Command::CloseModal => {
                let presentation = mem::take(&mut self.state).into_presentation();
                self.state = PresenterState::Presenting(presentation);
                true
            }
            Command::SkipPauses => {
                presentation.show_all_slide_chunks();
                true
            }
            // These are handled above as they don't require the presentation
            Command::Reload
            | Command::HardReload
            | Command::Exit
            | Command::Suspend
            | Command::Redraw
            | Command::ToggleLayoutGrid => {
                panic!("unreachable commands")
            }
        };
        if needs_redraw { CommandSideEffect::Redraw } else { CommandSideEffect::None }
    }

    fn try_reload(&mut self, path: &Path, force: bool) -> RenderResult {
        if matches!(self.options.mode, PresentMode::Presentation) && !force {
            return Ok(());
        }
        self.poller.send(PollerCommand::Reset);
        self.resources.clear_watches();
        match self.load_presentation(path) {
            Ok(mut presentation) => {
                let current = self.state.presentation();
                if let Some(modification) = PresentationDiffer::find_first_modification(current, &presentation) {
                    presentation.go_to_slide(modification.slide_index);
                    presentation.jump_chunk(modification.chunk_index);
                } else {
                    presentation.go_to_slide(current.current_slide_index());
                    presentation.jump_chunk(current.current_chunk());
                }
                self.start_automatic_async_renders(&mut presentation);
                self.state = self.validate_overflows(presentation);
                self.try_scale_transition_images()?;
            }
            Err(e) => {
                let presentation = mem::take(&mut self.state).into_presentation();
                self.state = PresenterState::failure(e, presentation, ErrorSource::Presentation, FailureMode::Other);
            }
        };
        Ok(())
    }

    fn try_scale_transition_images(&self) -> RenderResult {
        if self.options.transition.is_none() {
            return Ok(());
        }
        let options = RenderEngineOptions { max_size: self.options.max_size.clone(), ..Default::default() };
        let scaler = AsciiScaler::new(options);
        let dimensions = WindowSize::current(self.options.font_size_fallback)?;
        scaler.process(self.state.presentation(), &dimensions)?;
        Ok(())
    }

    fn trigger_slide_async_renders(presentation: &mut Presentation) -> Vec<Box<dyn Pollable>> {
        let slide = presentation.current_slide_mut();
        let mut pollables = Vec::new();
        for operation in slide.iter_visible_operations_mut() {
            if let RenderOperation::RenderAsync(operation) = operation {
                if let RenderAsyncStartPolicy::OnDemand = operation.start_policy() {
                    pollables.push(operation.pollable());
                }
            }
        }
        pollables
    }

    fn is_displaying_other_error(&self) -> bool {
        matches!(self.state, PresenterState::Failure { mode: FailureMode::Other, .. })
    }

    fn validate_overflows(&self, presentation: Presentation) -> PresenterState {
        if self.options.validate_overflows {
            let dimensions = match WindowSize::current(self.options.font_size_fallback) {
                Ok(dimensions) => dimensions,
                Err(e) => {
                    return PresenterState::failure(e, presentation, ErrorSource::Presentation, FailureMode::Other);
                }
            };
            match OverflowValidator::validate(&presentation, dimensions) {
                Ok(()) => PresenterState::Presenting(presentation),
                Err(e) => PresenterState::failure(e, presentation, ErrorSource::Presentation, FailureMode::Overflow),
            }
        } else {
            PresenterState::Presenting(presentation)
        }
    }

    fn load_presentation(&mut self, path: &Path) -> Result<Presentation, LoadPresentationError> {
        let presentation = PresentationBuilder::new(
            self.default_theme,
            self.resources.clone(),
            &mut self.third_party,
            self.code_executor.clone(),
            &self.themes,
            ImageRegistry::new(self.image_printer.clone()),
            self.options.bindings.clone(),
            &self.parser,
            self.options.builder_options.clone(),
        )?
        .build(path)?;
        Ok(presentation)
    }

    fn toggle_slide_index(&mut self) {
        let state = mem::take(&mut self.state);
        match state {
            PresenterState::Presenting(presentation) | PresenterState::KeyBindings(presentation) => {
                self.state = PresenterState::SlideIndex(presentation)
            }
            PresenterState::SlideIndex(presentation) => self.state = PresenterState::Presenting(presentation),
            other => self.state = other,
        }
    }

    fn toggle_key_bindings(&mut self) {
        let state = mem::take(&mut self.state);
        match state {
            PresenterState::Presenting(presentation) | PresenterState::SlideIndex(presentation) => {
                self.state = PresenterState::KeyBindings(presentation)
            }
            PresenterState::KeyBindings(presentation) => self.state = PresenterState::Presenting(presentation),
            other => self.state = other,
        }
    }

    fn suspend(&self, drawer: &mut TerminalDrawer) {
        #[cfg(unix)]
        unsafe {
            drawer.terminal.suspend();
            libc::raise(libc::SIGTSTP);
            drawer.terminal.resume();
        }
    }

    fn animate_next_slide(&mut self, drawer: &mut TerminalDrawer) -> RenderResult {
        let Some(config) = self.options.transition.clone() else {
            return Ok(());
        };

        let options = drawer.render_engine_options();
        let presentation = self.state.presentation_mut();
        let dimensions = WindowSize::current(self.options.font_size_fallback)?;
        presentation.jump_previous();
        let left = Self::virtual_render(presentation.current_slide(), dimensions, &options)?;
        presentation.jump_next();
        let right = Self::virtual_render(presentation.current_slide(), dimensions, &options)?;
        let direction = TransitionDirection::Next;
        self.animate_transition(drawer, left, right, direction, dimensions, config)
    }

    fn animate_previous_slide(&mut self, drawer: &mut TerminalDrawer) -> RenderResult {
        let Some(config) = self.options.transition.clone() else {
            return Ok(());
        };

        let options = drawer.render_engine_options();
        let presentation = self.state.presentation_mut();
        let dimensions = WindowSize::current(self.options.font_size_fallback)?;
        presentation.jump_next();

        // Re-borrow to avoid calling fns above while mutably borrowing
        let presentation = self.state.presentation_mut();

        let right = Self::virtual_render(presentation.current_slide(), dimensions, &options)?;
        presentation.jump_previous();
        let left = Self::virtual_render(presentation.current_slide(), dimensions, &options)?;
        let direction = TransitionDirection::Previous;
        self.animate_transition(drawer, left, right, direction, dimensions, config)
    }

    fn animate_transition(
        &mut self,
        drawer: &mut TerminalDrawer,
        left: TerminalGrid,
        right: TerminalGrid,
        direction: TransitionDirection,
        dimensions: WindowSize,
        config: SlideTransitionConfig,
    ) -> RenderResult {
        let first = match &direction {
            TransitionDirection::Next => left.clone(),
            TransitionDirection::Previous => right.clone(),
        };
        match &config.animation {
            SlideTransitionStyleConfig::SlideHorizontal => self.run_animation(
                drawer,
                first,
                SlideHorizontalAnimation::new(left, right, dimensions, direction),
                config,
            ),
            SlideTransitionStyleConfig::Fade => {
                self.run_animation(drawer, first, FadeAnimation::new(left, right, direction), config)
            }
            SlideTransitionStyleConfig::CollapseHorizontal => {
                self.run_animation(drawer, first, CollapseHorizontalAnimation::new(left, right, direction), config)
            }
        }
    }

    fn run_animation<T>(
        &mut self,
        drawer: &mut TerminalDrawer,
        first: TerminalGrid,
        animation: T,
        config: SlideTransitionConfig,
    ) -> RenderResult
    where
        T: AnimateTransition,
    {
        let total_time = Duration::from_millis(config.duration_millis as u64);
        let frames: usize = config.frames;
        let total_frames = animation.total_frames();
        let step = total_time / (frames as u32 * 2);
        let mut last_frame_index = 0;
        let mut frame_index = 1;
        // Render the first frame as text to have images as ascii
        Self::render_frame(&LinesFrame::from(&first).build_commands(), drawer)?;
        while frame_index < total_frames {
            let start = Instant::now();
            let frame = animation.build_frame(frame_index, last_frame_index);
            let commands = frame.build_commands();
            Self::render_frame(&commands, drawer)?;

            let elapsed = start.elapsed();
            let sleep_needed = step.saturating_sub(elapsed);
            if sleep_needed.as_millis() > 0 {
                std::thread::sleep(step);
            }
            last_frame_index = frame_index;
            frame_index += total_frames.div_ceil(frames);
        }
        Ok(())
    }

    fn render_frame(commands: &[TerminalCommand<'_>], drawer: &mut TerminalDrawer) -> RenderResult {
        drawer.terminal.execute(&TerminalCommand::BeginUpdate)?;
        for command in commands {
            drawer.terminal.execute(command)?;
        }
        drawer.terminal.execute(&TerminalCommand::EndUpdate)?;
        drawer.terminal.execute(&TerminalCommand::Flush)?;
        Ok(())
    }

    fn virtual_render(
        slide: &Slide,
        dimensions: WindowSize,
        options: &RenderEngineOptions,
    ) -> Result<TerminalGrid, RenderError> {
        let mut term = VirtualTerminal::new(dimensions, ImageBehavior::PrintAscii);
        let engine = RenderEngine::new(&mut term, dimensions, options.clone());
        engine.render(slide.iter_visible_operations())?;
        Ok(term.into_contents())
    }

    fn start_automatic_async_renders(&self, presentation: &mut Presentation) {
        for (index, slide) in presentation.iter_slides_mut().enumerate() {
            for operation in slide.iter_operations_mut() {
                if let RenderOperation::RenderAsync(operation) = operation {
                    if let RenderAsyncStartPolicy::Automatic = operation.start_policy() {
                        let pollable = operation.pollable();
                        self.poller.send(PollerCommand::Poll { pollable, slide: index });
                    }
                }
            }
        }
    }
}

enum CommandSideEffect {
    Exit,
    Suspend,
    Redraw,
    Reload,
    AnimateNextSlide,
    AnimatePreviousSlide,
    None,
}

#[derive(Default)]
enum PresenterState {
    #[default]
    Empty,
    Presenting(Presentation),
    SlideIndex(Presentation),
    KeyBindings(Presentation),
    Failure {
        error: String,
        presentation: Presentation,
        source: ErrorSource,
        mode: FailureMode,
    },
}

impl PresenterState {
    pub(crate) fn failure<E: Display>(
        error: E,
        presentation: Presentation,
        source: ErrorSource,
        mode: FailureMode,
    ) -> Self {
        PresenterState::Failure { error: error.to_string(), presentation, source, mode }
    }

    fn presentation(&self) -> &Presentation {
        match self {
            Self::Presenting(presentation)
            | Self::SlideIndex(presentation)
            | Self::KeyBindings(presentation)
            | Self::Failure { presentation, .. } => presentation,
            Self::Empty => panic!("state is empty"),
        }
    }

    fn presentation_mut(&mut self) -> &mut Presentation {
        match self {
            Self::Presenting(presentation)
            | Self::SlideIndex(presentation)
            | Self::KeyBindings(presentation)
            | Self::Failure { presentation, .. } => presentation,
            Self::Empty => panic!("state is empty"),
        }
    }

    fn into_presentation(self) -> Presentation {
        match self {
            Self::Presenting(presentation)
            | Self::SlideIndex(presentation)
            | Self::KeyBindings(presentation)
            | Self::Failure { presentation, .. } => presentation,
            Self::Empty => panic!("state is empty"),
        }
    }
}

enum FailureMode {
    Overflow,
    Other,
}

/// This presentation mode.
pub enum PresentMode {
    /// We are developing the presentation so we want live reloads when the input changes.
    Development,

    /// This is a live presentation so we don't want hot reloading.
    Presentation,
}

/// An error when loading a presentation.
#[derive(thiserror::Error, Debug)]
pub enum LoadPresentationError {
    #[error(transparent)]
    Processing(#[from] BuildError),

    #[error("processing theme: {0}")]
    ProcessingTheme(#[from] ProcessingThemeError),
}

/// An error during the presentation.
#[derive(thiserror::Error, Debug)]
pub enum PresentationError {
    #[error(transparent)]
    Render(#[from] RenderError),

    #[error("io: {0}")]
    Io(#[from] io::Error),
}