Commits

Anonymous committed 13c87a0

add cw_color.awk{,.sh} awk rewrite of cw_color.sh, faster but doesn't wrap on wordbreak

  • Participants
  • Parent commits 3ab8449

Comments (0)

Files changed (2)

extras/cw_color.awk

+#!/usr/bin/gawk -f
+
+function tput(n,    color) {
+	"tput -T " term " " n | getline color;
+	close("tput -T " term " " n);
+	return color;
+}
+
+BEGIN {
+	FS = " {1}";
+	num_colors = split("29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 "\
+	                   "62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 "\
+	                   "85 86 87 99 105 106 107 108 109 110 111 112 113 114 115 116 117 118 " \
+	                   "119 120 121 122 123 129 130 131 132 133 134 135 136 137 138 139 140 " \
+	                   "141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 " \
+	                   "158 159 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 " \
+	                   "180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 " \
+	                   "197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 " \
+	                   "214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 " \
+	                   "231", colors);
+
+	term = ENVIRON["TERM"];
+	cols = 132;
+	pad  = 12;
+	me   = ENVIRON["NICK"];
+
+	reset    = tput("sgr0");
+	notice   = tput("setaf 239");
+	action   = tput("setaf 13") "*" tput("setaf 14") "*" tput("setaf 11") "*" ;
+	mention  = "\a" tput("setaf 1");
+	nick_hash[me]    = tput("setaf 14");
+	nick_hash["-!-"] = notice;
+}
+
+$1 == "SIZE" {
+	cols = $3;
+}
+
+$1 != "SIZE" {
+	time = $2;
+	nick = $3;
+	sub("<", "", nick);
+	sub(">", "", nick);
+
+	$1 = $2 = $3 = "";
+
+	if (nick_hash[nick] == "") {
+		"echo '" nick "' | cksum" | getline hash;
+		close("echo '" nick "' | cksum");
+		hash  = substr(hash, 0, 10);
+		hash %= num_colors;
+		nick_hash[nick] = tput("setaf "colors[hash + 1]);
+		nick_len[nick]  = length(nick);
+	}
+
+	if (nick_len[nick] > pad)
+		head_len = nick_len[nick] + 4 + 2;
+	else
+		head_len = pad + 4 + 2;
+
+	$0 = substr($0, 4);
+	gsub(me, reset mention "&" reset nick_hash[nick]);
+	sub("^ACTION", action reset nick_hash[nick]);
+
+	printf("%s%s %s%*s%s | %s%.*s%s\n", notice, time, nick_hash[nick], pad, nick, reset, nick_hash[nick], cols - head_len - 3, $0, reset);
+	while ($0 = substr($0, cols - head_len - 2))
+		printf("%*s | %s%.*s%s\n", head_len, "", nick_hash[nick], cols - head_len - 3, $0, reset);
+}

extras/cw_color.awk.sh

+#!/bin/bash
+NICK="emg"
+
+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
+
+export NICK
+
+cw "${IN:?no -i infile specified}" "${OUT:?no -o outfile specified}" | cw_color.awk