-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathpowerline.bash
137 lines (115 loc) · 4.56 KB
/
powerline.bash
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
SEPERATOR_LEFT=${LAYOUTS_POWERLINE_SEPARATOR_LEFT:-''}
SEPERATOR_RIGHT=${LAYOUTS_POWERLINE_SEPARATOR_RIGHT:-''}
SPLITTER_LEFT=${LAYOUTS_POWERLINE_SPLITTER_LEFT:-''}
SPLITTER_RIGHT=${LAYOUTS_POWERLINE_SPLITTER_RIGHT:-''}
SEGMENTS_PROMPT_READY_ICON=${LAYOUTS_POWERLINE_PROMPT_READY_ICON:-'➜'}
SEGMENTS_GIT_ICON=${LAYOUTS_POWERLINE_GIT_ICON:-''}
SEGMENTS_GIT_INCOMING_ICON=${LAYOUTS_POWERLINE_GIT_INCOMING_ICON:-'↓'}
SEGMENTS_GIT_OUTGOING_ICON=${LAYOUTS_POWERLINE_GIT_OUTGOING_ICON:-'↑'}
PROMPT_COMPACT=${SBP_PROMPT_COMPACT:-false}
print_themed_prompt() {
local left_segments=$1
local right_segments=$2
local line_two_segments=$3
local prompt_gap_size=$4
# Remove the first seperator as it's not ending a previous segment
local seperator_left_size=${#SEPERATOR_LEFT}
left_segments=${left_segments#*"$SEPERATOR_LEFT"}
prompt_gap_size=$((seperator_left_size + prompt_gap_size))
# Remove the first seperator as it's not ending a previous segment
[[ -n $line_two_segments ]] && line_two_segments=${line_two_segments#*"$SEPERATOR_LEFT"}
local reset_color
decorate::print_colors 'reset_color'
if [[ $PROMPT_COMPACT == false ]]; then
left_segments="\n${left_segments}"
fi
local filler_segment
if [[ -n $right_segments || -n $line_two_segments ]]; then
print_themed_filler 'filler_segment' "$prompt_gap_size"
right_segments="${right_segments}${reset_color}\n"
elif [[ ${SBP_SEGMENTS_LEFT[-1]} != 'prompt_ready' ]]; then
# We need to finish the last segment in this case
print_themed_filler 'filler_segment' 1
filler_segment="${filler_segment// /}${reset_color} "
fi
line_two_segments="${line_two_segments}${reset_color}"
printf '%s' "${left_segments}${filler_segment}${right_segments}${line_two_segments}"
}
print_themed_filler() {
local -n print_themed_filler_result=$1
local seperator_size=${#SEPERATOR_LEFT}
# Account for seperator and padding
local filler_size=$(($2 - seperator_size - 2))
# shellcheck disable=SC2183
printf -v padding '%*s' "$filler_size"
SEGMENT_POSITION='left'
prompt_filler_output="$(print_themed_segment 'normal' "$padding")"
mapfile -t segment_output <<<"$prompt_filler_output"
# shellcheck disable=SC2034
print_themed_filler_result=${segment_output[1]}
}
print_themed_segment() {
local segment_type=$1
shift
local segment_parts=("${@}")
local segment_length=0
local part_length=0
local themed_segment
local seperator_themed
local part_splitter
if [[ $segment_type == 'highlight' ]]; then
PRIMARY_COLOR="$PRIMARY_COLOR_HIGHLIGHT"
SECONDARY_COLOR="$SECONDARY_COLOR_HIGHLIGHT"
fi
if [[ $SEGMENT_POSITION == 'left' ]]; then
part_splitter="$SPLITTER_LEFT"
seperator="$SEPERATOR_LEFT"
local seperator_color
decorate::print_bg_color 'seperator_color' "$PRIMARY_COLOR"
seperator_themed="${seperator_color}${seperator}"
local prepare_color=
decorate::print_fg_color 'prepare_color' "$PRIMARY_COLOR"
elif [[ $SEGMENT_POSITION == 'right' ]]; then
part_splitter="$SPLITTER_RIGHT"
seperator="$SEPERATOR_RIGHT"
local seperator_color
decorate::print_fg_color 'seperator_color' "$PRIMARY_COLOR"
seperator_themed="${seperator_color}${seperator}"
local prepare_color=
decorate::print_fg_color 'prepare_color' "$PRIMARY_COLOR"
fi
local segment_colors
decorate::print_colors 'segment_colors' "$SECONDARY_COLOR" "$PRIMARY_COLOR"
if [[ -n ${part_splitter/ /} ]]; then
part_splitter=" ${part_splitter} "
else
part_splitter=' '
fi
local part_splitter_length="${#part_splitter}"
segment_length="${#seperator}"
themed_segment="$seperator_themed"
themed_segment="${themed_segment}${segment_colors}"
if [[ ${#segment_parts[@]} -gt 1 ]]; then
local splitter_color_on
decorate::print_fg_color 'splitter_color_on' "$SPLITTER_COLOR"
local splitter_color_off
decorate::print_fg_color 'splitter_color_off' "$SECONDARY_COLOR"
part_splitter_themed="${splitter_color_on}${part_splitter}${splitter_color_off}"
fi
local themed_parts
for part in "${segment_parts[@]}"; do
[[ -z $part ]] && continue
part_length="${#part}"
if [[ -n $themed_parts ]]; then
themed_parts="${themed_parts}${part_splitter_themed}${part}"
segment_length=$((segment_length + part_length + part_splitter_length))
else
segment_length="$((segment_length + part_length))"
themed_parts="${part}"
fi
done
themed_segment="${themed_segment} ${themed_parts} "
segment_length=$((segment_length + 2))
themed_segment="${themed_segment}${prepare_color}"
printf '%s\n%s' "$segment_length" "$themed_segment"
}