ETC

[zsh] zsh에 oh my posh 설치하기 (oh my zsh 아님)

ddochea 2022. 8. 13. 18:14
반응형

oh-my-posh는 powershell을 예쁘게 꾸며주는 도구로 잘 알려져 있지만, 사실 mac에서 사용하는 zsh 에서도 사용 가능하다.
아래 스크립트를 .zprofile 파일에 추가하면 쉽게 적용할 수 있다.

사전에 oh-my-posh가 설치되어 있어야 한다. (brew를 이용한 설치 방법 : https://formulae.brew.sh/formula/oh-my-posh)

export POSH_THEME={테마 파일 경로}
export POWERLINE_COMMAND="oh-my-posh"
export CONDA_PROMPT_MODIFIER=false

# set secondary prompt
PS2="$(/opt/homebrew/bin/oh-my-posh print secondary --config="$POSH_THEME" --shell=zsh)"

function prompt_ohmyposh_preexec() {
  omp_start_time=$(/opt/homebrew/bin/oh-my-posh get millis)
}

function prompt_ohmyposh_precmd() {
  omp_last_error=$?
  omp_stack_count=${#dirstack[@]}
  omp_elapsed=-1
  if [ $omp_start_time ]; then
    omp_now=$(/opt/homebrew/bin/oh-my-posh get millis)
    omp_elapsed=$(($omp_now-$omp_start_time))
  fi
  eval "$(/opt/homebrew/bin/oh-my-posh print primary --config="$POSH_THEME" --error="$omp_last_error" --execution-time="$omp_elapsed" --stack-count="$omp_stack_count" --eval --shell=zsh --shell-version="$ZSH_VERSION")"
  unset omp_start_time
  unset omp_now
}

function _install-omp-hooks() {
  for s in "${preexec_functions[@]}"; do
    if [ "$s" = "prompt_ohmyposh_preexec" ]; then
      return
    fi
  done
  preexec_functions+=(prompt_ohmyposh_preexec)

  for s in "${precmd_functions[@]}"; do
    if [ "$s" = "prompt_ohmyposh_precmd" ]; then
      return
    fi
  done
  precmd_functions+=(prompt_ohmyposh_precmd)
}

if [ "$TERM" != "linux" ]; then
  _install-omp-hooks
fi

function self-insert() {
  # ignore an empty buffer
  if [[ -z  "$BUFFER"  ]]; then
    zle .self-insert
    return
  fi
  # trigger a tip check only if the input is a space character
  if [[ "$KEYS" = " " ]]; then
    tooltip=$(/opt/homebrew/bin/oh-my-posh print tooltip --config="$POSH_THEME" --shell=zsh --error="$omp_last_error" --command="$BUFFER" --shell-version="$ZSH_VERSION")
  fi
  # ignore an empty tooltip
  if [[ ! -z "$tooltip" ]]; then
    RPROMPT=$tooltip
    zle .reset-prompt
  fi
  zle .self-insert
}

if [[ "true" = "true" ]]; then
  zle -N self-insert
fi

function _posh-zle-line-init() {
    [[ $CONTEXT == start ]] || return 0

    # Start regular line editor
    (( $+zle_bracketed_paste )) && print -r -n - $zle_bracketed_paste[1]
    zle .recursive-edit
    local -i ret=$?
    (( $+zle_bracketed_paste )) && print -r -n - $zle_bracketed_paste[2]

    eval "$(/opt/homebrew/bin/oh-my-posh print transient --error="$omp_last_error" --execution-time="$omp_elapsed" --stack-count="$omp_stack_count" --config="$POSH_THEME" --eval --shell=zsh --shell-version="$ZSH_VERSION")"
    zle .reset-prompt

    # If we received EOT, we exit the shell
    if [[ $ret == 0 && $KEYS == $'\4' ]]; then
        exit
    fi

    # Ctrl-C
    if (( ret )); then
        zle .send-break
    else
        # Enter
        zle .accept-line
    fi
    return ret
}

if [[ "true" = "true" ]]; then
  zle -N zle-line-init _posh-zle-line-init
fi

해당 스크립트는 oh-my-posh 의 명령어로 출력 가능하다.

oh-my-posh init zsh
반응형