tty_form/control/
statictext.rs

1use crossterm::event::KeyEvent;
2use tty_interface::Style;
3
4use crate::{
5    dependency::{Action, DependencyId, Evaluation},
6    step::CompoundStep,
7    text::{DrawerContents, Segment, Text},
8};
9
10use super::Control;
11
12/// Static, unfocusable, formatable display text. May be dependent on other form elements.
13///
14/// # Examples
15/// ```
16/// use tty_interface::Style;
17///
18/// use tty_form::{
19///     step::CompoundStep,
20///     control::{Control, StaticText},
21/// };
22///
23/// let mut text = StaticText::new("Hello, world!");
24/// text.set_style(Style::new().set_bold(true));
25///
26/// let mut step = CompoundStep::new();
27/// text.add_to(&mut step);
28/// ```
29pub struct StaticText {
30    text: String,
31    style: Option<Style>,
32    dependency: Option<(DependencyId, Action)>,
33}
34
35impl StaticText {
36    /// Create a new static text control with the specified content.
37    pub fn new(text: &str) -> Self {
38        Self {
39            text: text.to_string(),
40            style: None,
41            dependency: None,
42        }
43    }
44
45    /// Set the text for this control.
46    pub fn set_text(&mut self, text: &str) {
47        self.text = text.to_string();
48    }
49
50    /// Set the optional style for this control.
51    pub fn set_style(&mut self, style: Style) {
52        self.style = Some(style);
53    }
54
55    /// Sets a dependency on the specified ID, performing some action if it evaluates true.
56    pub fn set_dependency(&mut self, id: DependencyId, action: Action) {
57        self.dependency = Some((id, action));
58    }
59}
60
61impl Control for StaticText {
62    fn focusable(&self) -> bool {
63        false
64    }
65
66    fn update(&mut self, _input: KeyEvent) {}
67
68    fn help(&self) -> Option<Segment> {
69        None
70    }
71
72    fn text(&self) -> (Segment, Option<u16>) {
73        (Text::new(self.text.to_string()).as_segment(), None)
74    }
75
76    fn drawer(&self) -> Option<DrawerContents> {
77        None
78    }
79
80    fn evaluation(&self) -> Option<(DependencyId, Evaluation)> {
81        None
82    }
83
84    fn dependency(&self) -> Option<(DependencyId, Action)> {
85        self.dependency.clone()
86    }
87
88    fn evaluate(&self, _evaluation: &Evaluation) -> bool {
89        false
90    }
91
92    fn add_to(self, step: &mut CompoundStep) {
93        step.add_control(Box::new(self));
94    }
95}