#!/usr/bin/env bash #-*-coding:utf-8 -*- #Auto updated? # Yes #File: # fzf_powermenu #Author: # The-Repo-Club [wayne6324@gmail.com] #Github: # https://github.com/The-Repo-Club/ # #Created: # Wed 10 March 2021, 12:34:47 PM [GMT+1] #Modified: # Tue 01 November 2022, 06:45:08 AM [GMT] # #Description: # # #Dependencies: # fzf # # shellcheck disable=all getuptime() { uptime -p >/dev/null 2>&1 if [ "$?" -eq 0 ]; then # Supports most Linux distro # when the machine is up for less than '0' minutes then # 'uptime -p' returns ONLY 'up', so we need to set a default value UP_SET_OR_EMPTY=$(uptime -p | awk -F 'up ' '{print $2}') UP=${UP_SET_OR_EMPTY:-'less than a minute'} else # Supports Mac OS X, Debian 7, etc UP=$(uptime | sed -E 's/^[^,]*up *//; s/mins/minutes/; s/hrs?/hours/; s/([[:digit:]]+):0?([[:digit:]]+)/\1 hours, \2 minutes/; s/^1 hours/1 hour/; s/ 1 hours/ 1 hour/; s/min,/minutes,/; s/ 0 minutes,/ less than a minute,/; s/ 1 minutes/ 1 minute/; s/ / /; s/, *[[:digit:]]* users?.*//') fi echo "$UP" } asksetting() { options=" Lock 望 Sleep  Logout  Restart 襤 Shutdown" echo -e "Uptime: $(getuptime) $options" | fzf --prompt="Power Settings: " --border=rounded --margin=1% --color=dark --height 100% --reverse --header=" POWER MENU " --info=hidden --header-first } triggerFunction() { init_system="$(cat /proc/1/comm)" if [[ $init_system = "systemd" ]]; then systemctl "$1" elif [[ $init_system = "init" ]]; then loginctl "$1" elif [[ $init_system = "runit" ]]; then loginctl "$1" else systemctl "$1" fi } LOOPSETTING="true" while [ -n "$LOOPSETTING" ]; do CHOICE="$(asksetting "$@")" [ -n "$CHOICE" ] || exit unset LOOPSETTING case "$CHOICE" in *Logout) if [[ "$DESKTOP_SESSION" == "i3" ]]; then i3-msg exit elif [[ "$DESKTOP_SESSION" == "herbstluftwm" ]]; then herbstclient quit else pkill -KILL -u "$USER" fi ;; *Lock) multimonitorlock -l -- --time-str="%I:%M:%S %p" ;; *Shutdown) triggerFunction poweroff ;; *Restart) triggerFunction reboot ;; *Sleep) triggerFunction suspend ;; *) echo "Program terminated." && exit 1 ;; esac done