使用ohmyposh美化终端

之前一直使用 iterm2 + zsh 配置的终端和主题,今天折腾了一下 Oh My Posh。记录一下

安装 Oh My Posh

brew tap jandedobbeleer/oh-my-posh
brew install jandedobbeleer/oh-my-posh/oh-my-posh

使用

因为我使用了 zsh ,所以直接配置就好了。其他的具体配置可以看官方文档

~/.zshrc

eval "$(oh-my-posh init zsh)"

安装字体

posh 主题使用了大量的图标, 因此需要使用 Nerd Fron字体,官方推荐安装 Meslo LGM NF, 我这里也是直接使用官方推荐。

在 iterm2 中添加字体配置 Preferences > Profiles > Text > Font = ‘MesloLGM NF’

如果还使用了 VSCode 编辑器的话也需要配置字体:

{
  "terminal.integrated.fontFamily": "MesloLGM NF",
}

配置主题

官方文档

这里贴一个自己的配置,利用 zsh hooks 对于不同目录使用不同的主题

# Oh My Posh
export POSH="$HOME/.oh-my-posh"
# https://ohmyposh.dev/docs/prompt
# eval "$(oh-my-posh init zsh)"

isContain() {
    local path=$1
    local str=$2
    if [[ $path == *$str* ]]; then
        return 0
    else
        return 1
    fi
}

loadTheme() {
  eval "$(oh-my-posh init zsh --config ${POSH}/themes/$1.omp.json)"
}

# https://ohmyposh.dev/docs/customize
autoload -U add-zsh-hook
toggerTheme(){
  if isContain $(pwd) $COMPANY; then
    loadTheme star
  elif isContain $(pwd) $STUDY; then
    loadTheme space
  elif isContain $(pwd) $MY; then
    loadTheme the-unnamed
  elif isContain $(pwd) $PLAY; then
    loadTheme zash
  else
    loadTheme default
  fi
}
add-zsh-hook chpwd toggerTheme
toggerTheme

参考文档

CC BY-NC-SA 4.0 2018–PRESENT © Wuxh

cd .. md