【问题标题】:fish shell: Prompt changing to '>'鱼壳:提示更改为'>'
【发布时间】:2020-05-21 03:39:13
【问题描述】:

我最近切换到 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 长于窗口的列大小时,整个提示只是>。我觉得使用$COLUMNS 应该可以,但是我不知道如何在回显之前检查pwd 的长度。

我不想使用prompt_pwd

提前致谢! ;)

【问题讨论】:

    标签: fish


    【解决方案1】:

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

    这是一个粗略的草图:

    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
    

    我不想使用 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
    

    【讨论】:

    • 我不知道鱼能做到var=value cmd ...。我看到from the docs 这是最近在 3.1 版中引入的
    • 有没有办法将 echo 的输出应用到变量中?
    • set var $firstline $secondline 会将行作为列表中的单独元素,因此printf %s\n $var 稍后将输出它。如果 $var 已设置,set var $var $nextlineset -a var $nextline 将向其添加 $nextline。此处不需要使用echo,也没有帮助。
    猜你喜欢
    • 1970-01-01
    • 2019-04-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-30
    • 1970-01-01
    • 2016-11-23
    • 1970-01-01
    相关资源
    最近更新 更多