#!/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 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