Source

pcw / extras / cw_color.sh

Evan Gates 828493a 
Evan Gates 79c4845 
Evan Gates 828493a 
Evan Gates be287f8 
Evan Gates e31d01d 
Evan Gates 9c1102c 
Evan Gates aa82d9b 

Evan Gates b63b951 
Evan Gates aa82d9b 
Evan Gates 828493a 
Evan Gates e31d01d 
Evan Gates aa82d9b 
Evan Gates eeb198e 



Evan Gates e31d01d 
Evan Gates 9c1102c 



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 b63b951 
Evan Gates be287f8 
Evan Gates b63b951 
Evan Gates 828493a 
Evan Gates be287f8 

Evan Gates aa82d9b 





Evan Gates f09b27b 
Evan Gates aa9ae1b 
Evan Gates 79c4845 
Evan Gates b63b951 


Evan Gates 79c4845 
Evan Gates b63b951 
Evan Gates f09b27b 
Evan Gates a7e8606 




Evan Gates 828493a 





Evan Gates 9c1102c 
Evan Gates 99b91d9 
Evan Gates be287f8 


Evan Gates eeb198e 
Evan Gates 79c4845 
Evan Gates 828493a 
Evan Gates b63b951 
Evan Gates a67b9fd 
Evan Gates aa82d9b 
Evan Gates 4462202 
Evan Gates aa82d9b 

Evan Gates 828493a 





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

PAD=12
NICK="$USER"

# NOTE: make sure you have a 256 color terminal, or change these options
# to find out which colors are readable...
# for i in {0..255}; do echo $(tput setaf $i) $i foobar baz qux; done
colors=({29..51} {62..87} 99 {105..123} {129..159} {165..231})
declare -A color_cache

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

action="$magenta*$cyan*$yellow*"  # used to prefix CTCP actions
notice_fmt="$(tput setaf 239)"    # used for time, and join/part/quit/etc. messages
my_nick_fmt="$red$(echo -e "\a")" # how to highlight your nick when someone mentions it
my_line_fmt="$cyan"               # how you want your own lines to appear

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

color_cache["-!-"]="$notice_fmt"
color_cache["${NICK%%+(_)}"]="$my_line_fmt"

set_format() {
	if [ -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 -r date time nick_msg; do
	nick=${nick_msg%% *}
	nick=${nick%>}
	nick=${nick#<}
	msg=${nick_msg#* }

	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="${notice_fmt}${time}${reset}"

	# remove all trailing underscores so nicks foo and foo_ are the same color
	# if extglob is not set, no harm done, just doesn't remove the underscores
	set_format "${nick%%+(_)}"
	nick=$(printf "%s%${PAD}s%s |%s" "$format" "$nick" "$reset" "$format")

	first=1
	oIFS="$IFS"
	IFS=$'\n'
	echo "$msg" | fmt -w $((cols - head_len - 1)) -s |
	while read -r 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
	IFS="$oIFS"
done