new tool: args2env
[hband-tools.git] / user-tools / lpset
blobfdbfeba8ba08db8d1cb79c6a43dd8f8ee3cf2d46
1 #!/bin/bash
3 NL=$'\n'
4 selitem=0
5 declare -a printers
6 declare -a optvals
7 declare -a args
8 declare -a jobs
10 clear_input_buffer()
12 while read -r -t 0
14 read -r -s -N 1
15 done
18 clear_screen()
20 echo -n "\e[1J\e[H"
23 selected()
25 [ $selitem = $nitems ]
28 header="\e[1;44;30m[lpset]"
29 for hotkey in Disable Enable Reject Accept LPRM Quit
31 header="$header \e[1;44;33m${hotkey:0:1}\e[0;44;37m${hotkey:1} "
32 done
33 header="$header\e[0m"
36 while true
38 dflt_prn=`LANG=C lpstat -d | sed -ne 's/.*\s\(\S\+\)/\1/p'`
39 printers=(`LANG=C lpstat -p | sed -ne 's/^\S\+\s\+\(\S\+\).*/\1/p' | sed -e "s/^\($dflt_prn\)\$/*\1/"`)
40 output="DefaultPrinter/Default Printer: ${printers[@]}$NL"
41 output=$output`lpoptions -l`
43 clear_screen
44 echo "$header"
46 nitems=0
47 nopts=0
48 optname=''
49 while IFS=: read -r key vals
51 label=${key#*/}
52 key=${key%%/*}
54 if selected
55 then
56 optname=$key
57 optvals=()
58 optsetv=
59 echo -n "> \e[1m"
60 else
61 echo -n " "
63 echo -n "$label\e[m: "
65 nval=0
66 for val in $vals
68 on=0
69 if [ "${val:0:1}" = '*' ]
70 then
71 on=1
72 val=${val:1}
73 echo -n "\e[7m"
75 if selected
76 then
77 optvals+=("$val")
78 if [ $on = 1 ]
79 then
80 optsetv=$nval
83 echo -n "$val"
84 echo -n "\e[m"
85 echo -n " "
86 let nval++
87 done
88 echo
89 let nitems++
90 let nopts++
91 done <<<"$output"
93 nlpqline=0
94 jobs=()
95 if lpstat -o "$dflt_prn" | read
96 then
97 while read lpqline
99 if [ $nlpqline = 0 ]
100 then
101 echo " \e[44;37m$lpqline\e[m"
102 else
103 re="[^ ]+ +[^ ]+ +([^ ]+)"
104 [[ "$lpqline" =~ $re ]]
105 jobs[$nitems]=${BASH_REMATCH[1]}
107 if selected
108 then
109 echo "> \e[1m$lpqline\e[m"
110 else
111 echo " $lpqline"
113 let nitems++
115 let nlpqline++
116 done < <(lpq | sed -e 1d)
118 echo "==================================="
119 n=`LANG=C lpstat -l -p "$dflt_prn" | sed -ne '/^\s*After fault/='`
120 lpstat -l -p "$dflt_prn" | head -n ${n:-11}
123 while true
125 read -r -s -N 1 c
126 case "$c" in
127 "\e")
128 read -r -s -N 1 c2
129 case "$c2" in
130 "\e")
131 break 2
133 "[")
134 read -r -s -N 1 c3
135 case "$c3" in
136 A) if [ $selitem -gt 0 ]
137 then
138 let selitem--
139 else
140 selitem=$[nitems-1]
142 break 1 ;;
143 B) if [ $selitem -lt $[nitems-1] ]
144 then
145 let selitem++
146 else
147 selitem=0
149 break 1 ;;
150 C|D)
151 if [ -n "$optname" ]
152 then
153 max=${#optvals[@]}
154 let max--
155 case "$c3" in
156 D) if [ $optsetv -gt 0 ]
157 then
158 newvaln=$[optsetv-1]
159 else
160 newvaln=$max
161 fi ;;
162 C) if [ $optsetv -lt $max ]
163 then
164 newvaln=$[optsetv+1]
165 else
166 newvaln=0
167 fi ;;
168 esac
169 newval=${optvals[$newvaln]}
170 if [ "$optname" = DefaultPrinter ]
171 then
172 lpoptions -d "$newval" >/dev/null
173 else
174 lpoptions -o "$optname=$newval"
176 break 1
179 *) clear_input_buffer
181 esac
183 *) clear_input_buffer
185 esac
187 d|e|r|a|l)
188 cmd=''
189 args=("$dflt_prn")
190 case "$c" in
191 d) cmd=cupsdisable;;
192 e) cmd=cupsenable;;
193 r) cmd=cupsreject;;
194 a) cmd=cupsaccept;;
195 l) job=${jobs[$selitem]}
196 if [ -n "$job" ]
197 then
198 cmd=lprm
199 args=("$job")
202 esac
203 if [ -n "$cmd" ]
204 then
205 command $cmd "${args[@]}"
207 break 1
209 q|Q)
210 break 2
212 esac
213 done
214 done
216 # LANG=C lpstat -d -p -o