Skip to content

Commit

Permalink
Statically include help documentation, add formatting with ANSI escap…
Browse files Browse the repository at this point in the history
…e codes, handle errors from trying to show documentation.
  • Loading branch information
PaulJuliusMartinez committed Feb 4, 2022
1 parent 5b898e7 commit 458271e
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 52 deletions.
28 changes: 24 additions & 4 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ enum Command {
Unknown,
}

// Help contents that we pipe to less.
const HELP: &'static str = std::include_str!("./jless.help");

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

Expand Down Expand Up @@ -476,11 +479,28 @@ impl App {

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())
let child = std::process::Command::new("qless")
.arg("-r")
.stdin(std::process::Stdio::piped())
.stdout(std::process::Stdio::inherit())
.output();
.spawn();

match child {
Ok(mut child) => {
if let Some(ref mut stdin) = child.stdin {
let _ = stdin.write(HELP.as_bytes());
let _ = stdin.flush();
}
let _ = dbg!(child.wait());
}
Err(err) => {
self.message = Some((
format!("Error piping help documentation to less: {}", err),
MessageSeverity::Error,
));
}
}

let _ = write!(
self.screen_writer.tty_writer.stdout,
"{}",
Expand Down
97 changes: 49 additions & 48 deletions src/jless.help
Original file line number Diff line number Diff line change
@@ -1,44 +1,44 @@

jless - a terminal JSON viewer

jless - a terminal JSON viewer
SUMMARY OF JLESS COMMANDS

SUMMARY OF JLESS COMMANDS

Commands marked with * may be preceded by a number, N, which will
Commands marked with * may be preceded by a number, N, which will
repeatedly perform a command the given number of times. A key
preceded by a caret indicates the Ctrl key; thus ^E is ctrl-E.

q ^c Exit jless.

F1 :help Show this help screen.

MOVING
MOVING

j DownArrow * Move focus down one line (or N lines).
j DownArrow * Move focus down one line (or N lines).
^n Enter Space

k UpArrow * Move focus up one line (or N lines).
k UpArrow * Move focus up one line (or N lines).
^p Backspace

h LeftArrow When focused on an expanded object or array, collapse the
object or array. Otherwise, move focus to the parent of
the focused node.
object or array. Otherwise, move focus to the parent of
the focused node.

H Focus the parent of the focused node, even if it is an
expanded object or array.
expanded object or array.

l RightArrow When focused on a collapsed object or array, expand the
object or array. When focused on an expanded object or
array, move focus to the first child. When focused on
non-container values, does nothing.
object or array. When focused on an expanded object or
array, move focus to the first child. When focused on
non-container values, does nothing.

J * Move to the focused node's next sibling 1 or N times.
K * Move to the focused node's previous sibling 1 or N times.
J * Move to the focused node's next sibling 1 or N times.
K * Move to the focused node's previous sibling 1 or N times.

w * Move forward until the next change in depth 1 or N times.
b * Move backwards until the next change in depth 1 or N times.
w * Move forward until the next change in depth 1 or N times.
b * Move backwards until the next change in depth 1 or N times.

PageDown * Move down by one window (or N windows).
PageUp * Move up by one window (or N windows).
PageDown * Move down by one window (or N windows).
PageUp * Move up by one window (or N windows).

0 ^ Move to the first sibling of the focused node's parent.
$ Move to the last sibling of the focused node's parent.
Expand All @@ -49,47 +49,47 @@
c Collapse the focused node and all its siblings.
e Expand the focused node and all its siblings.

SCROLLING
SCROLLING

^e * Scroll down one line (or N lines).
^y * Scroll up one line (or N lines).
^e * Scroll down one line (or N lines).
^y * Scroll up one line (or N lines).

zz Move the focused node to the center of the screen.
zt Move the focused node to the top of the screen.
zb Move the focused node to the bottom of the screen.

. * Scroll a truncated value one char to the right (or N chars).
, * Scroll a truncated value one char to the left (or N chars).
. * Scroll a truncated value one char to the right (or N chars).
, * Scroll a truncated value one char to the left (or N chars).
; Scroll a truncated value all the way to the end, or, if
already at the end, back to the start.
already at the end, back to the start.

< Decrease the indentation of every line by one (or N) tabs.
> Increase the indentation of every line by one (or N) tabs.
< Decrease the indentation of every line by one (or N) tabs.
> Increase the indentation of every line by one (or N) tabs.

SEARCH
SEARCH

jless supports full-text search over the input JSON.

/pattern Search forward for the given pattern
?pattern Search backwards for the given pattern

* * Move to the next occurrence of the object key on the focused
line (or move forward N occurrences)
line (or move forward N occurrences)
# * Move to the previous occurrence of the object key on the
focused line (or move backwards N occurrences)
focused line (or move backwards N occurrences)

n * Move in the search direction to the next match (or forward
N matches).
N matches).
N * Move in the opposite of the search direction to the previous match
(or previous N matches).
(or previous N matches).

Searching uses "smart case" by default. If the input pattern doesn't
contain any capital letters, a case insensitive search will be
performed. If there are any capital letters, it will be case sensitive.
You can force a case-sensitive search by appending '/s' to your query.

A trailing slash will be removed from a pattern; to search for a pattern
ending in '/' (or '/s'), just add another '/' to the end.
A trailing slash will be removed from a pattern; to search for a
pattern ending in '/' (or '/s'), just add another '/' to the end.

The search pattern are interpreted as mostly standard regular
expressions, with one exception. Because JSON data occurs many square
Expand All @@ -112,7 +112,7 @@

https://docs.rs/regex/latest/regex/index.html#syntax

SEARCH INPUT
SEARCH INPUT

The search is *not* performed over the original input, but over a
single-line pretty formatted version of the input JSON. Consider the
Expand Down Expand Up @@ -141,25 +141,26 @@
commas separating object entries and array elements.)

Searching will performed over this internal representation so that
patterns can include multiple elements without worrying about newlines
or the exact input format.
patterns can include multiple elements without worrying about
newlines or the exact input format.

When the input is newline-delimited JSON, an actual newline will
separate each top-level JSON element in the internal representation.

DATA MODE VS LINE MODE
DATA MODE VS LINE MODE

jless starts in "data" mode, which displays the JSON data in a more
streamlined fashion: no closing delimiters for objects or arrays, no
trailing commas, no quotes around object keys that are valid identifiers
in JavaScript. It also shows single-line previous of objects and arrays,
and array indexes before array elements. Note that when using full-text
search, object keys will still be surrounded by quotes.
jless starts in "data" mode, which displays the JSON data in a more
streamlined fashion: no closing delimiters for objects or arrays,
no trailing commas, no quotes around object keys that are valid
identifiers in JavaScript. It also shows single-line previous of
objects and arrays, and array indexes before array elements. Note
that when using full-text search, object keys will still be
surrounded by quotes.

By pressing 'm', you can switch jless to "line" mode, which displays
the input as pretty printed JSON.
By pressing 'm', you can switch jless to "line" mode, which displays
the input as pretty printed JSON.

In line mode you can press '%' when focused on a open or close delimiter
of an object or array to jump to its matching pair.
In line mode you can press '%' when focused on a open or close
delimiter of an object or array to jump to its matching pair.


0 comments on commit 458271e

Please sign in to comment.