2020-12-13 04:23:50 +01:00
|
|
|
use crate::compositor::{Component, Context, EventResult};
|
2020-12-11 10:25:09 +01:00
|
|
|
use crossterm::event::{Event, KeyCode, KeyEvent, KeyModifiers};
|
2020-12-13 05:29:34 +01:00
|
|
|
use helix_core::Position;
|
2020-12-11 10:25:09 +01:00
|
|
|
use helix_view::Editor;
|
2020-12-13 04:23:50 +01:00
|
|
|
use helix_view::Theme;
|
2020-10-09 22:55:45 +02:00
|
|
|
use std::string::String;
|
|
|
|
|
|
|
|
pub struct Prompt {
|
2020-10-16 09:58:26 +02:00
|
|
|
pub prompt: String,
|
|
|
|
pub line: String,
|
|
|
|
pub cursor: usize,
|
2020-11-03 10:57:12 +01:00
|
|
|
pub completion: Vec<String>,
|
2020-10-19 19:39:35 +02:00
|
|
|
pub should_close: bool,
|
2020-10-20 23:02:02 +02:00
|
|
|
pub completion_selection_index: Option<usize>,
|
2020-11-03 10:57:12 +01:00
|
|
|
completion_fn: Box<dyn FnMut(&str) -> Vec<String>>,
|
2020-10-16 07:37:12 +02:00
|
|
|
callback_fn: Box<dyn FnMut(&mut Editor, &str)>,
|
2020-10-09 22:55:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Prompt {
|
2020-10-15 12:08:01 +02:00
|
|
|
pub fn new(
|
2020-10-16 09:58:26 +02:00
|
|
|
prompt: String,
|
2020-11-03 10:57:12 +01:00
|
|
|
mut completion_fn: impl FnMut(&str) -> Vec<String> + 'static,
|
2020-10-16 07:37:12 +02:00
|
|
|
callback_fn: impl FnMut(&mut Editor, &str) + 'static,
|
2020-10-15 12:08:01 +02:00
|
|
|
) -> Prompt {
|
|
|
|
Prompt {
|
2020-10-16 09:58:26 +02:00
|
|
|
prompt,
|
|
|
|
line: String::new(),
|
|
|
|
cursor: 0,
|
2020-10-24 13:36:34 +02:00
|
|
|
completion: completion_fn(""),
|
2020-10-19 19:39:35 +02:00
|
|
|
should_close: false,
|
2020-10-20 23:02:02 +02:00
|
|
|
completion_selection_index: None,
|
2020-10-15 12:08:01 +02:00
|
|
|
completion_fn: Box::new(completion_fn),
|
|
|
|
callback_fn: Box::new(callback_fn),
|
|
|
|
}
|
2020-10-09 22:55:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn insert_char(&mut self, c: char) {
|
2020-10-16 09:58:26 +02:00
|
|
|
self.line.insert(self.cursor, c);
|
|
|
|
self.cursor += 1;
|
2020-10-20 23:02:02 +02:00
|
|
|
self.completion = (self.completion_fn)(&self.line);
|
2020-10-24 14:06:10 +02:00
|
|
|
self.exit_selection();
|
2020-10-09 22:55:45 +02:00
|
|
|
}
|
2020-10-13 00:23:48 +02:00
|
|
|
|
2020-10-13 19:10:50 +02:00
|
|
|
pub fn move_char_left(&mut self) {
|
2020-11-02 10:41:27 +01:00
|
|
|
if self.cursor > 0 {
|
2020-10-16 09:58:26 +02:00
|
|
|
self.cursor -= 1;
|
2020-10-13 18:57:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-13 19:10:50 +02:00
|
|
|
pub fn move_char_right(&mut self) {
|
2020-10-16 09:58:26 +02:00
|
|
|
if self.cursor < self.line.len() {
|
|
|
|
self.cursor += 1;
|
2020-10-13 18:57:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-13 19:10:50 +02:00
|
|
|
pub fn move_start(&mut self) {
|
2020-10-16 09:58:26 +02:00
|
|
|
self.cursor = 0;
|
2020-10-13 19:10:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn move_end(&mut self) {
|
2020-10-16 09:58:26 +02:00
|
|
|
self.cursor = self.line.len();
|
2020-10-13 19:10:50 +02:00
|
|
|
}
|
|
|
|
|
2020-10-13 18:57:55 +02:00
|
|
|
pub fn delete_char_backwards(&mut self) {
|
2020-10-16 09:58:26 +02:00
|
|
|
if self.cursor > 0 {
|
|
|
|
self.line.remove(self.cursor - 1);
|
|
|
|
self.cursor -= 1;
|
2020-10-20 23:02:02 +02:00
|
|
|
self.completion = (self.completion_fn)(&self.line);
|
|
|
|
}
|
2020-10-24 14:06:10 +02:00
|
|
|
self.exit_selection();
|
2020-10-20 23:02:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn change_completion_selection(&mut self) {
|
2020-11-13 00:07:21 +01:00
|
|
|
if self.completion.is_empty() {
|
|
|
|
return;
|
2020-11-02 10:41:27 +01:00
|
|
|
}
|
2020-11-13 00:07:21 +01:00
|
|
|
let index =
|
|
|
|
self.completion_selection_index.map(|i| i + 1).unwrap_or(0) % self.completion.len();
|
|
|
|
self.completion_selection_index = Some(index);
|
|
|
|
self.line = self.completion[index].clone();
|
2020-10-24 14:06:10 +02:00
|
|
|
}
|
|
|
|
pub fn exit_selection(&mut self) {
|
|
|
|
self.completion_selection_index = None;
|
2020-10-13 18:57:55 +02:00
|
|
|
}
|
2020-12-11 10:25:09 +01:00
|
|
|
}
|
|
|
|
|
2020-12-13 04:23:50 +01:00
|
|
|
use tui::{
|
|
|
|
buffer::Buffer as Surface,
|
|
|
|
layout::Rect,
|
|
|
|
style::{Color, Modifier, Style},
|
|
|
|
};
|
|
|
|
|
|
|
|
const BASE_WIDTH: u16 = 30;
|
2020-12-13 05:35:30 +01:00
|
|
|
use crate::ui::text_color;
|
2020-12-13 04:23:50 +01:00
|
|
|
|
|
|
|
impl Prompt {
|
|
|
|
pub fn render_prompt(&self, area: Rect, surface: &mut Surface, theme: &Theme) {
|
2020-12-13 05:35:30 +01:00
|
|
|
let text_color = text_color();
|
2020-12-13 04:23:50 +01:00
|
|
|
// completion
|
|
|
|
if !self.completion.is_empty() {
|
|
|
|
// TODO: find out better way of clearing individual lines of the screen
|
|
|
|
let mut row = 0;
|
|
|
|
let mut col = 0;
|
|
|
|
let max_col = area.width / BASE_WIDTH;
|
|
|
|
let col_height = ((self.completion.len() as u16 + max_col - 1) / max_col);
|
|
|
|
|
|
|
|
for i in (3..col_height + 3) {
|
|
|
|
surface.set_string(
|
|
|
|
0,
|
|
|
|
area.height - i as u16,
|
|
|
|
" ".repeat(area.width as usize),
|
2020-12-13 05:35:30 +01:00
|
|
|
text_color,
|
2020-12-13 04:23:50 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
surface.set_style(
|
|
|
|
Rect::new(0, area.height - col_height - 2, area.width, col_height),
|
|
|
|
theme.get("ui.statusline"),
|
|
|
|
);
|
|
|
|
for (i, command) in self.completion.iter().enumerate() {
|
|
|
|
let color = if self.completion_selection_index.is_some()
|
|
|
|
&& i == self.completion_selection_index.unwrap()
|
|
|
|
{
|
|
|
|
Style::default().bg(Color::Rgb(104, 060, 232))
|
|
|
|
} else {
|
2020-12-13 05:35:30 +01:00
|
|
|
text_color
|
2020-12-13 04:23:50 +01:00
|
|
|
};
|
|
|
|
surface.set_stringn(
|
|
|
|
1 + col * BASE_WIDTH,
|
|
|
|
area.height - col_height - 2 + row,
|
|
|
|
&command,
|
|
|
|
BASE_WIDTH as usize - 1,
|
|
|
|
color,
|
|
|
|
);
|
|
|
|
row += 1;
|
|
|
|
if row > col_height - 1 {
|
|
|
|
row = 0;
|
|
|
|
col += 1;
|
|
|
|
}
|
|
|
|
if col > max_col {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// render buffer text
|
2020-12-13 05:35:30 +01:00
|
|
|
surface.set_string(1, area.height - 1, &self.prompt, text_color);
|
|
|
|
surface.set_string(2, area.height - 1, &self.line, text_color);
|
2020-12-13 04:23:50 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-11 10:25:09 +01:00
|
|
|
impl Component for Prompt {
|
|
|
|
fn handle_event(&mut self, event: Event, cx: &mut Context) -> EventResult {
|
|
|
|
let event = match event {
|
|
|
|
Event::Key(event) => event,
|
|
|
|
_ => return EventResult::Ignored,
|
|
|
|
};
|
2020-10-13 18:57:55 +02:00
|
|
|
|
2020-12-11 10:25:09 +01:00
|
|
|
match event {
|
2020-10-13 00:23:48 +02:00
|
|
|
KeyEvent {
|
|
|
|
code: KeyCode::Char(c),
|
2020-10-13 19:10:50 +02:00
|
|
|
modifiers: KeyModifiers::NONE,
|
2020-10-13 00:23:48 +02:00
|
|
|
} => self.insert_char(c),
|
|
|
|
KeyEvent {
|
|
|
|
code: KeyCode::Esc, ..
|
2020-10-19 19:39:35 +02:00
|
|
|
} => self.should_close = true,
|
2020-10-13 18:57:55 +02:00
|
|
|
KeyEvent {
|
|
|
|
code: KeyCode::Right,
|
|
|
|
..
|
2020-10-13 19:10:50 +02:00
|
|
|
} => self.move_char_right(),
|
2020-10-13 18:57:55 +02:00
|
|
|
KeyEvent {
|
|
|
|
code: KeyCode::Left,
|
|
|
|
..
|
2020-10-13 19:10:50 +02:00
|
|
|
} => self.move_char_left(),
|
|
|
|
KeyEvent {
|
|
|
|
code: KeyCode::Char('e'),
|
|
|
|
modifiers: KeyModifiers::CONTROL,
|
|
|
|
} => self.move_end(),
|
|
|
|
KeyEvent {
|
|
|
|
code: KeyCode::Char('a'),
|
|
|
|
modifiers: KeyModifiers::CONTROL,
|
|
|
|
} => self.move_start(),
|
2020-10-13 18:57:55 +02:00
|
|
|
KeyEvent {
|
|
|
|
code: KeyCode::Backspace,
|
2020-10-24 14:06:10 +02:00
|
|
|
modifiers: KeyModifiers::NONE,
|
2020-10-13 18:57:55 +02:00
|
|
|
} => self.delete_char_backwards(),
|
2020-10-15 12:08:01 +02:00
|
|
|
KeyEvent {
|
|
|
|
code: KeyCode::Enter,
|
|
|
|
..
|
2020-12-11 10:25:09 +01:00
|
|
|
} => (self.callback_fn)(cx.editor, &self.line),
|
2020-10-19 16:16:00 +02:00
|
|
|
KeyEvent {
|
|
|
|
code: KeyCode::Tab, ..
|
2020-10-20 23:02:02 +02:00
|
|
|
} => self.change_completion_selection(),
|
2020-10-24 14:06:10 +02:00
|
|
|
KeyEvent {
|
|
|
|
code: KeyCode::Char('q'),
|
|
|
|
modifiers: KeyModifiers::CONTROL,
|
|
|
|
} => self.exit_selection(),
|
2020-10-13 00:23:48 +02:00
|
|
|
_ => (),
|
2020-12-11 10:25:09 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
EventResult::Consumed(None)
|
|
|
|
}
|
|
|
|
|
2020-12-13 04:23:50 +01:00
|
|
|
fn render(&self, area: Rect, surface: &mut Surface, cx: &mut Context) {
|
|
|
|
self.render_prompt(area, surface, &cx.editor.theme)
|
2020-10-13 00:23:48 +02:00
|
|
|
}
|
2020-12-13 05:29:34 +01:00
|
|
|
|
|
|
|
fn cursor_position(&self, area: Rect, ctx: &mut Context) -> Option<Position> {
|
|
|
|
Some(Position::new(
|
|
|
|
area.height as usize - 1,
|
|
|
|
area.x as usize + 2 + self.cursor,
|
|
|
|
))
|
|
|
|
}
|
2020-10-09 22:55:45 +02:00
|
|
|
}
|