Repository navigation
Expand file tree
/
Copy pathprinterctl
More file actions
executable file
·165 lines (144 loc) · 4.63 KB
/
Copy pathprinterctl
File metadata and controls
executable file
·165 lines (144 loc) · 4.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#!/usr/bin/env bash
# printerctl — terminal UI for managing CUPS printers and print jobs.
# Wraps lpstat, cancel, cupsenable, cupsdisable, lpadmin, lp.
set -uo pipefail
DIALOG=${DIALOG:-dialog}
TITLE="Printer Manager"
TMP=$(mktemp)
trap 'rm -f "$TMP"' EXIT
command -v "$DIALOG" >/dev/null || { echo "$DIALOG not installed"; exit 1; }
command -v lpstat >/dev/null || { echo "CUPS (lpstat) not installed"; exit 1; }
msg() { "$DIALOG" --title "$TITLE" --msgbox "$1" 16 72; }
list_printers() {
lpstat -p 2>/dev/null | awk '
/^printer / {
name = $2
if (index($0, "is idle")) status = "idle"
else if (index($0, "now printing")) status = "printing"
else if (index($0, "disabled")) status = "DISABLED"
else status = "?"
print name "|" status
}'
}
default_printer() {
lpstat -d 2>/dev/null | sed -n 's/^system default destination: //p'
}
run_cmd() {
local out rc
out=$("$@" 2>&1); rc=$?
if [ $rc -eq 0 ]; then
msg "OK: $*${out:+\n\n$out}"
else
msg "Failed (exit $rc): $*\n\n$out"
fi
}
show_queue() {
local p=$1 out
out=$(lpstat -o "$p" 2>&1)
[ -z "$out" ] && out="(queue for $p is empty)"
msg "$out"
}
show_all_queues() {
local out
out=$(lpstat -o 2>&1)
[ -z "$out" ] && out="(no jobs queued on any printer)"
msg "$out"
}
show_status() {
local p=$1 out
out=$(lpstat -p "$p" -l 2>&1)
msg "$out"
}
cancel_one() {
local p=$1 items=()
while IFS= read -r line; do
[ -z "$line" ] && continue
local id user size
id=$(awk '{print $1}' <<<"$line")
user=$(awk '{print $2}' <<<"$line")
size=$(awk '{print $3}' <<<"$line")
items+=("$id" "user=$user size=$size")
done < <(lpstat -o "$p" 2>/dev/null)
if [ ${#items[@]} -eq 0 ]; then
msg "No jobs to cancel for $p."
return
fi
"$DIALOG" --title "Cancel job — $p" --menu "Pick a job:" \
18 70 10 "${items[@]}" 2>"$TMP" || return
run_cmd cancel "$(<"$TMP")"
}
cancel_all() {
local p=$1
"$DIALOG" --title "Confirm" --yesno "Cancel ALL queued jobs on $p?" 7 50 || return
run_cmd cancel -a "$p"
}
print_test() {
local p=$1
"$DIALOG" --title "Confirm" --yesno "Print a test page on $p?" 7 50 || return
local f; f=$(mktemp)
{
echo "printerctl test page"
echo "Printer: $p"
echo "Date: $(date)"
echo "Host: $(hostname)"
} >"$f"
local out rc
out=$(lp -d "$p" "$f" 2>&1); rc=$?
rm -f "$f"
if [ $rc -eq 0 ]; then msg "Submitted:\n$out"; else msg "Failed:\n$out"; fi
}
printer_menu() {
local p=$1
while true; do
"$DIALOG" --title "$p" --cancel-label "Back" \
--menu "Action for $p:" 18 60 10 \
queue "View print queue" \
cancel "Cancel a specific job" \
cancel-all "Cancel all jobs" \
enable "Enable printer" \
disable "Disable (pause) printer" \
default "Set as system default" \
test "Print test page" \
status "Detailed status" \
2>"$TMP" || return
case "$(<"$TMP")" in
queue) show_queue "$p" ;;
cancel) cancel_one "$p" ;;
cancel-all) cancel_all "$p" ;;
enable) run_cmd cupsenable "$p" ;;
disable) run_cmd cupsdisable "$p" ;;
default) run_cmd lpadmin -d "$p" ;;
test) print_test "$p" ;;
status) show_status "$p" ;;
esac
done
}
main_menu() {
while true; do
local def items=()
def=$(default_printer)
while IFS='|' read -r name status; do
[ -z "$name" ] && continue
local marker=""
[ "$name" = "$def" ] && marker=" *"
items+=("$name" "[$status]$marker")
done < <(list_printers)
if [ ${#items[@]} -eq 0 ]; then
msg "No printers found. Is cupsd running? (systemctl status cups)"
return 1
fi
items+=("---queue" "Show all queued jobs")
items+=("---refresh" "Refresh list")
items+=("---quit" "Exit")
"$DIALOG" --title "$TITLE" --cancel-label "Quit" \
--menu "Select a printer (* = default):" 20 70 12 \
"${items[@]}" 2>"$TMP" || { clear; return 0; }
case "$(<"$TMP")" in
---refresh) continue ;;
---quit) clear; return 0 ;;
---queue) show_all_queues ;;
*) printer_menu "$(<"$TMP")" ;;
esac
done
}
main_menu