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

added closures to prompt

This commit is contained in:
Jan Hrastnik 2020-10-15 12:08:01 +02:00 committed by Blaž Hrastnik
parent 3c0f187c5b
commit fa55b1e51c
2 changed files with 31 additions and 18 deletions

View File

@ -36,7 +36,8 @@ pub struct Editor {
size: (u16, u16),
surface: Surface,
cache: Surface,
prompt: Prompt,
prompt: Option<Prompt>,
should_close: bool,
}
impl Editor {
@ -46,7 +47,6 @@ 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,
@ -55,7 +55,8 @@ pub fn new(mut args: Args) -> Result<Self, Error> {
surface: Surface::empty(area),
cache: Surface::empty(area),
// TODO; move to state
prompt,
prompt: None,
should_close: false,
};
if let Some(file) = args.values_of_t::<PathBuf>("files").unwrap().pop() {
@ -65,6 +66,15 @@ pub fn new(mut args: Args) -> Result<Self, Error> {
Ok(editor)
}
pub fn set_commands(self) {
let commands = |input: &str| match input {
"q" => self.should_close = true,
_ => (),
};
let prompt = Prompt::new(|input| None, commands);
self.prompt = Some(prompt);
}
pub fn open(&mut self, path: PathBuf) -> Result<(), Error> {
self.view = Some(View::open(path, self.size)?);
Ok(())
@ -304,10 +314,14 @@ pub async fn event_loop(&mut self) {
let mut reader = EventStream::new();
let keymap = keymap::default();
self.set_commands();
self.render();
loop {
// Handle key events
if self.should_close {
break;
}
let mut event = reader.next().await;
match event {
Some(Ok(Event::Resize(width, height))) => {
@ -324,13 +338,6 @@ pub async fn event_loop(&mut self) {
self.render();
}
Some(Ok(Event::Key(KeyEvent {
code: KeyCode::Char('q'),
..
}))) => {
break;
}
Some(Ok(Event::Key(event))) => {
// TODO: sequences (`gg`)
// TODO: handle count other than 1

View File

@ -6,15 +6,21 @@
pub struct Prompt {
pub buffer: String,
pub cursor_loc: usize,
completion_fn: Box<dyn FnMut(&str) -> Option<Vec<&str>>>,
callback_fn: Box<dyn Fn(&str)>,
}
impl Prompt {
pub fn new() -> Prompt {
let prompt = Prompt {
pub fn new(
completion_fn: impl FnMut(&str) -> Option<Vec<&str>> + 'static,
callback_fn: impl Fn(&str) + 'static,
) -> Prompt {
Prompt {
buffer: String::from(""),
cursor_loc: 0,
};
prompt
completion_fn: Box::new(completion_fn),
callback_fn: Box::new(callback_fn),
}
}
pub fn insert_char(&mut self, c: char) {
@ -49,10 +55,6 @@ pub fn delete_char_backwards(&mut self) {
}
}
pub fn success_fn() {
// TODO:
}
pub fn handle_input(&mut self, key_event: KeyEvent, view: &mut View) {
match key_event {
KeyEvent {
@ -82,6 +84,10 @@ pub fn handle_input(&mut self, key_event: KeyEvent, view: &mut View) {
code: KeyCode::Backspace,
..
} => self.delete_char_backwards(),
KeyEvent {
code: KeyCode::Enter,
..
} => (self.callback_fn)(&self.buffer),
_ => (),
}
}