1 # wrapper script for ii irc client http://tools.suckless.org/ii
4 II_DIR="$HOME/.cache/irc"
8 # Interactively prompt for a channel to connect to, and change the tail running.
14 find . -mindepth 1 -type d | cut -c 3- | iomenu -p 'chan' -l 9 > channel
18 IFS='' read -r channel < channel
19 [ -f "$channel/out" ] && tail -f -n 0 "$channel/out" | print_lines &
24 # Get channel name and kill associated "tail -f" instance.
28 read -r channel < channel
29 pid="$(pgrep -f "tail -f -n 0 $channel/out")"
30 [ "$pid" ] && kill -14 $pid
35 # Read one character from /dev/tty in raw mode.
40 dd count=1 2> /dev/null
46 # Manage characters typed by user.
54 while key="$(get_char)"
58 printf %s "$key" >> input
61 ( "$(printf '\020')" | "$(printf '\016')" ) # Ctrl-P Ctrl-N
62 [ "$pid" ] && kill -9 "$pid"
66 ( "$(printf '\033')" | "$(printf '\003')" ) # Escape Ctrl-C
70 ( "$(printf '\015')" ) # Ctrl-M Enter
71 IFS='' read -r channel < channel
72 sed 's/$/\n/' input > "$channel/in"
76 ( "$(printf '\025')" ) # Ctrl-U
80 ( "$(printf '\027')" ) # Ctrl-W
81 sed -i 's/[^ \t]* *$//' input
84 ( "$(printf '\010')" | "$(printf '\177')" ) # Ctrl-H Backspace
88 ( "$(printf '\014')" ) # Ctrl-L
89 IFS='' read -r channel < channel
101 # Print a prompt with the current input
105 size="$(stty -F /dev/tty size)"
107 IFS='' read -r channel < channel
108 IFS='' read -r input < input
110 width="$(((${#input} + ${#channel} + 4) - ${size#* }))"
112 if [ "$width" -gt 0 ]
114 input="$(printf %s "$input" | cut -c "$width"- | tr -d '\n')"
117 printf '\r\033[K%s> %s' "$channel" "$input"
122 # Update the buffer lines and prompt
126 size="$(stty -F /dev/tty size)"
128 printf '\033[H\033[J'
131 IFS='' read -r channel < channel
132 [ -f "$channel/out" ] && tail -n "${size#* }" "$channel/out"
141 # Print lines after formatting them and update the prompt.
145 while IFS='' read -r line
147 printf '\a\r\033[K%s\n' "$line"
156 cd "$II_DIR" || exit 1
159 trap 'print_screen' SIGWINCH
160 trap 'stty sane; stop' EXIT