Add cheats for navi itself (#599)

Fixes #380
This commit is contained in:
Denis Isidoro 2021-08-08 08:12:19 -03:00 committed by GitHub
parent 027ad539c2
commit 85ca69a263
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 99 additions and 40 deletions

View file

@ -88,6 +88,7 @@ Customization
-------------
You can:
- [setup your own config file](docs/config_file.md)
- [change colors](docs/customization.md#changing-colors)
- [resize columns](docs/customization.md#resizing-columns)
- [change how search is performed](docs/customization.md#overriding-fzf-options)

6
docs/cheat_example.cheat Normal file
View file

@ -0,0 +1,6 @@
% first cheat
# print something
echo "My name is <name>!"
$ name: whoami

29
docs/config_file.md Normal file
View file

@ -0,0 +1,29 @@
Config file
-----------------
* [Example](#example)
* [Location](#location)
* [Creating the file](#creating-the-file)
### Example
An example config can be found by running:
```sh
navi info config-example
```
You can also read it online by clicking [here](./config_file_example.yaml).
### Location
Run the following command to check where the config file is/should be located:
```sh
navi info config-path
```
### Creating the file
Run the following command to generate a config file with the default parameters:
```sh
navi info config-example > "$(navi info config-path)"
```

38
docs/navi.cheat Normal file
View file

@ -0,0 +1,38 @@
% cheatsheets
# Download default cheatsheets
navi repo add denisidoro/cheats
# Browse for cheatsheet repos
navi repo browse
# Edit main local cheatsheets
f="$(navi info cheats-path)/main.cheat"
[ -f "$f" ] || navi info cheats-example > "$f"
$EDITOR "$f"
% config
# Edit config file
f="$(navi info config-path)"
[ -f "$f" ] || navi info config-example > "$f"
$EDITOR "$f"
% 3rd-party
# Search using tldr
navi --tldr "<query>"
# Search using cheatsh
navi --cheatsh "<query>"
% help
# Read command-line help text
navi --help
# Read project README.md
navi fn url::open "https://github.com/denisidoro/navi"

View file

@ -11,7 +11,7 @@ use std::str::FromStr;
const FINDER_POSSIBLE_VALUES: &[&str] = &["fzf", "skim"];
const WIDGET_POSSIBLE_VALUES: &[&str] = &["bash", "zsh", "fish"];
const FUNC_POSSIBLE_VALUES: &[&str] = &["url::open", "welcome", "widget::last_command", "map::expand"];
const INFO_POSSIBLE_VALUES: &[&str] = &["cheats-path", "config-path", "config-example"];
const INFO_POSSIBLE_VALUES: &[&str] = &["cheats-example", "cheats-path", "config-path", "config-example"];
impl FromStr for Shell {
type Err = &'static str;
@ -45,6 +45,7 @@ impl FromStr for Info {
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"cheats-example" => Ok(Info::CheatsExample),
"cheats-path" => Ok(Info::CheatsPath),
"config-example" => Ok(Info::ConfigExample),
"config-path" => Ok(Info::ConfigPath),
@ -62,13 +63,7 @@ MORE ENVIRONMENT VARIABLES:
NAVI_CONFIG_YAML # config file content
CONFIG FILE:
By default it's located in:
navi info config-path
You can generate a config file by running:
navi info config-example > "$(navi info config-path)"
Please check the generated config file for more info
Please check the online documentation or run `navi fn welcome` to setup the file
FEATURE STABILITY:
experimental # may be removed or changed at any time
@ -76,6 +71,7 @@ FEATURE STABILITY:
EXAMPLES:
navi # default behavior
navi fn welcome # show cheatsheets for navi itself
navi --print # doesn't execute the snippet
navi --tldr docker # search for docker cheatsheets using tldr
navi --cheatsh docker # search for docker cheatsheets using cheatsh

View file

@ -33,7 +33,7 @@ pub fn main() -> Result<()> {
if let Some(variables) = res {
Ok(Some(variables))
} else {
welcome::populate_cheatsheet(stdin);
welcome::populate_cheatsheet(stdin)?;
Ok(Some(VariableMap::new()))
}
})

View file

@ -4,6 +4,7 @@ use anyhow::Result;
#[derive(Debug)]
pub enum Info {
CheatsExample,
CheatsPath,
ConfigPath,
ConfigExample,
@ -11,6 +12,7 @@ pub enum Info {
pub fn main(info: &Info) -> Result<()> {
match info {
Info::CheatsExample => println!("{}", include_str!("../../docs/cheat_example.cheat")),
Info::CheatsPath => println!("{}", pathbuf_to_string(&filesystem::default_cheat_pathbuf()?)?),
Info::ConfigPath => println!("{}", pathbuf_to_string(&filesystem::default_config_pathbuf()?)?),
Info::ConfigExample => println!("{}", include_str!("../../docs/config_file_example.yaml")),

View file

@ -115,7 +115,7 @@ fn write_cmd(
denylist: Option<&Vec<String>>,
visited_lines: &mut HashSet<u64>,
) -> Result<()> {
if item.snippet.len() <= 1 {
if item.comment.is_empty() || item.snippet.trim().is_empty() {
return Ok(());
}
@ -212,7 +212,7 @@ pub fn read_lines(
item.comment = without_prefix(&line);
}
// variable
else if line.starts_with('$') {
else if line.starts_with('$') && line.contains(':') {
should_break = write_cmd(&item, stdin, allowlist, denylist, visited_lines).is_err();
item.snippet = String::from("");
let (variable, command, opts) = parse_variable_line(&line).with_context(|| {

View file

@ -3,12 +3,10 @@ use crate::config::CONFIG;
use crate::extractor;
use crate::finder::structures::Opts as FinderOpts;
use crate::finder::Finder;
use crate::parser;
use crate::structures::cheat::VariableMap;
use crate::structures::item::Item;
use crate::writer;
use anyhow::Context;
use anyhow::Result;
use std::io::Write;
pub fn main() -> Result<()> {
let config = &CONFIG;
@ -17,7 +15,7 @@ pub fn main() -> Result<()> {
let (raw_selection, variables, files) = config
.finder()
.call(opts, |stdin, _| {
populate_cheatsheet(stdin);
populate_cheatsheet(stdin)?;
Ok(Some(VariableMap::new()))
})
.context("Failed getting selection and variables from finder")?;
@ -32,30 +30,19 @@ pub fn main() -> Result<()> {
Ok(())
}
fn add_msg(tags: &str, comment: &str, snippet: &str, stdin: &mut std::process::ChildStdin) {
let item = Item {
tags: tags.to_string(),
comment: comment.to_string(),
snippet: snippet.to_string(),
file_index: 0,
};
stdin
.write_all(writer::write(&item).as_bytes())
.expect("Could not write to fzf's stdin");
}
pub fn populate_cheatsheet(stdin: &mut std::process::ChildStdin) -> Result<()> {
let cheatsheet = include_str!("../docs/navi.cheat");
pub fn populate_cheatsheet(stdin: &mut std::process::ChildStdin) {
add_msg(
"cheatsheets",
"Download default cheatsheets",
"navi repo add denisidoro/cheats",
parser::read_lines(
cheatsheet.split('\n').into_iter().map(|s| Ok(s.to_string())),
"welcome",
0,
&mut VariableMap::new(),
&mut Default::default(),
stdin,
);
add_msg(
"cheatsheets",
"Browse for cheatsheet repos",
"navi repo browse",
stdin,
);
add_msg("more info", "Read --help message", "navi --help", stdin);
None,
None,
)?;
Ok(())
}

View file

@ -47,7 +47,7 @@ test::_escape() {
test::equals() {
local -r actual="$(cat)"
local -r expected="$(echo "${1:-}")"
local -r actual2="$(echo "$actual" | test::_escape)"
local -r expected2="$(echo "$expected" | test::_escape)"