Permitting file as argument
[iomenu.git] / io-ii
blob83ff3cda11475ea4eed5e59859e046fb5aae5faa
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" ] && pgrep -f "^ii -i . -s $host" &> /dev/null || 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         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 ]
143         then
144                 input="$(printf %s "$input" | cut -c "$width"- | tr -d '\n')"
145         fi
147         printf '\r\033[K%s> %s' "$channel" "$input"
152 # Update the buffer lines and prompt
154 print_screen()
156         size="$(stty -F /dev/tty size)"
158         printf '\033[H\033[J'
159         stty sane
161         IFS='' read -r channel < channel
162         [ -f "$channel/out" ] && tail -n "${size#* }" "$channel/out"
164         print_prompt
166         stty raw
171 # Print lines after formatting them and update the prompt.
173 print_lines()
175         while IFS='' read -r line
176         do
177                 printf '\a\r\033[K%s\n' "$line"
178                 print_prompt
179         done
183 main()
185         mkdir -p "$II_DIR"
186         cd "$II_DIR" || exit 1
187         touch channel in
189         trap 'print_screen'    SIGWINCH
190         trap 'stty sane; stop' EXIT
192         server
193         input
197 main "$@"