tty_form/
control.rs

1use crossterm::event::KeyEvent;
2
3use crate::{
4    dependency::{Action, DependencyId, Evaluation},
5    step::CompoundStep,
6    text::{DrawerContents, Segment},
7};
8
9mod selectinput;
10pub use selectinput::*;
11
12mod statictext;
13pub use statictext::*;
14
15mod textinput;
16pub use textinput::*;
17
18/// An element of a [CompoundStep] which may be a focusable input.
19pub trait Control {
20    /// Whether this control is a focusable input.
21    fn focusable(&self) -> bool;
22
23    /// Updates the control's state from the given input event.
24    fn update(&mut self, input: KeyEvent);
25
26    /// This control's descriptive help text, if available.
27    fn help(&self) -> Option<Segment>;
28
29    /// This control's rendered contents and an optional offset for the cursor.
30    fn text(&self) -> (Segment, Option<u16>);
31
32    /// This control's drawer contents, if available.
33    fn drawer(&self) -> Option<DrawerContents>;
34
35    /// This control's dependency evaluation which other controls may react to.
36    fn evaluation(&self) -> Option<(DependencyId, Evaluation)>;
37
38    /// This control's dependency which it may react to.
39    fn dependency(&self) -> Option<(DependencyId, Action)>;
40
41    /// Perform an evaluation against this control's current state.
42    fn evaluate(&self, evaluation: &Evaluation) -> bool;
43
44    /// Finish configuration and add this control to the specified form step.
45    fn add_to(self, step: &mut CompoundStep);
46}