mirror of https://github.com/sgoudham/neovide.git
more progress toward externalized command bar
parent
1a12766861
commit
22ddb545a9
@ -1,27 +1,101 @@
|
|||||||
use crate::events::{GridLineCell, RedrawEvent, StyledContent};
|
use std::collections::HashMap;
|
||||||
|
|
||||||
|
use crate::events::{RedrawEvent, StyledContent};
|
||||||
|
use crate::editor::{DrawCommand, Style};
|
||||||
|
|
||||||
pub struct CommandLine {
|
pub struct CommandLine {
|
||||||
visible: bool,
|
visible: bool,
|
||||||
prefix: String,
|
prefix: String,
|
||||||
content: StyledContent,
|
content: StyledContent,
|
||||||
cursor_position: u64,
|
cursor_position: u64,
|
||||||
special_char: (String, bool),
|
special_char: Option<(String, bool)>,
|
||||||
block: Vec<StyledContent>
|
block: Vec<StyledContent>
|
||||||
}
|
}
|
||||||
|
|
||||||
impl CommandLine {
|
impl CommandLine {
|
||||||
|
pub fn new() -> CommandLine {
|
||||||
|
CommandLine {
|
||||||
|
visible: false,
|
||||||
|
prefix: String::new(),
|
||||||
|
content: Vec::new(),
|
||||||
|
cursor_position: 0,
|
||||||
|
special_char: None,
|
||||||
|
block: Vec::new()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn draw(&self, window_size: (u64, u64), defined_styles: &HashMap<u64, Style>) -> Vec<DrawCommand> {
|
||||||
|
let mut draw_commands = Vec::new();
|
||||||
|
if self.content.len() > 0 {
|
||||||
|
let (width, height) = window_size;
|
||||||
|
let text_length: usize = self.content.iter().map(|(_, text)| text.len()).sum();
|
||||||
|
|
||||||
|
let x = (width / 2) - (text_length as u64 / 2);
|
||||||
|
let y = height / 2;
|
||||||
|
|
||||||
|
let mut start_x = x;
|
||||||
|
let mut commands = self.content.iter().map(|(style_id, text)| {
|
||||||
|
let command_width = text.len();
|
||||||
|
let style = defined_styles.get(style_id).map(|style| style.clone());
|
||||||
|
let command = DrawCommand::new(text.clone(), (start_x, y), style);
|
||||||
|
start_x = start_x + command_width as u64;
|
||||||
|
command
|
||||||
|
}).collect::<Vec<DrawCommand>>();
|
||||||
|
draw_commands.append(&mut commands);
|
||||||
|
}
|
||||||
|
draw_commands
|
||||||
|
}
|
||||||
|
|
||||||
pub fn handle_command_events(&mut self, event: RedrawEvent) {
|
pub fn handle_command_events(&mut self, event: RedrawEvent) {
|
||||||
match event {
|
match event {
|
||||||
RedrawEvent::CommandLineShow { content, position, first_character, prompt, indent, level } => {},
|
RedrawEvent::CommandLineShow { content, position, first_character, prompt, indent, level } => self.show(content, position, first_character, prompt, indent, level),
|
||||||
RedrawEvent::CommandLinePosition { position, level } => {},
|
RedrawEvent::CommandLinePosition { position, level } => self.set_position(position, level),
|
||||||
RedrawEvent::CommandLineSpecialCharacter { character, shift, level } => {},
|
RedrawEvent::CommandLineSpecialCharacter { character, shift, level } => self.set_special_character(character, shift, level),
|
||||||
RedrawEvent::CommandLineHide => {},
|
RedrawEvent::CommandLineHide => self.hide(),
|
||||||
RedrawEvent::CommandLineBlockShow { lines } => {},
|
RedrawEvent::CommandLineBlockShow { lines } => self.show_block(lines),
|
||||||
RedrawEvent::CommandLineBlockAppend { line } => {},
|
RedrawEvent::CommandLineBlockAppend { line } => self.append_line_to_block(line),
|
||||||
RedrawEvent::CommandLineBlockHide => {}
|
RedrawEvent::CommandLineBlockHide => self.hide_block(),
|
||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// fn show()
|
fn show(&mut self, content: StyledContent, position: u64, first_character: String, prompt: String, _indent: u64, _level: u64) {
|
||||||
|
let prefix;
|
||||||
|
if first_character.len() > 0 {
|
||||||
|
prefix = first_character;
|
||||||
|
} else {
|
||||||
|
prefix = prompt;
|
||||||
|
}
|
||||||
|
|
||||||
|
self.visible = true;
|
||||||
|
self.prefix = prefix;
|
||||||
|
self.content = content;
|
||||||
|
self.cursor_position = position;
|
||||||
|
self.block = Vec::new();
|
||||||
|
}
|
||||||
|
|
||||||
|
fn set_position(&mut self, position: u64, level: u64) {
|
||||||
|
self.cursor_position = position;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn set_special_character(&mut self, character: String, shift: bool, _level: u64) {
|
||||||
|
self.special_char = Some((character, shift));
|
||||||
|
}
|
||||||
|
|
||||||
|
fn hide(&mut self) {
|
||||||
|
self.visible = false;
|
||||||
|
self.special_char = None;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn show_block(&mut self, lines: Vec<StyledContent>) {
|
||||||
|
self.block = lines;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn append_line_to_block(&mut self, line: StyledContent) {
|
||||||
|
self.block.push(line);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn hide_block(&mut self) {
|
||||||
|
self.block.clear();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue