Updated readme presentation
[iomenu.git] / io-ii
blob42d978fa590abc8ba4233694e607994fb69754c2
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.
10 channel()
12         stop
14         find . -mindepth 1 -type d | cut -c 3- | iomenu -p 'chan' -l 9 > channel
16         print_screen
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.
26 stop()
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.
37 get_char()
39         stty -echo raw
40         dd count=1 2> /dev/null
41         stty sane
46 # Manage characters typed by user.
48 input()
50         printf '' > input
52         print_prompt
54         while key="$(get_char)"
55         do
56                 case "$key" in
57                 ( [[:print:]] )
58                         printf %s "$key" >> input
59                         ;;
61                 ( "$(printf '\020')" | "$(printf '\016')" )  # Ctrl-P  Ctrl-N
62                         [ "$pid" ] && kill -9 "$pid"
63                         channel
64                         ;;
66                 ( "$(printf '\033')" | "$(printf '\003')" )  # Escape  Ctrl-C
67                         exit 0
68                         ;;
70                 ( "$(printf '\015')" )                       # Ctrl-M  Enter
71                         IFS='' read -r channel < channel
72                         sed 's/$/\n/' input > "$channel/in"
73                         printf '' > input
74                         ;;
76                 ( "$(printf '\025')" )                       # Ctrl-U
77                         printf '' > input
78                         ;;
80                 ( "$(printf '\027')" )                       # Ctrl-W
81                         sed -i 's/[^ \t]* *$//' input
82                         ;;
84                 ( "$(printf '\010')" | "$(printf '\177')" )  # Ctrl-H  Backspace
85                         sed -i 's/.$//' input
86                         ;;
88                 ( "$(printf '\014')" )                       # Ctrl-L
89                         IFS='' read -r channel < channel
90                         less "$channel/out"
91                         print_screen
92                         ;;
93                 esac
95                 print_prompt
96         done
101 # Print a prompt with the current input
103 print_prompt()
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 ]
113         then
114                 input="$(printf %s "$input" | cut -c "$width"- | tr -d '\n')"
115         fi
117         printf '\r\033[K%s> %s' "$channel" "$input"
122 # Update the buffer lines and prompt
124 print_screen()
126         size="$(stty -F /dev/tty size)"
128         printf '\033[H\033[J'
129         stty sane
131         IFS='' read -r channel < channel
132         [ -f "$channel/out" ] && tail -n "${size#* }" "$channel/out"
134         print_prompt
136         stty raw
141 # Print lines after formatting them and update the prompt.
143 print_lines()
145         while IFS='' read -r line
146         do
147                 printf '\a\r\033[K%s\n' "$line"
148                 print_prompt
149         done
153 main()
155         mkdir -p "$II_DIR"
156         cd "$II_DIR" || exit 1
157         touch channel in
159         trap 'print_screen'    SIGWINCH
160         trap 'stty sane; stop' EXIT
162         channel
163         input
167 main "$@"