Skip to content

Commit

Permalink
v1.5.5 (#98)
Browse files Browse the repository at this point in the history
  • Loading branch information
janoamaral authored Jun 30, 2024
2 parents c3bc283 + 8f0f531 commit c245a09
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 46 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
# JUnit reports
**/report.xml
venv/**
120 changes: 86 additions & 34 deletions src/battery-widget.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,70 +4,122 @@
ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/.."
. "${ROOT_DIR}/lib/coreutils-compat.sh"

# check if not enabled
# Check if the battery widget is enabled
SHOW_BATTERY_WIDGET=$(tmux show-option -gv @tokyo-night-tmux_show_battery_widget 2>/dev/null)
if [ "${SHOW_BATTERY_WIDGET}" != "1" ]; then
exit 0
fi

# get value from tmux config
BATTERY_NAME=$(tmux show-option -gv @tokyo-night-tmux_battery_name 2>/dev/null) # default 'BAT1'
BATTERY_LOW=$(tmux show-option -gv @tokyo-night-tmux_battery_low_threshold 2>/dev/null) # default 21
# Get values from tmux config or set defaults
BATTERY_NAME=$(tmux show-option -gv @tokyo-night-tmux_battery_name 2>/dev/null)
BATTERY_LOW=$(tmux show-option -gv @tokyo-night-tmux_battery_low_threshold 2>/dev/null)
RESET="#[fg=brightwhite,bg=#15161e,nobold,noitalics,nounderscore,nodim]"

DISCHARGING_ICONS=("󰁺" "󰁻" "󰁼" "󰁽" "󰁾" "󰁿" "󰂀" "󰂁" "󰂂" "󰁹")
CHARGING_ICONS=("󰢜" "󰂆" "󰂇" "󰂈" "󰢝" "󰂉" "󰢞" "󰂊" "󰂋" "󰂅")
NOT_CHARGING_ICON="󰚥"
NO_BATTERY_ICON="󱉝"
DEFAULT_BATTERY_LOW=21

default_show_battery_percentage=1
default_battery_low="21"
if [[ "$(uname)" == "Darwin" ]]; then
default_battery_name="InternalBattery-0"
else
default_battery_name="BAT1"
fi

BATTERY_NAME="${BATTERY_NAME:-$default_battery_name}"
BATTERY_LOW="${BATTERY_LOW:-$default_battery_low}"
BATTERY_LOW="${BATTERY_LOW:-$DEFAULT_BATTERY_LOW}"

# get battery stats
if [[ "$(uname)" == "Darwin" ]]; then
pmstat=$(pmset -g batt | grep $BATTERY_NAME)
BATTERY_STATUS=$(echo $pmstat | awk '{print $4}' | sed 's/[^a-z]*//g')
BATTERY_PERCENTAGE=$(echo $pmstat | awk '{print $3}' | sed 's/[^0-9]*//g')
else
BATTERY_STATUS=$(</sys/class/power_supply/${BATTERY_NAME}/status)
BATTERY_PERCENTAGE=$(</sys/class/power_supply/${BATTERY_NAME}/capacity)
# Check if battery exists
battery_exists() {
case "$(uname)" in
"Darwin")
pmset -g batt | grep -q "$BATTERY_NAME"
;;
"Linux")
[[ -d "/sys/class/power_supply/$BATTERY_NAME" ]]
;;
"CYGWIN" | "MINGW" | "MSYS" | "Windows_NT")
WMIC PATH Win32_Battery Get EstimatedChargeRemaining 2>&1 | grep -q "EstimatedChargeRemaining"
;;
*)
return 1
;;
esac
}

# Exit if no battery is found
if ! battery_exists; then
echo "#[fg=green,bg=default]░ ${NOT_CHARGING_ICON}${RESET}#[bg=default]"
exit 0
fi

# set color and icon based on battery status
case "${BATTERY_STATUS}" in
"Charging" | "charging")
ICONS="${CHARGING_ICONS[$((BATTERY_PERCENTAGE / 10 - 1))]}"
# Get battery stats for different OS
get_battery_stats() {
local battery_name=$1
local battery_status=""
local battery_percentage=""

case "$(uname)" in
"Darwin")
pmstat=$(pmset -g batt | grep "$battery_name")
battery_status=$(echo "$pmstat" | awk '{print $4}' | sed 's/[^a-zA-Z]*//g')
battery_percentage=$(echo "$pmstat" | awk '{print $3}' | sed 's/[^0-9]*//g')
;;
"Linux")
if [[ -f "/sys/class/power_supply/${battery_name}/status" && -f "/sys/class/power_supply/${battery_name}/capacity" ]]; then
battery_status=$(<"/sys/class/power_supply/${battery_name}/status")
battery_percentage=$(<"/sys/class/power_supply/${battery_name}/capacity")
else
battery_status="Unknown"
battery_percentage="0"
fi
;;
"CYGWIN" | "MINGW" | "MSYS" | "Windows_NT")
battery_percentage=$(WMIC PATH Win32_Battery Get EstimatedChargeRemaining | grep -Eo '[0-9]+')
[[ -n $battery_percentage ]] && battery_status="Discharging" || battery_status="Unknown"
;;
*)
battery_status="UnsupportedOS"
battery_percentage=0
;;
esac

echo "$battery_status $battery_percentage"
}

# Fetch the battery status and percentage
read -r BATTERY_STATUS BATTERY_PERCENTAGE < <(get_battery_stats "$BATTERY_NAME")

# Ensure percentage is a number
if ! [[ $BATTERY_PERCENTAGE =~ ^[0-9]+$ ]]; then
BATTERY_PERCENTAGE=0
fi

# Determine icon and color based on battery status and percentage
case "$BATTERY_STATUS" in
"Charging" | "Charged" | "charging" | "Charged")
ICON="${CHARGING_ICONS[$((BATTERY_PERCENTAGE / 10))]}"
;;
"Discharging" | "discharging")
ICONS="${DISCHARGING_ICONS[$((BATTERY_PERCENTAGE / 10 - 1))]}"
;;
"Not charging" | "AC")
ICONS="${NOT_CHARGING_ICON}"
ICON="${DISCHARGING_ICONS[$((BATTERY_PERCENTAGE / 10))]}"
;;
"Full" | "charged")
ICONS="${NOT_CHARGING_ICON}"
"Full" | "charged" | "full" | "AC")
ICON="$NOT_CHARGING_ICON"
;;
*)
ICONS="${NO_BATTERY_ICON}"
BATTERY_PERCENTAGE="0"
ICON="$NO_BATTERY_ICON"
;;
esac

# set color on battery capacity
if [[ ${BATTERY_PERCENTAGE} -lt ${BATTERY_LOW} ]]; then
_color="#[fg=red,bg=default,bold]"
elif [[ ${BATTERY_PERCENTAGE} -ge 100 ]]; then
_color="#[fg=green,bg=default]"
# Set color based on battery percentage
if [[ $BATTERY_PERCENTAGE -lt $BATTERY_LOW ]]; then
color="#[fg=red,bg=default,bold]"
elif [[ $BATTERY_PERCENTAGE -ge 100 ]]; then
color="#[fg=green,bg=default]"
else
_color="#[fg=yellow,bg=default]"
color="#[fg=yellow,bg=default]"
fi

echo "${_color}${ICONS}${RESET}#[bg=default] ${BATTERY_PERCENTAGE}% "
# Print the battery status with some extra spaces for padding
echo "${color}${ICON}${RESET} #[bg=default] ${BATTERY_PERCENTAGE}% "
11 changes: 8 additions & 3 deletions src/custom-number.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/.."
. "${ROOT_DIR}/lib/coreutils-compat.sh"

format_hide=""
format_none="0123456789"
format_digital="🯰🯱🯲🯳🯴🯵🯶🯷🯸🯹"
format_fsquare="󰎡󰎤󰎧󰎪󰎭󰎱󰎳󰎶󰎹󰎼"
Expand All @@ -18,18 +19,22 @@ FORMAT=${2:-none}

# Preserve leading whitespace for bash
format="$(eval echo \"\$format_${FORMAT}\")"

if [ "$FORMAT" = "hide" ]; then
exit 0
fi

if [ -z "$format" ]; then
echo "Invalid format: $FORMAT"
exit 1
fi

# If format is roman numerals (-r), only handle IDs of 1 digit
if [ "$FORMAT" = "roman" ] && [ ${#ID} -gt 1 ]; then
echo -n $ID
continue
echo -n "$ID "
else
for ((i = 0; i < ${#ID}; i++)); do
DIGIT=${ID:i:1}
echo -n "${format:DIGIT:1}"
echo -n "${format:DIGIT:1} "
done
fi
25 changes: 18 additions & 7 deletions src/datetime-widget.sh
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,35 @@ time_string=""

if [[ $date_format == "YMD" ]]; then
# Year Month Day date format
date_string="%Y-%m-%d"
date_string=" %Y-%m-%d"
elif [[ $date_format == "MDY" ]]; then
# Month Day Year date format
date_string="%m-%d-%Y"
date_string=" %m-%d-%Y"
elif [[ $date_format == "DMY" ]]; then
# Day Month Year date format
date_string="%d-%m-%Y"
date_string=" %d-%m-%Y"
elif [[ $date_format == "hide" ]]; then
# Day Month Year date format
date_string=""
else
# Default to YMD date format if not specified
date_string="%Y-%m-%d"
date_string=" %Y-%m-%d"
fi

if [[ $time_format == "12H" ]]; then
# 12-hour format with AM/PM
time_string="%I:%M %p"
time_string="%I:%M %p "
elif [[ $time_format == "hide" ]]; then
# 24-hour format
time_string=""
else
# Default to 24-hour format if not specified
time_string="%H:%M"
time_string="%H:%M "
fi

separator=""
if [[ $date_string && $time_string ]]; then
separator=""
fi

echo "$RESET#[fg=${THEME[foreground]},bg=${THEME[bblack]}] $date_string #[]❬ $time_string "
echo "$RESET#[fg=${THEME[foreground]},bg=${THEME[bblack]}]$date_string $separator$time_string"
4 changes: 2 additions & 2 deletions tokyo-night.tmux
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ tmux set -g status-left "#[fg=${THEME[bblack]},bg=${THEME[blue]},bold] #{?client

#+--- Windows ---+
# Focus
tmux set -g window-status-current-format "$RESET#[fg=${THEME[green]},bg=${THEME[bblack]}] #{?#{==:#{pane_current_command},ssh},󰣀,} #[fg=${THEME[foreground]},bold,nodim]$window_number #W#[nobold]#{?window_zoomed_flag, $zoom_number, $custom_pane} #{?window_last_flag,,} "
tmux set -g window-status-current-format "$RESET#[fg=${THEME[green]},bg=${THEME[bblack]}] #{?#{==:#{pane_current_command},ssh},󰣀 , }#[fg=${THEME[foreground]},bold,nodim]$window_number#W#[nobold]#{?window_zoomed_flag, $zoom_number, $custom_pane}#{?window_last_flag, , }"
# Unfocused
tmux set -g window-status-format "$RESET#[fg=${THEME[foreground]}] #{?#{==:#{pane_current_command},ssh},󰣀,}${RESET} $window_number #W#[nobold,dim]#{?window_zoomed_flag, $zoom_number, $custom_pane} #[fg=${THEME[yellow]}]#{?window_last_flag,󰁯 , } "
tmux set -g window-status-format "$RESET#[fg=${THEME[foreground]}] #{?#{==:#{pane_current_command},ssh},󰣀 , }${RESET}$window_number#W#[nobold,dim]#{?window_zoomed_flag, $zoom_number, $custom_pane}#[fg=${THEME[yellow]}]#{?window_last_flag,󰁯 , }"

#+--- Bars RIGHT ---+
tmux set -g status-right "$battery_status$current_path$cmus_status$netspeed$git_status$wb_git_status$date_and_time"
Expand Down

0 comments on commit c245a09

Please sign in to comment.