aboutsummaryrefslogtreecommitdiff
path: root/lib/powerline.sh
blob: 3a3fb68e5e77d9363d7c03d2baee72f9f2451133 (plain)
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
# Library functions

print_powerline() {
	local side="$1"
	local upper_side=$(echo "$1" | tr '[:lower:]' '[:upper:]')
	eval "local input_segments=(\"\${TMUX_POWERLINE_${upper_side}_STATUS_SEGMENTS[@]}\")"
	local powerline_segments=()
	local powerline_segment_contents=()

	__check_platform

	__process_segment_defaults
	__process_scripts
	__process_colors

	__process_powerline
}

__process_segment_defaults() {
	for segment_index in "${!input_segments[@]}"; do
		local input_segment=(${input_segments[$segment_index]})
		eval "local default_separator=\$TMUX_POWERLINE_DEFAULT_${upper_side}SIDE_SEPARATOR"

		powerline_segment_with_defaults=(
			${input_segment[0]:-"no_script"} \
			${input_segment[1]:-$TMUX_POWERLINE_DEFAULT_BACKGROUND_COLOR} \
			${input_segment[2]:-$TMUX_POWERLINE_DEFAULT_FOREGROUND_COLOR} \
			${input_segment[3]:-$default_separator} \
		)

		powerline_segments[$segment_index]="${powerline_segment_with_defaults[@]}"
	done
}

__process_scripts() {
	for segment_index in "${!powerline_segments[@]}"; do
		local powerline_segment=(${powerline_segments[$segment_index]})

		if [ -n "$TMUX_POWERLINE_DIR_USER_SEGMENTS" ] && [ -f "$TMUX_POWERLINE_DIR_USER_SEGMENTS/${powerline_segment[0]}.sh" ] ; then
			local script="$TMUX_POWERLINE_DIR_USER_SEGMENTS/${powerline_segment[0]}.sh"
		else
			local script="$TMUX_POWERLINE_DIR_SEGMENTS/${powerline_segment[0]}.sh"
		fi

		export TMUX_POWERLINE_CUR_SEGMENT_BG="${powerline_segment[1]}"
		export TMUX_POWERLINE_CUR_SEGMENT_FG="${powerline_segment[2]}"
		source "$script"
		local output
		output=$(run_segment)
		local exit_code="$?"
		unset -f run_segment

		if [ "$exit_code" -ne 0 ] && debug_mode_enabled ; then
			local seg_name="${script##*/}"
			echo "Segment '${seg_name}' exited with code ${exit_code}. Aborting."
			exit 1
		fi

		if [ -n "$output" ]; then
			powerline_segment_contents[$segment_index]=" $output "
		else
			unset -v powerline_segments[$segment_index]
		fi
	done
}

__process_colors() {
	for segment_index in "${!powerline_segments[@]}"; do
		local powerline_segment=(${powerline_segments[$segment_index]})
	 	# Find the next segment that produces content (i.e. skip empty segments).
		for next_segment_index in $(eval echo {$(($segment_index + 1))..${#powerline_segments}}) ; do
			[[ -n ${powerline_segments[next_segment_index]} ]] && break
		done
		local next_segment=(${powerline_segments[$next_segment_index]})

		if [ $side == 'left' ]; then
			powerline_segment[4]=${next_segment[1]:-$TMUX_POWERLINE_DEFAULT_BACKGROUND_COLOR}
		elif [ $side == 'right' ]; then
			powerline_segment[4]=${previous_background_color:-$TMUX_POWERLINE_DEFAULT_BACKGROUND_COLOR}
		fi

		if __segment_separator_is_thin; then
			powerline_segment[5]=${powerline_segment[2]}
		else
			powerline_segment[5]=${powerline_segment[1]}
		fi

		local previous_background_color=${powerline_segment[1]}

		powerline_segments[$segment_index]="${powerline_segment[@]}"
	done
}

__process_powerline() {
	for segment_index in "${!powerline_segments[@]}"; do
		local powerline_segment=(${powerline_segments[$segment_index]})

		local background_color=${powerline_segment[1]}
		local foreground_color=${powerline_segment[2]}
		local separator=${powerline_segment[3]}
		local separator_background_color=${powerline_segment[4]}
		local separator_foreground_color=${powerline_segment[5]}

		eval "__print_${side}_segment ${segment_index} ${background_color} ${foreground_color} ${separator} ${separator_background_color} ${separator_foreground_color}"
	done
}

__print_left_segment() {
	local content=${powerline_segment_contents[$1]}
	local content_background_color=$2
	local content_foreground_color=$3
	local separator=$4
	local separator_background_color=$5
	local separator_foreground_color=$6

	__print_colored_content "$content" $content_background_color $content_foreground_color
	__print_colored_content $separator $separator_background_color $separator_foreground_color
}

__print_right_segment() {
	local content=${powerline_segment_contents[$1]}
	local content_background_color=$2
	local content_foreground_color=$3
	local separator=$4
	local separator_background_color=$5
	local separator_foreground_color=$6

	__print_colored_content $separator $separator_background_color $separator_foreground_color
	__print_colored_content "$content" $content_background_color $content_foreground_color
}

__segment_separator_is_thin() {
	[[ ${powerline_segment[3]} == $TMUX_POWERLINE_SEPARATOR_LEFT_THIN || \
		${powerline_segment[3]} == $TMUX_POWERLINE_SEPARATOR_RIGHT_THIN ]];
}

__check_platform() {
	if [ "$SHELL_PLATFORM" == "unknown" ] && debug_mode_enabled; then
		 echo "Unknown platform; modify config/shell.sh"  &1>&2
	fi
}