eeb49bf5b0b8b700713e5b91464003cc09d1b44d

Author
Sir Mobus Gochfulshigan Dorphin Esquire XXIII <celestialtuba@gmail.com>
Committer
GitHub <noreply@github.com>
Date

Message

systemd: add prompt function to show systemd units' status (#7657)

Co-authored-by: Marc Cornellà <marc.cornella@live.com>

Diff

 1diff --git a/plugins/systemd/README.md b/plugins/systemd/README.md
 2index d91329290ab40c3af01d3d9e3ce33ff3689de300..3fa1d21183faf9bb1aba9f9b7dfc76311675c20e 100644
 3--- a/plugins/systemd/README.md
 4+++ b/plugins/systemd/README.md
 5@@ -51,3 +51,44 @@ plugins=(... systemd)
 6 
 7 You can use the above aliases as `--user` by using the prefix `scu` instead of `sc`.
 8 For example: `scu-list-units` will be aliased to `systemctl --user list-units`.
 9+
10+### Unit Status Prompt
11+
12+You can add a token to your prompt in a similar way to the gitfast plugin. To add the token
13+to your prompt, drop `$(systemd_prompt_info [unit]...)` into your prompt (more than one unit
14+may be specified).
15+
16+The plugin will add the following to your prompt for each `$unit`.
17+```
18+<prefix><unit>:<active|notactive><suffix>
19+```
20+You can control these parts with the following variables:
21+
22+- `<prefix>`: Set `$ZSH_THEME_SYSTEMD_PROMPT_PREFIX`.
23+
24+- `<suffix>`: Set `$ZSH_THEME_SYSTEMD_PROMPT_SUFFIX`.
25+
26+- `<unit>`: name passed as parameter to the function. If you want it to be in ALL CAPS,
27+  you can set the variable `$ZSH_THEME_SYSTEMD_PROMPT_CAPS` to a non-empty string.
28+
29+- `<active>`: shown if the systemd unit is active.
30+  Set `$ZSH_THEME_SYSTEMD_PROMPT_ACTIVE`.
31+
32+- `<notactive>`: shown if the systemd unit is *not* active.
33+  Set `$ZSH_THEME_SYSTEMD_PROMPT_NOTACTIVE`.
34+
35+For example, if your prompt contains `PROMPT='$(systemd_prompt_info dhcpd httpd)'` and you set the following variables:
36+
37+```
38+ZSH_THEME_SYSTEMD_PROMPT_PREFIX="["
39+ZSH_THEME_SYSTEMD_PROMPT_SUFFIX="]"
40+ZSH_THEME_SYSTEMD_PROMPT_ACTIVE="+"
41+ZSH_THEME_SYSTEMD_PROMPT_NOTACTIVE="X"
42+ZSH_THEME_SYSTEMD_PROMPT_CAPS=1
43+```
44+
45+If `dhcpd` is running, and `httpd` is not, then your prompt will look like this:
46+
47+```
48+[DHCPD: +][HTTPD: X]
49+```
50diff --git a/plugins/systemd/systemd.plugin.zsh b/plugins/systemd/systemd.plugin.zsh
51index 201ffd9989eab64b6940fc23b4912c147493bd05..f2d1d6f1cc8edee2d863529412ad69a55f09a10c 100644
52--- a/plugins/systemd/systemd.plugin.zsh
53+++ b/plugins/systemd/systemd.plugin.zsh
54@@ -73,3 +73,18 @@ alias sc-mask-now="sc-mask --now"
55 alias scu-enable-now="scu-enable --now"
56 alias scu-disable-now="scu-disable --now"
57 alias scu-mask-now="scu-mask --now"
58+
59+function systemd_prompt_info {
60+  local unit
61+  for unit in $@; do
62+    echo -n "$ZSH_THEME_SYSTEMD_PROMPT_PREFIX"
63+    [[ -n "$ZSH_THEME_SYSTEMD_PROMPT_CAPS" ]] && echo "${(U)unit}:" || echo "$unit:"
64+    if systemctl is-active $unit &>/dev/null; then
65+        echo -n "$ZSH_THEME_SYSTEMD_PROMPT_ACTIVE" 
66+    else
67+        echo -n "$ZSH_THEME_SYSTEMD_PROMPT_NOTACTIVE"
68+    fi
69+    echo -n "$ZSH_THEME_SYSTEMD_PROMPT_SUFFIX"
70+  done
71+}
72+