diff --git a/src/editor/command_line.rs b/src/editor/command_line.rs index a57280e..6e5c20e 100644 --- a/src/editor/command_line.rs +++ b/src/editor/command_line.rs @@ -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 { visible: bool, prefix: String, content: StyledContent, cursor_position: u64, - special_char: (String, bool), + special_char: Option<(String, bool)>, block: Vec } 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) -> Vec { + 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::>(); + draw_commands.append(&mut commands); + } + draw_commands + } + pub fn handle_command_events(&mut self, event: RedrawEvent) { match event { - RedrawEvent::CommandLineShow { content, position, first_character, prompt, indent, level } => {}, - RedrawEvent::CommandLinePosition { position, level } => {}, - RedrawEvent::CommandLineSpecialCharacter { character, shift, level } => {}, - RedrawEvent::CommandLineHide => {}, - RedrawEvent::CommandLineBlockShow { lines } => {}, - RedrawEvent::CommandLineBlockAppend { line } => {}, - RedrawEvent::CommandLineBlockHide => {} + RedrawEvent::CommandLineShow { content, position, first_character, prompt, indent, level } => self.show(content, position, first_character, prompt, indent, level), + RedrawEvent::CommandLinePosition { position, level } => self.set_position(position, level), + RedrawEvent::CommandLineSpecialCharacter { character, shift, level } => self.set_special_character(character, shift, level), + RedrawEvent::CommandLineHide => self.hide(), + RedrawEvent::CommandLineBlockShow { lines } => self.show_block(lines), + RedrawEvent::CommandLineBlockAppend { line } => self.append_line_to_block(line), + 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) { + self.block = lines; + } + + fn append_line_to_block(&mut self, line: StyledContent) { + self.block.push(line); + } + + fn hide_block(&mut self) { + self.block.clear(); + } } diff --git a/src/editor/mod.rs b/src/editor/mod.rs index dc32160..43a150f 100644 --- a/src/editor/mod.rs +++ b/src/editor/mod.rs @@ -9,15 +9,18 @@ mod command_line; pub use cursor::{Cursor, CursorShape, CursorMode}; pub use style::{Colors, Style}; +use command_line::CommandLine; use crate::events::{GridLineCell, RedrawEvent}; -pub type GridCell = Option<(char, Style)>; +pub type GridCell = Option<(char, Option