From c36a274f664062d562eab3dfa934bb527df3a132 Mon Sep 17 00:00:00 2001 From: Robert Günzler Date: Wed, 30 Nov 2022 15:51:11 +0100 Subject: bin/rcmenu: port to s6 --- bin/rcmenu | 61 +++++++++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 45 insertions(+), 16 deletions(-) (limited to 'bin') 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${_outp}" + notify-send -i "${ico}" -a rc -u critical -t 30000 "${sv}" "${action}ing ${sv} failed:\n${msg}" 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" -- cgit 1.4.1