Files
dotfiles_i3_notebook_arch/polybar/scripts/brightness.sh
2025-09-22 08:27:51 +02:00

73 lines
1.8 KiB
Bash
Executable File

#!/bin/bash
DEVICE="intel_backlight"
BRIGHTNESS=$(brightnessctl get)
MAX_BRIGHTNESS=$(brightnessctl max)
PERCENTAGE=$((BRIGHTNESS * 100 / MAX_BRIGHTNESS))
# Define icons
ICON_BRIGHT=""
ICON_DIM=""
# Set icon based on brightness level
if [ $PERCENTAGE -ge 50 ]; then
ICON=$ICON_BRIGHT
else
ICON=$ICON_DIM
fi
# Function to calculate the step size dynamically
calculate_step() {
local percentage=$1
local min_step=$((MAX_BRIGHTNESS / 100)) # 1% step
local max_step=$((MAX_BRIGHTNESS * 15 / 100)) # 15% step
# Scale the step size linearly between min_step and max_step
local step=$((min_step + (max_step - min_step) * percentage / 100))
# Ensure the step is within bounds
if [ $step -lt $min_step ]; then
step=$min_step
elif [ $step -gt $max_step ]; then
step=$max_step
fi
echo $step
}
# Function to calculate the new brightness value
calculate_new_brightness() {
local current=$1
local max=$2
local change=$3
# Calculate the new brightness value
local new_value=$((current + change))
# Ensure the new value is within the valid range (1% to 100%)
if [ $new_value -lt 1 ]; then
new_value=1
elif [ $new_value -gt $max ]; then
new_value=$max
fi
echo $new_value
}
# Handle click actions
case $1 in
"up")
STEP=$(calculate_step $PERCENTAGE)
NEW_BRIGHTNESS=$(calculate_new_brightness $BRIGHTNESS $MAX_BRIGHTNESS $STEP)
brightnessctl set $NEW_BRIGHTNESS > /dev/null
;;
"down")
STEP=$(calculate_step $PERCENTAGE)
NEW_BRIGHTNESS=$(calculate_new_brightness $BRIGHTNESS $MAX_BRIGHTNESS $(( -STEP )))
brightnessctl set $NEW_BRIGHTNESS > /dev/null
;;
esac
# Output for Polybar
echo "$ICON $PERCENTAGE%"