refactor: create generalized function

This commit is contained in:
Abhishek Keshri 2024-03-17 02:57:19 +05:30 committed by Abhishek Keshri
parent 4ab501e2dd
commit 81f4092ce6

View file

@ -36,6 +36,7 @@ light_red='#ff0055'
light_yellow='#f1fa8c' light_yellow='#f1fa8c'
dark_gray='#282a36' dark_gray='#282a36'
light_gray='#45455a' light_gray='#45455a'
dark_gray='#282a36'
declare -A plugin_colors=( declare -A plugin_colors=(
["git"]="green dark_gray" ["git"]="green dark_gray"
@ -56,6 +57,44 @@ get_plugin_colors() {
get_tmux_option "@tmux2k-${plugin_name}-colors" "$default_colors" get_tmux_option "@tmux2k-${plugin_name}-colors" "$default_colors"
} }
build_status_bar() {
side=$1
if [ "$side" == "left" ]; then
plugins=("${lplugins[@]}")
else
plugins=("${rplugins[@]}")
fi
for plugin_index in "${!plugins[@]}"; do
plugin="${plugins[$plugin_index]}"
if [ -z "${plugin_colors[$plugin]}" ]; then
continue
fi
IFS=' ' read -r -a colors <<<"$(get_plugin_colors "$plugin")"
script="#($current_dir/$plugin.sh)"
if [ "$side" == "left" ]; then
if $show_powerline; then
next_plugin=${plugins[$((plugin_index + 1))]}
IFS=' ' read -r -a next_colors <<<"$(get_plugin_colors "$next_plugin")"
powerbg=${!next_colors[0]:-$gray}
tmux set-option -ga status-left "#[fg=${!colors[1]},bg=${!colors[0]}] $script #[fg=${!colors[0]},bg=${powerbg},nobold,nounderscore,noitalics]${left_sep}"
powerbg=${gray}
else
tmux set-option -ga status-left "#[fg=${!colors[1]},bg=${!colors[0]}] $script "
fi
else
if $show_powerline; then
tmux set-option -ga status-right "#[fg=${!colors[0]},bg=${powerbg},nobold,nounderscore,noitalics]${right_sep}#[fg=${!colors[1]},bg=${!colors[0]}] $script "
powerbg=${!colors[0]}
else
tmux set-option -ga status-right "#[fg=${!colors[1]},bg=${!colors[0]}] $script "
fi
fi
done
}
main() { main() {
case $show_left_icon in case $show_left_icon in
rocket) rocket)
@ -116,45 +155,8 @@ main() {
tmux set-option -g status-left "#[bg=${green},fg=${dark_gray}]#{?client_prefix,#[bg=${yellow}],} ${left_icon}" tmux set-option -g status-left "#[bg=${green},fg=${dark_gray}]#{?client_prefix,#[bg=${yellow}],} ${left_icon}"
fi fi
# Status left build_status_bar "left"
for lplugin_index in "${!lplugins[@]}"; do build_status_bar "right"
lplugin="${lplugins[$lplugin_index]}"
# Check if colors are defined for the plugin
if [ -z "${plugin_colors[$lplugin]}" ]; then
continue
fi
IFS=' ' read -r -a colors <<<"$(get_plugin_colors "$lplugin")"
script="#($current_dir/$lplugin.sh)"
if $show_powerline; then
next_plugin=${lplugins[$((lplugin_index + 1))]}
IFS=' ' read -r -a next_colors <<<"$(get_plugin_colors "$next_plugin")"
powerbg=${!next_colors[0]:-$gray}
tmux set-option -ga status-left "#[fg=${!colors[1]},bg=${!colors[0]}] $script #[fg=${!colors[0]},bg=${powerbg},nobold,nounderscore,noitalics]${left_sep}"
powerbg=${gray}
else
tmux set-option -ga status-left "#[fg=${!colors[1]},bg=${!colors[0]}] $script "
fi
done
# Status right
for rplugin_index in "${!rplugins[@]}"; do
rplugin="${rplugins[$rplugin_index]}"
if [ -z "${plugin_colors[$rplugin]}" ]; then
continue
fi
IFS=' ' read -r -a colors <<<"$(get_plugin_colors "$rplugin")"
script="#($current_dir/$rplugin.sh)"
if $show_powerline; then
tmux set-option -ga status-right "#[fg=${!colors[0]},bg=${powerbg},nobold,nounderscore,noitalics]${right_sep}#[fg=${!colors[1]},bg=${!colors[0]}] $script "
powerbg=${!colors[0]}
else
tmux set-option -ga status-right "#[fg=${!colors[1]},bg=${!colors[0]}] $script "
fi
done
} }
# run main function # run main function