trunc: Truncate lines to fit terminal width or a specified width

#!/usr/bin/env bash
help_text="
usage: trunc [options] <filename>
-h|--help:  This help text.
-w|--width: Specify width (default is terminal width less 1).
"

help_line="Truncate lines to fit terminal width or a specified width"
web_desc_line="Truncate lines to fit terminal width or a specified width"

case $1 in
    -h|--help)
        echo "$help_text"
        exit
        ;;
    -w|--width)
        w="$2"
        shift 2
        ;;
esac

if [ -z "$1" ] && [ -t 0 ]; then
    echo "Usage: $(basename "$0") <filename>  (or pipe data in)" >&2
    echo "Error: no filename given and no data piped in." >&2
    exit 1
fi

if [ -z "$w" ]; then
    w=$(( $(tput cols) - 1 ))
fi

cat "$@" | perl -s -pe '
    s/^((?:\e\[[0-9;]*[[:alpha:]]|[^\e]){$w}).*/$1/
' -- -w="$w"