From 5b2f99bcb7186bfb11b91a8686d750af95e41eff Mon Sep 17 00:00:00 2001 From: scrouthtv Date: Wed, 29 Dec 2021 10:31:29 +0100 Subject: [PATCH] fix(systemd): remove `sudo` from power-related aliases (#9441) --- plugins/systemd/README.md | 13 ++++--- plugins/systemd/systemd.plugin.zsh | 62 +++++++++++++++++++++--------- 2 files changed, 52 insertions(+), 23 deletions(-) diff --git a/plugins/systemd/README.md b/plugins/systemd/README.md index 3fa1d2118..ffa0567a8 100644 --- a/plugins/systemd/README.md +++ b/plugins/systemd/README.md @@ -3,7 +3,8 @@ The systemd plugin provides many useful aliases for systemd. To use it, add systemd to the plugins array of your zshrc file: -``` + +```zsh plugins=(... systemd) ``` @@ -22,7 +23,7 @@ plugins=(... systemd) | `sc-show-environment` | `systemctl show-environment` | Dump the systemd manager environment block | | `sc-cat` | `systemctl cat` | Show backing files of one or more units | | `sc-list-timers` | `systemctl list-timers` | List timer units currently in memory | -| **Aliases with sudo** | +| **Aliases with sudo** ||| | `sc-start` | `sudo systemctl start` | Start Unit(s) | | `sc-stop` | `sudo systemctl stop` | Stop Unit(s) | | `sc-reload` | `sudo systemctl reload` | Reload Unit(s) | @@ -59,9 +60,11 @@ to your prompt, drop `$(systemd_prompt_info [unit]...)` into your prompt (more t may be specified). The plugin will add the following to your prompt for each `$unit`. -``` + +```text : ``` + You can control these parts with the following variables: - ``: Set `$ZSH_THEME_SYSTEMD_PROMPT_PREFIX`. @@ -79,7 +82,7 @@ You can control these parts with the following variables: For example, if your prompt contains `PROMPT='$(systemd_prompt_info dhcpd httpd)'` and you set the following variables: -``` +```sh ZSH_THEME_SYSTEMD_PROMPT_PREFIX="[" ZSH_THEME_SYSTEMD_PROMPT_SUFFIX="]" ZSH_THEME_SYSTEMD_PROMPT_ACTIVE="+" @@ -89,6 +92,6 @@ ZSH_THEME_SYSTEMD_PROMPT_CAPS=1 If `dhcpd` is running, and `httpd` is not, then your prompt will look like this: -``` +```text [DHCPD: +][HTTPD: X] ``` diff --git a/plugins/systemd/systemd.plugin.zsh b/plugins/systemd/systemd.plugin.zsh index c6fd52990..e9ac64382 100644 --- a/plugins/systemd/systemd.plugin.zsh +++ b/plugins/systemd/systemd.plugin.zsh @@ -1,3 +1,4 @@ +# systemctl aliases user_commands=( cat get-default @@ -14,7 +15,8 @@ user_commands=( list-units show show-environment - status) + status +) sudo_commands=( add-requires @@ -28,8 +30,6 @@ sudo_commands=( emergency enable halt - hibernate - hybrid-sleep import-environment isolate kexec @@ -38,34 +38,53 @@ sudo_commands=( list-machines load mask - poweroff preset preset-all - reboot reenable reload reload-or-restart reset-failed rescue - restart revert set-default set-environment set-property start stop - suspend switch-root try-reload-or-restart try-restart unmask - unset-environment) + unset-environment +) -for c in $user_commands; do; alias sc-$c="systemctl $c"; done -for c in $sudo_commands; do; alias sc-$c="sudo systemctl $c"; done -for c in $user_commands; do; alias scu-$c="systemctl --user $c"; done -for c in $sudo_commands; do; alias scu-$c="systemctl --user $c"; done +power_commands=( + hibernate + hybrid-sleep + poweroff + reboot + restart + suspend +) +for c in $user_commands; do + alias "sc-$c"="systemctl $c" + alias "scu-$c"="systemctl --user $c" +done + +for c in $sudo_commands; do + alias "sc-$c"="sudo systemctl $c" + alias "scu-$c"="systemctl --user $c" +done + +for c in $power_commands; do + alias "sc-$c"="systemctl $c" +done + +unset c user_commands sudo_commands power_commands + + +# --now commands alias sc-enable-now="sc-enable --now" alias sc-disable-now="sc-disable --now" alias sc-mask-now="sc-mask --now" @@ -74,17 +93,24 @@ alias scu-enable-now="scu-enable --now" alias scu-disable-now="scu-disable --now" alias scu-mask-now="scu-mask --now" + function systemd_prompt_info { local unit - for unit in $@; do + for unit in "$@"; do echo -n "$ZSH_THEME_SYSTEMD_PROMPT_PREFIX" - [[ -n "$ZSH_THEME_SYSTEMD_PROMPT_CAPS" ]] && echo -n "${(U)unit}:" || echo -n "$unit:" - if systemctl is-active $unit &>/dev/null; then - echo -n "$ZSH_THEME_SYSTEMD_PROMPT_ACTIVE" + + if [[ -n "$ZSH_THEME_SYSTEMD_PROMPT_CAPS" ]]; then + echo -n "${(U)unit:gs/%/%%}:" else - echo -n "$ZSH_THEME_SYSTEMD_PROMPT_NOTACTIVE" + echo -n "${unit:gs/%/%%}:" fi + + if systemctl is-active "$unit" &>/dev/null; then + echo -n "$ZSH_THEME_SYSTEMD_PROMPT_ACTIVE" + else + echo -n "$ZSH_THEME_SYSTEMD_PROMPT_NOTACTIVE" + fi + echo -n "$ZSH_THEME_SYSTEMD_PROMPT_SUFFIX" done } -