1
0
Fork 0
mirror of https://github.com/helix-editor/helix synced 2024-06-08 10:06:04 +02:00

created prompt.rs

This commit is contained in:
Jan Hrastnik 2020-10-09 22:55:45 +02:00 committed by Blaž Hrastnik
parent 9e7b6465c6
commit c60f1a6553
4 changed files with 45 additions and 6 deletions

View File

@ -1,6 +1,6 @@
use clap::ArgMatches as Args;
use helix_core::{indent::TAB_WIDTH, state::Mode, syntax::HighlightEvent, Position, Range, State};
use helix_view::{commands, keymap, View};
use helix_view::{commands, keymap, prompt::Prompt, View};
use std::{
borrow::Cow,
@ -36,6 +36,7 @@ pub struct Editor {
size: (u16, u16),
surface: Surface,
cache: Surface,
prompt: Prompt,
}
impl Editor {
@ -45,6 +46,7 @@ pub fn new(mut args: Args) -> Result<Self, Error> {
let mut terminal = Terminal::new(backend)?;
let size = terminal::size().unwrap();
let area = Rect::new(0, 0, size.0, size.1);
let prompt = Prompt::new();
let mut editor = Editor {
terminal,
@ -52,6 +54,8 @@ pub fn new(mut args: Args) -> Result<Self, Error> {
size,
surface: Surface::empty(area),
cache: Surface::empty(area),
// TODO; move to state
prompt,
};
if let Some(file) = args.values_of_t::<PathBuf>("files").unwrap().pop() {
@ -264,8 +268,14 @@ pub fn render_prompt(&mut self, text_color: Style) {
Rect::new(0, self.size.1 - 1, self.size.0, 1),
view.theme.get("ui.statusline"),
);
// render buffer text
let buffer_string = &self.prompt.buffer;
self.surface
.set_string(2, self.size.1 - 1, buffer_string, text_color);
self.surface
.set_string(1, self.size.1 - 1, mode, text_color);
self.terminal
.backend_mut()
.draw(self.cache.diff(&self.surface).into_iter());
@ -282,7 +292,7 @@ pub fn render_cursor(&mut self, viewport: Rect, text_color: Style) {
mode => write!(stdout, "\x1B[2 q"),
};
if view.state.mode() == Mode::Command {
pos = Position::new(self.size.0 as usize, 2);
pos = Position::new(self.size.0 as usize, 2 + self.prompt.buffer.len());
} else {
if let Some(path) = view.state.path() {
self.surface
@ -349,12 +359,16 @@ pub async fn event_loop(&mut self) {
commands::insert::insert_char(view, c);
}
view.ensure_cursor_in_view();
self.render();
}
Mode::Command => {
if let Some(command) = keymap[&Mode::Command].get(&keys) {
command(view, 1);
self.render();
} else if let KeyEvent {
code: KeyCode::Char(c),
..
} = event
{
commands::insert_char_prompt(&mut self.prompt, c)
}
}
mode => {
@ -363,7 +377,6 @@ pub async fn event_loop(&mut self) {
// TODO: simplistic ensure cursor in view for now
view.ensure_cursor_in_view();
self.render();
}
}
}

View File

@ -8,7 +8,10 @@
};
use once_cell::sync::Lazy;
use crate::view::{View, PADDING};
use crate::{
prompt::Prompt,
view::{View, PADDING},
};
/// A command is a function that takes the current state and a count, and does a side-effect on the
/// state (usually by creating and applying a transaction).
@ -478,6 +481,10 @@ pub fn delete_char_forward(view: &mut View, count: usize) {
}
}
pub fn insert_char_prompt(prompt: &mut Prompt, c: char) {
prompt.insert_char(c);
}
// Undo / Redo
pub fn undo(view: &mut View, _count: usize) {

View File

@ -1,5 +1,6 @@
pub mod commands;
pub mod keymap;
pub mod prompt;
pub mod theme;
pub mod view;

18
helix-view/src/prompt.rs Normal file
View File

@ -0,0 +1,18 @@
use std::string::String;
pub struct Prompt {
pub buffer: String,
}
impl Prompt {
pub fn new() -> Prompt {
let prompt = Prompt {
buffer: String::from(""),
};
prompt
}
pub fn insert_char(&mut self, c: char) {
self.buffer.push(c);
}
}