From 5276b45d682541521f63aa1f93ff26621f340381 Mon Sep 17 00:00:00 2001 From: Andrew Cady Date: Sun, 11 Jul 2021 17:24:57 -0400 Subject: cmd: choose-printer --- dot/local/bin/choose-printer | 66 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100755 dot/local/bin/choose-printer 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 @@ +#!/bin/bash + +die() +{ + echo "Error: $*" >&2 + exit 1 +} + +show_default() +{ + d=$(lpstat -d) + case "$d" in + 'system default destination: '*) echo "${d##* }" ;; + esac + +} + +set_default() +{ + ( + set -x + lpoptions -d "$1" + ) +} + +printers() +{ + lpstat -p | while read line + do + case "$line" in + "printer "*) + p="${line#printer }" + echo "${p%% *}" + set -- "$p" "$line" + ;; + esac + done +} + +show_printers_with_default() +{ + default_printer=$(show_default) # can be empty + for p + do + if [ "$p" = "$default_printer" ] + then + echo "[default] $p" + else + echo " $p" + fi + done | nl +} + +set -e +set -- $(printers) + +echo +show_printers_with_default "$@" +echo + +read -p 'Choose default printer> ' i || die "Error reading input" +[ "$i" ] || exit 0 +[ "$i" -gt 0 ] || die "Invalid choice: $i" +choice=${!i} && [ "$choice" ] || die "Invalid choice: $i" + +set_default $choice -- cgit v1.2.3