summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrew Cady <d@cryptonomic.net>2021-07-11 17:24:57 -0400
committerAndrew Cady <d@cryptonomic.net>2021-07-11 17:24:57 -0400
commit5276b45d682541521f63aa1f93ff26621f340381 (patch)
tree37dc9d5fa323c9afd501c0a6885f7581588fe8c3
parentfd9c04398afd1ae01e73b4b2c1c9214e01b0001e (diff)
cmd: choose-printer
-rwxr-xr-xdot/local/bin/choose-printer66
1 files changed, 66 insertions, 0 deletions
diff --git a/dot/local/bin/choose-printer b/dot/local/bin/choose-printer
new file mode 100755
index 0000000..1208045
--- /dev/null
+++ b/dot/local/bin/choose-printer
@@ -0,0 +1,66 @@
1#!/bin/bash
2
3die()
4{
5 echo "Error: $*" >&2
6 exit 1
7}
8
9show_default()
10{
11 d=$(lpstat -d)
12 case "$d" in
13 'system default destination: '*) echo "${d##* }" ;;
14 esac
15
16}
17
18set_default()
19{
20 (
21 set -x
22 lpoptions -d "$1"
23 )
24}
25
26printers()
27{
28 lpstat -p | while read line
29 do
30 case "$line" in
31 "printer "*)
32 p="${line#printer }"
33 echo "${p%% *}"
34 set -- "$p" "$line"
35 ;;
36 esac
37 done
38}
39
40show_printers_with_default()
41{
42 default_printer=$(show_default) # can be empty
43 for p
44 do
45 if [ "$p" = "$default_printer" ]
46 then
47 echo "[default] $p"
48 else
49 echo " $p"
50 fi
51 done | nl
52}
53
54set -e
55set -- $(printers)
56
57echo
58show_printers_with_default "$@"
59echo
60
61read -p 'Choose default printer> ' i || die "Error reading input"
62[ "$i" ] || exit 0
63[ "$i" -gt 0 ] || die "Invalid choice: $i"
64choice=${!i} && [ "$choice" ] || die "Invalid choice: $i"
65
66set_default $choice