Skip to content
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
31 changes: 31 additions & 0 deletions docs/usage/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,37 @@ For example, if we want to search for cheatsheets in `/some/dir` and in `/other/
navi --path '/some/dir:/other/dir'
```

## Printing the executed command

You can print the selected command to stdout before execution using `--print-command` (or `-P`):

```sh
navi --print-command
```

This is useful to keep a visible record of what was run, e.g. in a terminal session log.

## Printing the full cheat before executing

You can print a formatted block showing the comment and resolved command before execution using `--print-cheat` (or `-C`):

```sh
navi --print-cheat
```

Example output:

```
──────────────────────────────────────
# Check updates for installed packages
apt list --upgradeable
──────────────────────────────────────
Listing... Done
...
```

The separator adapts to the longest line (capped at 80 characters).

## Logging

The log file will be created under the same directory where the configuration file is located.\
Expand Down
2 changes: 1 addition & 1 deletion rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[toolchain]
channel = "1.81.0"
channel = "1.88.0"
components = [ "rustfmt", "clippy" ]
25 changes: 24 additions & 1 deletion src/commands/core/actor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ use crate::finder::structures::{Opts as FinderOpts, SuggestionType};
use crate::prelude::*;
use crate::structures::cheat::{Suggestion, VariableMap};
use crate::structures::item::Item;
use crossterm::style::{Attribute, Color, ResetColor, SetAttribute, SetForegroundColor};
use shell::EOF;
use std::io::Write;
use std::process::Stdio;

fn prompt_finder(
Expand Down Expand Up @@ -224,7 +226,7 @@ pub fn act(

env_var::set(env_var::PREVIEW_INITIAL_SNIPPET, &snippet);
env_var::set(env_var::PREVIEW_TAGS, &tags);
env_var::set(env_var::PREVIEW_COMMENT, comment);
env_var::set(env_var::PREVIEW_COMMENT, &comment);

let interpolated_snippet = {
let mut s = replace_variables_from_snippet(
Expand All @@ -247,6 +249,27 @@ pub fn act(
clipboard::copy(interpolated_snippet)?;
}
_ => {
if CONFIG.print_cheat() {
let comment_line = format!("# {comment}");
let width = comment_line.len().max(interpolated_snippet.len()).min(80);
let sep = "─".repeat(width);
let mut out = std::io::stdout();
let _ = write!(out, "{}", SetAttribute(Attribute::Dim));
let _ = writeln!(out, "{sep}");
let _ = write!(out, "{}", SetAttribute(Attribute::Reset));
let _ = write!(out, "{}", SetForegroundColor(Color::DarkGrey));
let _ = writeln!(out, "{comment_line}");
let _ = write!(out, "{}", SetForegroundColor(Color::Cyan));
let _ = writeln!(out, "{interpolated_snippet}");
let _ = write!(out, "{}", ResetColor);
let _ = write!(out, "{}", SetAttribute(Attribute::Dim));
let _ = writeln!(out, "{sep}");
let _ = write!(out, "{}", SetAttribute(Attribute::Reset));
let _ = out.flush();
} else if CONFIG.print_command() {
println!("navi> {interpolated_snippet}");
let _ = std::io::Write::flush(&mut std::io::stdout());
}
let mut cmd = shell::out();
cmd.arg(&interpolated_snippet[..]);
debug!(cmd = ?cmd);
Expand Down
9 changes: 9 additions & 0 deletions src/config/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use clap::{crate_version, Parser, Subcommand};
navi # default behavior
navi fn welcome # show cheatsheets for navi itself
navi --print # doesn't execute the snippet
navi --print-command # prints the command before executing it
navi --tldr docker # search for docker cheatsheets using tldr
navi --cheatsh docker # search for docker cheatsheets using cheatsh
navi --path '/some/dir:/other/dir' # use .cheat files from custom paths
Expand All @@ -48,6 +49,14 @@ pub(super) struct ClapConfig {
#[cfg(not(feature = "disable-command-execution"))]
pub print: bool,

/// Prints the command to stdout before executing it

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please consider making it print to stderr instead?

This way it won't break commands like navi --print-command -q 'command which echoes to stdout' | grep 'foo'.

By printing to stderr, you can still break commands which read from stderr, but that's much more rare.

#[arg(short = 'P', long)]

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd rather not add the short option, because we'll need to maintain it forever (unless there's a breaking change).

pub print_command: bool,

/// Prints the full cheat (comment + command) before executing it
#[arg(short = 'C', long)]

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above

pub print_cheat: bool,

/// Returns the best match
#[arg(long)]
pub best_match: bool,
Expand Down
8 changes: 8 additions & 0 deletions src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,14 @@ impl Config {
self.yaml.style.snippet.min_width
}

pub fn print_command(&self) -> bool {
self.clap.print_command
}

pub fn print_cheat(&self) -> bool {
self.clap.print_cheat
}

#[cfg(feature = "disable-command-execution")]
fn print(&self) -> bool {
true
Expand Down
14 changes: 14 additions & 0 deletions tests/run
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,16 @@ _navi_cheatspath() {
| grep -q "/cheats"
}

_navi_print_command() {
_navi --print-command --query "trivial" --best-match \
| test::contains "navi> echo"
}

_navi_print_cheat() {
_navi --print-cheat --query "trivial" --best-match \
| test::contains "# trivial case"
}

_kill_tmux() {
pkill -f tmux 2>/dev/null || true
}
Expand Down Expand Up @@ -154,6 +164,10 @@ done
test::set_suite "info"
test::run "cheats_path" _navi_cheatspath

test::set_suite "print"
test::run "print_command" _navi_print_command
test::run "print_cheat" _navi_print_cheat

test::set_suite "widget"
test::run "bash" _navi_widget "bash"
test::run "zsh" _navi_widget "zsh"
Expand Down