summary refs log tree commit diff
path: root/bin/mnts
diff options
context:
space:
mode:
authorRobert Günzler <r@gnzler.io>2021-07-29 23:45:43 +0200
committerRobert Günzler <r@gnzler.io>2021-07-29 23:48:28 +0200
commite77881a72b735ca12aa055054656afdfbd6a8fc8 (patch)
tree53ed4778acca18f55141e4efbdc0a841b64116a5 /bin/mnts
parent3079e935c793a9aa1831fd165e28636eb7a87dae (diff)
bin: format scripts
Diffstat (limited to 'bin/mnts')
-rwxr-xr-xbin/mnts43
1 files changed, 29 insertions, 14 deletions
diff --git a/bin/mnts b/bin/mnts
index 817d372..56eb185 100755
--- a/bin/mnts
+++ b/bin/mnts
@@ -5,9 +5,10 @@
 #
 
 [ -n "$DEBUG" ] && set -x
+[ "$1" = "-o" ] && open=1
 
 # check requirements
-for app in awk jq lsblk notify-send udisksctl; do
+for app in awk jq lsblk notify-send udisksctl swx vifm; do
 	if ! command -v "$app" >/dev/null; then
 		printf "%s: %s not installed but required\n" "$(basename "$0")" "$app" >&2
 		exit 1
@@ -16,21 +17,35 @@ done
 
 export MENU_PROMPT="󰕓 mnts"
 
-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}"
+mount() {
+	line=$1
+	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}"
+	logfile=$(mktemp --tmpdir -t "mnts-${label}-XXXXX")
+	if udisksctl "${mode}" -b "${drv}" >"${logfile}" 2>&1; then
+		if [ "${mode}" = "mount" ]; then
+			op=plus
+			_op="+"
+		else
+			op=remove
+			_op="-"
+		fi
+		notify-send -i /usr/share/icons/mdi/scalable/harddisk-${op}.svg -a mnts mnts "$(cat "${logfile}")"
+		printf "%s\t%s\t%s\n" "${_op}" "${drv}" "${path}"
+		[ -n "${open}" ] && [ "${mode}" = "mount" ] && swx -t -- vifm "${path}"
 	else
-		notify-send -a mnts -u critical -t 30000 mnts "${mode}ing ${label} failed:\n<i>${_outp}</i>"
+		notify-send -i /usr/share/icons/mdi/scalable/harddisk.svg -a mnts -u critical -t 30000 mnts "${mode}ing ${label} failed:\n<i>$(cat "${logfile}")</i>"
 	fi
+	rm "${logfile}"
+}
+
+handle() {
+	while read -r line; do
+		mount "${line}" &
+	done
 }
 
 lsblk -fJ \