0

I have a problem with terminal display configuration in macOS Big Sur 11.1 I want it to look like windows terminal from CMDer editor. Which can be used in the picture below

(img) I want to have something like this

I was very used to to this look of a terminal so I started to dig for solution. I found, that I need to configure .zshrc file to change colour and add git information when needed.

I found the code which needs to be set in .zshrc file:

parse_git_branch() {
    git branch 2> /dev/null | sed -n -e 's/^\* \(.*\)/[\1]/p'
}

COLOR_DEF=$'\e[0m'
COLOR_USR=$'\e[38;5;44m'
COLOR_DIR=$'\e[38;5;106m'
COLOR_GIT=$'\e[38;5;208m'
NEWLINE=$'\n'

setopt PROMPT_SUBST
export PROMPT='${COLOR_USR}%n ${COLOR_DEF}in ${COLOR_DIR}%d ${COLOR_GIT}$(parse_git_branch)${COLOR_DEF}${NEWLINE}Ⲗ '

And it works when it comes to colours. I’m not able to make parse_git_branch function to work. I don't know what is going on here. In other post there is info that the method works.

(img) currently, I have something like this. With no git branching information

Pafcio
  • 11
  • Please read, review and take to heart the items on this page : https://stackoverflow.com/tags/bash/info . Search for "Before asking about Problematic code" and "How to turn a bad script into a good question" . You can probably boil your problem down to 2-3 lines of copy/pasteable code. You'll get answers much more quickly with example code that demonstrates the problem. Be sure to include instructions for any special environments that are needed. Good luck. – shellter Dec 23 '20 at 02:53
  • `zsh` has its own terminal-independent way of specifying colors in the prompt; don't mess around with raw ANSI escape codes. – chepner Dec 23 '20 at 21:39

1 Answers1

0

Here you go:

parse_git_branch() {
  local branch=$(git rev-parse --abbrev-ref HEAD 2>/dev/null)

  # If the branch is non-zero
  if [[ -n $branch ]]; then

    # -n: Don't print a newline at the end.
    # -P: Substitute prompt expansions.
    # %F{y}: Foreground color yellow
    print -nP - "%F{y}($branch"

    local tracking=$(git rev-parse --abbrev-ref --symbolic-full-name @{u} 2>/dev/null)
    [[ -n $tracking ]] &&
      print -nP - " -> $tracking"
    print - ')'
  fi
}

setopt promptsubst

# %F{g}: Foreground color green
# %~: pwd with named dir substitution
# %f: Default foreground color
export PS1='%F{g}%~ $(parse_git_branch)
%fⲖ '

References:

Marlon Richert
  • 5,250
  • 1
  • 18
  • 27