Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added the --man subcommand #82

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -725,6 +725,7 @@ impl Default for Commands {
.command("e/frames", "Edit frames as view", |p| {
p.then(paths()).map(|(_, paths)| Command::EditFrames(paths))
})
.command("h", "Display help", |p| p.value(Command::Mode(Mode::Help)))
.command("help", "Display help", |p| {
p.value(Command::Mode(Mode::Help))
})
Expand Down
9 changes: 1 addition & 8 deletions src/draw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -800,14 +800,7 @@ pub fn draw_help(session: &Session, text: &mut TextBatch, shape: &mut shape2d::B
TextAlign::Left,
);

let (normal_kbs, visual_kbs): (
Vec<(&String, &session::KeyBinding)>,
Vec<(&String, &session::KeyBinding)>,
) = session
.key_bindings
.iter()
.filter_map(|kb| kb.display.as_ref().map(|d| (d, kb)))
.partition(|(_, kb)| kb.modes.contains(&Mode::Normal));
let (normal_kbs, visual_kbs) = session.key_bindings.get_key_bindings();

let mut line = (0..(session.height as usize - self::LINE_HEIGHT as usize * 4))
.rev()
Expand Down
50 changes: 50 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,56 @@ mod gfx;
#[macro_use]
pub mod util;

// manual prints a quick reference given default Session parameters
pub fn manual() -> std::io::Result<String> {
use std::io;

let mut manual: String = String::new();

let proj_dirs = dirs::ProjectDirs::from("io", "cloudhead", "rx")
.ok_or_else(|| io::Error::new(io::ErrorKind::NotFound, "config directory not found"))?;
let base_dirs = dirs::BaseDirs::new()
.ok_or_else(|| io::Error::new(io::ErrorKind::NotFound, "home directory not found"))?;
let cwd = std::env::current_dir()?;
let options = Options {
headless: true,
..Default::default()
};

let session = Session::new(1, 1, cwd, ResourceManager::new(), proj_dirs, base_dirs)
.with_blank(
FileStatus::NoFile,
Session::DEFAULT_VIEW_W,
Session::DEFAULT_VIEW_H,
)
.init(options.source.clone())?;

&manual.push_str(&format!("rx v{}: quick reference\n", crate::VERSION));

let session_kbs = session.key_bindings;
let (normal_kbs, visual_kbs) = session_kbs.get_key_bindings();

// colour the header? Windows versions before Linux subshell update doesn't support ANSI
&manual.push_str("\nNORMAL MODE\n\n");
for (display, kb) in normal_kbs.iter() {
&manual.push_str(&format!("{:<36} {}\n", display, kb.command));
}

&manual.push_str("\nVISUAL MODE\n\n");
for (display, kb) in visual_kbs.iter() {
&manual.push_str(&format!("{:<36} {}\n", display, kb.command));
}

&manual.push_str("\nCOMMAND MODE\n\n");
for (key, def, _) in cmd::Commands::default().iter() {
&manual.push_str(&format!(":{:<36} {}\n", key, def));
}

manual.push_str(session::SETTINGS);

Ok(manual)
}

use cmd::Value;
use event::Event;
use execution::{DigestMode, Execution, ExecutionMode, GifMode};
Expand Down
6 changes: 6 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ OPTIONS

-v Verbose mode
-u <script> Use the commands in <script> for initialization
-m --man Prints the `:help` command to the terminal

--record <dir> Record user input to a directory
--replay <dir> Replay user input from a directory
Expand Down Expand Up @@ -49,6 +50,11 @@ fn execute(mut args: pico_args::Arguments) -> Result<(), Box<dyn std::error::Err
return Ok(());
}

if args.contains(["-m", "--man"]) {
println!("{}", rx::manual()?);
return Ok(());
}

let verbose = args.contains("-v");
let debug = args.contains("--debug");
let width = args.opt_value_from_str("--width")?;
Expand Down
7 changes: 7 additions & 0 deletions src/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,13 @@ impl KeyBindings {
pub fn iter(&self) -> std::slice::Iter<'_, KeyBinding> {
self.elems.iter()
}

/// Return tuple of normal mode and visual mode key bindings
pub fn get_key_bindings(&self) -> (Vec<(&String, &KeyBinding)>, Vec<(&String, &KeyBinding)>) {
self.iter()
.filter_map(|kb| kb.display.as_ref().map(|d| (d, kb)))
.partition(|(_, kb)| kb.modes.contains(&Mode::Normal))
}
}

///////////////////////////////////////////////////////////////////////////////
Expand Down