forked from reobin/typewritten
-
Notifications
You must be signed in to change notification settings - Fork 0
/
typewritten.zsh
162 lines (134 loc) · 4.87 KB
/
typewritten.zsh
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#!/usr/bin/env zsh
# ____ ____ ____ ____ ____ ____ ____ ____ ____ ____ ____
# ||t |||y |||p |||e |||w |||r |||i |||t |||t |||e |||n ||
# ||__|||__|||__|||__|||__|||__|||__|||__|||__|||__|||__||
# |/__\|/__\|/__\|/__\|/__\|/__\|/__\|/__\|/__\|/__\|/__\|
#
# A minimal, informative zsh prompt theme
#
export TYPEWRITTEN_ROOT=${${(%):-%x}:A:h}
source "$TYPEWRITTEN_ROOT/async.zsh"
async_init
source "$TYPEWRITTEN_ROOT/lib/colors.zsh"
source "$TYPEWRITTEN_ROOT/lib/git.zsh"
BREAK_LINE="
"
tw_right_prompt_prefix="%F{$tw_colors[right_prompt_prefix]}$TYPEWRITTEN_RIGHT_PROMPT_PREFIX"
local tw_prompt_symbol="❯"
if [ ! -z "$TYPEWRITTEN_SYMBOL" ]; then
tw_prompt_symbol="$TYPEWRITTEN_SYMBOL"
fi;
local tw_prompt_color="%(?,%F{$tw_colors[symbol]},%F{$tw_colors[error_code]})"
local tw_return_code="%(?,,%F{$tw_colors[symbol_error]}%? )"
if [ "$TYPEWRITTEN_DISABLEtw_return_code" = true ]; then
tw_prompt_color="%F{$tw_colors[prompt]}"
tw_return_code=""
fi;
tw_user_host="%F{$tw_colors[host]}%n%F{$tw_colors[host_user_connector]}@%F{$tw_colors[user]}%m"
tw_prompt="$tw_prompt_color$tw_return_code$tw_prompt_symbol %F{$tw_colors[prompt]}"
tw_current_directory_color="$tw_colors[current_directory]"
tw_git_branch_color="$tw_colors[git_branch]"
tw_current_directory="%F{$tw_current_directory_color}%c"
tw_verbose_current_directory="%F{$tw_current_directory_color]}%~"
tw_arrow="%F{$tw_colors[arrow]}->"
tw_redraw() {
local tw_virtual_env=""
if [[ ! -z $VIRTUAL_ENV ]] && [[ -z $VIRTUAL_ENV_DISABLE_PROMPT ]]; then
tw_virtual_env="%F{$tw_colors[virtual_env]}($(basename $VIRTUAL_ENV)) "
fi;
tw_env_prompt="$tw_virtual_env$tw_prompt"
tw_layout="$TYPEWRITTEN_PROMPT_LAYOUT"
tw_git_info="$tw_prompt_data[tw_git_branch]$tw_prompt_data[tw_git_status]"
if [ "$tw_layout" = "half_pure" ]; then
PROMPT="$BREAK_LINE%F{$tw_git_branch_color}$tw_git_info$BREAK_LINE$tw_env_prompt"
RPROMPT="$tw_right_prompt_prefix%F{$tw_current_directory_color}$tw_prompt_data[tw_git_home]$tw_current_directory"
else
local tw_git_arrow_info=""
if [ "$tw_git_info" != "" ]; then
tw_git_arrow_info=" $tw_arrow %F{$tw_git_branch_color}$tw_git_info"
fi;
if [ "$tw_layout" = "pure" ]; then
PROMPT="$BREAK_LINE$tw_verbose_current_directory$tw_git_arrow_info$BREAK_LINE$tw_env_prompt"
RPROMPT=""
else
if [ "$tw_layout" = "singleline_verbose" ]; then
PROMPT="$tw_user_host $tw_env_prompt"
elif [ "$tw_layout" = "multiline" ]; then
PROMPT="$BREAK_LINE$tw_user_host$BREAK_LINE$tw_env_prompt"
else
PROMPT="$tw_env_prompt"
fi;
RPROMPT="$tw_right_prompt_prefix%F{$tw_current_directory_color}$tw_prompt_data[tw_git_home]$tw_current_directory$tw_git_arrow_info"
fi;
fi;
zle && zle .reset-prompt
}
tw_async_init_worker() {
async_start_worker tw_worker -n
async_register_callback tw_worker tw_prompt_callback
}
tw_prompt_callback() {
local tw_name=$1 tw_code=$2 tw_output=$3
if (( tw_code == 2 )) || (( tw_code == 3 )) || (( tw_code == 130 )); then
# reinit async workers
async_stop_worker tw_worker
tw_async_init_worker
tw_async_init_tasks
elif (( code )); then
tw_async_init_tasks
fi;
tw_prompt_data[$tw_name]=$tw_output
tw_redraw
}
tw_async_init_tasks() {
typeset -Ag tw_prompt_data
local tw_current_pwd="$PWD"
async_worker_eval tw_worker builtin cd -q $tw_current_pwd
local tw_git_hide_status="$(git config --get oh-my-zsh.hide-status 2>/dev/null)"
if [[ "$tw_git_hide_status" != "1" ]]; then
local tw_git_toplevel="$(git rev-parse --show-toplevel 2>/dev/null)"
if [[ "$tw_git_toplevel" != $tw_prompt_data[tw_git_toplevel] ]]; then
async_flush_jobs tw_worker
tw_prompt_data[tw_git_branch]=
tw_prompt_data[tw_git_status]=
fi;
if [[ "$tw_current_pwd" != $tw_prompt_data[tw_current_pwd] ]]; then
async_flush_jobs tw_worker
tw_prompt_data[tw_git_home]=
fi;
tw_prompt_data[tw_git_toplevel]="$tw_git_toplevel"
tw_prompt_data[tw_current_pwd]="$tw_current_pwd"
if [[ "$TYPEWRITTEN_GIT_RELATIVE_PATH" != false ]]; then
async_job tw_worker tw_git_home $tw_current_pwd $tw_git_toplevel
fi;
async_job tw_worker tw_git_branch
async_job tw_worker tw_git_status
else
tw_prompt_data[tw_git_branch]=
tw_prompt_data[tw_git_status]=
fi;
tw_redraw
}
# prompt cursor fix when exiting vim
tw_fix_cursor() {
local tw_cursor="\e[3 q"
if [ "$TYPEWRITTEN_CURSOR" = "block" ]; then
tw_cursor="\e[1 q"
elif [ "$TYPEWRITTEN_CURSOR" = "beam" ]; then
tw_cursor="\e[5 q"
fi;
echo -ne "$tw_cursor"
}
tw_setup() {
tw_async_init_worker
tw_async_init_tasks
zmodload zsh/zle
autoload -Uz add-zsh-hook
if [ "$TYPEWRITTEN_CURSOR" != "terminal" ]; then
add-zsh-hook precmd tw_fix_cursor
fi;
add-zsh-hook precmd tw_async_init_tasks
PROMPT="$tw_prompt"
}
tw_setup
zle_highlight=( default:fg=$tw_colors[prompt] )