navi icon indicating copy to clipboard operation
navi copied to clipboard

Add a check before running command and use glow to print markdown

Open aemonge opened this issue 7 months ago • 1 comments

Use this alias:

# default navi to print, not to execute
navi() {
    if command navi --print "$@" > /tmp/navi_output; then
        /usr/bin/glow /tmp/navi_output
    else
        echo "navi command failed" >&2
        return 1
    fi
}

_run_last_navi_or_builtin() {
    if [ $# -gt 0 ]; then
        # Use the built-in . (source) command if arguments are provided
        builtin . "$@"
    elif [ ! -s /tmp/navi_output ]; then
        echo "No navi output found or file is empty. Run 'navi' command first." >&2
        return 1
    else
        # Execute commands from navi output if no arguments
        if ! /usr/bin/grep -q '^\$ ' /tmp/navi_output; then
            echo "No executable commands found in navi output." >&2
            return 1
        fi
        /usr/bin/grep '^\$ ' /tmp/navi_output | while read -r line; do
            cmd="${line#\$ }"
            echo "Executing: $cmd"
            eval "$cmd"
        done
    fi
}
alias .="_run_last_navi_or_builtin"

With this you can use navi to search for a comand , and if you want to run it just hit .

aemonge avatar Jul 03 '24 11:07 aemonge