on master help sign in
created prompt.rs
c60f1a6Jan Hrastnik
helix-term/src/editor.rs+18−5
@@ -1,6 +1,6 @@
1use clap::ArgMatches as Args;
1use clap::ArgMatches as Args;
2use helix_core::{indent::TAB_WIDTH, state::Mode, syntax::HighlightEvent, Position, Range, State};
2use helix_core::{indent::TAB_WIDTH, state::Mode, syntax::HighlightEvent, Position, Range, State};
3−use helix_view::{commands, keymap, View};
3+use helix_view::{commands, keymap, prompt::Prompt, View};
4
4
5use std::{
5use std::{
6 borrow::Cow,
6 borrow::Cow,
@@ -36,6 +36,7 @@ pub struct Editor {
36 size: (u16, u16),
36 size: (u16, u16),
37 surface: Surface,
37 surface: Surface,
38 cache: Surface,
38 cache: Surface,
39+ prompt: Prompt,
39}
40}
40
41
41impl Editor {
42impl Editor {
@@ -45,6 +46,7 @@ impl Editor {
45 let mut terminal = Terminal::new(backend)?;
46 let mut terminal = Terminal::new(backend)?;
46 let size = terminal::size().unwrap();
47 let size = terminal::size().unwrap();
47 let area = Rect::new(0, 0, size.0, size.1);
48 let area = Rect::new(0, 0, size.0, size.1);
49+ let prompt = Prompt::new();
48
50
49 let mut editor = Editor {
51 let mut editor = Editor {
50 terminal,
52 terminal,
@@ -52,6 +54,8 @@ impl Editor {
52 size,
54 size,
53 surface: Surface::empty(area),
55 surface: Surface::empty(area),
54 cache: Surface::empty(area),
56 cache: Surface::empty(area),
57+ // TODO; move to state
58+ prompt,
55 };
59 };
56
60
57 if let Some(file) = args.values_of_t::<PathBuf>("files").unwrap().pop() {
61 if let Some(file) = args.values_of_t::<PathBuf>("files").unwrap().pop() {
@@ -264,8 +268,14 @@ impl Editor {
264 Rect::new(0, self.size.1 - 1, self.size.0, 1),
268 Rect::new(0, self.size.1 - 1, self.size.0, 1),
265 view.theme.get("ui.statusline"),
269 view.theme.get("ui.statusline"),
266 );
270 );
271+ // render buffer text
272+ let buffer_string = &self.prompt.buffer;
273+ self.surface
274+ .set_string(2, self.size.1 - 1, buffer_string, text_color);
275+
267 self.surface
276 self.surface
268 .set_string(1, self.size.1 - 1, mode, text_color);
277 .set_string(1, self.size.1 - 1, mode, text_color);
278+
269 self.terminal
279 self.terminal
270 .backend_mut()
280 .backend_mut()
271 .draw(self.cache.diff(&self.surface).into_iter());
281 .draw(self.cache.diff(&self.surface).into_iter());
@@ -282,7 +292,7 @@ impl Editor {
282 mode => write!(stdout, "\x1B[2 q"),
292 mode => write!(stdout, "\x1B[2 q"),
283 };
293 };
284 if view.state.mode() == Mode::Command {
294 if view.state.mode() == Mode::Command {
285− pos = Position::new(self.size.0 as usize, 2);
295+ pos = Position::new(self.size.0 as usize, 2 + self.prompt.buffer.len());
286 } else {
296 } else {
287 if let Some(path) = view.state.path() {
297 if let Some(path) = view.state.path() {
288 self.surface
298 self.surface
@@ -349,12 +359,16 @@ impl Editor {
349 commands::insert::insert_char(view, c);
359 commands::insert::insert_char(view, c);
350 }
360 }
351 view.ensure_cursor_in_view();
361 view.ensure_cursor_in_view();
352− self.render();
353 }
362 }
354 Mode::Command => {
363 Mode::Command => {
355 if let Some(command) = keymap[&Mode::Command].get(&keys) {
364 if let Some(command) = keymap[&Mode::Command].get(&keys) {
356 command(view, 1);
365 command(view, 1);
357− self.render();
366+ } else if let KeyEvent {
367+ code: KeyCode::Char(c),
368+ ..
369+ } = event
370+ {
371+ commands::insert_char_prompt(&mut self.prompt, c)
358 }
372 }
359 }
373 }
360 mode => {
374 mode => {
@@ -363,7 +377,6 @@ impl Editor {
363
377
364 // TODO: simplistic ensure cursor in view for now
378 // TODO: simplistic ensure cursor in view for now
365 view.ensure_cursor_in_view();
379 view.ensure_cursor_in_view();
366− self.render();
367 }
380 }
368 }
381 }
369 }
382 }
helix-view/src/commands.rs+8−1
@@ -8,7 +8,10 @@ use helix_core::{
8};
8};
9use once_cell::sync::Lazy;
9use once_cell::sync::Lazy;
10
10
11+use crate::{
12+ prompt::Prompt,
11−use crate::view::{View, PADDING};
13+ view::{View, PADDING},
14+};
12
15
13/// A command is a function that takes the current state and a count, and does a side-effect on the
16/// A command is a function that takes the current state and a count, and does a side-effect on the
14/// state (usually by creating and applying a transaction).
17/// state (usually by creating and applying a transaction).
@@ -478,6 +481,10 @@ pub mod insert {
478 }
481 }
479}
482}
480
483
484+pub fn insert_char_prompt(prompt: &mut Prompt, c: char) {
485+ prompt.insert_char(c);
486+}
487+
481// Undo / Redo
488// Undo / Redo
482
489
483pub fn undo(view: &mut View, _count: usize) {
490pub fn undo(view: &mut View, _count: usize) {
helix-view/src/lib.rs+1
@@ -1,5 +1,6 @@
1pub mod commands;
1pub mod commands;
2pub mod keymap;
2pub mod keymap;
3+pub mod prompt;
3pub mod theme;
4pub mod theme;
4pub mod view;
5pub mod view;
5
6
helix-view/src/prompt.rsnew file+18
@@ -0,0 +1,18 @@
1+use std::string::String;
2+
3+pub struct Prompt {
4+ pub buffer: String,
5+}
6+
7+impl Prompt {
8+ pub fn new() -> Prompt {
9+ let prompt = Prompt {
10+ buffer: String::from(""),
11+ };
12+ prompt
13+ }
14+
15+ pub fn insert_char(&mut self, c: char) {
16+ self.buffer.push(c);
17+ }
18+}