Skip to content

Commit

Permalink
add -o output_file option for vimcat
Browse files Browse the repository at this point in the history
Add -o option to specify output file for vimcat, with this option
whether or not output is a terminal is ignored. If output_file is - then
the output is written to STDOUT, but without the extra newline if there
isn't one on the last line.

If there is no -o option, then the normal behavior of passing through to
cat if the output is not a terminal is preserved.

Update docs/vimpager.md to describe the -o option.
  • Loading branch information
rkitover committed Jul 27, 2015
1 parent d6386b6 commit 31b15ba
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 19 deletions.
12 changes: 11 additions & 1 deletion doc/vimcat.md
Expand Up @@ -8,7 +8,7 @@ vimcat - vim based syntax highlighter

# SYNOPSIS

vimcat *some_file*
vimcat [options] file1 [file2 ...]

# DESCRIPTION
cat's a file to stdout, syntax-highlighting it using vim as a backend.
Expand All @@ -19,6 +19,9 @@ To use a different vimrc with vimcat, put your settings into a ~/.vimcatrc.

To disable loading plugins, put "set noloadplugins" into a ~/.vimcatrc file.

If output is not a terminal, it will simply run cat, so using vimcat in
pipe commands is safe. If you actually need the ANSI codes, use -o .

# COMMAND LINE OPTIONS

## -c cmd
Expand All @@ -34,3 +37,10 @@ arguments are supported.
## -u vimrc

Use an alternate .vimrc or .vimcatrc.

## -o output_file

Write output to output_file instead of the terminal. This works when
vimcat is not run on a terminal as well.

If output_file is "-" then output is written to STDOUT.
56 changes: 38 additions & 18 deletions vimcat
Expand Up @@ -4,10 +4,6 @@
#! Based on _v by Magnus Woldrich: https://github.com/trapd00r/utils/blob/master/_v

: if 0
# Just pass through if not on a tty
if [ ! -t 1 ]; then
exec cat "${@}"
fi
# try to find a better shell, especially on Solaris

PATH=$PATH:/usr/local/bin:/opt/csw/bin:/opt/local/bin:/usr/dt/bin:/usr/xpg4/bin:/usr/bin:/bin
Expand Down Expand Up @@ -105,6 +101,11 @@
vimcatrc="${1}"
shift
;;
"-o")
shift
output_file="${1}"
shift
;;
"--")
shift
break
Expand All @@ -119,6 +120,13 @@
esac
done

# Just pass through if not on a tty, unless -o was given
if [ -z "${output_file}" ]; then
if [ ! -t 1 ]; then
exec cat "${@}"
fi
fi

if [ -z "${vimcatrc}" ]; then
if [ -f ~/.vimcatrc ]; then
vimcatrc="~/.vimcatrc"
Expand Down Expand Up @@ -153,24 +161,36 @@
# Check that the file exists
if test -r "${file}" -a -f "${file}"; then
if test -s "${file}"; then
if [ -n "${vimcatrc}" ]; then
vim -N -E -X -R -i NONE -u "${vimcatrc}" --cmd "visual | ${extra_cmd}" -c "silent! source ${script} | ${extra_c} | call AnsiHighlight(\"${tmp_file}\") | q" -- "${file}" </dev/tty >/dev/null 2>&1
if [ -z "${output_file}" -o "${output_file}" = "-" ]; then
dest_file="${tmp_file}"
else
vim -N -E -X -R -i NONE --cmd "visual | ${extra_cmd}" -c "silent! source ${script} | ${extra_c} | call AnsiHighlight(\"${tmp_file}\") | q" -- "${file}" </dev/tty >/dev/null 2>&1
dest_file="${output_file}"
fi
cat "${tmp_file}"
# if file doesn't end in a newline, output a newline
# regular Solaris tail does not work for this
tail_cmd=tail
if [ -x /usr/xpg4/bin/tail ]; then
tail_cmd=/usr/xpg4/bin/tail

if [ -n "${vimcatrc}" ]; then
vim -N -E -X -R -i NONE -u "${vimcatrc}" --cmd "visual | ${extra_cmd}" -c "silent! source ${script} | ${extra_c} | call AnsiHighlight(\"${dest_file}\") | q" -- "${file}" </dev/tty >/dev/null 2>&1
else
vim -N -E -X -R -i NONE --cmd "visual | ${extra_cmd}" -c "silent! source ${script} | ${extra_c} | call AnsiHighlight(\"${dest_file}\") | q" -- "${file}" </dev/tty >/dev/null 2>&1
fi
if command -v gtail >/dev/null; then
tail_cmd=gtail

if [ -z "${output_file}" -o "${output_file}" = "-" ]; then
cat "${tmp_file}"
fi
last_char=$($tail_cmd -c 1 "${tmp_file}")
if [ "${last_char}" != "" ]; then
echo

if [ -z "${output_file}" ]; then
# if file doesn't end in a newline, output a newline
# regular Solaris tail does not work for this
tail_cmd=tail
if [ -x /usr/xpg4/bin/tail ]; then
tail_cmd=/usr/xpg4/bin/tail
fi
if command -v gtail >/dev/null; then
tail_cmd=gtail
fi
last_char=$($tail_cmd -c 1 "${tmp_file}")
if [ "${last_char}" != "" ]; then
echo
fi
fi
fi
else
Expand Down

0 comments on commit 31b15ba

Please sign in to comment.