Fixed io-ii
[iomenu.git] / io-ii
blobbd18dc82ea61756d8d22445377ed6cfd299a5e51
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
12 server()
14         stop
16         host="$(
17                 find . -maxdepth 1 -mindepth 1 -type d | cut -c 3- |
18                         iomenu -p 'host' -l 10
19         )"
21         [ "$host" ] && while
22                 nohup ii -i . -s "$host" -p 6667 -n "$USER" -f "$FULL" &> errors
23                 [ "$?" -eq 2 ]
24         do :
25         done &
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.
36 channel()
38         stop
40         find . -mindepth 1 -type d | cut -c 3- | iomenu -p 'chan' -l 9 > channel
42         print_screen
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.
52 stop()
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.
63 get_char()
65         stty -echo raw
66         dd count=1 2> /dev/null
67         stty sane
72 # Manage characters typed by user.
74 input()
76         printf '' > input
78         print_prompt
80         while key="$(get_char)"
81         do
82                 case "$key" in
83                 [[:print:]] )
84                         printf %s "$key" >> input
85                         ;;
87                 "$(printf '\020')" | "$(printf '\016')" )  # Ctrl-P  Ctrl-N
88                         [ "$pid" ] && kill -9 "$pid"
89                         channel
90                         ;;
92                 "$(printf '\023')" )                       # Ctrl-S
93                         server
94                         ;;
96                 "$(printf '\033')" | "$(printf '\003')" )  # Escape  Ctrl-C
97                         exit 0
98                         ;;
100                 "$(printf '\015')" )                       # Ctrl-M  Enter
101                         IFS='' read -r channel < channel
102                         sed 's/$/\n/' input > "$channel/in"
103                         printf '' > input
104                         ;;
106                 "$(printf '\025')" )                       # Ctrl-U
107                         printf '' > input
108                         ;;
110                 "$(printf '\027')" )                       # Ctrl-W
111                         sed -i 's/[^ \t]* *$//' input
112                         ;;
114                 "$(printf '\010')" | "$(printf '\177')" )  # Ctrl-H  Backspace
115                         sed -i 's/.$//' input
116                         ;;
118                 "$(printf '\014')" )                       # Ctrl-L
119                         IFS='' read -r channel < channel
120                         less "$channel/out"
121                         print_screen
122                         ;;
123                 esac
125                 print_prompt
126         done
131 # Print a prompt with the current input
133 print_prompt()
135         IFS='' read -r channel < channel
136         IFS='' read -r input   < input
138         printf '\r\033[K%s> %s' "$channel" "$input"
143 # Update the buffer lines and prompt
145 print_screen()
147         col="$(stty size)"; col="${col#* }"
149         printf '\033[H\033[J'
150         stty sane
152         IFS='' read -r channel < channel
153         [ -f "$channel/out" ] && tail -n "$col" "$channel/out"
155         print_prompt
157         stty raw
162 # Print lines after formatting them and update the prompt.
164 print_lines()
166         while IFS='' read -r line
167         do
168                 printf '\r\033[K%s\n' "$line"
169                 print_prompt
170         done
174 main()
176         mkdir -p "$II_DIR"
177         cd "$II_DIR" || exit 1
178         touch channel in
180         trap 'print_screen'    SIGWINCH
181         trap 'stty sane; stop' EXIT
183         server
184         input
188 main "$@"