Commits

Anonymous committed 077c4e3

new tmux pcw

Comments (0)

Files changed (3)

 # SERVERS should be a file with a list of servers, each followed by the ii
 # command line for that server (except the -s because we start with the server)
 # A -i prefix argument will override the one provided here, useful if you want
-# a specific server somehwere other than the given hierarchy, but it should be
+# a specific server somewhere 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 5). Each server block
 #      reopen a window when it does so. the "nickserv/in /l w" is at the end
 #      so I don't have to wait for that command before executing the others
 
+TITLE="pcw"
 PREFIX="$HOME/irc"
 SERVERS="$PREFIX/servers"
 LOG="$PREFIX/log"
 
 DELAY=5 # seconds to wait when we get a "w"
 
+if ! tmux list-sessions | grep -q "^$TITLE"; then
+	tmux new-session -d -s "$TITLE"
+fi
+
 {
 	echo
 	date
 		done >> "$PREFIX/$serv/log" 2>&1 & # background so we can start ii
 
 		IFS="$oIFS"
-		ii -i "$PREFIX" -s $args
+		eval ii -i "$PREFIX" -s $args
 		[ $? -eq 2 ] || break # only restart ii on timeout (not default for ii)
 	done >> "$PREFIX/$serv/log" 2>&1 & # instead of locking the main one...
 
 done < "$SERVERS" >> "$LOG" 2>&1
 
+tmux attach-session -t "$TITLE"
+
 # vim: set ts=4 sw=4 noexpandtab:
-#!/bin/sh
-# Make sure to change NICK to your correct nick and feel free to change the formatting to your liking
-#
+#!/bin/bash
+
+PAD=12
+NICK=emg
+
+# to find out which colors are readable...
+# for i in {0..255}; do echo $(tput setaf $i) $i foobar baz qux; done
+colors=({9..11} {13..14} {26..51} {62..87} 99 {105..123} {130..159} {165..231})
+declare -A color_cache
 
 reset="$(tput sgr0)"
+bold="$(tput bold)"
+black="$(tput setaf 0)"
+red="$(tput setaf 9)"
+cyan="$(tput setaf 14)"
+magenta="$(tput setaf 13)"
+green="$(tput setaf 10)"
+yellow="$(tput setaf 11)"
 
-bold="$(tput bold)"
-dim="$(tput dim)"
-under="$(tput smul)"
-ununder="$(tput rmul)"
-blink="$(tput blink)"
-rev="$(tput rev)"
-invis="$(tput invis)"
+action="$magenta*$cyan*$yellow*"
+notice_fmt="$black$bold"
+my_nick_fmt="$red$(echo -e "\a")"
+my_line_fmt="$cyan"
 
-black="$(tput setaf 0)"
-red="$(tput setaf 1)"
-green="$(tput setaf 2)"
-yellow="$(tput setaf 3)"
-blue="$(tput setaf 4)"
-magenta="$(tput setaf 5)"
-cyan="$(tput setaf 6)"
-white="$(tput setaf 7)"
-
-bg_black="$(tput setab 0)"
-bg_red="$(tput setab 1)"
-bg_green="$(tput setab 2)"
-bg_yellow="$(tput setab 3)"
-bg_blue="$(tput setab 4)"
-bg_magenta="$(tput setab 5)"
-bg_cyan="$(tput setab 6)"
-bg_white="$(tput setab 7)"
-
-date_fmt="$bold$black"
-serv_fmt="$bold$black"
-nick_fmt="$cyan"
-act_fmt="$magenta"
-me_fmt="$red"
-
-NICK="emg"
+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
 
-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" '{
-FS = "[ ]";
-if ($0 ~ /^[0-9]*;[0-9]*$/) {
-	cols = substr($0, index($0, ";") + 1)
-	next;
-}
-if ($0 ~ /^\033\[/) {
-	print($0);
-	next;
+set_format() {
+	if [ "$*" = "-!-" ]; then
+		format="$notice_fmt"
+	elif [ "$*" = "$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
 }
 
-sub("<", "", $3);
-sub(">", "", $3);
-$3 = sprintf("%12s", $3);
+cw "${IN:?no -i infile specified}" "${OUT:?no -o outfile specified}" |
+while read date time nick msg; do
+	nick=${nick#<}
+	nick=${nick%>}
 
-act  = 0;
-srv  = 0;
-line = $0;
+	if [ "$date" = "SIZE" ]; then
+		cols=$nick
+		continue
+	fi
 
-mesg = substr($0, length($1 $2 $3) + 4);
+	if [ ${#nick} -lt $PAD ]; then
+		head_len=$(($PAD + ${#time} + 2))
+	else
+		head_len=$((${#nick} + ${#time} + 2))
+	fi
 
-if (match(mesg, "\001ACTION ") == 1) {
-	act = 1;
-	gsub("\001", "", mesg);
-	sub("ACTION", "", mesg);
-} else if (match($3, "-!-") > 0) {
-	srv = 1;
-}
+	time="${bold}${black}${time}${reset}"
 
-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);
+	set_format "$nick"
+	nick=$(printf "%s%${PAD}s%s |%s" "$format" "$nick" "$reset" "$format")
+
+	first=1
+	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
-			line = sprintf("%s %s | %s", $2, $3, mesg);
-		marg = index(line, mesg);
-		str  = substr(line, 1, cols);
-		line = substr(line, cols + 1);
-		first = 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);
-		first = 0;
-	}
-
-	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);
-	gsub(/\[/, "\\[", $3);
-	gsub(/\]/, "\\]", $3);
-	gsub(/\|/, "\\|", $3);
-	gsub(/\^/, "\\^", $3);
-	if (first)
-	    sub($3, nick, str);
-	gsub(me, me_col, str);
-	printf("%s%s\n", str, reset_fmt);
-	fflush();
-}
-
-}'
+			printf "%${head_len}s| %s\n" "" "$format$line$reset"
+		fi
+	done
+done
 # does mean however that some ideas won't work out of the box, for instance
 # using urxvtc or screen.  You should be able to keep track of some stuff here
 # in order to decide whether or not to launch a new window, have fun!
+#
+# NOTE: if you use cw_color.sh make sure you have a 256 color terminal, or edit
+#       cw_color.sh accordingly
 
 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"
+
+#exec urxvt -title "$CPATH" -e srw -l -p "$(basename "$CPATH")> " cw_color.sh -i "$OUT" -o "$IN" -n "$NICK"
+
+# Open a new tmux window for each channel, this one has been requested for a while, and now it works!
+# make sure to add 'set -g default-terminal "screen-256color"' to your ~/.tmux.conf
+# if you don't want color, you can change the cw_color.sh part to cw etc. like the other examples
+TITLE="pcw"
+
+if ! tmux list-sessions | grep -q "^$TITLE"; then
+	tmux new-session -d -s "$TITLE" -n "$(basename "$CPATH")" "srw -p \"$(basename "$CPATH")> \" cw_color.sh -i \"$OUT\" -o \"$IN\" -n \"$NICK\""
+elif ! tmux list-windows -t "$TITLE" | grep -q "$(basename "$CPATH")"; then
+	tmux new-window -d -t "$TITLE" -n "$(basename "$CPATH")" "srw -p \"$(basename "$CPATH")> \" cw_color.sh -i \"$OUT\" -o \"$IN\" -n \"$NICK\""
+fi
 
 # The default command
 # exec urxvt -title "$CPATH" -e srw -p "$(basename "$CPATH")> " cw "$OUT" "$IN"
-#
+
 # If for some reason you really don't want to use srw...
 # exec urxvt -title "$CPATH" -e cw "$OUT" "$IN"
-#
-# For colors, bell on nick, better line wrapping etc.
+
+# For colors, bell on nick, better line wrapping etc. (this is what i use)
 # exec urxvt -title "$CPATH" -e srw -p "$(basename "$CPATH")> " cw_color.sh -i "$OUT" -o "$IN" -n "$NICK"
-#
+
 # And to add reflowing text when the window changes size.  Although beware, in
 # this case reflow means reprint the whole file at the new width.  This can be
 # slow, but if you use chat.sh it gzips the old out files everytime you startup
 # again.  (This is the one I use, along with chat.sh)
 # exec urxvt -title "$CPATH" -e srw -w -p "$(basename "$CPATH")> " cw_color.sh -i "$OUT" -o "$IN" -n "$NICK"
-# 
+ 
 # If you want to use tabbed (http://tools.suckless.org/tabbed) so each new
 # channel has it's own tab.  Start tabbed with -d then export the window id as
 # pcw_window_id (or whatever you want...) then start pcw.  It should be fairly
 # simple to do that in a script, use cw_color.sh if you want the colors etc.
 # exec urxvt -embed "$pcw_window_id" -title "$CPATH", -e srw -p "$(basename "$CPATH")> " cw "$OUT" "$IN"
-#
+
 # If you want notifications in dzen, then click on a specific message to spawn
 # a terminal for that channel.
 # NOTE: alpha quality at best, more of a proof of concept, but have fun with it