summary refs log tree commit diff
diff options
context:
space:
mode:
authorRobert Günzler <r@gnzler.io>2020-12-07 19:28:34 +0100
committerRobert Günzler <r@gnzler.io>2021-01-21 19:39:12 +0100
commit58830b3a99613b90b4664f50da0c7c53f20010c0 (patch)
treeaac28358ed3f0bcf4677edd1fb59065879237f86
parentfa17a5238d61ebcbd65798fe03e30e18096a0041 (diff)
bin/mnts: overhaul
-rwxr-xr-xbin/mnts54
1 files changed, 33 insertions, 21 deletions
diff --git a/bin/mnts b/bin/mnts
index 2673834..cb1093c 100755
--- a/bin/mnts
+++ b/bin/mnts
@@ -1,28 +1,40 @@
 #!/bin/sh
-set -e
+#
+# DESCRIPTION:
+#  dmenu-based mount/unmount helper
+#
+
 [ -n "$DEBUG" ] && set -x
 
+# check requirements
+for app in awk jq lsblk notify-send udisksctl; do
+	if ! command -v "$app" >/dev/null; then
+		printf "%s: %s not installed but required\n" "$(basename "$0")" "$app" >&2
+		exit 1
+	fi
+done
+
 export MENU_PROMPT="mnts"
 
-filter='/(\/dev\/nvme0n1.*|\/dev\/mapper\/.*hibiki.*)/'
+handle() {
+	line=$(awk '{print $0}')
+	[ -z "${line}" ] && exit 0
+
+	path=$(echo "${line}"|awk -F\\t '{print $3}'|xargs)
+	mode=$(echo "${line}"|awk -F\\t '{if(length($3)<1) print "mount";else print "unmount";}'|xargs)
+	drv=$(echo "${line}"|awk -F\\t '{print $1}'|xargs)
+	label=$(echo "${line}"|awk -F\\t '{print $2}'|xargs)
+	[ -n "${DEBUG}" ] && printf 'DEBUG: mode:%s, drv:%s, path:%s\n' "${mode}" "${drv}" "${path}";
+	_outp=$(udisksctl "${mode}" -b "${drv}" 2>&1)
+	if [ $? = 0 ]; then \
+		notify-send -a mnts mnts "${_outp}"
+	else
+		notify-send -a mnts -u critical -t 30000 mnts "${mode}ing ${label} failed:\n<i>${_outp}</i>"
+	fi
+}
 
-{ lsblk -nrpo 'name,type,size,mountpoint' \
-	| awk "{d=\$1} d~${filter}{next} length(\$4){next} length(d)>19{d=substr(d,1,27)\"...\"} {printf \"%-30s (%6s)  %-5s\n\", d, \$3, \$4}"; \
-  mount \
-	| grep -v -E '(proc|tmpfs|devpts|sysfs|securityfs|debugfs|mqueue|configfs|fusectl|efivarfs|cgroup|binfmt_misc|nsfs|portal)' \
-	| awk "{d=\$1} d~${filter}{next} {\"df -h --output=size \"\$3\" 2>/dev/null | sed -n 2p\" | getline s} length(d)>19{d=substr(d,1,27)\"...\"} {printf \"%-30s (%6s)  %-5s\n\", d, s, \$3}"; \
-} \
+lsblk -fJ \
+	| jq -r '.. | select(.uuid?!="" and .name?) | select((.name | test("hibiki|nvme") | not) and .fstype?) | [ .name, .label, .mountpoint // "" ] | @tsv' \
+	| awk -F'\t' '{printf "/dev/%-10s\t%-15s\t%s\n", $1, $2, $3;}' \
 	| menu \
-	| xargs -i sh -c " \
-		mode=\$(echo '{}' |awk '{if(length(\$3)<1) print \"mount\";else print \"unmount\";}'); \
-		drv=\$(echo '{}' |cut -d' ' -f1); \
-		path=\$(echo '{}' |tr -d '[:blank:]' |cut -d\) -f2); \
-		case \$mode in \
-		mount) udisksctl \${mode} -b \${drv} ;; \
-		unmount) umount \${path} ;; \
-		esac; \
-		if [ \$? = 0 ]; then \
-		  notify-send mnts \"\${mode}ed \${drv}\"; \
-		else \
-		  notify-send mnts \"\${mode}ing \${drv} failed\\n\${err}\";  \
-		fi"
+	| handle