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

use crate::{
    dependency::DependencyState,
    style::{error_style, help_style},
    text::{set_segment_subset_style, DrawerContents, Segment, Text},
    utility::render_segment,
    Form,
};

use super::{InputResult, Step};

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

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

    /// Set this text block's top and bottom margins.
    pub fn set_margins(&mut self, top_margin: Option<u16>, bottom_margin: Option<u16>) {
        self.top_margin = top_margin;
        self.bottom_margin = bottom_margin;
    }

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

    /// Set whether this text block should trim trailing whitespace.
    pub fn set_trim_trailing_whitespace(&mut self, trim: bool) {
        self.trim_trailing_whitespace = trim;
    }
}

impl Step for TextBlockStep {
    fn initialize(&mut self, _dependency_state: &mut DependencyState, _index: usize) {}

    fn render(
        &self,
        interface: &mut Interface,
        _dependency_state: &DependencyState,
        position: Position,
        is_focused: bool,
    ) -> u16 {
        if !is_focused && self.text.value().is_empty() {
            return 1;
        }

        let mut offset_y = 0;
        if let Some(top_margin) = self.top_margin {
            for line in 0..top_margin {
                interface.clear_line(position.y() + line);
            }

            offset_y += top_margin;
        }

        let lines = self.text.lines();
        for (line_index, line) in lines.iter().enumerate() {
            let line_position = pos!(0, position.y() + line_index as u16 + offset_y);

            // If the line exceeds the max length, render the tail as an error
            if let Some(max_length) = self.max_line_length {
                let line_length = line.len() as u16;
                if line_length > max_length {
                    let mut segment = Text::new(line.to_string()).as_segment();

                    set_segment_subset_style(
                        &mut segment,
                        max_length.into(),
                        line_length.into(),
                        error_style(),
                    );

                    render_segment(interface, line_position, segment);
                    continue;
                }
            }

            interface.set(line_position, 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() + offset_y)));
        }

        if let Some(bottom_margin) = self.bottom_margin {
            for line in 0..bottom_margin {
                interface.clear_line(position.y() + line + offset_y + lines.len() as u16);
            }

            offset_y += bottom_margin;
        }

        lines.len() as u16 + offset_y
    }

    fn update(
        &mut self,
        _dependency_state: &mut DependencyState,
        input: KeyEvent,
    ) -> Option<InputResult> {
        // If there are two empty lines, advance the form
        if input.code == KeyCode::Enter || input.code == KeyCode::Tab {
            let lines = self.text.lines().to_vec();
            if lines.len() >= 2 {
                let last_lines_empty =
                    lines[lines.len() - 1].is_empty() && lines[lines.len() - 2].is_empty();

                if last_lines_empty {
                    // If we're trailing whitespace, delete the last two blank lines
                    if self.trim_trailing_whitespace {
                        self.text.handle_input(Key::Backspace);
                        self.text.handle_input(Key::Backspace);
                    }

                    return Some(InputResult::AdvanceForm);
                }
            }
        }

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

        match input.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 help(&self) -> Segment {
        Text::new_styled(self.prompt.to_string(), help_style()).as_segment()
    }

    fn drawer(&self) -> Option<DrawerContents> {
        None
    }

    fn result(&self, _dependency_state: &DependencyState) -> String {
        if self.text.value().is_empty() {
            return "\n".to_string();
        }

        let mut result = String::new();

        if let Some(top_margin) = self.top_margin {
            for _ in 0..top_margin {
                result.push('\n');
            }
        }

        result.push_str(&self.text.value());

        if let Some(bottom_margin) = self.bottom_margin {
            for _ in 0..bottom_margin + 1 {
                result.push('\n');
            }
        }

        result
    }

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