Much less memory leaks.
[iomenu.git] / io-ii
blob595348bfce65406848c636c1de8745948d028378
1 II_DIR="$HOME/.cache/irc"
2 USER="$(whoami)"
3 FULLNAME="$USER + ii (tools.suckless.org/ii)"
7 # Prompt for server to connect to and maintain ii connection active
9 prompt_server()
11         host="$(find . -maxdepth 1 -mindepth 1 -type d | cut -c 3- |
12                         iomenu -p 'host' -l 10)"
14         [ "$host" ] && while
15                 nohup ii -i . -s "${host#./}" -p 6667 -n "$USER" &> errors
16                 [ "$?" -eq 2 ]
17         do :
18         done &
20         sleep 1
21         switch_channel
26 # Interactively prompt for a channel to connect to, and change the tail
27 # instance
29 switch_channel()
31         find . -mindepth 1 -type d | cut -c 3- | iomenu -p '#' -l 10 > channel
33         update_screen
35         IFS='' read -r channel < channel
36         [ -f "$channel/out" ] && tail -f -n 0 "$channel/out" | print_lines &
41 # Read one character from /dev/tty in raw mode.
43 get_char()
45         stty -echo raw
46         dd if=/dev/tty count=1 status=none
47         stty sane
52 # Manage characters typed by user.
54 get_input()
56         switch_channel
58         printf '' > input
59         update_prompt
61         while char="$(get_char)"
62         do
63                 case "$char" in
64                 [[:print:]] )
65                         printf '%s' "$char" >> input
66                         ;;
68                 "$(printf '\020')" | "$(printf '\016')" )  # Ctrl-P | Ctrl-N
69                         killall -q tail  #FIXME this kills ALL running tail
70                         switch_channel
71                         ;;
73                 "$(printf '\023')" )                       # Ctrl-S
74                         prompt_server
75                         ;;
77                 "$(printf '\033')" | "$(printf '\003')" )  # Escape | Ctrl-C
78                         exit 0
79                         ;;
81                 "$(printf '\015')" )                       # Ctrl-M | Enter
82                         IFS='' read -r channel < channel
83                         sed 's/$/\n/' input > "$channel/in"
84                         printf '' > input
85                         ;;
87                 "$(printf '\025')" )                       # Ctrl-U
88                         printf '' > input
89                         ;;
91                 "$(printf '\027')" )                       # Ctrl-W
92                         sed -i 's/[^ \t]* *$//' input
93                         ;;
95                 "$(printf '\010')" | "$(printf '\177')" )  # Ctrl-H | Backspace
96                         sed -i 's/.$//' input
97                         ;;
99                 "$(printf '\014')" )                       # Ctrl-L
100                         IFS='' read -r channel < channel
101                         less "$channel/out"
102                         update_screen
103                         ;;
104                 esac
106                 update_prompt
107         done
112 # Print a prompt with the current input
114 update_prompt()
116         IFS='' read -r channel < channel
117         IFS='' read -r input   < input
119         printf '\r\033[K%s> %s' "$channel" "$input"
124 # Update the buffer lines and prompt
126 update_screen()
128         local col="$(stty size)"; col="${col#* }"
130         printf '\033[2J;\033[0;0H'
132         stty sane
134         IFS='' read -r channel < channel
135         [ -f "$channel/out" ] && tail -n "$col" "$channel/out"
137         update_prompt
139         stty raw
144 # Print lines after formatting them and update the prompt.
146 print_lines()
148         while IFS='' read -r line
149         do
150                 printf '\r\033[K%s\n' "$line"
151                 update_prompt
152         done
156 main()
158         mkdir -p "$II_DIR"
159         cd "$II_DIR"
160         touch channel input
162         trap 'update_screen'           SIGWINCH
163         trap 'stty sane; killall tail' EXIT
165         get_input
169 main $@