Commits

Anonymous committed aa82d9b

create a cache of nick->color associations, use bash builtin replacements instead of sed, get a 4x speedup

  • Participants
  • Parent commits 828493a

Comments (0)

Files changed (1)

File extras/cw_color.sh

 PAD=12
 NICK=emg
 
-colors=(21 {26..51} {57..87} {93..123} {128..159} {164..231})
+# to find out which colors are readable...
+# for i in {0..255}; do echo $(tput setaf $i) $i foobar baz qux; done
+colors=({9..14} {26..51} {62..87} {99..123} {130..159} {165..231})
+declare -A color_cache
 
 reset="$(tput sgr0)"
 bold="$(tput bold)"
 black="$(tput setaf 0)"
-red="$(tput setaf 196)"
+red="$(tput setaf 9)"
+cyan="$(tput setaf 6)"
 magenta="$(tput setaf 5)"
+green="$(tput setaf 2)"
+yellow="$(tput setaf 3)"
 
-me_fmt="${red}\a"
+action="$magenta*$cyan*$yellow*"
+my_nick_fmt="$red$(echo -e "\a")"
+my_line_fmt="$cyan"
 
 format=""
-cols=""
+cols="132"
 
 while [ $# -gt 0 ]; do
 	case $1 in
 done
 
 set_format() {
-	chksm="$(echo "$*" | cksum | sed "s/^0\+//; s/ .*$//")"
-	chksm=$((chksm % ${#colors[@]}))
-	format="$(tput setaf ${colors[$chksm]})"
+	if [ "$*" = "$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#<}
 	fi
 
 	first=1
-	echo $msg | fmt -$((cols - head_len - 1)) -s |
+	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 | sed "/$NICK/s/$NICK/${reset}${me_fmt}&${format}/g; /ACTION/s/|.*ACTION \(.*\)/${reset}***${format} \1/"
+	done
 done