#!/bin/bash
# tlog
# ====
# Usage: tlog TARGET [OPTIONS]
#
# Writes input to a target file while stripping ANSI codes, and returns input
# as it was.
help_msg() {
cat <<-EOF
Usage: tlog FILENAME [OPTIONS]
Options:
-a, --append append to target file instead of overwriting
-h, --help print this message
EOF
exit 1
}
err_msg() {
(>&2 echo "$1")
exit 1
}
if [[ $# -lt 1 ]]; then
err_msg "Usage: tlog TARGET [OPTIONS]"
fi
APPEND=0
LOGFILE=
while [[ $# -gt 0 ]]; do
case $1 in
-a|--append) APPEND=1; shift;;
-h|--help) help_msg;;
*) LOGFILE=$1; shift;;
esac
done
if [[ -z $LOGFILE ]]; then
err_msg "Usage: tlog TARGET [OPTIONS]"
elif [[ $APPEND -eq 1 ]]; then
tee >(sed 's/\x1b\[[0-9;]*[mK]//g' >> "$LOGFILE")
else
tee >(sed 's/\x1b\[[0-9;]*[mK]//g' > "$LOGFILE")
fi