diff options
Diffstat (limited to 'dot/local')
-rwxr-xr-x | dot/local/bin/choose-printer | 66 |
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 | |||
3 | die() | ||
4 | { | ||
5 | echo "Error: $*" >&2 | ||
6 | exit 1 | ||
7 | } | ||
8 | |||
9 | show_default() | ||
10 | { | ||
11 | d=$(lpstat -d) | ||
12 | case "$d" in | ||
13 | 'system default destination: '*) echo "${d##* }" ;; | ||
14 | esac | ||
15 | |||
16 | } | ||
17 | |||
18 | set_default() | ||
19 | { | ||
20 | ( | ||
21 | set -x | ||
22 | lpoptions -d "$1" | ||
23 | ) | ||
24 | } | ||
25 | |||
26 | printers() | ||
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 | |||
40 | show_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 | |||
54 | set -e | ||
55 | set -- $(printers) | ||
56 | |||
57 | echo | ||
58 | show_printers_with_default "$@" | ||
59 | echo | ||
60 | |||
61 | read -p 'Choose default printer> ' i || die "Error reading input" | ||
62 | [ "$i" ] || exit 0 | ||
63 | [ "$i" -gt 0 ] || die "Invalid choice: $i" | ||
64 | choice=${!i} && [ "$choice" ] || die "Invalid choice: $i" | ||
65 | |||
66 | set_default $choice | ||