tty_form/
step.rs

1use crossterm::event::KeyEvent;
2use tty_interface::{Interface, Position};
3
4use crate::{
5    dependency::DependencyState,
6    text::{DrawerContents, Segment},
7    Form,
8};
9
10mod compound;
11pub use compound::*;
12
13mod keyvalue;
14pub use keyvalue::*;
15
16mod textblock;
17pub use textblock::*;
18
19mod yesno;
20pub use yesno::*;
21
22/// A distinct, vertically-separated phase of the form.
23pub trait Step {
24    /// Perform any post-configuration initialization actions for this step.
25    fn initialize(&mut self, dependency_state: &mut DependencyState, index: usize);
26
27    /// Render this step at the specified position and return the height of the rendered content.
28    fn render(
29        &self,
30        interface: &mut Interface,
31        dependency_state: &DependencyState,
32        position: Position,
33        is_focused: bool,
34    ) -> u16;
35
36    /// Handle the specified input event, optionally returning an instruction for the form.
37    fn update(
38        &mut self,
39        dependency_state: &mut DependencyState,
40        input: KeyEvent,
41    ) -> Option<InputResult>;
42
43    /// Retrieve this step's current help text.
44    fn help(&self) -> Segment;
45
46    /// Retrieve this step's current drawer contents, if applicable.
47    fn drawer(&self) -> Option<DrawerContents>;
48
49    /// Retrieves this step's final WYSIWYG result.
50    fn result(&self, dependency_state: &DependencyState) -> String;
51
52    /// Complete configuration and add this step to the form.
53    fn add_to(self, form: &mut Form);
54}
55
56/// After processing an input event, an action may be returned to the form from the step.
57pub enum InputResult {
58    /// Advance the form to the next step.
59    AdvanceForm,
60    /// Retreat the form to the previous step.
61    RetreatForm,
62}