diff --git a/scripts/cpu_info.sh b/scripts/cpu_info.sh index b73db09a..482612b1 100755 --- a/scripts/cpu_info.sh +++ b/scripts/cpu_info.sh @@ -10,7 +10,7 @@ get_percent() case $(uname -s) in Linux) percent=$(LC_NUMERIC=en_US.UTF-8 top -bn2 -d 0.01 | grep "Cpu(s)" | tail -1 | sed "s/.*, *\([0-9.]*\)%* id.*/\1/" | awk '{print 100 - $1"%"}') - normalize_percent_len $percent + normalize_string_length $percent ;; Darwin) @@ -18,7 +18,9 @@ get_percent() cpucores=$(sysctl -n hw.logicalcpu) cpuusage=$(( cpuvalue / cpucores )) percent="$cpuusage%" - normalize_percent_len $percent + # a/b will get a integer, no decimal, like "1%" or "99%". + # So set the length to 3 below. + normalize_string_length $percent 3 ;; CYGWIN*|MINGW32*|MSYS*|MINGW*) diff --git a/scripts/dracula.sh b/scripts/dracula.sh index af0b2224..094a1e99 100755 --- a/scripts/dracula.sh +++ b/scripts/dracula.sh @@ -7,28 +7,20 @@ source $current_dir/utils.sh main() { - # set configuration option variables - terraform_label=$(get_tmux_option "@dracula-terraform-label" "") - show_fahrenheit=$(get_tmux_option "@dracula-show-fahrenheit" true) - show_location=$(get_tmux_option "@dracula-show-location" true) - fixed_location=$(get_tmux_option "@dracula-fixed-location") - show_powerline=$(get_tmux_option "@dracula-show-powerline" false) - show_flags=$(get_tmux_option "@dracula-show-flags" false) - show_left_icon=$(get_tmux_option "@dracula-show-left-icon" smiley) - show_left_icon_padding=$(get_tmux_option "@dracula-left-icon-padding" 1) - show_military=$(get_tmux_option "@dracula-military-time" false) - show_timezone=$(get_tmux_option "@dracula-show-timezone" true) - show_left_sep=$(get_tmux_option "@dracula-show-left-sep" ) - show_right_sep=$(get_tmux_option "@dracula-show-right-sep" ) - show_border_contrast=$(get_tmux_option "@dracula-border-contrast" false) - show_day_month=$(get_tmux_option "@dracula-day-month" false) - show_refresh=$(get_tmux_option "@dracula-refresh-rate" 5) - time_format=$(get_tmux_option "@dracula-time-format" "") - show_kubernetes_context_label=$(get_tmux_option "@dracula-kubernetes-context-label" "") - IFS=' ' read -r -a plugins <<< $(get_tmux_option "@dracula-plugins" "battery network weather") - show_empty_plugins=$(get_tmux_option "@dracula-show-empty-plugins" true) - # Dracula Color Pallette +# # Main Structure +# - Color Pallette +# - Get Tmux Option Variables +# - General +# - For Left Icon +# - For Window +# - For Plugins +# - Left Icon Area +# - Window Area +# - Right Plugins Area + + +# Dracula Color Pallette{ white='#f8f8f2' gray='#44475a' dark_gray='#282a36' @@ -40,57 +32,85 @@ main() red='#ff5555' pink='#ff79c6' yellow='#f1fa8c' +# } - # Handle left icon configuration - case $show_left_icon in - smiley) - left_icon="☺";; - session) - left_icon="#S";; - window) - left_icon="#W";; - *) - left_icon=$show_left_icon;; - esac - # Handle left icon padding - padding="" - if [ "$show_left_icon_padding" -gt "0" ]; then - padding="$(printf '%*s' $show_left_icon_padding)" - fi - left_icon="$left_icon$padding" +# Get Tmux Option Variables { - # Handle powerline option - if $show_powerline; then - right_sep="$show_right_sep" - left_sep="$show_left_sep" - fi + # general + show_powerline=$(get_tmux_option "@dracula-show-powerline" false) + show_left_sep=$(get_tmux_option "@dracula-show-left-sep" ) + show_right_sep=$(get_tmux_option "@dracula-show-right-sep" ) + show_border_contrast=$(get_tmux_option "@dracula-border-contrast" false) + status_bg=$(get_tmux_option "@dracula-status-bg" gray) + + # left icon area + left_icon=$(get_tmux_option "@dracula-show-left-icon" smiley) + left_icon_bg=$(get_tmux_option "@dracula-left-icon-bg" green) + left_icon_fg=$(get_tmux_option "@dracula-left-icon-fg" dark_gray) + left_icon_prefix_bg=$(get_tmux_option "@dracula-left-icon-prefix-on-bg" red) + left_icon_prefix_fg=$(get_tmux_option "@dracula-left-icon-prefix-on-fg" white) + left_icon_padding_left=$(get_tmux_option "@dracula-left-icon-padding-left" 1) + left_icon_padding_right=$(get_tmux_option "@dracula-left-icon-padding-right" 1) + left_icon_margin_right=$(get_tmux_option "@dracula-left-icon-margin-right" 1) + + # window area + show_flags=$(get_tmux_option "@dracula-show-flags" false) + window_bg=$(get_tmux_option "@dracula-window-bg" gray) + window_fg=$(get_tmux_option "@dracula-window-fg" white) + window_current_bg=$(get_tmux_option "@dracula-window-current-bg" dark_purple) + window_current_fg=$(get_tmux_option "@dracula-window-current-fg" white) + window_padding_left=$(get_tmux_option "@dracula-window-padding-left" 1) + window_padding_right=$(get_tmux_option "@dracula-window-padding-right" 1) + window_margin_right=$(get_tmux_option "@dracula-window-margin-right" 0) + window_left_sep=$(get_tmux_option "@dracula-window-left-sep" "") + window_right_sep=$(get_tmux_option "@dracula-window-right-sep" "") + window_left_sep_invert=$(get_tmux_option "@dracula-window-left-sep-invert" true) + window_disabled=$(get_tmux_option "@dracula-window-disabled" false) + + # right plugins area + # plugins general + show_refresh=$(get_tmux_option "@dracula-refresh-rate" 5) + show_empty_plugins=$(get_tmux_option "@dracula-show-empty-plugins" true) + IFS=' ' read -r -a plugins <<< $(get_tmux_option "@dracula-plugins" "battery network weather") - # Set timezone unless hidden by configuration - case $show_timezone in - false) - timezone="";; - true) - timezone="#(date +%Z)";; - esac + plugin_padding_left=$(get_tmux_option "@dracula-plugin-padding-left" 1) + plugin_padding_right=$(get_tmux_option "@dracula-plugin-padding-right" 1) + plugin_padding_rightmost=$(get_tmux_option "@dracula-plugin-padding-rightmost" 1) + + # terraform + terraform_label=$(get_tmux_option "@dracula-terraform-label" "") + + # weather + show_fahrenheit=$(get_tmux_option "@dracula-show-fahrenheit" true) + show_location=$(get_tmux_option "@dracula-show-location" true) + fixed_location=$(get_tmux_option "@dracula-fixed-location") + + # time + show_military=$(get_tmux_option "@dracula-military-time" false) + show_timezone=$(get_tmux_option "@dracula-show-timezone" true) + show_day_month=$(get_tmux_option "@dracula-day-month" false) + time_format=$(get_tmux_option "@dracula-time-format" "") + + # kubernetes-context + show_kubernetes_context_label=$(get_tmux_option "@dracula-kubernetes-context-label" "") +# } - case $show_flags in - false) - flags="" - current_flags="";; - true) - flags="#{?window_flags,#[fg=${dark_purple}]#{window_flags},}" - current_flags="#{?window_flags,#[fg=${light_purple}]#{window_flags},}" - esac + +# General Settings { # sets refresh interval to every 5 seconds 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 + # Handle powerline option + if $show_powerline; then + left_sep="$show_left_sep" + right_sep="$show_right_sep" + else # if disable powerline mark, equal to '', unify the logic of string. + left_sep='' + right_sep='' + window_left_sep='' + window_right_sep='' fi # set length @@ -109,20 +129,158 @@ main() tmux set-option -g message-style "bg=${gray},fg=${white}" # status bar - tmux set-option -g status-style "bg=${gray},fg=${white}" + tmux set-option -g status-style "bg=${!status_bg},fg=${white}" +# } - # Status left - if $show_powerline; then - tmux set-option -g status-left "#[bg=${green},fg=${dark_gray}]#{?client_prefix,#[bg=${yellow}],} ${left_icon} #[fg=${green},bg=${gray}]#{?client_prefix,#[fg=${yellow}],}${left_sep}" - powerbg=${gray} + +# Left Icon Area { + # Handle left icon configuration + case $left_icon in + smiley) + left_icon_content="☺";; + session) + left_icon_content="#S";; + window) + left_icon_content="#W";; + *) + left_icon_content=$left_icon;; + esac + + # Handle left icon padding + icon_pd_l="" + if [ "$left_icon_padding_left" -gt "0" ]; then + icon_pd_l="$(printf '%*s' $left_icon_padding_left)" + fi + icon_pd_r="" + if [ "$left_icon_padding_right" -gt "0" ]; then + icon_pd_r="$(printf '%*s' $left_icon_padding_right)" + fi + + # Handle left icon margin + icon_mg_r="" + if [ "$left_icon_margin_right" -gt "0" ]; then + icon_mg_r="$(printf '%*s' $left_icon_margin_right)" + fi + + # Left icon, with prefix status + tmux set-option -g status-left "\ +#{?client_prefix,#[fg=${!left_icon_prefix_fg}],#[fg=${!left_icon_fg}]}\ +#{?client_prefix,#[bg=${!left_icon_prefix_bg}],#[bg=${!left_icon_bg}]}\ +${icon_pd_l}${left_icon_content}${icon_pd_r}\ +#{?client_prefix,#[fg=${!left_icon_prefix_bg}],#[fg=${!left_icon_bg}]}\ +#[bg=${!status_bg}]\ +${left_sep}\ +${icon_mg_r}" + powerbg=${!status_bg} + +# } + + +# Window Area { + + # Handle window tabs padding & margin + win_pd_l="" + if [ "$window_padding_left" -gt "0" ]; then + win_pd_l="$(printf '%*s' $window_padding_left)" + fi + win_pd_r="" + if [ "$window_padding_right" -gt "0" ]; then + win_pd_r="$(printf '%*s' $window_padding_right)" + fi + win_mg_r="" + if [ "$window_margin_right" -gt "0" ]; then + win_mg_r="$(printf '%*s' $window_margin_right)" + fi + + # Handle window sep mark + # If window's separator not set, use the general separators. + if [ "$window_left_sep" = "" ]; then + window_left_sep=$left_sep + fi + if [ "$window_right_sep" = "" ]; then + window_right_sep=$left_sep + fi + + # Handle left separator if invert color + if $window_left_sep_invert; then + win_left_sep="#[bg=${!window_bg},fg=${!status_bg}]${window_left_sep}" + win_current_left_sep="#[bg=${!window_current_bg},fg=${!status_bg}]${window_left_sep}" else - tmux set-option -g status-left "#[bg=${green},fg=${dark_gray}]#{?client_prefix,#[bg=${yellow}],} ${left_icon}" + win_left_sep="#[bg=${!status_bg},fg=${!window_bg}]${window_left_sep}" + win_current_left_sep="#[bg=${!status_bg},fg=${!window_current_bg}]${window_left_sep}" fi - # Status right - tmux set-option -g status-right "" + # Handle window flags + case $show_flags in + false) + win_flags="";; + true) + win_flags=" #{?window_flags,#{window_flags},}";; + esac + + # Merge window tab + # If window disable, window won't be set, you can set it in tmux.conf. + if ! $window_disabled; then + tmux set-window-option -g window-status-format "\ +#[nobold,nounderscore,noitalics]\ +${win_left_sep}\ +#[fg=${!window_fg},bg=${!window_bg}]\ +${win_pd_l}#I #W${win_flags}${win_pd_r}\ +#[fg=${!window_bg},bg=${!status_bg}]${window_right_sep}${win_mg_r}" + + tmux set-window-option -g window-status-current-format "\ +#[nobold,nounderscore,noitalics]\ +${win_current_left_sep}\ +#[fg=${!window_current_fg},bg=${!window_current_bg}]\ +${win_pd_l}#I #W${win_flags}${win_pd_r}\ +#[fg=${!window_current_bg},bg=${!status_bg}]${window_right_sep}${win_mg_r}" + fi + + tmux set-window-option -g window-status-activity-style "bold" + tmux set-window-option -g window-status-bell-style "bold" +# } + + +# Right Plugins Area{ + tmux set-option -g status-right "" # reset + + # Handle plugins padding + plugin_pd_l="" + if [ "$plugin_padding_left" -gt "0" ]; then + plugin_pd_l="$(printf '%*s' $plugin_padding_left)" + fi + plugin_pd_r="" + if [ "$plugin_padding_right" -gt "0" ]; then + plugin_pd_r="$(printf '%*s' $plugin_padding_right)" + fi + plugin_pd_rm="" + if [ "$plugin_padding_rightmost" -gt "0" ]; then + plugin_pd_rm="$(printf '%*s' $plugin_padding_rightmost)" + fi + + # Set timezone unless hidden by configuration + case $show_timezone in + false) + timezone="";; + true) + timezone=" #(date +%Z)";; + esac + + # 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 + + # Prepare for handle last one + length=${#plugins[@]} + index=0 # counter + + # Loop add plugins for plugin in "${plugins[@]}"; do + index=$((index + 1)) # count if case $plugin in custom:*) true;; *) false;; esac; then script=${plugin#"custom:"} @@ -212,13 +370,13 @@ main() script=${time_format} else if $show_day_month && $show_military ; then # military time and dd/mm - script="%a %d/%m %R ${timezone} " + script="%a %d/%m %R${timezone}" elif $show_military; then # only military time - script="%a %m/%d %R ${timezone} " + script="%a %m/%d %R${timezone}" elif $show_day_month; then # only dd/mm - script="%a %d/%m %I:%M %p ${timezone} " + script="%a %d/%m %I:%M %p${timezone}" else - script="%a %m/%d %I:%M %p ${timezone} " + script="%a %m/%d %I:%M %p${timezone}" fi fi @@ -226,32 +384,30 @@ main() continue fi - if $show_powerline; then - if $show_empty_plugins; then - tmux set-option -ga status-right "#[fg=${!colors[0]},bg=${powerbg},nobold,nounderscore,noitalics]${right_sep}#[fg=${!colors[1]},bg=${!colors[0]}] $script " - else - tmux set-option -ga status-right "#{?#{==:$script,},,#[fg=${!colors[0]},nobold,nounderscore,noitalics]${right_sep}#[fg=${!colors[1]},bg=${!colors[0]}] $script }" - fi - powerbg=${!colors[0]} + # Last one padding right + if [[ "$index" -eq "$length" ]]; then + pd_r=$plugin_pd_rm else - if $show_empty_plugins; then - tmux set-option -ga status-right "#[fg=${!colors[1]},bg=${!colors[0]}] $script " - else - tmux set-option -ga status-right "#{?#{==:$script,},,#[fg=${!colors[1]},bg=${!colors[0]}] $script }" - fi + pd_r=$plugin_pd_r fi - done - # Window option - if $show_powerline; then - tmux set-window-option -g window-status-current-format "#[fg=${gray},bg=${dark_purple}]${left_sep}#[fg=${white},bg=${dark_purple}] #I #W${current_flags} #[fg=${dark_purple},bg=${gray}]${left_sep}" - else - tmux set-window-option -g window-status-current-format "#[fg=${white},bg=${dark_purple}] #I #W${current_flags} " - fi + # Merge plugin + if $show_empty_plugins; then + tmux set-option -ga status-right "\ +#[fg=${!colors[0]},bg=${powerbg}nobold,nounderscore,noitalics]\ +${right_sep}#[fg=${!colors[1]},bg=${!colors[0]}]\ +${plugin_pd_l}$script${pd_r}" + else + tmux set-option -ga status-right "\ +#{?#{==:$script,},,#[fg=${!colors[0]},nobold,nounderscore,noitalics]\ +${right_sep}#[fg=${!colors[1]},bg=${!colors[0]}]\ +${plugin_pd_l}$script${pd_r}}" + fi +#[nobold,nounderscore,noitalics]\ + powerbg=${!colors[0]} - tmux set-window-option -g window-status-format "#[fg=${white}]#[bg=${gray}] #I #W${flags}" - tmux set-window-option -g window-status-activity-style "bold" - tmux set-window-option -g window-status-bell-style "bold" + done +# } } # run main function diff --git a/scripts/gpu_power.sh b/scripts/gpu_power.sh index c97e6ee6..fd46e530 100755 --- a/scripts/gpu_power.sh +++ b/scripts/gpu_power.sh @@ -32,7 +32,7 @@ get_gpu() else usage='unknown' fi - normalize_percent_len $usage + normalize_string_length $usage } main() diff --git a/scripts/gpu_ram_info.sh b/scripts/gpu_ram_info.sh index 07de6dfa..e8f83883 100755 --- a/scripts/gpu_ram_info.sh +++ b/scripts/gpu_ram_info.sh @@ -31,7 +31,7 @@ get_gpu() else usage='unknown' fi - normalize_percent_len $usage + normalize_string_length $usage } main() diff --git a/scripts/gpu_usage.sh b/scripts/gpu_usage.sh index 552ed2b7..d3d4964f 100755 --- a/scripts/gpu_usage.sh +++ b/scripts/gpu_usage.sh @@ -31,7 +31,7 @@ get_gpu() else usage='unknown' fi - normalize_percent_len $usage + normalize_string_length $usage } main() diff --git a/scripts/network_ping.sh b/scripts/network_ping.sh index 442f6be4..de0d5d05 100755 --- a/scripts/network_ping.sh +++ b/scripts/network_ping.sh @@ -15,7 +15,9 @@ ping_function() { # storing the hostname/IP in the variable PINGSERVER, default is google.com pingserver=$(get_tmux_option "@dracula-ping-server" "google.com") pingtime=$(ping -c 1 "$pingserver" | tail -1 | awk '{print $4}' | cut -d '/' -f 2) - echo "$pingtime ms" + # Truncate the integer part + # Network ping doesn't always stable, so keep min length to 5(999ms). + normalize_string_length "${pingtime%.*}ms" 5 ;; CYGWIN* | MINGW32* | MSYS* | MINGW*) @@ -26,7 +28,10 @@ ping_function() { main() { - echo $(ping_function) + # Label Ref            + ping_label=$(get_tmux_option "@dracula-ping-label" "Ping") + echo "$ping_label $(ping_function)" + RATE=$(get_tmux_option "@dracula-ping-rate" 5) sleep $RATE } diff --git a/scripts/utils.sh b/scripts/utils.sh index a296192d..bdc1fbe8 100644 --- a/scripts/utils.sh +++ b/scripts/utils.sh @@ -11,15 +11,29 @@ get_tmux_option() { fi } -# normalize the percentage string to always have a length of 5 -normalize_percent_len() { - # the max length that the percent can reach, which happens for a two digit number with a decimal house: "99.9%" - max_len=5 +# normalize the percentage string to customize length and align +# $1 percentage string +# $2 max length, default is 3 (99%) +# $3 align (left, center, right), default is right +normalize_string_length() { + # the max length that the percent can reach, which happens for a two digit number with a decimal house: "99.9%". percent_len=${#1} + max_len=${2:-5} # default + align=${3:-'right'} let diff_len=$max_len-$percent_len - # if the diff_len is even, left will have 1 more space than right - let left_spaces=($diff_len+1)/2 - let right_spaces=($diff_len)/2 + case $align in + 'center') + # if the diff_len is even, left will have 1 more space than right + let left_spaces=($diff_len+1)/2 + let right_spaces=($diff_len)/2;; + 'left') + let left_spaces=0 + let right_spaces=$diff_len;; + 'right') + let left_spaces=$diff_len + let right_spaces=0;; + esac + printf "%${left_spaces}s%s%${right_spaces}s\n" "" $1 "" }