1 # wrapper script for ii irc client http://tools.suckless.org/ii
4 II_DIR="$HOME/.cache/irc"
5 USER="${USER:-$(whoami)}"
6 FULL="$USER - http://tools.suckless.org/ii"
10 # Prompt for server to connect to and maintain ii connection active
17 find . -maxdepth 1 -mindepth 1 -type d | cut -c 3- |
18 iomenu -p 'host' -l 10
21 [ "$host" ] && pgrep -f "^ii -i . -s $host" &> /dev/null || while
22 nohup ii -i . -s "$host" -p 6667 -n "$USER" -f "$FULL" &> errors
27 while [ ! -f "$host/out" ]; do sleep 0.1; done
28 printf '%s\n' "$host" > channel
29 tail -f -n 0 "$host/out" | print_lines &
34 # Interactively prompt for a channel to connect to, and change the tail running.
40 find . -mindepth 1 -type d | cut -c 3- | iomenu -p 'chan' -l 9 > channel
44 IFS='' read -r channel < channel
45 [ -f "$channel/out" ] && tail -f -n 0 "$channel/out" | print_lines &
50 # Get channel name and kill associated "tail -f" instance.
54 read -r channel < channel
55 pid="$(pgrep -f "tail -f -n 0 $channel/out")"
56 [ "$pid" ] && kill -14 $pid
61 # Read one character from /dev/tty in raw mode.
66 dd count=1 2> /dev/null
72 # Manage characters typed by user.
80 while key="$(get_char)"
84 printf %s "$key" >> input
87 "$(printf '\020')" | "$(printf '\016')" ) # Ctrl-P Ctrl-N
88 [ "$pid" ] && kill -9 "$pid"
92 "$(printf '\023')" ) # Ctrl-S
96 "$(printf '\033')" | "$(printf '\003')" ) # Escape Ctrl-C
100 "$(printf '\015')" ) # Ctrl-M Enter
101 IFS='' read -r channel < channel
102 sed 's/$/\n/' input > "$channel/in"
106 "$(printf '\025')" ) # Ctrl-U
110 "$(printf '\027')" ) # Ctrl-W
111 sed -i 's/[^ \t]* *$//' input
114 "$(printf '\010')" | "$(printf '\177')" ) # Ctrl-H Backspace
115 sed -i 's/.$//' input
118 "$(printf '\014')" ) # Ctrl-L
119 IFS='' read -r channel < channel
131 # Print a prompt with the current input
135 size="$(stty -F /dev/tty size)"
137 IFS='' read -r channel < channel
138 IFS='' read -r input < input
140 width="$(((${#input} + ${#channel} + 4) - ${size#* }))"
142 if [ "$width" -gt 0 ]
144 input="$(printf %s "$input" | cut -c "$width"- | tr -d '\n')"
147 printf '\r\033[K%s> %s' "$channel" "$input"
152 # Update the buffer lines and prompt
156 size="$(stty -F /dev/tty size)"
158 printf '\033[H\033[J'
161 IFS='' read -r channel < channel
162 [ -f "$channel/out" ] && tail -n "${size#* }" "$channel/out"
171 # Print lines after formatting them and update the prompt.
175 while IFS='' read -r line
177 printf '\a\r\033[K%s\n' "$line"
186 cd "$II_DIR" || exit 1
189 trap 'print_screen' SIGWINCH
190 trap 'stty sane; stop' EXIT