mirror of
https://github.com/fish-shell/fish-shell
synced 2024-12-27 05:13:10 +00:00
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:
parent
de9e8cb897
commit
e2f03fa8a7
11 changed files with 106 additions and 56 deletions
|
@ -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
|
||||
-------------------------------
|
||||
|
|
31
doc_src/cmds/fish_is_root_user.rst
Normal file
31
doc_src/cmds/fish_is_root_user.rst
Normal 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
|
10
share/functions/fish_is_root_user.fish
Normal file
10
share/functions/fish_is_root_user.fish
Normal 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
|
|
@ -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
|
||||
|
|
|
@ -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 "
|
||||
|
|
|
@ -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) \
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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 '╰─>'
|
||||
|
|
Loading…
Reference in a new issue