aboutsummaryrefslogtreecommitdiff
path: root/bin/tlcs
blob: 6a22f4ecc8e043a9e93241ab0e0ff4b0f598c835 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#!/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"
}

# Check we have tl(1)
hash tl || exit

# 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 sgr0
    } 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")