refactor: extract out new time script

This commit is contained in:
Abhishek Keshri 2024-03-17 01:17:05 +05:30 committed by Abhishek Keshri
parent ff7ac9af33
commit a7617ac891
2 changed files with 49 additions and 60 deletions

37
scripts/time.sh Executable file
View file

@ -0,0 +1,37 @@
#!/usr/bin/env bash
export LC_ALL=en_US.UTF-8
current_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
source "$current_dir"/utils.sh
show_military=$(get_tmux_option "@tmux2k-military-time" false)
show_timezone=$(get_tmux_option "@tmux2k-show-timezone" false)
show_day_month=$(get_tmux_option "@tmux2k-day-month" false)
get_timezone() {
if $show_timezone; then
date +"%Z "
fi
}
main() {
timezone=$(get_timezone)
if $show_military; then
tmux set-option -g clock-mode-style 24
else
tmux set-option -g clock-mode-style 12
fi
if $show_day_month && $show_military; then
date +" %a %d/%m %R ${timezone}"
elif $show_military; then
date +" %a %m/%d %R ${timezone}"
elif $show_day_month; then
date +" %a %b %d %I:%M %p ${timezone}"
else
date +" %a %I:%M %p ${timezone}"
fi
}
main

View file

@ -13,12 +13,9 @@ show_powerline=$(get_tmux_option "@tmux2k-show-powerline" true)
show_flags=$(get_tmux_option "@tmux2k-show-flags" true) show_flags=$(get_tmux_option "@tmux2k-show-flags" true)
show_left_icon=$(get_tmux_option "@tmux2k-show-left-icon" rocket) show_left_icon=$(get_tmux_option "@tmux2k-show-left-icon" rocket)
show_left_icon_padding=$(get_tmux_option "@tmux2k-left-icon-padding" 0) show_left_icon_padding=$(get_tmux_option "@tmux2k-left-icon-padding" 0)
show_military=$(get_tmux_option "@tmux2k-military-time" true)
show_timezone=$(get_tmux_option "@tmux2k-show-timezone" false)
show_left_sep=$(get_tmux_option "@tmux2k-show-left-sep") show_left_sep=$(get_tmux_option "@tmux2k-show-left-sep")
show_right_sep=$(get_tmux_option "@tmux2k-show-right-sep") show_right_sep=$(get_tmux_option "@tmux2k-show-right-sep")
show_border_contrast=$(get_tmux_option "@tmux2k-border-contrast" true) show_border_contrast=$(get_tmux_option "@tmux2k-border-contrast" true)
show_day_month=$(get_tmux_option "@tmux2k-day-month" false)
show_refresh=$(get_tmux_option "@tmux2k-refresh-rate" 60) show_refresh=$(get_tmux_option "@tmux2k-refresh-rate" 60)
IFS=' ' read -r -a rplugins <<<"$(get_tmux_option '@tmux2k-right-plugins' 'battery network time')" IFS=' ' read -r -a rplugins <<<"$(get_tmux_option '@tmux2k-right-plugins' 'battery network time')"
IFS=' ' read -r -a lplugins <<<"$(get_tmux_option '@tmux2k-left-plugins' 'git cpu-usage ram-usage')" IFS=' ' read -r -a lplugins <<<"$(get_tmux_option '@tmux2k-left-plugins' 'git cpu-usage ram-usage')"
@ -97,37 +94,9 @@ main() {
"$current_dir"/sleep_weather.sh "$show_fahrenheit" "$show_location" "$fixed_location" & "$current_dir"/sleep_weather.sh "$show_fahrenheit" "$show_location" "$fixed_location" &
fi fi
# Set timezone unless hidden by configuration
case $show_timezone in
false)
timezone=""
;;
true)
timezone="#(date +%Z)"
;;
esac
case $show_flags in
false)
flags=""
current_flags=""
;;
true)
flags="#{?window_flags,#[fg=${light_purple}]#{window_flags},}"
current_flags="#{?window_flags,#[fg=${light_red}]#{window_flags},}"
;;
esac
# sets refresh interval to every 5 seconds # sets refresh interval to every 5 seconds
tmux set-option -g status-interval "$show_refresh" tmux set-option -g status-interval "$show_refresh"
# set the prefix + t time format
if $show_military; then
tmux set-option -g clock-mode-style 24
else
tmux set-option -g clock-mode-style 12
fi
# set length # set length
tmux set-option -g status-left-length 100 tmux set-option -g status-left-length 100
tmux set-option -g status-right-length 100 tmux set-option -g status-right-length 100
@ -172,18 +141,6 @@ main() {
script="#(cat $datafile)" script="#(cat $datafile)"
powerbg=${lplugins[$((lplugin_index + 1))]} powerbg=${lplugins[$((lplugin_index + 1))]}
;; ;;
"time")
if $show_day_month && $show_military; then # military time and dd/mm
script=" %a %d/%m %R ${timezone}"
elif $show_military; then # only military time
script=" %a %m/%d %R ${timezone}"
elif $show_day_month; then # only dd/mm
script=" %a %d/%m %I:%M %p ${timezone}"
else
script=" %a %m/%d %I:%M %p ${timezone}"
fi
powerbg=${lplugins[$((lplugin_index + 1))]}
;;
*) *)
script="#($current_dir/$lplugin.sh)" script="#($current_dir/$lplugin.sh)"
next_plugin=${lplugins[$((lplugin_index + 1))]} next_plugin=${lplugins[$((lplugin_index + 1))]}
@ -208,15 +165,22 @@ main() {
tmux set -g status-justify absolute-centre tmux set -g status-justify absolute-centre
# Window option # Window option
if $show_powerline; then case $show_flags in
tmux set-window-option -g window-status-current-format "#[fg=${blue},bg=${gray}]${left_win_sep}#[bg=${blue}]${current_flags}#[fg=${black}] #I:#W #[fg=${blue},bg=${gray}]${right_win_sep}" false)
else flags=""
tmux set-window-option -g window-status-current-format "#[fg=${white},bg=${blue}] #I:#W ${current_flags} " current_flags=""
fi ;;
true)
flags="#{?window_flags,#[fg=${light_purple}]#{window_flags},}"
current_flags="#{?window_flags,#[fg=${light_red}]#{window_flags},}"
;;
esac
if $show_powerline; then if $show_powerline; then
tmux set-window-option -g window-status-current-format "#[fg=${blue},bg=${gray}]${left_win_sep}#[bg=${blue}]${current_flags}#[fg=${black}] #I:#W #[fg=${blue},bg=${gray}]${right_win_sep}"
tmux set-window-option -g window-status-format "#[fg=${light_gray},bg=${gray}]${left_win_sep}#[bg=${light_gray}]${flags}#[fg=${white}] #I:#W #[fg=${light_gray},bg=${gray}]${right_win_sep}" tmux set-window-option -g window-status-format "#[fg=${light_gray},bg=${gray}]${left_win_sep}#[bg=${light_gray}]${flags}#[fg=${white}] #I:#W #[fg=${light_gray},bg=${gray}]${right_win_sep}"
else else
tmux set-window-option -g window-status-current-format "#[fg=${white},bg=${blue}] #I:#W ${current_flags} "
tmux set-window-option -g window-status-format "#[fg=${white},bg=${light_gray}] #I:#W ${flags} " tmux set-window-option -g window-status-format "#[fg=${white},bg=${light_gray}] #I:#W ${flags} "
fi fi
@ -244,17 +208,6 @@ main() {
done done
script="#(cat $datafile)" script="#(cat $datafile)"
;; ;;
"time")
if $show_day_month && $show_military; then # military time and dd/mm
script=" %a %d/%m %R ${timezone}"
elif $show_military; then # only military time
script=" %a %m/%d %R ${timezone}"
elif $show_day_month; then # only dd/mm
script=" %a %d/%m %I:%M %p ${timezone}"
else
script=" %a %m/%d %I:%M %p ${timezone}"
fi
;;
*) *)
script="#($current_dir/$rplugin.sh)" script="#($current_dir/$rplugin.sh)"
;; ;;
@ -267,7 +220,6 @@ main() {
tmux set-option -ga status-right "#[fg=${!colors[1]},bg=${!colors[0]}] $script " tmux set-option -ga status-right "#[fg=${!colors[1]},bg=${!colors[0]}] $script "
fi fi
done done
} }
# run main function # run main function