lucc / nvimpager

Use nvim as a pager to view manpages, diffs, etc with nvim's syntax highlighting
Other
372 stars 20 forks source link

Feature request: manually set the syntax when reading from stdin #11

Closed kbtz closed 5 years ago

kbtz commented 5 years ago

It would be nice to have a option to set a file extension on the temporary file used to read from stdin, so we can handle syntax on these contexts too.

Here's an use case where I'd try to just print a function definition from type with syntax enabled:

alias ?=wat
function wat() {
    shopt -s extdebug
    declare -F $1
    shopt -u extdebug

    typedef=$(type $1)

    # Prints the debug output without highlighting
    echo "$typedef" | head -n1

    typedef=$(echo "$typedef" | tail -n+2)
    if [[ ! -z $typedef ]]; then
        if tty -s; then
            # Prints the function definition with syntax highlighting
            # '-s sh' produces /tmp/tmp.aP6kTGvrLT.sh so nvim reads it as a shell script
            echo "$typedef" | nvimpager -c -s sh
        else
            echo "$typedef"
        fi
    fi
}

Does that make sense?

lucc commented 5 years ago

Nvimpager supports all options that nvim also supports so you could do something like echo foo | nvimpager -c -- -c 'setf sh'. Does that work for you?

kbtz commented 5 years ago

Very neat! Thank you!