【工具】在Terminal中显示当前分支

293 阅读1分钟

一、需求背景

想要在Terminal中直接看到当前git分支是哪个,懒得输入命令去查看,有时候推代码推错分支也挺尴尬的。

二、MacOS

方案:配置.bash_profile

效果

image.png

步骤

  • 编辑:vim ~/.bash_profile

    vim ~/.bash_profile
    

    添加以下内容:

    ######### zsh git ########
    # 自动补全
    autoload -Uz compinit && compinit
    
    # 加载版本控制信息
    autoload -Uz vcs_info
    precmd() { vcs_info }
    
    # 格式化 vcs_info_msg_0_ 变量
    zstyle ':vcs_info:git:*' formats '(%b)'
    
    # 设置prompt显示内容,这里可以自定义 (with git branch name)
    setopt PROMPT_SUBST
    PROMPT='%n@MBP ${PWD##*/}${vcs_info_msg_0_} %% '
    
  • 应用:

    source ~/.bash_profile
    

三、Windows 10

方案oh-my-posh + 自定义主题

效果

image.png

步骤

  • 安装:scoop 使用国内镜像

    iwr -useb get.scoop.sh | iex
    
  • 安装:oh-my-posh

    scoop install https://ghproxy.com/https://github.com/JanDeDobbeleer/oh-my-posh/releases/latest/download/oh-my-posh.json
    
  • 设置主题

    notepad $PROFILE
    

    填入以下内容:

    oh-my-posh init pwsh --config 'C:/Users/Chef5/AppData/Local/Programs/oh-my-posh/themes/half-life.omp.json' | Invoke-Expression 
    

    自定义主题,可以直接编辑'C:/Users/Chef5/AppData/Local/Programs/oh-my-posh/themes/half-life.omp.json',下面是我用的:

    {
        "$schema": "https://raw.githubusercontent.com/JanDeDobbeleer/oh-my-posh/main/themes/schema.json",
        "blocks": [
            {
                "alignment": "left",
                "segments": [
                    {
                        "foreground": "#FFFFFF",
                        "properties": {
                            "style": "full"
                        },
                        "style": "plain",
                        "template": "{{ .Path }} ",
                        "type": "path"
                    },
                    {
                        "foreground": "#87FF00",
                        "style": "plain",
                        "template": "<#ffffff>on</> {{ .HEAD }}{{ if .Staging.Changed }}<#87FF00> \u25cf {{ .Staging.String }}</>{{ end }}{{ if .Working.Changed }}<#D75F00> \u25cf {{ .Working.String }}</>{{ end }} ",
                        "type": "git"
                    },
                    {
                        "foreground": "#D75F00",
                        "style": "plain",
                        "template": "> ",
                        "type": "text"
                    }
                ],
                "type": "prompt"
            }
        ],
        "version": 2
    }
    

四、结束

巴拉巴拉。