~tardypad/dotfiles

ref: 6c6da645bdf106f6cb251579c4eb42cbb0a19e8d dotfiles/files/configs/zsh/config.d/prompt.zsh -rw-r--r-- 1.8 KiB
6c6da645 — Damien Tardy-Panis Remove unused _operands function in _time-tracker completion 9 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# define modes symbol
VI_INS_MODE_SYMBOL='>'
VI_CMD_MODE_SYMBOL='<'

# define initial mode
VI_MODE_SYMBOL="${VI_INS_MODE_SYMBOL}"

# on keymap change, define the mode and redraw prompt
zle-keymap-select() {
  if [ "${KEYMAP}" = 'vicmd' ]; then
    VI_MODE_SYMBOL="${VI_CMD_MODE_SYMBOL}"
  else
    VI_MODE_SYMBOL="${VI_INS_MODE_SYMBOL}"
  fi
  zle reset-prompt
}
zle -N zle-keymap-select

# reset to default mode at the end of line input reading
zle-line-finish() {
  VI_MODE_SYMBOL="${VI_INS_MODE_SYMBOL}"
}
zle -N zle-line-finish

# Fix a bug when you C-c in CMD mode, you'd be prompted with CMD mode indicator
# while in fact you would be in INS mode.
# Fixed by catching SIGINT (C-c), set mode to INS and repropagate the SIGINT,
# so if anything else depends on it, we will not break it.
TRAPINT() {
  VI_MODE_SYMBOL="${VI_INS_MODE_SYMBOL}"
  return $(( 128 + $1 ))
}

prompt_preexec() {
  # spacer between command and result, if there is any command ran
  if [ -n "$1" ]; then
    echo
  fi
}

prompt_precmd() {
  # spacer between last command result and new prompt
  echo
}

prompt_set() {
  local root='%{%F{{color/alert/name}}%}'
  local normal='%{%F{{color/base09/name}}%}'
  local underline='%{%U%}'
  local bold='%{%B%}'
  local stop_foreground='%{%f%}'
  local stop_underline='%{%u%}'
  local stop_bold='%{%b%}'

  PROMPT=
  # red prompt for root, blue otherwise
  PROMPT="${PROMPT}%(!.${root}.${normal})"
  # underline prompt if last command exited unsuccessfully
  PROMPT="${PROMPT}%(?..${underline})"
  # add bold symbol based on vi mode
  PROMPT="${PROMPT}${bold}"'$VI_MODE_SYMBOL'"${stop_bold}"
  # clear styling and add space
  PROMPT="${PROMPT}${stop_foreground}${stop_underline} "
}

setopt prompt_subst

autoload -U add-zsh-hook
add-zsh-hook preexec prompt_preexec
add-zsh-hook precmd prompt_precmd
prompt_set

unset -f prompt_set