From e2f03fa8a73f37894d397b2402f905ea7da89336 Mon Sep 17 00:00:00 2001 From: Daniel Bengtsson Date: Wed, 17 Jun 2020 17:55:23 +0200 Subject: [PATCH] Add a function to check if the user is root. Add a helper function to check if the user is root. This function can be useful for the prompts for example. Modify the prompts made root checked to use the function instead. Add also the support of Administrator like a root user. Fixes: #7031 --- CHANGELOG.rst | 1 + doc_src/cmds/fish_is_root_user.rst | 31 +++++++++++++++++++ share/functions/fish_is_root_user.fish | 10 ++++++ share/functions/fish_prompt.fish | 2 +- .../web_config/sample_prompts/classic.fish | 21 +++++++------ .../sample_prompts/classic_status.fish | 21 +++++++------ .../sample_prompts/classic_vcs.fish | 2 +- .../sample_prompts/debian_chroot.fish | 19 ++++++------ .../sample_prompts/informative.fish | 23 +++++++------- .../sample_prompts/informative_vcs.fish | 19 ++++++------ .../tools/web_config/sample_prompts/nim.fish | 13 ++++++-- 11 files changed, 106 insertions(+), 56 deletions(-) create mode 100644 doc_src/cmds/fish_is_root_user.rst create mode 100644 share/functions/fish_is_root_user.fish diff --git a/CHANGELOG.rst b/CHANGELOG.rst index c204b20b1..e5fadba68 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -31,6 +31,7 @@ Notable improvements and fixes - ``fish_preexec`` and ``fish_postexec`` events are no longer triggered for empty commands. - The ``test`` builtin now better shows where an error occured (#6030). +- Add a helper function to know if the user is root (#7031). Syntax changes and new commands ------------------------------- diff --git a/doc_src/cmds/fish_is_root_user.rst b/doc_src/cmds/fish_is_root_user.rst new file mode 100644 index 000000000..6410525cf --- /dev/null +++ b/doc_src/cmds/fish_is_root_user.rst @@ -0,0 +1,31 @@ +.. _cmd-fg: + +fish_is_root_user - check if the current user is root +===================================================== + +Synopsis +-------- + +:: + + fish_is_root_user + +Description +----------- + +``fish_is_root_user`` will check if the current user is root. It can be useful +for the prompt to display something different if the user is root, for example. + + +Example +------- + +A simple example: + +:: + + function example --description 'Just an example' + if fish_is_root_user + do_something_different + end + end diff --git a/share/functions/fish_is_root_user.fish b/share/functions/fish_is_root_user.fish new file mode 100644 index 000000000..812a6a041 --- /dev/null +++ b/share/functions/fish_is_root_user.fish @@ -0,0 +1,10 @@ +# To know if the user is root. Used by several prompts to display something +# else if the user is root. + +function fish_is_root_user --description "Check if the user is root" + if contains -- $USER root toor Administrator + return 0 + end + + return 1 +end diff --git a/share/functions/fish_prompt.fish b/share/functions/fish_prompt.fish index e09191ade..b7dfe3354 100644 --- a/share/functions/fish_prompt.fish +++ b/share/functions/fish_prompt.fish @@ -9,7 +9,7 @@ function fish_prompt --description 'Write out the prompt' # Color the prompt differently when we're root set -l color_cwd $fish_color_cwd set -l suffix '>' - if contains -- $USER root toor + if fish_is_root_user if set -q fish_color_cwd_root set color_cwd $fish_color_cwd_root end diff --git a/share/tools/web_config/sample_prompts/classic.fish b/share/tools/web_config/sample_prompts/classic.fish index 6bc71c962..b4d5887c6 100644 --- a/share/tools/web_config/sample_prompts/classic.fish +++ b/share/tools/web_config/sample_prompts/classic.fish @@ -2,17 +2,18 @@ function fish_prompt --description "Write out the prompt" set -l color_cwd set -l suffix - switch "$USER" - case root toor - if set -q fish_color_cwd_root - set color_cwd $fish_color_cwd_root - else - set color_cwd $fish_color_cwd - end - set suffix '#' - case '*' + + if fish_is_root_user + if set -q fish_color_cwd_root + set color_cwd $fish_color_cwd_root + else set color_cwd $fish_color_cwd - set suffix '>' + end + + set suffix '#' + else + set color_cwd $fish_color_cwd + set suffix '>' end echo -n -s "$USER" @ (prompt_hostname) ' ' (set_color $color_cwd) (prompt_pwd) (set_color normal) "$suffix " diff --git a/share/tools/web_config/sample_prompts/classic_status.fish b/share/tools/web_config/sample_prompts/classic_status.fish index 334d17dcc..0a2d50c89 100644 --- a/share/tools/web_config/sample_prompts/classic_status.fish +++ b/share/tools/web_config/sample_prompts/classic_status.fish @@ -8,17 +8,18 @@ function fish_prompt --description "Write out the prompt" set -l color_cwd set -l suffix - switch "$USER" - case root toor - if set -q fish_color_cwd_root - set color_cwd $fish_color_cwd_root - else - set color_cwd $fish_color_cwd - end - set suffix '#' - case '*' + + if fish_is_root_user + if set -q fish_color_cwd_root + set color_cwd $fish_color_cwd_root + else set color_cwd $fish_color_cwd - set suffix '>' + end + + set suffix '#' + else + set color_cwd $fish_color_cwd + set suffix '>' end echo -n -s "$USER" @ (prompt_hostname) ' ' (set_color $color_cwd) (prompt_pwd) \ diff --git a/share/tools/web_config/sample_prompts/classic_vcs.fish b/share/tools/web_config/sample_prompts/classic_vcs.fish index 05bcd92a5..6536e52a8 100644 --- a/share/tools/web_config/sample_prompts/classic_vcs.fish +++ b/share/tools/web_config/sample_prompts/classic_vcs.fish @@ -9,7 +9,7 @@ function fish_prompt --description 'Write out the prompt' # Color the prompt differently when we're root set -l color_cwd $fish_color_cwd set -l suffix '>' - if contains -- $USER root toor + if fish_is_root_user if set -q fish_color_cwd_root set color_cwd $fish_color_cwd_root end diff --git a/share/tools/web_config/sample_prompts/debian_chroot.fish b/share/tools/web_config/sample_prompts/debian_chroot.fish index d1b89c46f..38bd46618 100644 --- a/share/tools/web_config/sample_prompts/debian_chroot.fish +++ b/share/tools/web_config/sample_prompts/debian_chroot.fish @@ -4,10 +4,12 @@ function fish_prompt --description 'Write out the prompt, prepending the Debian chroot environment if present' # Set variable identifying the chroot you work in (used in the prompt below) set -l debian_chroot $debian_chroot + if not set -q debian_chroot[1] and test -r /etc/debian_chroot set debian_chroot (cat /etc/debian_chroot) end + if not set -q __fish_debian_chroot_prompt and set -q debian_chroot[1] and test -n "$debian_chroot" @@ -19,16 +21,15 @@ function fish_prompt --description 'Write out the prompt, prepending the Debian echo -n -s (set_color yellow) "$__fish_debian_chroot_prompt" (set_color normal) ' ' end - switch "$USER" - case root toor - echo -n -s "$USER" @ (prompt_hostname) ' ' (set -q fish_color_cwd_root - and set_color $fish_color_cwd_root - or set_color $fish_color_cwd) (prompt_pwd) \ - (set_color normal) '# ' + if fish_is_root_user + echo -n -s "$USER" @ (prompt_hostname) ' ' (set -q fish_color_cwd_root + and set_color $fish_color_cwd_root + or set_color $fish_color_cwd) (prompt_pwd) \ + (set_color normal) '# ' - case '*' - echo -n -s "$USER" @ (prompt_hostname) ' ' (set_color $fish_color_cwd) (prompt_pwd) \ - (set_color normal) '> ' + else + echo -n -s "$USER" @ (prompt_hostname) ' ' (set_color $fish_color_cwd) (prompt_pwd) \ + (set_color normal) '> ' end end diff --git a/share/tools/web_config/sample_prompts/informative.fish b/share/tools/web_config/sample_prompts/informative.fish index d2b16c74d..02c7296ce 100644 --- a/share/tools/web_config/sample_prompts/informative.fish +++ b/share/tools/web_config/sample_prompts/informative.fish @@ -6,18 +6,17 @@ function fish_prompt --description 'Informative prompt' set -l last_pipestatus $pipestatus set -l last_status $status - switch "$USER" - case root toor - printf '%s@%s %s%s%s# ' $USER (prompt_hostname) (set -q fish_color_cwd_root - and set_color $fish_color_cwd_root - or set_color $fish_color_cwd) \ - (prompt_pwd) (set_color normal) - case '*' - set -l pipestatus_string (__fish_print_pipestatus $last_status "[" "] " "|" (set_color $fish_color_status) \ - (set_color --bold $fish_color_status) $last_pipestatus) + if fish_is_root_user + printf '%s@%s %s%s%s# ' $USER (prompt_hostname) (set -q fish_color_cwd_root + and set_color $fish_color_cwd_root + or set_color $fish_color_cwd) \ + (prompt_pwd) (set_color normal) + else + set -l pipestatus_string (__fish_print_pipestatus $last_status "[" "] " "|" (set_color $fish_color_status) \ + (set_color --bold $fish_color_status) $last_pipestatus) - printf '[%s] %s%s@%s %s%s %s%s%s \f\r> ' (date "+%H:%M:%S") (set_color brblue) \ - $USER (prompt_hostname) (set_color $fish_color_cwd) $PWD $pipestatus_string \ - (set_color normal) + printf '[%s] %s%s@%s %s%s %s%s%s \f\r> ' (date "+%H:%M:%S") (set_color brblue) \ + $USER (prompt_hostname) (set_color $fish_color_cwd) $PWD $pipestatus_string \ + (set_color normal) end end diff --git a/share/tools/web_config/sample_prompts/informative_vcs.fish b/share/tools/web_config/sample_prompts/informative_vcs.fish index b36c248e6..62fac6eb8 100644 --- a/share/tools/web_config/sample_prompts/informative_vcs.fish +++ b/share/tools/web_config/sample_prompts/informative_vcs.fish @@ -59,17 +59,16 @@ function fish_prompt --description 'Write out the prompt' set -l color_cwd set -l suffix - switch "$USER" - case root toor - if set -q fish_color_cwd_root - set color_cwd $fish_color_cwd_root - else - set color_cwd $fish_color_cwd - end - set suffix '#' - case '*' + if fish_is_root_user + if set -q fish_color_cwd_root + set color_cwd $fish_color_cwd_root + else set color_cwd $fish_color_cwd - set suffix '$' + end + set suffix '#' + else + set color_cwd $fish_color_cwd + set suffix '$' end # PWD diff --git a/share/tools/web_config/sample_prompts/nim.fish b/share/tools/web_config/sample_prompts/nim.fish index 3215064b3..6034bb41f 100644 --- a/share/tools/web_config/sample_prompts/nim.fish +++ b/share/tools/web_config/sample_prompts/nim.fish @@ -20,8 +20,8 @@ function fish_prompt # To: # ┬─[nim@Hattori:~/w/dashboard]─[11:37:14]─[V:django20]─[G:master↑1|●1✚1…1]─[B:85%, 05:41:42 remaining] - # │ 2 15054 0% arrêtée sleep 100000 - # │ 1 15048 0% arrêtée sleep 100000 + # │ 2 15054 0% arrêtée sleep 100000 + # │ 1 15048 0% arrêtée sleep 100000 # ╰─>$ echo there set -l retc red @@ -53,19 +53,23 @@ function fish_prompt echo -n '┬─' set_color -o green echo -n [ - if test "$USER" = root -o "$USER" = toor + + if fish_is_root_user set_color -o red else set_color -o yellow end + echo -n $USER set_color -o white echo -n @ + if [ -z "$SSH_CLIENT" ] set_color -o blue else set_color -o cyan end + echo -n (prompt_hostname) set_color -o white echo -n :(prompt_pwd) @@ -79,6 +83,7 @@ function fish_prompt # The default mode prompt would be prefixed, which ruins our alignment. function fish_mode_prompt end + if test "$fish_key_bindings" = fish_vi_key_bindings or test "$fish_key_bindings" = fish_hybrid_key_bindings set -l mode @@ -121,12 +126,14 @@ function fish_prompt # Background jobs set_color normal + for job in (jobs) set_color $retc echo -n '│ ' set_color brown echo $job end + set_color normal set_color $retc echo -n '╰─>'