mirror of https://github.com/helix-editor/helix
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
213 lines
6.4 KiB
Rust
213 lines
6.4 KiB
Rust
4 years ago
|
use crate::compositor::{Component, Context, EventResult};
|
||
4 years ago
|
use crossterm::event::{Event, KeyCode, KeyEvent, KeyModifiers};
|
||
4 years ago
|
use helix_core::Position;
|
||
4 years ago
|
use helix_view::Editor;
|
||
4 years ago
|
use helix_view::Theme;
|
||
4 years ago
|
use std::string::String;
|
||
|
|
||
|
pub struct Prompt {
|
||
4 years ago
|
pub prompt: String,
|
||
|
pub line: String,
|
||
|
pub cursor: usize,
|
||
4 years ago
|
pub completion: Vec<String>,
|
||
4 years ago
|
pub should_close: bool,
|
||
4 years ago
|
pub completion_selection_index: Option<usize>,
|
||
4 years ago
|
completion_fn: Box<dyn FnMut(&str) -> Vec<String>>,
|
||
4 years ago
|
callback_fn: Box<dyn FnMut(&mut Editor, &str)>,
|
||
4 years ago
|
}
|
||
|
|
||
|
impl Prompt {
|
||
4 years ago
|
pub fn new(
|
||
4 years ago
|
prompt: String,
|
||
4 years ago
|
mut completion_fn: impl FnMut(&str) -> Vec<String> + 'static,
|
||
4 years ago
|
callback_fn: impl FnMut(&mut Editor, &str) + 'static,
|
||
4 years ago
|
) -> Prompt {
|
||
|
Prompt {
|
||
4 years ago
|
prompt,
|
||
|
line: String::new(),
|
||
|
cursor: 0,
|
||
4 years ago
|
completion: completion_fn(""),
|
||
4 years ago
|
should_close: false,
|
||
4 years ago
|
completion_selection_index: None,
|
||
4 years ago
|
completion_fn: Box::new(completion_fn),
|
||
|
callback_fn: Box::new(callback_fn),
|
||
|
}
|
||
4 years ago
|
}
|
||
|
|
||
|
pub fn insert_char(&mut self, c: char) {
|
||
4 years ago
|
self.line.insert(self.cursor, c);
|
||
|
self.cursor += 1;
|
||
4 years ago
|
self.completion = (self.completion_fn)(&self.line);
|
||
4 years ago
|
self.exit_selection();
|
||
4 years ago
|
}
|
||
4 years ago
|
|
||
4 years ago
|
pub fn move_char_left(&mut self) {
|
||
4 years ago
|
if self.cursor > 0 {
|
||
4 years ago
|
self.cursor -= 1;
|
||
4 years ago
|
}
|
||
|
}
|
||
|
|
||
4 years ago
|
pub fn move_char_right(&mut self) {
|
||
4 years ago
|
if self.cursor < self.line.len() {
|
||
|
self.cursor += 1;
|
||
4 years ago
|
}
|
||
|
}
|
||
|
|
||
4 years ago
|
pub fn move_start(&mut self) {
|
||
4 years ago
|
self.cursor = 0;
|
||
4 years ago
|
}
|
||
|
|
||
|
pub fn move_end(&mut self) {
|
||
4 years ago
|
self.cursor = self.line.len();
|
||
4 years ago
|
}
|
||
|
|
||
4 years ago
|
pub fn delete_char_backwards(&mut self) {
|
||
4 years ago
|
if self.cursor > 0 {
|
||
|
self.line.remove(self.cursor - 1);
|
||
|
self.cursor -= 1;
|
||
4 years ago
|
self.completion = (self.completion_fn)(&self.line);
|
||
|
}
|
||
4 years ago
|
self.exit_selection();
|
||
4 years ago
|
}
|
||
|
|
||
|
pub fn change_completion_selection(&mut self) {
|
||
4 years ago
|
if self.completion.is_empty() {
|
||
|
return;
|
||
4 years ago
|
}
|
||
4 years ago
|
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();
|
||
4 years ago
|
}
|
||
|
pub fn exit_selection(&mut self) {
|
||
|
self.completion_selection_index = None;
|
||
4 years ago
|
}
|
||
4 years ago
|
}
|
||
|
|
||
4 years ago
|
use tui::{
|
||
|
buffer::Buffer as Surface,
|
||
|
layout::Rect,
|
||
|
style::{Color, Modifier, Style},
|
||
|
};
|
||
|
|
||
|
const BASE_WIDTH: u16 = 30;
|
||
4 years ago
|
use crate::ui::text_color;
|
||
4 years ago
|
|
||
|
impl Prompt {
|
||
|
pub fn render_prompt(&self, area: Rect, surface: &mut Surface, theme: &Theme) {
|
||
4 years ago
|
let text_color = text_color();
|
||
4 years ago
|
// 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),
|
||
4 years ago
|
text_color,
|
||
4 years ago
|
);
|
||
|
}
|
||
|
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 {
|
||
4 years ago
|
text_color
|
||
4 years ago
|
};
|
||
|
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
|
||
4 years ago
|
surface.set_string(1, area.height - 1, &self.prompt, text_color);
|
||
|
surface.set_string(2, area.height - 1, &self.line, text_color);
|
||
4 years ago
|
}
|
||
|
}
|
||
|
|
||
4 years ago
|
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,
|
||
|
};
|
||
4 years ago
|
|
||
4 years ago
|
match event {
|
||
4 years ago
|
KeyEvent {
|
||
|
code: KeyCode::Char(c),
|
||
4 years ago
|
modifiers: KeyModifiers::NONE,
|
||
4 years ago
|
} => self.insert_char(c),
|
||
|
KeyEvent {
|
||
|
code: KeyCode::Esc, ..
|
||
4 years ago
|
} => self.should_close = true,
|
||
4 years ago
|
KeyEvent {
|
||
|
code: KeyCode::Right,
|
||
|
..
|
||
4 years ago
|
} => self.move_char_right(),
|
||
4 years ago
|
KeyEvent {
|
||
|
code: KeyCode::Left,
|
||
|
..
|
||
4 years ago
|
} => 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(),
|
||
4 years ago
|
KeyEvent {
|
||
|
code: KeyCode::Backspace,
|
||
4 years ago
|
modifiers: KeyModifiers::NONE,
|
||
4 years ago
|
} => self.delete_char_backwards(),
|
||
4 years ago
|
KeyEvent {
|
||
|
code: KeyCode::Enter,
|
||
|
..
|
||
4 years ago
|
} => (self.callback_fn)(cx.editor, &self.line),
|
||
4 years ago
|
KeyEvent {
|
||
|
code: KeyCode::Tab, ..
|
||
4 years ago
|
} => self.change_completion_selection(),
|
||
4 years ago
|
KeyEvent {
|
||
|
code: KeyCode::Char('q'),
|
||
|
modifiers: KeyModifiers::CONTROL,
|
||
|
} => self.exit_selection(),
|
||
4 years ago
|
_ => (),
|
||
4 years ago
|
};
|
||
|
|
||
|
EventResult::Consumed(None)
|
||
|
}
|
||
|
|
||
4 years ago
|
fn render(&self, area: Rect, surface: &mut Surface, cx: &mut Context) {
|
||
|
self.render_prompt(area, surface, &cx.editor.theme)
|
||
4 years ago
|
}
|
||
4 years ago
|
|
||
|
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,
|
||
|
))
|
||
|
}
|
||
4 years ago
|
}
|