Anonymous avatar Anonymous committed 99b91d9

add sigwinch handling to cw.c, update cw_color.sh to match, add a simple pcw_win.sh because i forgot to add the real one

Comments (0)

Files changed (3)

 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <termios.h>
 #include <unistd.h>
 
 #include <sys/ioctl.h>
 static FILE *in = NULL, *out = NULL;
 static char buf[4096];
 
+static void sigwinch(int unused)
+{
+	struct winsize ws;
+
+	if (ioctl(STDERR, TIOCGWINSZ, &ws) < 0)
+		warn("failed ioctl");
+
+	printf("\033[2J");
+	fflush(stdout);
+	printf("\033[1;1H");
+	fflush(stdout);
+	printf("\n\033[%d;%dR\n", ws.ws_row, ws.ws_col);
+	fflush(stdout);
+
+	rewind(in);
+	while (fgets(buf, sizeof(buf), in))
+		fputs(buf, stdout);
+	fflush(stdout);
+}
+
 int main(int argc, char **argv)
 {
 	int evq, len;
 	if ((out_wd = inotify_add_watch(evq, argv[2], IN_CLOSE_NOWRITE)) < 0)
 		err(1, "failed inotify_add_watch on %s", argv[2]);
 
-	while (fgets(buf, sizeof(buf), in))
-		fputs(buf, stdout);
-	fflush(stdout);
+	sigwinch(0);
+	if (signal(SIGWINCH, sigwinch) == SIG_ERR)
+		err(1, "failed to install SIGWINCH handler");
 
 	for (;;) {
 		FD_ZERO(&rd);

extras/cw_color.sh

 act_fmt="$magenta"
 me_fmt="$red"
 
-
-
-
 NICK="$USER"
-WINCHED=1
 
 while [ $# -gt 0 ]; do
 	case $1 in
 	shift 2
 done
 
-trap "winch" WINCH
-
-# 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
+cw "$IN" "$OUT" | 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" '{
+if ($0 ~ /^\033\[[0-9]*;[0-9]*R$/) {
+    cols = substr($0, index($0, ";") + 1)
+    sub(/R$/, "", cols);
+    next;
+}
+if ($0 ~ /^\033\[/) {
+    print($0);
+    next;
 }
 
-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);
+sub("<", "", $3);
+sub(">", "", $3);
+$3 = sprintf("%12s", $3);
 
-	act  = 0;
-	srv  = 0;
-	line = $0;
+act  = 0;
+srv  = 0;
+line = $0;
 
-	mesg = substr($0, length($1 $2 $3) + 4);
+mesg = substr($0, length($1 $2 $3) + 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 {
-			if (act == 1)
-				str = sprintf("%*s %s%s", marg - 1, "", act_fmt, substr(line, 1, cols - marg + 1));
-			else if (srv == 1)
-				str = sprintf("%*s%s!  %s", marg - 4, "", srv_fmt, substr(line, 1, cols - marg + 1));
-			else
-				str = sprintf("%*s | %s", marg - 4, "", substr(line, 1, cols - marg + 1));
-			line = substr(line, cols - marg + 2);
-		}
+if (match(mesg, "\001ACTION ") == 1) {
+	act = 1;
+	gsub("\001", "", mesg);
+	sub("ACTION", "", mesg);
+} else if (match($3, "-!-") > 0) {
+	srv = 1;
+}
 
-		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("\a%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);
+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 {
+		if (act == 1)
+			str = sprintf("%*s %s%s", marg - 1, "", act_fmt, substr(line, 1, cols - marg + 1));
+		else if (srv == 1)
+			str = sprintf("%*s%s!  %s", marg - 4, "", srv_fmt, substr(line, 1, cols - marg + 1));
+		else
+			str = sprintf("%*s | %s", marg - 4, "", substr(line, 1, cols - marg + 1));
+		line = substr(line, cols - marg + 2);
 	}
 
-	}' &
-	wait
-done
+	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("\a%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);
+	fflush();
+}
+
+}'
+CPATH="$1"
+OUT="$2"
+IN="$3"
+NICK="$4"
+
+exec urxvt -title "$CPATH" -e srw -p "$(basename "$CPATH")> " cw_color.sh -i "$OUT" -o "$IN" -n "$NICK"
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.