Skip to content

Commit

Permalink
added toggleable help message
Browse files Browse the repository at this point in the history
  • Loading branch information
curlpipe committed Jul 16, 2024
1 parent a1dcf65 commit 1710288
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
22 changes: 20 additions & 2 deletions src/editor.rs
Original file line number Diff line number Diff line change
@@ -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::{
Expand Down Expand Up @@ -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,
}
Expand All @@ -42,6 +44,7 @@ impl Editor {
config: Config::read(config)?,
active: true,
greet: false,
help: false,
highlighter: vec![],
feedback: Feedback::None,
})
Expand Down Expand Up @@ -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)?;
Expand Down Expand Up @@ -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)?;
Expand Down Expand Up @@ -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));
}
Expand Down
21 changes: 21 additions & 0 deletions src/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Size> {
let (w, h) = terminal::size()?;
Expand All @@ -18,6 +37,7 @@ pub fn size() -> Result<Size> {
})
}

/// Represents different status messages
pub enum Feedback {
Info(String),
Warning(String),
Expand All @@ -26,6 +46,7 @@ pub enum Feedback {
}

impl Feedback {
/// Actually render the status message
pub fn render(&self, colors: &Colors, w: usize) -> Result<String> {
let start = match self {
Self::Info(_) =>
Expand Down

0 comments on commit 1710288

Please sign in to comment.