From 17102880f4387a3694ae9ef1e0401c0ade679a62 Mon Sep 17 00:00:00 2001 From: Luke <11898833+curlpipe@users.noreply.github.com> Date: Tue, 16 Jul 2024 15:01:48 +0100 Subject: [PATCH] added toggleable help message --- src/editor.rs | 22 ++++++++++++++++++++-- src/ui.rs | 21 +++++++++++++++++++++ 2 files changed, 41 insertions(+), 2 deletions(-) diff --git a/src/editor.rs b/src/editor.rs index 77cdc91..31d7127 100644 --- a/src/editor.rs +++ b/src/editor.rs @@ -1,4 +1,4 @@ -use crate::ui::{size, Terminal, Feedback}; +use crate::ui::{size, Terminal, Feedback, HELP_TEXT}; use crate::config::Config; use crate::error::{OxError, Result}; use crossterm::{ @@ -28,6 +28,8 @@ pub struct Editor { active: bool, /// true if the editor should show a greeting message on next render greet: bool, + /// Whether or not to show the help message + help: bool, /// The feedback message to display below the status line pub feedback: Feedback, } @@ -42,6 +44,7 @@ impl Editor { config: Config::read(config)?, active: true, greet: false, + help: false, highlighter: vec![], feedback: Feedback::None, }) @@ -251,9 +254,11 @@ impl Editor { self.render_document(w, h)?; // Leave last line for status line self.render_status_line(w, h)?; - // Render greeting if applicable + // Render greeting or help message if applicable if self.greet { self.render_greeting(w, h)?; + } else if self.help { + self.render_help_message(w, h)?; } // Render feedback line self.render_feedback_line(w, h)?; @@ -381,6 +386,18 @@ impl Editor { } /// Render the greeting message + fn render_help_message(&mut self, w: usize, h: usize) -> Result<()> { + let color = self.config.colors.borrow().highlight.to_color()?; + let editor_fg = self.config.colors.borrow().editor_fg.to_color()?; + let message: Vec<&str> = HELP_TEXT.split('\n').collect(); + for (c, line) in message.iter().enumerate().take(h - h / 4) { + self.terminal.goto(w - 30, h / 4 + c + 1)?; + write!(self.terminal.stdout, "{}{line}{}", Fg(color), Fg(editor_fg))?; + } + Ok(()) + } + + /// Render the help message fn render_greeting(&mut self, w: usize, h: usize) -> Result<()> { let colors = self.config.colors.borrow(); let greeting = self.config.greeting_message.borrow().render(&colors)?; @@ -802,6 +819,7 @@ impl Editor { } ["readonly", "true"] => self.doc_mut().read_only = true, ["readonly", "false"] => self.doc_mut().read_only = false, + ["help"] => self.help = !self.help, _ => { self.feedback = Feedback::Error(format!("Command '{}' not found", cmd)); } diff --git a/src/ui.rs b/src/ui.rs index 0b34c0e..3d6355a 100644 --- a/src/ui.rs +++ b/src/ui.rs @@ -9,6 +9,25 @@ use crossterm::{ use kaolinite::utils::{Size}; use std::io::{stdout, Stdout, Write}; +/// Constant that shows the help message +pub const HELP_TEXT: &str = " + Default Key Bindings: + Ctrl + N: New + Ctrl + O: Open + Ctrl + Q: Quit + Ctrl + S: Save + Ctrl + W: Save as + Ctrl + A: Save all + Ctrl + Z: Undo + Ctrl + Y: Redo + Ctrl + F: Find + Ctrl + R: Replace + Ctrl + D: Delete Line + Ctrl + K: Command Line + Shift + ->: Next Tab + Shift + <-: Previous Tab +"; + /// Gets the size of the terminal pub fn size() -> Result { let (w, h) = terminal::size()?; @@ -18,6 +37,7 @@ pub fn size() -> Result { }) } +/// Represents different status messages pub enum Feedback { Info(String), Warning(String), @@ -26,6 +46,7 @@ pub enum Feedback { } impl Feedback { + /// Actually render the status message pub fn render(&self, colors: &Colors, w: usize) -> Result { let start = match self { Self::Info(_) =>