PS1 行与 Git 当前分支和颜色

发布于 2024-10-01 04:18:44 字数 115 浏览 5 评论 0原文

这是我当前的 PS1:

export PS1='[\u@\h \W$(__git_ps1 " (%s)")]\$ '

如何以不同的颜色显示当前分支?

Here is my current PS1:

export PS1='[\u@\h \W$(__git_ps1 " (%s)")]\$ '

How can I display the current branch in a different color?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(16

揽月 2024-10-08 04:18:44

这是一部分(没有 Ruby):

function color_my_prompt {
    local __user_and_host="\[\033[01;32m\]\u@\h"
    local __cur_location="\[\033[01;34m\]\w"
    local __git_branch_color="\[\033[31m\]"
    #local __git_branch="\`ruby -e \"print (%x{git branch 2> /dev/null}.grep(/^\*/).first || '').gsub(/^\* (.+)$/, '(\1) ')\"\`"
    local __git_branch='`git branch 2> /dev/null | grep -e ^* | sed -E  s/^\\\\\*\ \(.+\)$/\(\\\\\1\)\ /`'
    local __prompt_tail="\[\033[35m\]$"
    local __last_color="\[\033[00m\]"
    export PS1="$__user_and_host $__cur_location $__git_branch_color$__git_branch$__prompt_tail$__last_color "
}
color_my_prompt

看起来像这样(使用我自己的终端调色板):

Colored Prompt

另外,请参阅这篇文章。

Here is, part by part (and no Ruby):

function color_my_prompt {
    local __user_and_host="\[\033[01;32m\]\u@\h"
    local __cur_location="\[\033[01;34m\]\w"
    local __git_branch_color="\[\033[31m\]"
    #local __git_branch="\`ruby -e \"print (%x{git branch 2> /dev/null}.grep(/^\*/).first || '').gsub(/^\* (.+)$/, '(\1) ')\"\`"
    local __git_branch='`git branch 2> /dev/null | grep -e ^* | sed -E  s/^\\\\\*\ \(.+\)$/\(\\\\\1\)\ /`'
    local __prompt_tail="\[\033[35m\]$"
    local __last_color="\[\033[00m\]"
    export PS1="$__user_and_host $__cur_location $__git_branch_color$__git_branch$__prompt_tail$__last_color "
}
color_my_prompt

Looks like this (with my own terminal palette):

Colored prompt

Also, see this and this article.

任谁 2024-10-08 04:18:44

您可以使用以下内容将所需颜色的部分包裹起来:

\e[0;32m - 设置颜色(在本例中为绿色)

\e[m -将颜色设置回默认值

例如,这会将提示设置为当前路径的最后一个标记(绿色),后跟默认颜色的 $

export PS1='\e[0;32m\w\e[m 

也可以使用其他颜色。请参阅这篇文章的彩色部分以获得完整的列表的替代方案。

也可以使用其他颜色。请参阅这篇文章的彩色部分以获得完整的列表的替代方案。

You can wrap the part that you want in colour with the following:

\e[0;32m - sets colour (in this case, to green)

\e[m - sets colour back to the default

For example, this sets the prompt to the last token of the current path, in green, followed by $ in the default colour:

export PS1='\e[0;32m\w\e[m 

Other colours are available too. Have a look at this article under colorization for a comprehensive list of alternatives.

Other colours are available too. Have a look at this article under colorization for a comprehensive list of alternatives.

梦旅人picnic 2024-10-08 04:18:44

这是我的 PS1 行:

\n\[\e[1;37m\]|-- \[\e[1;32m\]\u\[\e[0;39m\]@\[\e[1;36m\]\h\[\e[0;39m\]:\[\e[1;33m\]\w\[\e[0;39m\]\[\e[1;35m\]$(__git_ps1 " (%s)")\[\e[0;39m\] \[\e[1;37m\]--|\[\e[0;39m\]\n$

alt text

Here is my PS1 line:

\n\[\e[1;37m\]|-- \[\e[1;32m\]\u\[\e[0;39m\]@\[\e[1;36m\]\h\[\e[0;39m\]:\[\e[1;33m\]\w\[\e[0;39m\]\[\e[1;35m\]$(__git_ps1 " (%s)")\[\e[0;39m\] \[\e[1;37m\]--|\[\e[0;39m\]\n$

alt text

顾冷 2024-10-08 04:18:44
function pc {
  [ -d .git ] && git name-rev --name-only @
}
PS1='\e];\s\a\n\e[33m\w \e[36m$(pc)\e[m\n$ '

ps1

来源

function pc {
  [ -d .git ] && git name-rev --name-only @
}
PS1='\e];\s\a\n\e[33m\w \e[36m$(pc)\e[m\n$ '

ps1

Source

度的依靠╰つ 2024-10-08 04:18:44

这是我的 PS1 解决方案。

它在带有小说主题的 Mac 上看起来很棒。
抱歉,但我的缩进有点被破坏了。破解它直到你喜欢为止。

function we_are_in_git_work_tree {
    git rev-parse --is-inside-work-tree &> /dev/null
}

function parse_git_branch {
    if we_are_in_git_work_tree
    then
    local BR=$(git rev-parse --symbolic-full-name --abbrev-ref HEAD 2> /dev/null)
    if [ "$BR" == HEAD ]
    then
        local NM=$(git name-rev --name-only HEAD 2> /dev/null)
        if [ "$NM" != undefined ]
        then echo -n "@$NM"
        else git rev-parse --short HEAD 2> /dev/null
        fi
    else
        echo -n $BR
       fi
    fi
}

function parse_git_status {
    if we_are_in_git_work_tree
    then
    local ST=$(git status --short 2> /dev/null)
    if [ -n "$ST" ]
    then echo -n " + "
    else echo -n " - "
    fi
    fi
}

function pwd_depth_limit_2 {
    if [ "$PWD" = "$HOME" ]
    then echo -n "~"
    else pwd | sed -e "s|.*/\(.*/.*\)|\1|"
    fi
}

COLBROWN="\[\033[1;33m\]"
COLRED="\[\033[1;31m\]"
COLCLEAR="\[\033[0m\]"

# Export all these for subshells
export -f parse_git_branch parse_git_status we_are_in_git_work_tree pwd_depth_limit_2
export PS1="$COLRED<$COLBROWN \$(pwd_depth_limit_2)$COLRED\$(parse_git_status)$COLBROWN\$(parse_git_branch) $COLRED>$COLCLEAR "
export TERM="xterm-color"

如果您在分行结账,您会得到分行名称。

如果您处于刚刚初始化的 Git 项目中,您只需得到“@”。

如果你是无头的,你会得到一个与某个分支或标签相关的好听的人名,名字前面有一个“@”。

如果你是无头的并且不是某个分支或标签的祖先,你只会得到短 SHA1。

另外,红色“-”表示干净的工作目录和索引,红色“+”表示相反。

This is my PS1 solution.

It looks great on a Mac with the Novel theme.
Sorry, but my indentation got munged a bit. Hack it till you like it.

function we_are_in_git_work_tree {
    git rev-parse --is-inside-work-tree &> /dev/null
}

function parse_git_branch {
    if we_are_in_git_work_tree
    then
    local BR=$(git rev-parse --symbolic-full-name --abbrev-ref HEAD 2> /dev/null)
    if [ "$BR" == HEAD ]
    then
        local NM=$(git name-rev --name-only HEAD 2> /dev/null)
        if [ "$NM" != undefined ]
        then echo -n "@$NM"
        else git rev-parse --short HEAD 2> /dev/null
        fi
    else
        echo -n $BR
       fi
    fi
}

function parse_git_status {
    if we_are_in_git_work_tree
    then
    local ST=$(git status --short 2> /dev/null)
    if [ -n "$ST" ]
    then echo -n " + "
    else echo -n " - "
    fi
    fi
}

function pwd_depth_limit_2 {
    if [ "$PWD" = "$HOME" ]
    then echo -n "~"
    else pwd | sed -e "s|.*/\(.*/.*\)|\1|"
    fi
}

COLBROWN="\[\033[1;33m\]"
COLRED="\[\033[1;31m\]"
COLCLEAR="\[\033[0m\]"

# Export all these for subshells
export -f parse_git_branch parse_git_status we_are_in_git_work_tree pwd_depth_limit_2
export PS1="$COLRED<$COLBROWN \$(pwd_depth_limit_2)$COLRED\$(parse_git_status)$COLBROWN\$(parse_git_branch) $COLRED>$COLCLEAR "
export TERM="xterm-color"

If you are checked out at a branch, you get the branch name.

If you are in a just init'd Git project, you just get '@'.

If you are headless, you get a nice human name relative to some branch or tag, with an '@' preceding the name.

If you are headless and not an ancestor of some branch or tag you just get the short SHA1.

In addition, a red '-' signifies a clean work directory and index, and a red '+' signifies the opposite.

很酷又爱笑 2024-10-08 04:18:44

我的超级强大的多行 Linux 提示符!

将其放在 .bashrc 中或更好:将其保存在 /etc/bash-prompt 中并从 .bashrc 中获取它。
使用 tput 应该是处理颜色的正确方法。

#!/bin/bash

set_prompt()
{
   local last_cmd=$?
   local txtreset='$(tput sgr0)'
   local txtbold='$(tput bold)'
   local txtblack='$(tput setaf 0)'
   local txtred='$(tput setaf 1)'
   local txtgreen='$(tput setaf 2)'
   local txtyellow='$(tput setaf 3)'
   local txtblue='$(tput setaf 4)'
   local txtpurple='$(tput setaf 5)'
   local txtcyan='$(tput setaf 6)'
   local txtwhite='$(tput setaf 7)'
   # unicode "✗"
   local fancyx='\342\234\227'
   # unicode "✓"
   local checkmark='\342\234\223'
   # Line 1: Full date + full time (24h)
   # Line 2: current path
   PS1="\[$txtbold\]\[$txtwhite\]\n\D{%A %d %B %Y %H:%M:%S}\n\[$txtgreen\]\w\n"
   # User color: red for root, yellow for others
   if [[ $EUID == 0 ]]; then
       PS1+="\[$txtred\]"
   else
       PS1+="\[$txtyellow\]"   
   fi
   # Line 3: user@host
   PS1+="\u\[$txtwhite\]@\h\n"
   # Line 4: a red "✗" or a green "✓" and the error number
   if [[ $last_cmd == 0 ]]; then
      PS1+="\[$txtgreen\]$checkmark \[$txtwhite\](0)"
   else
      PS1+="\[$txtred\]$fancyx \[$txtwhite\]($last_cmd)"
   fi
   # Line 4: green git branch
   PS1+="\[$txtgreen\]$(__git_ps1 ' (%s)')\[$txtwhite\]"
   # Line 4: good old prompt, $ for user, # for root
   PS1+=" \\$ "
}
PROMPT_COMMAND='set_prompt'

My uber-powerful multi-line Linux prompt!

Put it either in your .bashrc or better: save it in /etc/bash-prompt and source it from your .bashrc.
Using tput is supposed to be the right way to do colors.

#!/bin/bash

set_prompt()
{
   local last_cmd=$?
   local txtreset='$(tput sgr0)'
   local txtbold='$(tput bold)'
   local txtblack='$(tput setaf 0)'
   local txtred='$(tput setaf 1)'
   local txtgreen='$(tput setaf 2)'
   local txtyellow='$(tput setaf 3)'
   local txtblue='$(tput setaf 4)'
   local txtpurple='$(tput setaf 5)'
   local txtcyan='$(tput setaf 6)'
   local txtwhite='$(tput setaf 7)'
   # unicode "✗"
   local fancyx='\342\234\227'
   # unicode "✓"
   local checkmark='\342\234\223'
   # Line 1: Full date + full time (24h)
   # Line 2: current path
   PS1="\[$txtbold\]\[$txtwhite\]\n\D{%A %d %B %Y %H:%M:%S}\n\[$txtgreen\]\w\n"
   # User color: red for root, yellow for others
   if [[ $EUID == 0 ]]; then
       PS1+="\[$txtred\]"
   else
       PS1+="\[$txtyellow\]"   
   fi
   # Line 3: user@host
   PS1+="\u\[$txtwhite\]@\h\n"
   # Line 4: a red "✗" or a green "✓" and the error number
   if [[ $last_cmd == 0 ]]; then
      PS1+="\[$txtgreen\]$checkmark \[$txtwhite\](0)"
   else
      PS1+="\[$txtred\]$fancyx \[$txtwhite\]($last_cmd)"
   fi
   # Line 4: green git branch
   PS1+="\[$txtgreen\]$(__git_ps1 ' (%s)')\[$txtwhite\]"
   # Line 4: good old prompt, $ for user, # for root
   PS1+=" \\$ "
}
PROMPT_COMMAND='set_prompt'
〃温暖了心ぐ 2024-10-08 04:18:44
  1. 将其添加到 ~/.bashrc
parse_git_branch() {
     git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/'
}

export PS1="\u@\h \[\e[32m\]\w \[\e[91m\]$(parse_git_branch)\[\e[00m\]$ "
  1. 重新启动终端,或源 ~/.bashrc

在此处输入图像描述

更多详细信息:https://medium.com/@ thucnc/如何显示当前 git-branch-with-colors-in-bash-prompt-380d05a24745

  1. Add this to ~/.bashrc:
parse_git_branch() {
     git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/'
}

export PS1="\u@\h \[\e[32m\]\w \[\e[91m\]$(parse_git_branch)\[\e[00m\]$ "
  1. Restart the terminal, or source ~/.bashrc:

enter image description here

More detail: https://medium.com/@thucnc/how-to-show-current-git-branch-with-colors-in-bash-prompt-380d05a24745

锦欢 2024-10-08 04:18:44

@cmcginty 提示符的修改版本,添加了 git 解析函数并使用略有不同的间距:

# So I know where I am in repos:
parse_git_branch() {
    git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
}

# Modified from:
# https://stackoverflow.com/a/4138531/2662028
export PS1='\n\[\e[1;37m\]|-- \[\e[1;32m\]\u\[\e[0;39m\]@\[\e[1;36m\]\h\[\e[0;39m\]:\[\e[1;33m\]\w\[\e[0;39m\]\[\e[1;35m\]$(parse_git_branch " (%s)")\[\e[0;39m\] \[\e[1;37m\]--|\[\e[0;39m\]\n\$ '

这也在提示符中使用 \$ 而不是 $ >,这意味着当您是 root 时,您将得到 #

Modified version of @cmcginty's prompt that adds in the git parsing function and uses slightly different spacing:

# So I know where I am in repos:
parse_git_branch() {
    git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
}

# Modified from:
# https://stackoverflow.com/a/4138531/2662028
export PS1='\n\[\e[1;37m\]|-- \[\e[1;32m\]\u\[\e[0;39m\]@\[\e[1;36m\]\h\[\e[0;39m\]:\[\e[1;33m\]\w\[\e[0;39m\]\[\e[1;35m\]$(parse_git_branch " (%s)")\[\e[0;39m\] \[\e[1;37m\]--|\[\e[0;39m\]\n\$ '

This also uses \$ in the prompt instead of $, which means you will get # when you are root.

青朷 2024-10-08 04:18:44

对于我的带有 Homebrew 主题的 Mac,这效果非常好。完全调试且速度非常快,并且完全独立。
额外奖励:足够智能,当您实际上位于 git 存储库中时,仅将 git 分支显示为提示的一部分! :)

# Color prompt for git
reset=$(tput sgr0)
boldgreen=$(tput setaf 2)$(tput bold)
cyan=$(tput sgr0)$(tput setaf 6)
boldred=$(tput setaf 1)$(tput bold)
boldwhite=$(tput setaf 7)$(tput bold)
boldyellow=$(tput setaf 3)$(tput bold)

PARENCLR=

它看起来像这样:
Mac + Homebrew + Color Git Prompt

如果您想要完整路径(或删除 . ../),然后只需将 -W 更改为 -w (并删除 .../)。

\001\e[0;36m\002' BRANCHCLR=

它看起来像这样:
Mac + Homebrew + Color Git Prompt

如果您想要完整路径(或删除 . ../),然后只需将 -W 更改为 -w (并删除 .../)。

\001\e[1;33m\002' alias branchname="git branch 2>/dev/null | sed -ne 's/^* \(.*\)/ ${PARENCLR}(${BRANCHCLR}\1${PARENCLR}\)/p'" GIT_STATUS='$(branchname)' PROMPT_CHAR="\$" PS1="\[$boldgreen\]\u\[$cyan\]::\[$boldred\]\h \[$cyan\]{\[$boldwhite\].../\W\[$cyan\]}\[$reset\]$GIT_STATUS\[$reset\]$PROMPT_CHAR "

它看起来像这样:
Mac + Homebrew + Color Git Prompt

如果您想要完整路径(或删除 . ../),然后只需将 -W 更改为 -w (并删除 .../)。

For my Mac with the Homebrew theme, this works really well. Fully debugged and very fast, and completely self-contained.
BONUS: Smart enough to ONLY show a git branch as part of the prompt when you're actually in a git repo! :)

# Color prompt for git
reset=$(tput sgr0)
boldgreen=$(tput setaf 2)$(tput bold)
cyan=$(tput sgr0)$(tput setaf 6)
boldred=$(tput setaf 1)$(tput bold)
boldwhite=$(tput setaf 7)$(tput bold)
boldyellow=$(tput setaf 3)$(tput bold)

PARENCLR=

Here's what it looks like:
Mac + Homebrew + Color Git Prompt

If you want to have the full path (or remove the .../), then just change the -W to -w (and remove the .../).

\001\e[0;36m\002' BRANCHCLR=

Here's what it looks like:
Mac + Homebrew + Color Git Prompt

If you want to have the full path (or remove the .../), then just change the -W to -w (and remove the .../).

\001\e[1;33m\002' alias branchname="git branch 2>/dev/null | sed -ne 's/^* \(.*\)/ ${PARENCLR}(${BRANCHCLR}\1${PARENCLR}\)/p'" GIT_STATUS='$(branchname)' PROMPT_CHAR="\$" PS1="\[$boldgreen\]\u\[$cyan\]::\[$boldred\]\h \[$cyan\]{\[$boldwhite\].../\W\[$cyan\]}\[$reset\]$GIT_STATUS\[$reset\]$PROMPT_CHAR "

Here's what it looks like:
Mac + Homebrew + Color Git Prompt

If you want to have the full path (or remove the .../), then just change the -W to -w (and remove the .../).

我三岁 2024-10-08 04:18:44

看看liquidprompt:

https://github.com/nojhan/liquidprompt

可能有点太重了 来关闭功能,

您的要求,但您可以通过设置LP_ENABLE_...=0

请参阅上页的文档。

Take a look at liquidprompt:

https://github.com/nojhan/liquidprompt

Maybe a bit too heavy for your requirements, but you can switch features off by setting

LP_ENABLE_...=0

See the documentation on above page.

掩饰不了的爱 2024-10-08 04:18:44

只需使用适当的参数调用 tput 即可。请参阅 tput(1)terminfo(5) 手册页。

Just invoke tput with the appropriate parameters. See the tput(1) and terminfo(5) man pages.

み格子的夏天 2024-10-08 04:18:44

这是我的

export PS1="\n\[\033[1;30m\][$:$PPID - \j:\!\[\033[1;30m\]]\[\033[0;36m\] \T \
\[\033[1;30m\][\[\033[1;34m\]\u@\H\[\033[1;30m\]:\[\033[0;37m\]${SSH_TTY:-o} \
\[\033[0;32m\]+${SHLVL}\[\033[1;30m\]] \[\033[1;37m\]\w\[\033[0;37m\]\[\033[1;34m\]\$(__git_ps1 \" (%s)\") \[\033[0;37m\] \n\$ "

Here's mine

export PS1="\n\[\033[1;30m\][$:$PPID - \j:\!\[\033[1;30m\]]\[\033[0;36m\] \T \
\[\033[1;30m\][\[\033[1;34m\]\u@\H\[\033[1;30m\]:\[\033[0;37m\]${SSH_TTY:-o} \
\[\033[0;32m\]+${SHLVL}\[\033[1;30m\]] \[\033[1;37m\]\w\[\033[0;37m\]\[\033[1;34m\]\$(__git_ps1 \" (%s)\") \[\033[0;37m\] \n\$ "
甜味超标? 2024-10-08 04:18:44

这个 PS1 会将你当前的 git 分支染成黄色:

export PS1="\[\033[38;5;11m\]\u\[$(tput sgr0)\]@\h:\[$(tput sgr0)\]\[\033[38;5;6m\][\w]\[$(tput sgr0)\]\[\033[38;5;226m\]($(git branch 2>/dev/null | grep '^*' | colrm 1 2))\[$(tput sgr0)\]: \[$(tput sgr0)\]"

要以更具交互性的方式查看如何构建 PS1 字符串(显然其他方法也是可能的),这里有一个非常方便的 PS1 字符串生成器的链接。 bashrc。我将它用于上面的字符串:

http://bashrcgenerator.com/

它以简单的方式解决了您的问题更普遍的是构建自定义和彩色 shell 提示符的问题,包括当前的 git 分支

This PS1 will color your current git branch in yellow:

export PS1="\[\033[38;5;11m\]\u\[$(tput sgr0)\]@\h:\[$(tput sgr0)\]\[\033[38;5;6m\][\w]\[$(tput sgr0)\]\[\033[38;5;226m\]($(git branch 2>/dev/null | grep '^*' | colrm 1 2))\[$(tput sgr0)\]: \[$(tput sgr0)\]"

To see in a more interactive way the how the building of a PS1 string can be done (other ways are possible obviously), here the link to a very handy PS1 string generator for your .bashrc. I used it for the string above:

http://bashrcgenerator.com/

It solves in a simple way your question and more generally the issue of building of a customized and colorized shell prompt, including the current git branch

小帐篷 2024-10-08 04:18:44

对于更复杂的 Git 状态,您可以使用一些更大的脚本

For more complicated status of Git you can use some larger script.

呆萌少年 2024-10-08 04:18:44

我是这样做的:

eyes=(O o ∘ ◦ ⍤ ⍥)
en=${#eyes[@]}
mouth='_'
linesymbol='-'

RED='\e[31m'
GRN='\e[32m'
YLW='\e[33m'
BLU='\e[34m'
MGN='\e[35m'
DEF='\e[0m'
BLD='\e[1m'
DIM='\e[2m'

face () { # gen random face
    [[ $error -gt 0 ]] && ecolor=$RED || ecolor=$YLW
    [[ $1 ]] && printf "${eyes[$[RANDOM%en]]}$mouth${eyes[$[RANDOM%en]]}" \
             || printf "$ecolor${eyes[$[RANDOM%en]]}$YLW$mouth$ecolor${eyes[$[RANDOM%en]]}$DEF"
}

info () { error=$? git_tst= git_clr=
    [[ -d .git ]] && {
         git_tst=($(git -c color.ui=never  status -sb)) git_tst="GIT ${git_tst[*]} " # Test output
         git_clr=($(git -c color.ui=always status -sb)) git_clr="GIT ${git_clr[*]} " # Colored output
    }
    [[ $debian_chroot ]] && chrt="($debian_chroot)" || chrt=  # If in chroot
    name_length="{ $HOSTNAME$chrt }"
    name_length=${#name_length}
    face_tst='O_o  o_O'
    top_line_left=$[(COLUMNS-name_length)/2]
    top_line_right=$[COLUMNS-(top_line_left+name_length)]
    printf -v top_line "%${top_line_left}s{_S_$DEF$BLD$HOSTNAME$chrt${DEF}_S_$GRN}%${top_line_right}s"
    printf -v bot_line "%${COLUMNS}s"
    printf -v date  "%(%a %d %b %T)T"
    top_line=${top_line// /-}
    top_line=$GRN${top_line//_S_/ }$DEF
    bot_line=$GRN${bot_line// /-}$DEF
    spaces=$[COLUMNS-${#date}-${#PWD}-${#git_tst}-${#face_tst}]; ((spaces<0)) && spaces=1
    printf "$top_line\n$(face) $BLD$BLU$PWD$DEF%${spaces}s$git_clr$DIM$date $(face)\n$bot_line"
}


PS1='\n$(info)\n$ '
case "$TERM" in xterm*|rxvt*) PS1="\[\e]0;$(face 1) \h\w\a\]$PS1";; esac

这段代码应该转到~/.bashrc。结果如下所示:

info bar

代码位于此处

Here is how I did this:

eyes=(O o ∘ ◦ ⍤ ⍥)
en=${#eyes[@]}
mouth='_'
linesymbol='-'

RED='\e[31m'
GRN='\e[32m'
YLW='\e[33m'
BLU='\e[34m'
MGN='\e[35m'
DEF='\e[0m'
BLD='\e[1m'
DIM='\e[2m'

face () { # gen random face
    [[ $error -gt 0 ]] && ecolor=$RED || ecolor=$YLW
    [[ $1 ]] && printf "${eyes[$[RANDOM%en]]}$mouth${eyes[$[RANDOM%en]]}" \
             || printf "$ecolor${eyes[$[RANDOM%en]]}$YLW$mouth$ecolor${eyes[$[RANDOM%en]]}$DEF"
}

info () { error=$? git_tst= git_clr=
    [[ -d .git ]] && {
         git_tst=($(git -c color.ui=never  status -sb)) git_tst="GIT ${git_tst[*]} " # Test output
         git_clr=($(git -c color.ui=always status -sb)) git_clr="GIT ${git_clr[*]} " # Colored output
    }
    [[ $debian_chroot ]] && chrt="($debian_chroot)" || chrt=  # If in chroot
    name_length="{ $HOSTNAME$chrt }"
    name_length=${#name_length}
    face_tst='O_o  o_O'
    top_line_left=$[(COLUMNS-name_length)/2]
    top_line_right=$[COLUMNS-(top_line_left+name_length)]
    printf -v top_line "%${top_line_left}s{_S_$DEF$BLD$HOSTNAME$chrt${DEF}_S_$GRN}%${top_line_right}s"
    printf -v bot_line "%${COLUMNS}s"
    printf -v date  "%(%a %d %b %T)T"
    top_line=${top_line// /-}
    top_line=$GRN${top_line//_S_/ }$DEF
    bot_line=$GRN${bot_line// /-}$DEF
    spaces=$[COLUMNS-${#date}-${#PWD}-${#git_tst}-${#face_tst}]; ((spaces<0)) && spaces=1
    printf "$top_line\n$(face) $BLD$BLU$PWD$DEF%${spaces}s$git_clr$DIM$date $(face)\n$bot_line"
}


PS1='\n$(info)\n$ '
case "$TERM" in xterm*|rxvt*) PS1="\[\e]0;$(face 1) \h\w\a\]$PS1";; esac

This code is supposed to go to ~/.bashrc. The result looks like this:

info bar

Code is available here

小情绪 2024-10-08 04:18:44

这是一个 Windows/Cygwin/Bash 解决方案。

将以下内容添加到您的 ~/.bashrc 文件中。

xxx 是本地 Git 存储库的位置。

GetBranch()
{
    cat /cygdrive/c/xxx/.git/HEAD | sed 's+^ref: refs/heads/++'
}
export PS1="\[\e]0;\w\a\]\n\[\e[32m\]\u@\h \[\e[36m\]\$(GetBranch) \[\e[33m\]\w \[\e[0m\] \n\$ "

Here is a Windows/Cygwin/Bash solution.

Add the following to your ~/.bashrc file.

xxx is the location of your local Git repository.

GetBranch()
{
    cat /cygdrive/c/xxx/.git/HEAD | sed 's+^ref: refs/heads/++'
}
export PS1="\[\e]0;\w\a\]\n\[\e[32m\]\u@\h \[\e[36m\]\$(GetBranch) \[\e[33m\]\w \[\e[0m\] \n\$ "
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文