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
This commit is contained in:
Daniel Bengtsson 2020-06-17 17:55:23 +02:00 committed by Fabian Homborg
parent de9e8cb897
commit e2f03fa8a7
11 changed files with 106 additions and 56 deletions

View file

@ -31,6 +31,7 @@ Notable improvements and fixes
- ``fish_preexec`` and ``fish_postexec`` events are no longer triggered - ``fish_preexec`` and ``fish_postexec`` events are no longer triggered
for empty commands. for empty commands.
- The ``test`` builtin now better shows where an error occured (#6030). - 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 Syntax changes and new commands
------------------------------- -------------------------------

View file

@ -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

View file

@ -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

View file

@ -9,7 +9,7 @@ function fish_prompt --description 'Write out the prompt'
# Color the prompt differently when we're root # Color the prompt differently when we're root
set -l color_cwd $fish_color_cwd set -l color_cwd $fish_color_cwd
set -l suffix '>' set -l suffix '>'
if contains -- $USER root toor if fish_is_root_user
if set -q fish_color_cwd_root if set -q fish_color_cwd_root
set color_cwd $fish_color_cwd_root set color_cwd $fish_color_cwd_root
end end

View file

@ -2,17 +2,18 @@
function fish_prompt --description "Write out the prompt" function fish_prompt --description "Write out the prompt"
set -l color_cwd set -l color_cwd
set -l suffix set -l suffix
switch "$USER"
case root toor if fish_is_root_user
if set -q fish_color_cwd_root if set -q fish_color_cwd_root
set color_cwd $fish_color_cwd_root set color_cwd $fish_color_cwd_root
else else
set color_cwd $fish_color_cwd
end
set suffix '#'
case '*'
set color_cwd $fish_color_cwd set color_cwd $fish_color_cwd
set suffix '>' end
set suffix '#'
else
set color_cwd $fish_color_cwd
set suffix '>'
end end
echo -n -s "$USER" @ (prompt_hostname) ' ' (set_color $color_cwd) (prompt_pwd) (set_color normal) "$suffix " echo -n -s "$USER" @ (prompt_hostname) ' ' (set_color $color_cwd) (prompt_pwd) (set_color normal) "$suffix "

View file

@ -8,17 +8,18 @@ function fish_prompt --description "Write out the prompt"
set -l color_cwd set -l color_cwd
set -l suffix set -l suffix
switch "$USER"
case root toor if fish_is_root_user
if set -q fish_color_cwd_root if set -q fish_color_cwd_root
set color_cwd $fish_color_cwd_root set color_cwd $fish_color_cwd_root
else else
set color_cwd $fish_color_cwd
end
set suffix '#'
case '*'
set color_cwd $fish_color_cwd set color_cwd $fish_color_cwd
set suffix '>' end
set suffix '#'
else
set color_cwd $fish_color_cwd
set suffix '>'
end end
echo -n -s "$USER" @ (prompt_hostname) ' ' (set_color $color_cwd) (prompt_pwd) \ echo -n -s "$USER" @ (prompt_hostname) ' ' (set_color $color_cwd) (prompt_pwd) \

View file

@ -9,7 +9,7 @@ function fish_prompt --description 'Write out the prompt'
# Color the prompt differently when we're root # Color the prompt differently when we're root
set -l color_cwd $fish_color_cwd set -l color_cwd $fish_color_cwd
set -l suffix '>' set -l suffix '>'
if contains -- $USER root toor if fish_is_root_user
if set -q fish_color_cwd_root if set -q fish_color_cwd_root
set color_cwd $fish_color_cwd_root set color_cwd $fish_color_cwd_root
end end

View file

@ -4,10 +4,12 @@
function fish_prompt --description 'Write out the prompt, prepending the Debian chroot environment if present' 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 variable identifying the chroot you work in (used in the prompt below)
set -l debian_chroot $debian_chroot set -l debian_chroot $debian_chroot
if not set -q debian_chroot[1] if not set -q debian_chroot[1]
and test -r /etc/debian_chroot and test -r /etc/debian_chroot
set debian_chroot (cat /etc/debian_chroot) set debian_chroot (cat /etc/debian_chroot)
end end
if not set -q __fish_debian_chroot_prompt if not set -q __fish_debian_chroot_prompt
and set -q debian_chroot[1] and set -q debian_chroot[1]
and test -n "$debian_chroot" 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) ' ' echo -n -s (set_color yellow) "$__fish_debian_chroot_prompt" (set_color normal) ' '
end end
switch "$USER" if fish_is_root_user
case root toor echo -n -s "$USER" @ (prompt_hostname) ' ' (set -q fish_color_cwd_root
echo -n -s "$USER" @ (prompt_hostname) ' ' (set -q fish_color_cwd_root and set_color $fish_color_cwd_root
and set_color $fish_color_cwd_root or set_color $fish_color_cwd) (prompt_pwd) \
or set_color $fish_color_cwd) (prompt_pwd) \ (set_color normal) '# '
(set_color normal) '# '
case '*' else
echo -n -s "$USER" @ (prompt_hostname) ' ' (set_color $fish_color_cwd) (prompt_pwd) \ echo -n -s "$USER" @ (prompt_hostname) ' ' (set_color $fish_color_cwd) (prompt_pwd) \
(set_color normal) '> ' (set_color normal) '> '
end end
end end

View file

@ -6,18 +6,17 @@ function fish_prompt --description 'Informative prompt'
set -l last_pipestatus $pipestatus set -l last_pipestatus $pipestatus
set -l last_status $status set -l last_status $status
switch "$USER" if fish_is_root_user
case root toor printf '%s@%s %s%s%s# ' $USER (prompt_hostname) (set -q fish_color_cwd_root
printf '%s@%s %s%s%s# ' $USER (prompt_hostname) (set -q fish_color_cwd_root and set_color $fish_color_cwd_root
and set_color $fish_color_cwd_root or set_color $fish_color_cwd) \
or set_color $fish_color_cwd) \ (prompt_pwd) (set_color normal)
(prompt_pwd) (set_color normal) else
case '*' set -l pipestatus_string (__fish_print_pipestatus $last_status "[" "] " "|" (set_color $fish_color_status) \
set -l pipestatus_string (__fish_print_pipestatus $last_status "[" "] " "|" (set_color $fish_color_status) \ (set_color --bold $fish_color_status) $last_pipestatus)
(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) \ 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 \ $USER (prompt_hostname) (set_color $fish_color_cwd) $PWD $pipestatus_string \
(set_color normal) (set_color normal)
end end
end end

View file

@ -59,17 +59,16 @@ function fish_prompt --description 'Write out the prompt'
set -l color_cwd set -l color_cwd
set -l suffix set -l suffix
switch "$USER" if fish_is_root_user
case root toor if set -q fish_color_cwd_root
if set -q fish_color_cwd_root set color_cwd $fish_color_cwd_root
set color_cwd $fish_color_cwd_root else
else
set color_cwd $fish_color_cwd
end
set suffix '#'
case '*'
set color_cwd $fish_color_cwd set color_cwd $fish_color_cwd
set suffix '$' end
set suffix '#'
else
set color_cwd $fish_color_cwd
set suffix '$'
end end
# PWD # PWD

View file

@ -20,8 +20,8 @@ function fish_prompt
# To: # To:
# ┬─[nim@Hattori:~/w/dashboard][11:37:14][V:django20][G:master↑1|111][B:85%, 05:41:42 remaining] # ┬─[nim@Hattori:~/w/dashboard][11:37:14][V:django20][G:master↑1|111][B:85%, 05:41:42 remaining]
# │ 2 15054 0% arrêtée sleep 100000 # │ 2 15054 0% arrêtée sleep 100000
# │ 1 15048 0% arrêtée sleep 100000 # │ 1 15048 0% arrêtée sleep 100000
# ╰─>$ echo there # ╰─>$ echo there
set -l retc red set -l retc red
@ -53,19 +53,23 @@ function fish_prompt
echo -n '┬─' echo -n '┬─'
set_color -o green set_color -o green
echo -n [ echo -n [
if test "$USER" = root -o "$USER" = toor
if fish_is_root_user
set_color -o red set_color -o red
else else
set_color -o yellow set_color -o yellow
end end
echo -n $USER echo -n $USER
set_color -o white set_color -o white
echo -n @ echo -n @
if [ -z "$SSH_CLIENT" ] if [ -z "$SSH_CLIENT" ]
set_color -o blue set_color -o blue
else else
set_color -o cyan set_color -o cyan
end end
echo -n (prompt_hostname) echo -n (prompt_hostname)
set_color -o white set_color -o white
echo -n :(prompt_pwd) echo -n :(prompt_pwd)
@ -79,6 +83,7 @@ function fish_prompt
# The default mode prompt would be prefixed, which ruins our alignment. # The default mode prompt would be prefixed, which ruins our alignment.
function fish_mode_prompt function fish_mode_prompt
end end
if test "$fish_key_bindings" = fish_vi_key_bindings if test "$fish_key_bindings" = fish_vi_key_bindings
or test "$fish_key_bindings" = fish_hybrid_key_bindings or test "$fish_key_bindings" = fish_hybrid_key_bindings
set -l mode set -l mode
@ -121,12 +126,14 @@ function fish_prompt
# Background jobs # Background jobs
set_color normal set_color normal
for job in (jobs) for job in (jobs)
set_color $retc set_color $retc
echo -n '│ ' echo -n '│ '
set_color brown set_color brown
echo $job echo $job
end end
set_color normal set_color normal
set_color $retc set_color $retc
echo -n '╰─>' echo -n '╰─>'