-
Notifications
You must be signed in to change notification settings - Fork 0
/
hydrogen.zsh-theme
108 lines (98 loc) · 2.85 KB
/
hydrogen.zsh-theme
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
# vim:ft=zsh
#
# hydrogen zsh theme
# https://github.com/xylous/hydrogen
#
# Code licensed under the MIT License
# https://raw.githubusercontent.com/xylous/hydrogen/master/LICENSE
#
# @author xylous <xylous.e@gmail.com>
# @maintainer xylous <xylous.e@gmail.com>
###
# usage: hydrogen_theme
# Set PROMPT and RPROMPT to display the theme
###
function hydrogen_theme() {
setopt PROMPT_SUBST
set_colours
PROMPT='$(fill_line "$(top_left_part)" "$(gitstatus) ")'
PROMPT+=$'\n'
PROMPT+='└ '
# If the last exit code is > 0, the integer between brackets will be coloured
# red, otherwise green (indicating success)
RPROMPT="[%(?.${HYDROGEN_GREEN}%?${HYDROGEN_CLR}.${HYDROGEN_RED}%?${HYDROGEN_CLR})]"
}
###
# usage: set_colours
# Export global variables that hydrogen uses. If terminal isn't capable of
# handling more than 256 colours, use standard 8 bit palette
###
function set_colours() {
export HYDROGEN_CLR='%F{default}'
if [[ $(tput colors) -ge 256 ]]; then
export HYDROGEN_USERNAME='%F{167}'
export HYDROGEN_HOSTNAME='%F{67}'
export HYDROGEN_PATH="%F{43}"
export HYDROGEN_GREEN='%F{34}'
export HYDROGEN_RED='%F{9}'
else
export HYDROGEN_USERNAME='%F{red}'
export HYDROGEN_HOSTNAME='%F{blue}'
export HYDROGEN_PATH="%F{cyan}"
export HYDROGEN_GREEN='%F{green}'
export HYDROGEN_RED='%F{red}'
fi
}
###
# usage: top_left_part
# Print the top left part of the prompt to stdout, with all variables expanded
###
function top_left_part()
{
# Username and hostname
SECTION_A="(${HYDROGEN_USERNAME}%n${HYDROGEN_CLR}@${HYDROGEN_HOSTNAME}%m${HYDROGEN_CLR})"
# Path
SECTION_B="${HYDROGEN_PATH}%~${HYDROGEN_CLR}"
echo "┌─${SECTION_A} | ${SECTION_B}"
}
###
# Usage: fill_line LEFT RIGHT
# Sets REPLY to LEFT<spaces>RIGHT with enough spaces in the middle to fill a
# terminal line.
###
function fill_line() {
emulate -L zsh
prompt_length $1
local -i left_len=$REPLY
prompt_length $2 9999
local -i right_len=$REPLY
local -i pad_len=$((COLUMNS - left_len - right_len - ${ZLE_RPROMPT_INDENT:-1}))
if (( pad_len < 1 )); then
# Not enough space for the right part. Drop it.
echo "$1"
else
local pad=${(pl.$pad_len.. .)} # pad_len spaces
echo "${1}${pad}${2}"
fi
}
###
# Usage: prompt_length PROMPT
# Determines the length of the outputted text of a string with zsh prompt syntax
###
function prompt_length() {
emulate -L zsh
local -i COLUMNS=${2:-COLUMNS}
local -i x y=${#1} m
if (( y )); then
while (( ${${(%):-$1%$y(l.1.0)}[-1]} )); do
x=y
(( y *= 2 ))
done
while (( y > x + 1 )); do
(( m = x + (y - x) / 2 ))
(( ${${(%):-$1%$m(l.x.y)}[-1]} = m ))
done
fi
typeset -g REPLY="$x"
}
hydrogen_theme