~andrewzah/dotfiles

ref: osx dotfiles/zsh/theme.zsh -rw-r--r-- 1.2 KiB
8b6e98a5Andrew Zah update 1 year, 8 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
function preexec() {
  timer=${timer:-$SECONDS}
}

function precmd() {
  if [ $timer ]; then
    timer_show=$(($SECONDS - $timer))
    set_prompt
    unset timer
  fi
}

get_time() {
  if [[ -n "${timer_show}" ]]; then
    echo -e "${timer_show}"
  fi
}

convertsecs() {
  local T=$1
  local D=$((T/60/60/24))
  local H=$((T/60/60%24))
  local M=$((T/60%60))
  local S=$((T%60))
  [[ $D > 0 ]] && printf '%dd ' $D
  [[ $H > 0 ]] && printf '%dh ' $H
  [[ $M > 0 ]] && printf '%dm ' $M
  [[ $D > 0 || $H > 0 || $M > 0 ]] && printf ''
  printf '%ds\n' $S
}

# -- info commands

function dir_status() {
  echo "%{$fg_bold[cyan]%}%2~%{$reset_color%}"
}

function exit_code_status() {
  if [[ "$?" != "0" ]]; then
    echo -e "%{$fg_bold[red]%}"
  else
    echo -e "%{$fg_bold[green]%}"
  fi
}

function parse_git_branch() {
  echo -e "$(git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ [\1]/')"
}

function set_prompt() {
  PROMPT=' $(exit_code_status)λ ($(convertsecs $(get_time))) $(dir_status) $(exit_code_status)\$ %{$reset_color%}'

  RPROMPT='%{$fg[green]%}$(parse_git_branch)%{$reset_color%}'
}


function zle-line-init zle-keymap-select {
  set_prompt
  zle reset-prompt
}

zle -N zle-line-init
zle -N zle-keymap-select