summary refs log tree commit diff
diff options
context:
space:
mode:
-rwxr-xr-xbin/rcmenu61
1 files changed, 45 insertions, 16 deletions
diff --git a/bin/rcmenu b/bin/rcmenu
index 0fce6f4..06e940c 100755
--- a/bin/rcmenu
+++ b/bin/rcmenu
@@ -1,28 +1,57 @@
-#!/bin/sh
+#!/bin/bash
 
-set -e
 [ -n "$DEBUG" ] && set -x
+set -u
 
 export MENU_PROMPT="󰑮 rc"
+svdir=/run/s6-rc/servicedirs
 ico=/usr/share/icons/mdi/scalable/run-fast.svg
+menu_exitcode=-1
 
-handle() {
+__list() {
+	for sv in "$svdir"/*; do
+		printf "%s " "$(basename "$sv")"
+		doas -n s6-svstat "$sv"
+	done | column -t
+}
+
+__handle() {
 	while read -r line; do
-		svc=$(printf "%s" "${line}" | awk '{print $1}')
-		mode=$(printf "%s" "${line}" | awk '{print $2}')
-		act=restart
-		[ "${mode}" = "started" ] && act=stop
-		_outp=$(doas rc-service "${svc}" "${act}")
-		if [ $? = 0 ]; then
-			notify-send -i "${ico}" -a rc "${MENU_PROMPT}: ${svc}" "${_outp}"
+		sv=$(printf "%s" "${line}" | awk '{print $1}')
+		state=$(printf "%s" "${line}" | awk '{print $2}')
+
+		case "$state" in
+		up)
+			msg=$(doas -n s6-rc -v2 -d change "$sv")
+			if [ $menu_exitcode -eq 18 ]; then
+				action=stopp
+			else
+				action=restart
+				sleep 0.5
+				msg=$(doas -n s6-rc -v2 -u change "$sv")
+			fi
+			;;
+
+		down)
+			action=start
+			msg=$(doas -n s6-rc -v2 -u change "$sv")
+			;;
+
+		*)
+			msg=$(printf "unknown service state: %s\n" "$state")
+			false
+			;;
+		esac
+
+		if [ "$?" = 0 ]; then
+			notify-send -i "${ico}" -a rc "${sv}" "${action}ed"
 		else
-			notify-send -i "${ico}" -a rc -u critical -t 30000 "${MENU_PROMPT}: ${svc}" "${act}ing ${svc} failed:\n<i>${_outp}</i>"
+			notify-send -i "${ico}" -a rc -u critical -t 30000 "${sv}" "${action}ing ${sv} failed:\n<i>${msg}</i>"
 		fi
-		printf "%s\n" "${_outp}"
+		printf "%s\n" "${msg}"
 	done
 }
 
-rc-status --servicelist |
-	menu --list=10 |
-	tr -d '[' | tr -d ']' |
-	handle
+svs=$(__list | menu --list=10)
+menu_exitcode=$?
+__handle <<<"$svs"