Commits

Anonymous committed f09b27b

change formatting from cw to cw_color.sh

Comments (0)

Files changed (3)

 
 static FILE *in = NULL, *out = NULL;
 static char buf[4096];
-static struct winsize ws;
-static char *delim = NULL, *margin = NULL;
-static int field = 0;
-
-static int break_len(char *start, char *end, int def)
-{
-	char *p;
-
-	if (end - start < def)
-		return end - start;
-
-	for (p = start + def; p > start && *p != ' '; p--)
-		;
-	return (p == start) ? def : p - start + (*p == ' ');
-}
-
-static void print_line(char *line)
-{
-	char m, *p = NULL;
-	int len, wlen, w, i, cols = ws.ws_col - 1;
-
-	if (delim && field > 0)
-		for (i = 1, p = line; i < field && p; i++, p = strpbrk(p, delim))
-			if (*(p + 1))
-				p++;
-	if (p)
-		line = (*p == ' ') ? p + 1 : p;
-
-	len = strlen(line);
-	if (margin) p = strpbrk(line, margin);
-	w = p - line;
-
-	if (!margin || !p || w + 20 >= cols) { // 20, pretty random choice
-		fputs(line, stdout);
-		return;
-	}
-
-	m = *p;
-
-	if (line[len - 1] == '\n')
-		line[--len] = '\0';
-
-	p = (*(p + 1) == ' ') ? p + 2 : p + 1;
-
-	wlen = p - line + break_len(p, line + len, cols - (p - line));
-	printf("%.*s\n", wlen, line);
-
-	for (p = line + wlen; p < line + len; p += wlen) {
-		wlen = break_len(p, line + len, cols - 2 - w);
-		printf("%*s%c %.*s\n", w, "", m, wlen, p);
-	}
-}
-
-static void sigwinch(int unused)
-{
-	if (ioctl(STDERR, TIOCGWINSZ, &ws) < 0)
-		warn("failed ioctl");
-
-	rewind(in);
-
-	fputs("\033[1;1H", stdout);
-	fputs("\033[2J", stdout);
-	fflush(stdout);
-
-	while (fgets(buf, sizeof(buf), in))
-		print_line(buf);
-	fflush(stdout);
-}
-
-static void usage(void)
-{
-	printf("Usage: cw -i infile -o outfile [-d delim] [-f field] [-m margin] [-v]\n");
-	exit(1);
-}
 
 int main(int argc, char **argv)
 {
-	int evq, len, i;
+	int evq, len;
 	int in_wd = 0, out_wd = 0;
 	struct inotify_event *cur;
 	fd_set rd;
 
+	if (argc != 3) {
+		printf("Usage: cw infile outfile\n");
+		printf("cw-"VERSION" © Evan Gates\n");
+		exit(1);
+	}
+
 	if ((evq = inotify_init()) < 0)
 		err(1, "failed inotify_init()");
 
-	for (i = 1; i + 1 < argc; i++) {
-		if (argv[i][0] != '-' || argv[i][1] == '\0' || argv[i][2] != '\0')
-			usage();
-		switch (argv[i][1]) {
-			case 'd' : delim  = argv[++i]; break;
-			case 'f' : field  = strtol(argv[++i], NULL, 10); break;
-			case 'i' :
-				if ((in = fopen(argv[++i], "r")) == NULL)
-					err(1, "failed to fopen %s", argv[i]);
-				if ((in_wd = inotify_add_watch(evq, argv[i], IN_MODIFY)) < 0)
-					err(1, "failed inotify_add_watch on %s", argv[i]);
-				break;
-			case 'm' : margin = argv[++i]; break;
-			case 'o' :
-				if ((out = fopen(argv[++i], "w")) == NULL)
-					err(1, "failed to fopen %s", argv[i]);
-				if ((out_wd = inotify_add_watch(evq, argv[i], IN_CLOSE_NOWRITE)) < 0)
-					err(1, "failed inotify_add_watch on %s", argv[i]);
-				break;
-			case 'v' : printf("cw-"VERSION" © Evan Gates\n"); exit(1);
-			default  : usage(); break;
-		}
-	}
-	if (i != argc || in == NULL || out == NULL || in_wd == 0 || out_wd == 0)
-		usage();
+	if ((in = fopen(argv[1], "r")) == NULL)
+		err(1, "failed to fopen %s", argv[1]);
+	if ((in_wd = inotify_add_watch(evq, argv[1], IN_MODIFY)) < 0)
+		err(1, "failed inotify_add_watch on %s", argv[1]);
 
-	if (signal(SIGWINCH, sigwinch) == SIG_ERR)
-		err(1, "failed to install sigwinch handler");
+	if ((out = fopen(argv[2], "w")) == NULL)
+		err(1, "failed to fopen %s", argv[2]);
+	if ((out_wd = inotify_add_watch(evq, argv[2], IN_CLOSE_NOWRITE)) < 0)
+		err(1, "failed inotify_add_watch on %s", argv[2]);
 
-	sigwinch(0); // set winsize and print what we start with
+	while (fgets(buf, sizeof(buf), in))
+		fputs(buf, stdout);
+	fflush(stdout);
 
 	for (;;) {
 		FD_ZERO(&rd);
 					errx(1, "outfile %s no longer being read", argv[2]);
 
 			while (fgets(buf, sizeof(buf), in))
-				print_line(buf);
+				fputs(buf, stdout);
 			fflush(stdout);
 		}
 		if (FD_ISSET(fileno(stdin), &rd)) {
 # a specific server somehwere other than the given hierarchy, but it should be
 # noted that the fifo path will still be relative to $PREFIX/$serv/. There are
 # then a list of "fifo command" pairs for each server. If a line ends with a "w"
-# we wait a few seconds before executing it (default 10). Each server block
+# we wait a few seconds before executing it (default 5). Each server block
 # should be separated by a new line. The commands for a given server are
 # performed in serial, and each block runs in parallel. I do not do any error
 # checking for you, so don't screw up the format. I never claimed this was
 # For example, my $SERVERS file (nick and pass changed of course)
 #
 # <start of $SERVERS>
-# irc.oftc.net -n mynick -m 12
-# in /j nickserv identify mypass w
+# irc.oftc.net -n mynick
+# nickserv/in identify mypass w
 # in /j #ii
 # in /j #suckless
 # in /j #heresy
 # nickserv/in /l w
 #
-# localhost -n mynick -m 12 -p 13245
+# localhost -n mynick -p 13245
 # &bitlbee/in identify mypass
 # &bitlbee/in blist w
 # <EOF>
 SERVERS="$PREFIX/servers"
 LOG="$PREFIX/log"
 
-DELAY=10 # seconds to wait when we get a "w"
+DELAY=5 # seconds to wait when we get a "w"
 
 {
 	echo

extras/cw_color.sh

 bg_cyan="$(tput setab 6)"
 bg_white="$(tput setab 7)"
 
-date="$bold$black"
-serv="$bold$black"
-nick="$cyan"
-act="$magenta"
-me="$red"
+date_fmt="$bold$black"
+serv_fmt="$bold$black"
+nick_fmt="$cyan"
+act_fmt="$magenta"
+me_fmt="$red"
+
+
+
 
 NICK="$USER"
-MARGIN='>|!'
-FIELD=2
-DELIM=" "
+WINCHED=1
 
 while [ $# -gt 0 ]; do
 	case $1 in
 			;;
 		-o) OUT="$2"
 			;;
-		-m) MARGIN="$2"
-			;;
 		-n) NICK="${2:-$NICK}"
 			;;
-		-f) FIELD="$2"
-			;;
-		-d) DELIM="$2"
-			;;
 		*)  echo "bad argument $1"
 			exit 2
 	esac
 	shift 2
 done
 
-# in order, do
-# change notice continuation lines: color $serv
-# change notice lines: color $serv
-# change CTCP ACTION lines: remove ACTION and MARGIN and color $act
-# change normal lines: color time $date, color nick $nick
-# change lines that are as of yet unmatched: color time $date
-# color nick $me and add bell
+trap "winch" WINCH
 
-cw -i "$IN" -o "$OUT" -m "$MARGIN" -d "$DELIM" -f "$FIELD" |
-	sed \
-		-e "s/^\( *!.*\)$/${serv}\1${reset}/" \
-		-e "s/^\([-0-9]\{10\} \)\?\([:0-9]\{5\}\)\?\( *-!-.*\)$/${date}\1\2\3${reset}/" \
-		-e "s/^\([-0-9]\{10\} \)\?\([:0-9]\{5\}\)\?\( *<\?\)\( *[^ $MARGIN]*\)\( \?[$MARGIN]\) ACTION\(.*\)$/${date}\1\2${reset}\3${act}\4\6${reset}/" \
-		-e "s/^\([-0-9]\{10\} \)\?\([:0-9]\{5\}\)\?\( *<\?\)\( *[^ $MARGIN]*\)\( \?[$MARGIN]\)/${date}\1\2${reset}\3${nick}\4${reset}\5/" \
-		-e "s/^\([-0-9]\{10\} \)\?\([:0-9]\{5\}\)\?/${date}\1\2${reset}/" \
-		-e "s/$NICK/\a${reset}${me}&${reset}/g"
+# please forgive me for what you're about to read
+# on SIGWINCH open $OUT for reading, when it closes cw will exit. we have to
+# cat in the background, so we can move on and kill it. we need to sleep
+# shortly to ensure that it has actually started, and then shortly again after
+# so we don't get a Terminated: message when it does die
+winch()
+{
+	WINCHED=1
+	cat "$OUT" > /dev/null 2>&1 &
+	sleep 0.01
+	kill $!
+	sleep 0.01
+}
 
-# vim: set ts=4 sw=4 noexpandtab:
+while [ $WINCHED -eq 1 ]; do
+	WINCHED=0
+	clear
+	cw "$IN" "$OUT" 2>/dev/null | awk -v cols=$(tput cols) -v me="$NICK" -v date_fmt="$date_fmt" -v srv_fmt="$serv_fmt" -v nick_fmt="$nick_fmt" -v me_fmt="$me_fmt" -v act_fmt="$act_fmt" -v reset_fmt="$reset" '{
+	sub("<", "", $3);
+	sub(">", "", $3);
+	$3 = sprintf("%12s", $3);
+
+	act  = 0;
+	srv  = 0;
+	line = $0;
+
+	mesg = substr($0, index($0, $4));
+
+	if (match(mesg, "ACTION ") == 1) {
+		act = 1;
+		gsub("", "", mesg);
+		sub("ACTION", "", mesg);
+	} else if (match($3, "-!-") > 0) {
+		srv = 1;
+	}
+	
+	while (line != "") {
+		if (line == $0) {
+			if (act == 1)
+				line = sprintf("%s %s%s", $2, $3, mesg);
+			else if (srv == 1)
+				line = sprintf("%s    %s %s", $2, $3, mesg);
+			else
+				line = sprintf("%s %s | %s", $2, $3, mesg);
+			marg = index(line, mesg);
+			str  = substr(line, 1, cols);
+			line = substr(line, cols + 1);
+		} else {
+			str = sprintf("%*s | %s", marg - 4, "", substr(line, 1, cols - marg + 1));
+			line = substr(line, cols - marg + 2);
+		}
+
+		if (act == 1) {
+			nick = sprintf("%s%s", act_fmt, $3);
+			me_col = sprintf("%s%s%s%s", reset_fmt, me_fmt, me, act_fmt);
+		} else if (srv == 1) {
+			nick = sprintf("%s%s", srv_fmt, $3);
+			me_col = sprintf("%s%s%s%s", reset_fmt, me_fmt, me, srv_fmt);
+		} else {
+			nick = sprintf("%s%s%s", nick_fmt, $3, reset_fmt);
+			me_col = sprintf("%s%s%s", me_fmt, me, reset_fmt);
+		}
+		time = sprintf("%s%s%s", date_fmt, $2, reset_fmt);
+		sub($2, time, str);
+		sub($3, nick, str);
+		gsub(me, me_col, str);
+		printf("%s%s\n", str, reset_fmt);
+	}
+
+	}' &
+	wait
+done