fish shell: 提示更改为 '>'

fish shell: Prompt changing to '>'

我最近切换到 fish 并修改了 fish_config 中的一个提示,使其看起来像这样。

function fish_greeting
    fortune
end

function fish_prompt

    echo

    set -l retc brblack
    test $status = 0; and set retc bryellow

    set -q __fish_git_prompt_showupstream
    or set -g __fish_git_prompt_showupstream auto

    function _nim_prompt_wrapper
        set retc $argv[1]
        set cust $argv[4]
        set field_name $argv[2]
        set field_value $argv[3]

        set_color normal
        set_color $retc
        echo
        echo -n ' ├─'
        echo -n '[ '

        set_color normal
        test -n $field_name
        and echo -n $field_name

        set_color -o brblack
        echo -n ' ▶ '

        set_color $retc
        set_color $cust
        echo -n $field_value

        set_color $retc
        echo -n ' ]'
    end

    set_color $retc
    echo -n '─┬─'
    echo -n '[ '

    set_color -o red
    echo -n (prompt_hostname)
    echo -n ': '

    if test "$USER" = root -o "$USER" = toor
        set_color -o brred
    else
        set_color -o brwhite
    end
    echo -n $USER

    set_color -o brblack
    echo -n ' ▶ '

    set_color -o brcyan
    echo -n (pwd)

    set_color $retc
    echo -n ' ]'

    # Virtual Environment
    set -q VIRTUAL_ENV_DISABLE_PROMPT
    or set -g VIRTUAL_ENV_DISABLE_PROMPT true
    set -q VIRTUAL_ENV
    and _nim_prompt_wrapper $retc ' ' (basename "$VIRTUAL_ENV") cyan

    # git
    set prompt_git (fish_git_prompt | string trim -c ' ()')
    test -n "$prompt_git"
    and _nim_prompt_wrapper $retc (basename -s .git (git config --get remote.origin.url) 2> /dev/null) $prompt_git

    # New line
    echo

    # 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 ' ╰─> '
    set_color normal
end

我提示的大致布局:

─┬─[ hostname: user ▶ pwd ]
 ╰─> _

吹气是我想要的而不是>:

─┬─[ hostname: user ]
 ├─[ pwd ]
 ╰─> _

─┬─[ hostname: username ]
 ├─⎡ as_much_as_possible ⎤
 ├─⎣ the_rest_of_PWD     ⎦
 ╰─> _

但是,当 $PWD 长于 window 的列大小时,整个提示只是 >。我觉得使用 $COLUMNS 应该可以,但我不知道如何在回显之前检查 pwd 的长度。

我不想使用 prompt_pwd

提前致谢! ;)

您可以在变量中存储任何您想要的内容,然后检查它,根据需要修改它,然后回显它。

这是一个粗略的草图:

set -l firstline '─┬─[' (prompt_hostname): $USER ▶ $PWD ']'
set -l secondline
if test (string length -- "$firstline") -gt $COLUMNS
     # move $PWD to the second line
     set firstline '─┬─[' (prompt_hostname): $USER ']'
     set secondline '├─[' $PWD ']'
end
echo $firstline
echo $secondline

I DO NOT WANT TO USE prompt_pwd.

你很可能会。它处理用 ~ 替换 $HOME (这节省了很多列)并缩短为 $fish_prompt_pwd_dir_length 字符,或者如果设置为 0,则除了 ~ 之外没有缩短。

您甚至可以将缩写调整为 $COLUMNS。根据我的提示:

# Shorten pwd if prompt is too long
set -l pwd (prompt_pwd)
# 0 means unshortened
for i in $fish_prompt_pwd_dir_length 0 10 9 8 7 6 5 4 3 2 1
    set pwd (fish_prompt_pwd_dir_length=$i prompt_pwd)
    set -l len (string length -- $prompt_host_nocolor$pwd$last_status$delim' ')
    if test $len -lt $COLUMNS
        break
    end
end