Source

pcw / extras / cw_color.sh

Full commit
Evan Gates 828493a 
Evan Gates 79c4845 
Evan Gates 828493a 
Evan Gates 7e28d25 
Evan Gates e31d01d 
Evan Gates aa82d9b 

Evan Gates a7e8606 
Evan Gates aa82d9b 
Evan Gates 828493a 
Evan Gates e31d01d 


Evan Gates aa82d9b 
Evan Gates eeb198e 



Evan Gates e31d01d 
Evan Gates aa82d9b 
Evan Gates eeb198e 
Evan Gates aa82d9b 

Evan Gates e31d01d 
Evan Gates 828493a 
Evan Gates aa82d9b 
Evan Gates 828493a 
Evan Gates a1be3b4 
Evan Gates aa9ae1b 

Evan Gates 79c4845 
Evan Gates aa9ae1b 

Evan Gates 181cfa8 
Evan Gates aa9ae1b 


Evan Gates 79c4845 
Evan Gates aa9ae1b 



Evan Gates 828493a 
Evan Gates eeb198e 


Evan Gates aa82d9b 








Evan Gates f09b27b 
Evan Gates aa9ae1b 
Evan Gates 79c4845 



Evan Gates f09b27b 
Evan Gates a7e8606 




Evan Gates 828493a 






Evan Gates 99b91d9 
Evan Gates eeb198e 

Evan Gates 79c4845 
Evan Gates 828493a 
Evan Gates aa82d9b 
Evan Gates 828493a 
Evan Gates aa82d9b 

Evan Gates 828493a 





Evan Gates aa82d9b 
Evan Gates 828493a 
#!/bin/bash

PAD=12
NICK=$USER

# to find out which colors are readable...
# for i in {0..255}; do echo $(tput setaf $i) $i foobar baz qux; done
colors=({9..11} {13..14} {26..51} {62..87} 99 {105..123} {130..159} {165..231})
declare -A color_cache

reset="$(tput sgr0)"
bold="$(tput bold)"
black="$(tput setaf 0)"
red="$(tput setaf 9)"
cyan="$(tput setaf 14)"
magenta="$(tput setaf 13)"
green="$(tput setaf 10)"
yellow="$(tput setaf 11)"

action="$magenta*$cyan*$yellow*"
notice_fmt="$black$bold"
my_nick_fmt="$red$(echo -e "\a")"
my_line_fmt="$cyan"

format=""
cols="132"

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() {
	if [ "$*" = "-!-" ]; then
		format="$notice_fmt"
	elif [ "$*" = "$NICK" ]; then
		format="$my_line_fmt"
	elif [ -n "${color_cache["$*"]}" ]; then
		format=${color_cache["$*"]}
	else
		chksm="$(echo "$*" | cksum | sed "s/^0\+//; s/ .*$//")"
		chksm=$((chksm % ${#colors[@]}))
		format="$(tput setaf ${colors[$chksm]})"
		color_cache["$*"]=$format
	fi
}

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

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

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

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

	set_format "$nick"
	nick=$(printf "%s%${PAD}s%s |%s" "$format" "$nick" "$reset" "$format")

	first=1
	echo "$msg" | fmt -w $((cols - head_len - 1)) -s |
	while read line; do
		line=${line//$NICK/$reset$my_nick_fmt$NICK$reset$format}
		line=${line/#ACTION/"$action$reset$format"}
		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
done