aboutsummaryrefslogtreecommitdiff
path: root/bin/pre
blob: fdbd3d9904fbc53c7ee46abeabd893fb3e643c7f (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
#!/bin/sh
set -eu

usage() {
	cat <<-'EOF'
		Usage:
		  pre [-c COLOR] PREFIX
		  pre -h
	EOF
}

help() {
	cat <<-'EOF'


		Options:
		  -c COLOR      ANSI color to be used on the prefix text
		  -h, --help    show this message

		  PREFIX        the string to insert at the beginning


		Prefix STDIN with PREFIX.


		Examples:

		  Prefix with 'database':

		    $ ./run-db.sh | pre 'database'


		  Prefix with yellow 'numbers':

		    $ seq 10 | pre -c yellow numbers
	EOF
}

for flag in "$@"; do
	case "$flag" in
		--)
			break
			;;
		--help)
			usage
			help
			exit
			;;
		*)
			;;
	esac
done

COLOR=''
while getopts 'c:h' flag; do
	case "$flag" in
		c)
			COLOR="$OPTARG"
			;;
		h)
			usage
			help
			exit
			;;
		*)
			usage >&2
			exit 2
			;;
	esac
done
shift $((OPTIND - 1))

PREFIX="${1:-}"
eval "$(assert-arg -- "$PREFIX" 'PREFIX')"

while read -r line; do
	if [ -z "$COLOR" ]; then
		printf '%s: %s\n' "$PREFIX" "$line"
	else
		printf '%s: %s\n' "$(color -c "$COLOR" "$PREFIX")" "$line"
	fi
done