aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--README.markdown4
-rwxr-xr-xbin/tl68
-rwxr-xr-xbin/tlcs100
-rw-r--r--man/man1/tl.121
-rw-r--r--man/man1/tlcs.124
5 files changed, 217 insertions, 0 deletions
diff --git a/README.markdown b/README.markdown
index 12bf0b12..a8559958 100644
--- a/README.markdown
+++ b/README.markdown
@@ -287,6 +287,10 @@ Scripts
privileges.
* `td(1)` manages a to-do file for you with `$EDITOR` and `git(1)`; I used to
use Taskwarrior, but found it too complex and buggy.
+* `tl(1)` tags input lines with a prefix or suffix, basically a `sed(1)`
+ shortcut
+* `tlcs(1)` executes a command and uses `tl(1)` to tag stdout and stderr
+ lines, and color them if you want
* `try(1)` repeats a command up to a given number of times until it succeeds,
only printing error output if all three attempts failed. Good for
tolerating blips or temporary failures in `cron(8)` scripts.
diff --git a/bin/tl b/bin/tl
new file mode 100755
index 00000000..ae1bd2a5
--- /dev/null
+++ b/bin/tl
@@ -0,0 +1,68 @@
+#!/usr/bin/env bash
+
+#
+# tl(1): Tag lines from files or stdin with a string prefix or suffix before
+# writing them to stdout. Specifying neither prefix nor suffix is acceptable,
+# in which case the stream is simply reproduced on stdout, acting like cat(1).
+#
+# Option -h gives help. Specify a prefix with -p, and/or a suffix with -s. If
+# no file arguments are given, defaults to reading standard input.
+#
+# Author: Tom Ryder <tom@sanctum.geek.nz>
+# Copyright: 2016
+# License: Public domain
+#
+
+# Name self
+self=tl
+
+# Define usage function
+usage() {
+ printf 'USAGE: %s [-h] [-p PREFIX] [-s SUFFIX] [--] [FILE1 FILE2 ...]\n' "$self"
+}
+
+# Start with empty prefix/suffix, or use the environment variables
+prefix=$TL_PREFIX
+suffix=$TL_SUFFIX
+
+# Parse options out, give help if necessary
+while getopts 'hp:s:' opt ; do
+ case $opt in
+
+ # -h: Print help
+ h)
+ usage
+ exit
+ ;;
+
+ # -p: Specify prefix
+ p)
+ prefix=$OPTARG
+ ;;
+
+ # -s: Specify suffix
+ s)
+ suffix=$OPTARG
+ ;;
+
+ # Unknown option
+ \?)
+ usage >&2
+ exit 2
+ ;;
+ esac
+done
+shift "$((OPTIND-1))"
+
+# Need at least one tag
+if ! (($#)) ; then
+ set /dev/stdin
+fi
+
+# Print each line as we read it, prepending the tags, separated by spaces
+for file in "$@" ; do
+ while IFS= read -r line ; do
+ printf '%s%s%s\n' "$prefix" "$line" "$suffix"
+ done < "$file"
+done
+
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")
+
diff --git a/man/man1/tl.1 b/man/man1/tl.1
new file mode 100644
index 00000000..7560debb
--- /dev/null
+++ b/man/man1/tl.1
@@ -0,0 +1,21 @@
+.TH TL 1 "February 2016" "Manual page for tl"
+.SH NAME
+.B tl
+\- tag lines with a string prefix or suffix
+.SH USAGE
+.B tl [-h] [-p PREFIX] [-s SUFFIX] [--] [FILE1 FILE2 ...]
+.SH DESCRIPTION
+Tag lines from files or stdin with a string prefix or suffix before writing
+them to stdout. Specifying neither prefix nor suffix is acceptable, in which
+case the stream is simply reproduced on stdout, acting like cat(1).
+.P
+Option -h gives help. Specify a prefix with -p, and/or a suffix with -s. If no
+file arguments are given, defaults to reading standard input.
+.P
+ $ tl -p 'file: ' /path/to/file
+ $ dmesg | tl -s ' (stdout)'
+.SH SEE ALSO
+tlcs(1)
+.SH AUTHOR
+Tom Ryder <tom@sanctum.geek.nz>
+
diff --git a/man/man1/tlcs.1 b/man/man1/tlcs.1
new file mode 100644
index 00000000..4c39612e
--- /dev/null
+++ b/man/man1/tlcs.1
@@ -0,0 +1,24 @@
+.TH TLCS 1 "February 2016" "Manual page for tlcs"
+.SH NAME
+.B tlcs
+\- execute command and tag stdout/stderr lines
+.SH USAGE
+.B tlcs [-h] [-c] [-o STDOUT_PREFIX] [-e STDERR_PREFIX] [--] COMMAND [ARG1...]
+.SH DESCRIPTION
+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.
+.P
+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.
+.P
+ $ tlcs cat ~/.vimrc
+ $ tlcs -e 'FAIL: ' nonexistent-command
+.SH SEE ALSO
+tl(1)
+.SH AUTHOR
+Tom Ryder <tom@sanctum.geek.nz>
+