agnoster.zsh-theme 1.96 KB
Newer Older
Isaac Wolkerstorfer's avatar
Isaac Wolkerstorfer committed
1
2
### Segment drawing
# A few utility functions to make it easy and re-usable to draw segmented prompts
Isaac Wolkerstorfer's avatar
Isaac Wolkerstorfer committed
3

Isaac Wolkerstorfer's avatar
Isaac Wolkerstorfer committed
4
5
6
7
8
9
10
11
12
13
14
15
16
17
CURRENT_BG=''
SEGMENT_SEPARATOR='⮀'
function segment_start() {
  local bg=$1
  local fg=$2
  if [[ -n $CURRENT_BG && $bg != $CURRENT_BG ]]; then
    echo -n " %{%K{$bg}%F{$CURRENT_BG}%}$SEGMENT_SEPARATOR"
  else
    echo -n "%{%K{$bg}%}"
  fi
  [[ -n $fg ]] && fg="%F{$fg}" || fg="%f"
  echo -n "%{$fg%} "
  CURRENT_BG=$bg
}
Isaac Wolkerstorfer's avatar
Isaac Wolkerstorfer committed
18

Isaac Wolkerstorfer's avatar
Isaac Wolkerstorfer committed
19
20
21
22
23
24
25
26
function segment_stop() {
  if [[ -n $CURRENT_BG ]]; then
    echo -n " %{%k%F{$CURRENT_BG}%}$SEGMENT_SEPARATOR"
  else
    echo -n "%{%k%}"
  fi
  echo -n "%{%f%}"
  CURRENT_BG=''
Isaac Wolkerstorfer's avatar
Isaac Wolkerstorfer committed
27
28
}

Isaac Wolkerstorfer's avatar
Isaac Wolkerstorfer committed
29
30
31
32
33
34
35
36
37
38
39
40
41
42
### Prompt components
# Each component will draw itself, and hide itself if no information needs to be shown

function prompt_context() {
  local user=`whoami`

  if [[ ("$user" != "$DEFAULT_USER") || (-n "$SSH_CLIENT") ]]; then
    segment_start black
    #echo -n "%{%F{yellow}%}$user%{%F{gray}%}@%{%F{green}%}%m%{%f%}"
    echo -n "%(!.%{%F{yellow}%}.)$user@%m"
  fi
}

function prompt_git() {
Isaac Wolkerstorfer's avatar
Isaac Wolkerstorfer committed
43
  if $(git rev-parse --is-inside-work-tree >/dev/null 2>&1); then
Isaac Wolkerstorfer's avatar
Isaac Wolkerstorfer committed
44
45
46
47
48
49
50
51
    ZSH_THEME_GIT_PROMPT_DIRTY='±'
    local dirty=$(parse_git_dirty)
    local ref
    ref=$(git symbolic-ref HEAD 2> /dev/null) || ref="➦ $(git show-ref --head -s --abbrev |head -n1 2> /dev/null)"
    if [[ -n $dirty ]]; then
      segment_start yellow black
    else
      segment_start green black
Isaac Wolkerstorfer's avatar
Isaac Wolkerstorfer committed
52
    fi
Isaac Wolkerstorfer's avatar
Isaac Wolkerstorfer committed
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
    echo -n "${ref/refs\/heads\//⭠ }$dirty"
  fi
}

function prompt_dir() {
  segment_start blue white
  echo -n '%~'
}

function prompt_status() {
  local symbols
  symbols=()
  [[ $RETVAL -ne 0 ]] && symbols+="%{%F{red}%}✘"
  [[ $UID -eq 0 ]] && symbols+="%{%F{yellow}%}⚡"
  jobs=$(jobs -l | wc -l)
  [[ $jobs -gt 0 ]] && symbols+="%{%F{cyan}%}⚙"
  if [[ -n "$symbols" ]]; then
    segment_start black white
    echo -n "${symbols}"
Isaac Wolkerstorfer's avatar
Isaac Wolkerstorfer committed
72
73
74
  fi
}

Isaac Wolkerstorfer's avatar
Isaac Wolkerstorfer committed
75
76
77
78
79
80
81
82
83
## Main prompt
function build_prompt() {
  RETVAL=$?
  prompt_status
  prompt_context
  prompt_dir
  prompt_git
  segment_stop
}
Isaac Wolkerstorfer's avatar
Isaac Wolkerstorfer committed
84
85

PROMPT='%{%f%b%k%}
Isaac Wolkerstorfer's avatar
Isaac Wolkerstorfer committed
86
$(build_prompt) '