on master help sign in
created prompt.rs
commitc60f1a655333f41460e21a2e193734864f43211c authorJan Hrastnik <jan.hrastnik2@gmail.com> dateFri 9 Oct 2020, 22:55 (6 years ago) parent9e7b646
4 files changed+45−6
helix-term/src/editor.rs+18−5 helix-view/src/commands.rs+8−1 helix-view/src/lib.rs+1 helix-view/src/prompt.rs+18
helix-term/src/editor.rs+18−5
@@ -1,6 +1,6 @@
11use clap::ArgMatches as Args;
22use 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};
44
55use std::{
66 borrow::Cow,
@@ -36,6 +36,7 @@ pub struct Editor {
3636 size: (u16, u16),
3737 surface: Surface,
3838 cache: Surface,
39+ prompt: Prompt,
3940}
4041
4142impl Editor {
@@ -45,6 +46,7 @@ impl Editor {
4546 let mut terminal = Terminal::new(backend)?;
4647 let size = terminal::size().unwrap();
4748 let area = Rect::new(0, 0, size.0, size.1);
49+ let prompt = Prompt::new();
4850
4951 let mut editor = Editor {
5052 terminal,
@@ -52,6 +54,8 @@ impl Editor {
5254 size,
5355 surface: Surface::empty(area),
5456 cache: Surface::empty(area),
57+ // TODO; move to state
58+ prompt,
5559 };
5660
5761 if let Some(file) = args.values_of_t::<PathBuf>("files").unwrap().pop() {
@@ -264,8 +268,14 @@ impl Editor {
264268 Rect::new(0, self.size.1 - 1, self.size.0, 1),
265269 view.theme.get("ui.statusline"),
266270 );
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+
267276 self.surface
268277 .set_string(1, self.size.1 - 1, mode, text_color);
278+
269279 self.terminal
270280 .backend_mut()
271281 .draw(self.cache.diff(&self.surface).into_iter());
@@ -282,7 +292,7 @@ impl Editor {
282292 mode => write!(stdout, "\x1B[2 q"),
283293 };
284294 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());
286296 } else {
287297 if let Some(path) = view.state.path() {
288298 self.surface
@@ -349,12 +359,16 @@ impl Editor {
349359 commands::insert::insert_char(view, c);
350360 }
351361 view.ensure_cursor_in_view();
352− self.render();
353362 }
354363 Mode::Command => {
355364 if let Some(command) = keymap[&Mode::Command].get(&keys) {
356365 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)
358372 }
359373 }
360374 mode => {
@@ -363,7 +377,6 @@ impl Editor {
363377
364378 // TODO: simplistic ensure cursor in view for now
365379 view.ensure_cursor_in_view();
366− self.render();
367380 }
368381 }
369382 }
helix-view/src/commands.rs+8−1
@@ -8,7 +8,10 @@ use helix_core::{
88};
99use once_cell::sync::Lazy;
1010
11−use crate::view::{View, PADDING};
11+use crate::{
12+ prompt::Prompt,
13+ view::{View, PADDING},
14+};
1215
1316/// A command is a function that takes the current state and a count, and does a side-effect on the
1417/// state (usually by creating and applying a transaction).
@@ -478,6 +481,10 @@ pub mod insert {
478481 }
479482}
480483
484+pub fn insert_char_prompt(prompt: &mut Prompt, c: char) {
485+ prompt.insert_char(c);
486+}
487+
481488// Undo / Redo
482489
483490pub fn undo(view: &mut View, _count: usize) {
helix-view/src/lib.rs+1
@@ -1,5 +1,6 @@
11pub mod commands;
22pub mod keymap;
3+pub mod prompt;
34pub mod theme;
45pub mod view;
56
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+}