aboutsummaryrefslogtreecommitdiff
path: root/helix-term/src/editor.rs
blob: c4aa72e624f0fbb26ddfeeec5d09bc60c5e65251 (plain) (blame)
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
use anyhow::Error;
use termwiz::caps::Capabilities;
use termwiz::cell::AttributeChange;
use termwiz::color::{AnsiColor, ColorAttribute, RgbColor};
use termwiz::input::*;
use termwiz::surface::Change;
use termwiz::terminal::{buffered::BufferedTerminal, SystemTerminal, Terminal};
use termwiz::widgets::*;

use crate::Args;

use std::{env, path::PathBuf};

use helix_core::Buffer;

/// This is a widget for our application
pub struct MainScreen {}

impl MainScreen {
    pub fn new() -> Self {
        Self {}
    }
}

impl Widget for MainScreen {
    fn process_event(&mut self, event: &WidgetEvent, _args: &mut UpdateArgs) -> bool {
        true // handled it all
    }

    /// Draw ourselves into the surface provided by RenderArgs
    fn render(&mut self, args: &mut RenderArgs) {
        // args.surface.add_change(Change::ClearScreen(
        //     ColorAttribute::TrueColorWithPaletteFallback(
        //         RgbColor::new(0x31, 0x1B, 0x92),
        //         AnsiColor::Black.into(),
        //     ),
        // ));
        // args.surface
        //     .add_change(Change::Attribute(AttributeChange::Foreground(
        //         ColorAttribute::TrueColorWithPaletteFallback(
        //             RgbColor::new(0xB3, 0x88, 0xFF),
        //             AnsiColor::Purple.into(),
        //         ),
        //     )));
    }

    fn get_size_constraints(&self) -> layout::Constraints {
        let mut constraints = layout::Constraints::default();
        constraints.child_orientation = layout::ChildOrientation::Vertical;
        constraints
    }
}

pub struct BufferComponent<'a> {
    text: String,
    buffer: &'a mut Buffer,

    first_line: usize,
}

impl<'a> BufferComponent<'a> {
    /// Initialize the widget with the input text
    pub fn new(buffer: &'a mut Buffer) -> Self {
        Self {
            buffer,
            text: String::new(),

            first_line: 0,
        }
    }
}

impl<'a> Widget for BufferComponent<'a> {
    fn process_event(&mut self, event: &WidgetEvent, _args: &mut UpdateArgs) -> bool {
        match event {
            WidgetEvent::Input(InputEvent::Key(KeyEvent {
                key: KeyCode::Char('k'),
                ..
            })) => {
                self.first_line = self.first_line.saturating_sub(1);
            }
            WidgetEvent::Input(InputEvent::Key(KeyEvent {
                key: KeyCode::Char('j'),
                ..
            })) => {
                self.first_line = self.first_line.saturating_add(1);
            }
            WidgetEvent::Input(InputEvent::Key(KeyEvent {
                key: KeyCode::Enter,
                ..
            })) => {
                self.text.push_str("\r\n");
            }
            WidgetEvent::Input(InputEvent::Paste(s)) => {
                self.text.push_str(&s);
            }
            _ => {}
        }

        true // handled it all
    }

    /// Draw ourselves into the surface provided by RenderArgs
    fn render(&mut self, args: &mut RenderArgs) {
        args.surface
            .add_change(Change::ClearScreen(ColorAttribute::Default));

        // args.surface
        //     .add_change(Change::Attribute(AttributeChange::Foreground(
        //         ColorAttribute::TrueColorWithPaletteFallback(
        //             RgbColor::new(0x11, 0x00, 0xFF),
        //             AnsiColor::Purple.into(),
        //         ),
        //     )));
        let (_width, height) = args.surface.dimensions();

        for line in self.buffer.contents.lines_at(self.first_line).take(height) {
            args.surface
                .add_change(unsafe { String::from_utf8_unchecked(line.bytes().collect()) });
            args.surface.add_change("\r");
        }
        // args.surface
        //     .add_change(format!("🤷 surface size is {:?}\r\n", dims));
        // args.surface.add_change(self.text.clone());

        // Place the cursor at the end of the text.
        // A more advanced text editing widget would manage the
        // cursor position differently.
        *args.cursor = CursorShapeAndPosition {
            coords: args.surface.cursor_position().into(),
            shape: termwiz::surface::CursorShape::SteadyBar,
            ..Default::default()
        };
    }

    fn get_size_constraints(&self) -> layout::Constraints {
        let mut c = layout::Constraints::default();
        c.set_valign(layout::VerticalAlignment::Top);
        c
    }
}

pub struct StatusLine {}

impl StatusLine {
    pub fn new() -> Self {
        StatusLine {}
    }
}
impl Widget for StatusLine {
    fn process_event(&mut self, event: &WidgetEvent, _args: &mut UpdateArgs) -> bool {
        true
    }

    fn render(&mut self, args: &mut RenderArgs) {
        args.surface.add_change(Change::ClearScreen(
            ColorAttribute::TrueColorWithPaletteFallback(
                RgbColor::new(0xFF, 0xFF, 0xFF),
                AnsiColor::Black.into(),
            ),
        ));
        args.surface
            .add_change(Change::Attribute(AttributeChange::Foreground(
                ColorAttribute::TrueColorWithPaletteFallback(
                    RgbColor::new(0x00, 0x00, 0x00),
                    AnsiColor::Black.into(),
                ),
            )));

        args.surface.add_change(" helix");
    }

    fn get_size_constraints(&self) -> layout::Constraints {
        *layout::Constraints::default()
            .set_fixed_height(1)
            .set_valign(layout::VerticalAlignment::Bottom)
    }
}

pub struct Editor {
    terminal: BufferedTerminal<SystemTerminal>,

    buffer: Option<Buffer>,
}

impl Editor {
    pub fn new(mut args: Args) -> Result<Self, Error> {
        // Create a terminal
        let caps = Capabilities::new_from_env()?;
        let mut terminal = BufferedTerminal::new(SystemTerminal::new(caps)?)?;

        let mut editor = Editor {
            terminal,
            buffer: None,
        };

        if let Some(file) = args.files.pop() {
            editor.open(file)?;
        }

        Ok(editor)
    }

    pub fn open(&mut self, path: PathBuf) -> Result<(), Error> {
        let buffer = Buffer::load(path)?;
        self.buffer = Some(buffer);
        Ok(())
    }

    pub fn run(&mut self) -> Result<(), Error> {
        // Start with an empty string; typing into the app will
        // update this string.
        let mut typed_text = String::new();

        {
            let buf = &mut self.terminal;
            // Put the terminal in raw mode + alternate screen
            buf.terminal().enter_alternate_screen()?;
            buf.terminal().set_raw_mode()?;

            // Set up the UI
            let mut ui = Ui::new();

            let root_id = ui.set_root(MainScreen::new());
            let buffer_id =
                ui.add_child(root_id, BufferComponent::new(self.buffer.as_mut().unwrap()));
            // let root_id = ui.set_root(Buffer::new(&mut typed_text));
            ui.add_child(root_id, StatusLine::new());
            ui.set_focus(buffer_id);

            loop {
                ui.process_event_queue()?;

                // After updating and processing all of the widgets, compose them
                // and render them to the screen.
                if ui.render_to_screen(buf)? {
                    // We have more events to process immediately; don't block waiting
                    // for input below, but jump to the top of the loop to re-run the
                    // updates.
                    continue;
                }
                // Compute an optimized delta to apply to the terminal and display it
                buf.flush()?;

                // Wait for user input
                match buf.terminal().poll_input(None) {
                    Ok(Some(InputEvent::Resized { rows, cols })) => {
                        // FIXME: this is working around a bug where we don't realize
                        // that we should redraw everything on resize in BufferedTerminal.
                        buf.add_change(Change::ClearScreen(Default::default()));
                        buf.resize(cols, rows);
                    }
                    Ok(Some(input)) => match input {
                        InputEvent::Key(KeyEvent {
                            key: KeyCode::Escape,
                            ..
                        }) => {
                            // Quit the app when escape is pressed
                            break;
                        }
                        input @ _ => {
                            // Feed input into the Ui
                            ui.queue_event(WidgetEvent::Input(input));
                        }
                    },
                    Ok(None) => {}
                    Err(e) => {
                        print!("{:?}\r\n", e);
                        break;
                    }
                }
            }
        }

        // After we've stopped the full screen raw terminal,
        // print out the final edited value of the input text.
        println!("The text you entered: {}", typed_text);
        Ok(())
    }

    pub fn render(&self) {
        // create a new window sized surface
        // paint all components
        // diff vs last frame, swap
        // paint diff
    }
}