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
use crossterm::event::KeyEvent;
use tty_interface::{Interface, Position};

use crate::{
    dependency::DependencyState,
    text::{DrawerContents, Segment},
    Form,
};

mod compound;
pub use compound::*;

mod keyvalue;
pub use keyvalue::*;

mod textblock;
pub use textblock::*;

mod yesno;
pub use yesno::*;

/// A distinct, vertically-separated phase of the form.
pub trait Step {
    /// Perform any post-configuration initialization actions for this step.
    fn initialize(&mut self, dependency_state: &mut DependencyState, index: usize);

    /// Render this step at the specified position and return the height of the rendered content.
    fn render(
        &self,
        interface: &mut Interface,
        dependency_state: &DependencyState,
        position: Position,
        is_focused: bool,
    ) -> u16;

    /// Handle the specified input event, optionally returning an instruction for the form.
    fn update(
        &mut self,
        dependency_state: &mut DependencyState,
        input: KeyEvent,
    ) -> Option<InputResult>;

    /// Retrieve this step's current help text.
    fn help(&self) -> Segment;

    /// Retrieve this step's current drawer contents, if applicable.
    fn drawer(&self) -> Option<DrawerContents>;

    /// Retrieves this step's final WYSIWYG result.
    fn result(&self, dependency_state: &DependencyState) -> String;

    /// Complete configuration and add this step to the form.
    fn add_to(self, form: &mut Form);
}

/// After processing an input event, an action may be returned to the form from the step.
pub enum InputResult {
    /// Advance the form to the next step.
    AdvanceForm,
    /// Retreat the form to the previous step.
    RetreatForm,
}