diff options
author | Tom Ryder <tom@sanctum.geek.nz> | 2016-02-24 10:14:28 +1300 |
---|---|---|
committer | Tom Ryder <tom@sanctum.geek.nz> | 2016-02-24 10:14:28 +1300 |
commit | 949d55ab57d690f80a530acc7d674055bc8e3814 (patch) | |
tree | 5c7c94c02cd58105d9219bb0f560930303e32f80 /bin/tlcs | |
parent | Update submodules (diff) | |
download | dotfiles-949d55ab57d690f80a530acc7d674055bc8e3814.tar.gz dotfiles-949d55ab57d690f80a530acc7d674055bc8e3814.zip |
Add tl(1) and tlcs(1)
Diffstat (limited to 'bin/tlcs')
-rwxr-xr-x | bin/tlcs | 100 |
1 files changed, 100 insertions, 0 deletions
diff --git a/bin/tlcs b/bin/tlcs new file mode 100755 index 00000000..9af16efe --- /dev/null +++ b/bin/tlcs @@ -0,0 +1,100 @@ +#!/usr/bin/env bash + +# +# tlcs(1): Execute a command and tag the output of the stdout and stderr +# streams, line by line, using tl(1) under the hood. Add -c when writing to a +# terminal to color the lines. +# +# Option -h gives help. Specify a stdout prefix with -o (default "stdout: "), +# and/or a stderr prefix with -e (default "stderr: "). Option -c prints stdout +# lines in green and stderr lines in red if the respective streams are writing +# to appropriate terminals. Remaining arguments are assumed to be a command and +# its arguments. +# +# Author: Tom Ryder <tom@sanctum.geek.nz> +# Copyright: 2016 +# License: Public domain +# +self=tlcs + +# Define usage function +usage() { + printf 'USAGE: %s [-h] [-c] [-o STDOUT_PREFIX] [-e STDERR_PREFIX] [--] COMMAND [ARG1...]\n' "$self" +} + +# Set the default prefixes and suffixes for stdout/stderr +stdout_prefix='stdout: ' +stderr_prefix='stderr: ' +stdout_suffix= +stderr_suffix= + +# No color by default +declare -i color +color=0 + +# Parse options out, give help if necessary +while getopts 'hco:e:' opt ; do + case $opt in + + # -h: Print help + h) + usage + exit + ;; + + # -c: Add color + c) + color=1 + ;; + + # -o: Specify stdout prefix + o) + stdout_prefix=$OPTARG + ;; + + # -e: Specify stderr prefix + e) + stderr_prefix=$OPTARG + ;; + + # Unknown option + \?) + usage >&2 + exit 2 + ;; + esac +done +shift "$((OPTIND-1))" + +# If color was requested for the output, figure out what we can do +if ((color)) ; then + + # Color code for resetting + color_reset=$( { + tput me || tput sgr 0 + } 2>/dev/null ) + + # If stdout is a terminal, color-code for green + if [[ -t 1 ]] ; then + color_green=$( { + tput AF 2 || tput setaf 2 + } 2>/dev/null ) + stdout_prefix=${color_green}${stderr_prefix} + stdout_suffix=$color_reset + fi + + # If stderr is a terminal, color-code for green + if [[ -t 2 ]] ; then + color_red=$( { + tput AF 1 || tput setaf 1 + } 2>/dev/null ) + stderr_prefix=${color_red}${stderr_prefix} + stdout_suffix=$color_reset + fi +fi + +# Execute the command, passing stdout and stderr to tl(1) calls as appropriate +"$@" \ + 2> >(tl -p "$stderr_prefix" -s "$stderr_suffix") \ + 1> >(tl -p "$stdout_prefix" -s "$stdout_suffix") + |