Source

pcw / extras / cw_color.sh

#!/bin/bash

PAD=12
NICK=emg

colors=(21 {26..51} {57..87} {93..123} {128..159} {164..231})

reset="$(tput sgr0)"
bold="$(tput bold)"
black="$(tput setaf 0)"
red="$(tput setaf 196)"
magenta="$(tput setaf 5)"

me_fmt="${red}\a"

format=""
cols=""

while [ $# -gt 0 ]; do
	case $1 in
		-i) IN="$2"
		   	;;
		-o) OUT="$2"
			;;
		-n) NICK="${2:-$NICK}"
			;;
		*)  echo "bad argument $1"
			exit 2
			;;
	esac
	shift 2
done

set_format() {
	chksm="$(echo "$*" | cksum | sed "s/^0\+//; s/ .*$//")"
	chksm=$((chksm % ${#colors[@]}))
	format="$(tput setaf ${colors[$chksm]})"
}


cw "${IN:?no -i infile specified}" "${OUT:?no -o outfile specified}" |
while read date time nick msg; do
	nick=${nick#<}
	nick=${nick%>}

	if [ ${#nick} -lt $PAD ]; then
		head_len=$(($PAD + ${#time} + 2))
	else
		head_len=$((${#nick} + ${#time} + 2))
	fi

	if [ "$date" = "SIZE" ]; then
		cols=$nick
		continue
	fi

	time="${bold}${black}${time}${reset}"

	if [ "$nick" = "-!-" ]; then
		format="${bold}${black}"
		nick=$(printf "%s%$((PAD + 3))s" "$format" "$nick")
	else
		set_format "$nick"
		nick=$(printf "%s%${PAD}s%s |%s" "$format" "$nick" "$reset" "$format")
	fi

	first=1
	echo $msg | fmt -$((cols - head_len - 1)) -s |
	while read line; do
		if [ -n "$first" ]; then
			printf "%s %s %s\n" "$time" "$nick" "$line$reset"
			first=""
		else
			printf "%${head_len}s| %s\n" "" "$format$line$reset"
		fi
	done | sed "/$NICK/s/$NICK/${reset}${me_fmt}&${format}/g; /ACTION/s/|.*ACTION \(.*\)/${reset}***${format} \1/"
done