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

use crate::{Control, Form};

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

    /// Render this step at the specified position.
    fn render(&mut self, position: Position, interface: &mut Interface, is_focused: bool);

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

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

    /// Retrieves this step's final WYSIWYG result.
    fn get_result(&self) -> String;
}

/// 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,
}

/// A single-line step which controls multple controls including static and input elements.
///
/// # Examples
/// ```
/// use tty_form::{Form, Step, CompoundStep, Control, StaticText, TextInput};
///
/// let mut form = Form::new();
///
/// let mut step = CompoundStep::new();
/// StaticText::new("A Form - ").add_to_step(&mut step);
/// TextInput::new("Enter your name:", false).add_to_step(&mut step);
/// step.add_to_form(&mut form);
/// ```
pub struct CompoundStep {
    controls: Vec<Box<dyn Control>>,
    max_line_length: Option<u8>,
    active_control: usize,
}

impl CompoundStep {
    /// Create a new compound step with no controls.
    pub fn new() -> Self {
        Self {
            controls: Vec::new(),
            max_line_length: None,

            active_control: 0,
        }
    }

    /// Append the specified control to this step.
    pub fn add_control(&mut self, control: Box<dyn Control>) {
        self.controls.push(control);
    }

    /// Set this step's maximum total line length.
    pub fn set_max_line_length(&mut self, max_length: u8) {
        self.max_line_length = Some(max_length);
    }

    /// Advance the step's state to the next control. Returns true if we've reached the end of this
    /// step and the form should advance to the next.
    fn advance_control(&mut self) -> bool {
        loop {
            if self.active_control + 1 >= self.controls.len() {
                return true;
            }

            self.active_control += 1;

            if self.controls[self.active_control].is_focusable() {
                break;
            }
        }

        false
    }

    /// Retreat the step's state to the previous control. Returns true if we've reached the start
    /// of this step and the form should retreat to the previous.
    fn retreat_control(&mut self) -> bool {
        loop {
            if self.active_control == 0 {
                return true;
            }

            self.active_control -= 1;

            if self.controls[self.active_control].is_focusable() {
                break;
            }
        }

        false
    }
}

impl Step for CompoundStep {
    fn initialize(&mut self) {
        if !self.controls[0].is_focusable() {
            self.advance_control();
        }
    }

    fn render(&mut self, mut position: Position, interface: &mut Interface, is_focused: bool) {
        interface.clear_line(position.y());

        let mut cursor_position = None;
        for (control_index, control) in self.controls.iter().enumerate() {
            let (text, cursor_offset) = control.get_text();
            interface.set(position, &text);

            if control_index == self.active_control {
                if let Some(offset) = cursor_offset {
                    cursor_position = Some(pos!(position.x() + offset, position.y()));
                }
            }

            position = pos!(position.x() + text.len() as u16, position.y());
        }

        if is_focused {
            interface.set_cursor(cursor_position);
        }
    }

    fn handle_input(&mut self, key_event: KeyEvent) -> Option<InputResult> {
        match (key_event.modifiers, key_event.code) {
            (_, KeyCode::Enter) => {
                if self.advance_control() {
                    return Some(InputResult::AdvanceForm);
                }
            }
            (_, KeyCode::Esc) => {
                if self.retreat_control() {
                    return Some(InputResult::RetreatForm);
                }
            }
            _ => self.controls[self.active_control].handle_input(key_event),
        }

        None
    }

    fn add_to_form(self, form: &mut Form) {
        form.add_step(Box::new(self));
    }

    fn get_result(&self) -> String {
        let mut result = String::new();

        for control in &self.controls {
            let (text, _) = control.get_text();
            result.push_str(&text);
        }

        result.push('\n');

        result
    }
}

/// A multi-line text input step.
///
/// # Examples
/// ```
/// use tty_form::{Form, Step, TextBlockStep};
///
/// let mut form = Form::new();
///
/// let mut step = TextBlockStep::new("Enter your story:");
/// step.set_max_line_length(100);
/// step.add_to_form(&mut form);
/// ```
pub struct TextBlockStep {
    prompt: String,
    text: Text,
    max_line_length: Option<u8>,
}

impl TextBlockStep {
    /// Create a new, default text block step.
    pub fn new(prompt: &str) -> Self {
        Self {
            prompt: prompt.to_string(),
            text: Text::new(true),
            max_line_length: None,
        }
    }

    /// Set this text block step's optional maximum line grapheme length.
    pub fn set_max_line_length(&mut self, max_length: u8) {
        self.max_line_length = Some(max_length);
    }
}

impl Step for TextBlockStep {
    fn initialize(&mut self) {}

    fn render(&mut self, position: Position, interface: &mut Interface, is_focused: bool) {
        interface.set(position, &self.prompt);

        for (line_index, line) in self.text.lines().iter().enumerate() {
            interface.set(pos!(0, position.y() + line_index as u16 + 1), line);
        }

        if is_focused {
            let cursor = self.text.cursor();
            let (x, y) = (cursor.0 as u16, cursor.1 as u16);
            interface.set_cursor(Some(pos!(x, y + position.y() + 1)));
        }
    }

    fn handle_input(&mut self, event: KeyEvent) -> Option<InputResult> {
        if event.code == KeyCode::Enter {
            let mut last_two_empty = self.text.lines().iter().count() > 2;
            if last_two_empty {
                for line in self.text.lines().iter().rev().take(2) {
                    if !line.is_empty() {
                        last_two_empty = false;
                    }
                }
            }

            if last_two_empty {
                return Some(InputResult::AdvanceForm);
            }
        }

        if event.code == KeyCode::Esc {
            return Some(InputResult::RetreatForm);
        }

        match event.code {
            KeyCode::Enter => self.text.handle_input(Key::Enter),
            KeyCode::Char(ch) => self.text.handle_input(Key::Char(ch)),
            KeyCode::Backspace => self.text.handle_input(Key::Backspace),
            KeyCode::Up => self.text.handle_input(Key::Up),
            KeyCode::Down => self.text.handle_input(Key::Down),
            KeyCode::Left => self.text.handle_input(Key::Left),
            KeyCode::Right => self.text.handle_input(Key::Right),
            _ => {}
        };

        None
    }

    fn add_to_form(self, form: &mut Form) {
        form.add_step(Box::new(self));
    }

    fn get_result(&self) -> String {
        let mut result = self.text.value();
        result.push('\n');
        result
    }
}