]> git.r.bdr.sh - rbdr/dotfiles/blame_incremental - zsh/modules/prompt.zsh
Use tree-sitter-cli
[rbdr/dotfiles] / zsh / modules / prompt.zsh
... / ...
CommitLineData
1autoload -Uz promptinit && promptinit
2
3function tool_versions() {
4 if [[ -s ".tool-versions" ]]; then
5 echo '.tool-versions'
6 return
7 fi
8 echo "${HOME}/.tool-versions"
9}
10
11if [[ "$TERM" == (dumb|linux|*bsd*) ]] || (( $#prompt_argv < 1 )); then
12 prompt 'off'
13fi
14
15#
16# Functions
17#
18
19function env_info_provider() { echo "%{%F{green}%}n/a"; }
20function env_info {
21 echo "[%{%F{cyan}%}ENV%{%F{black}%} $(env_info_provider)%{%F{black}%}]"
22}
23
24function python_info {
25 pythoninfo=`cat "$(tool_versions)" | rg python | cut -d ' ' -f 2`
26 pythoninfo=${ASDF_PYTHON_VERSION:-$pythoninfo}
27 venv="${VIRTUAL_ENV##*/}"
28 echo "%{%F{green}%}py%{%F{black}%} ${pythoninfo:-no}@${venv:-default}"
29}
30
31function rust_info {
32 rustinfo=`cat "$(tool_versions)" | rg rust | cut -d ' ' -f 2`
33 rustinfo=${ASDF_RUST_VERSION:-$rustinfo}
34 echo "%{%F{red}%}rs%{%F{black}%} ${rustinfo:-no}"
35}
36
37function node_info {
38 nodeinfo=`cat "$(tool_versions)" | rg nodejs | cut -d ' ' -f 2`
39 nodeinfo=${ASDF_NODEJS_VERSION:-$nodeinfo}
40 echo "%{%F{yellow}%}js%{%F{black}%} ${nodeinfo:-no}"
41}
42
43function git_info {
44 branch=$(git branch --show-current 2> /dev/null)
45
46 if [ ! -z $branch ]; then
47 echo -n "@%F{cyan}$branch%f"
48
49 status_output=$(git status --short)
50
51 if echo "$status_output" | grep -q '^??'; then
52 echo -n "%{%F{magenta}%}?%f"
53 fi
54
55 if echo "$status_output" | grep -q '^ M'; then
56 echo -n "%{%F{magenta}%}!%f"
57 fi
58
59 if echo "$status_output" | grep -q '^ D'; then
60 echo -n "%{%F{magenta}%}×%f"
61 fi
62
63 if echo "$status_output" | grep -q '^A '; then
64 echo -n "%{%F{green}%}+%f"
65 fi
66 fi
67}
68
69function box_name {
70 [ -f ~/.box-name ] && cat ~/.box-name || hostname -s
71}
72
73#
74# ZLE widgets
75#
76
77function zle-line-init zle-keymap-select {
78
79 # Couldn't figure out how to get tmux and iTerm to agree on how to render
80 # these characters... so there you go.
81
82 local normal_symbol="N"
83 local insert_symbol="I"
84 local error_symbol="X"
85
86 # Show vi status / return status on the right side
87
88 local return_status="%{%F{red}%}%(?.. $error_symbol)%{$reset_color%}"
89 if [ $KEYMAP = 'vicmd' ]; then
90 local edit_status="%{%F{cyan}%}$normal_symbol"
91 else
92 local edit_status="%{%F{green}%}$insert_symbol"
93 fi
94
95 RPROMPT="${edit_status}${return_status}%{$reset_color%}"
96 zle reset-prompt
97}
98zle -N zle-line-init
99zle -N zle-keymap-select
100
101#
102# Setup Prompt
103#
104
105function prompt_setup {
106 setopt LOCAL_OPTIONS
107 unsetopt XTRACE KSH_ARRAYS
108 prompt_opts=(cr percent subst)
109
110 # Load required functions.
111 autoload -Uz add-zsh-hook
112
113 # Define prompts.
114 PROMPT='
115%{%F{black}%}$(env_info) $(node_info) $(rust_info) $(python_info)
116%{%F{magenta}%}%n%{%f%}@%{%F{yellow}%}$(box_name)%{%f%} %{%B%F{green}%}${PWD/#$HOME/~}%{%f%b%}$(git_info)%{%f%}
117%(?,,%{${%B%F{white}%}[%?]%{%f%b%} )%% '
118}
119
120prompt_setup "$@"
121setopt PROMPT_SUBST
122setopt PROMPT_PERCENT
123setopt TRANSIENT_RPROMPT