diff options
author | EuAndreh <eu@euandre.org> | 2022-05-12 05:06:14 -0300 |
---|---|---|
committer | EuAndreh <eu@euandre.org> | 2022-05-12 05:06:14 -0300 |
commit | b92cf88765107069a0e019fa64cf9f51a12d9709 (patch) | |
tree | 0479e0c76dfba4b2c9b25b795c9ed03436ecec4d | |
parent | .config/bash/vcs-ps1.sh: Add adapted version, using `color` utility (diff) | |
download | dotfiles-b92cf88765107069a0e019fa64cf9f51a12d9709.tar.gz dotfiles-b92cf88765107069a0e019fa64cf9f51a12d9709.tar.xz |
.local/bin/pre: Add working version
-rwxr-xr-x | .local/bin/pre | 72 |
1 files changed, 72 insertions, 0 deletions
diff --git a/.local/bin/pre b/.local/bin/pre new file mode 100755 index 0000000..233ac1a --- /dev/null +++ b/.local/bin/pre @@ -0,0 +1,72 @@ +#!/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 + 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)) + +assert_arg() { + if [ -z "$1" ]; then + printf 'Missing %s\n\n' "$2" >&2 + usage >&2 + exit 2 + fi +} + +PREFIX="${1:-}" +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 |