Skip to content

Commit

Permalink
Open up help page in less on :help command; also handle :quit and :exit.
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulJuliusMartinez committed Feb 4, 2022
1 parent fbfd842 commit 5b898e7
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 13 deletions.
45 changes: 42 additions & 3 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use rustyline::Editor;
use termion::event::Key;
use termion::event::MouseButton::{Left, WheelDown, WheelUp};
use termion::event::MouseEvent::Press;
use termion::screen::{ToAlternateScreen, ToMainScreen};

use crate::flatjson;
use crate::input::TuiEvent;
Expand All @@ -24,6 +25,12 @@ pub struct App {
message: Option<(String, MessageSeverity)>,
}

enum Command {
Quit,
Help,
Unknown,
}

pub const MAX_BUFFER_SIZE: usize = 9;
const BELL: &'static str = "\x07";

Expand Down Expand Up @@ -209,9 +216,19 @@ impl App {
None
}
Key::Char(':') => {
let _readline = self.screen_writer.get_command(":");
// Something like this?
// Some(Action::Command(parse_command(_readline))
if let Ok(command) = self.screen_writer.get_command(":") {
match Self::parse_command(&command) {
Command::Quit => break,
Command::Help => self.show_help(),
Command::Unknown => {
self.message = Some((
format!("Unknown command: {}", command),
MessageSeverity::Info,
));
}
}
}

None
}
Key::Char('/') => {
Expand Down Expand Up @@ -448,4 +465,26 @@ impl App {
);
Some(Action::MoveTo(destination))
}

fn parse_command(command: &str) -> Command {
match command {
"h" | "he" | "hel" | "help" => Command::Help,
"q" | "qu" | "qui" | "quit" | "quit()" | "exit" | "exit()" => Command::Quit,
_ => Command::Unknown,
}
}

fn show_help(&mut self) {
let _ = write!(self.screen_writer.tty_writer.stdout, "{}", ToMainScreen);
let _ = std::process::Command::new("less")
.arg("src/jless.help")
.stdin(std::process::Stdio::inherit())
.stdout(std::process::Stdio::inherit())
.output();
let _ = write!(
self.screen_writer.tty_writer.stdout,
"{}",
ToAlternateScreen
);
}
}
10 changes: 0 additions & 10 deletions src/screenwriter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,16 +179,6 @@ impl ScreenWriter {
self.tty_writer.position_cursor(1, self.dimensions.height)?;
self.tty_writer.clear_line()?;

match &result {
Ok(line) => {
write!(self.tty_writer, "Executed command: {}", line)?;
}
Err(err) => {
write!(self.tty_writer, "Command error: {:?}", err)?;
}
}

self.tty_writer.flush()?;
result
}

Expand Down

0 comments on commit 5b898e7

Please sign in to comment.