Skip to content

Instantly share code, notes, and snippets.

@aMir733
Last active June 6, 2022 20:58
Show Gist options
  • Save aMir733/c58ebe759c8bba6b6f6170f98e292500 to your computer and use it in GitHub Desktop.
Save aMir733/c58ebe759c8bba6b6f6170f98e292500 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Variables
ADB="adb -s 52105802eed17447"
THRESHOLD=35
INTERVAL=1
INTERVAL_ERROR=300
GTK3_SETTINGS="$HOME/.config/gtk-3.0/settings.ini"
GTK3_OPTION="gtk-application-prefer-dark-theme"
GTK4_OPTION="org.gnome.desktop.interface color-scheme"
# Functions
set_gtk3 () {
[[ "$CUR_GTK3" == "$1" ]] && return 0
sed -i 's/^'${GTK3_OPTION}'=[0-1]$/'${GTK3_OPTION}'='${1}'/' $GTK3_SETTINGS &>/dev/null
CUR_GTK3="$1"
}
set_gtk4 () {
[[ "$CUR_GTK4" == "$1" ]] && return 0
gsettings set $GTK4_OPTION "prefer-${1}"
CUR_GTK4="$1"
}
set_theme () {
[[ "$1" == 0 ]] && THEME=light || THEME=dark
set_gtk3 $1
set_gtk4 $THEME
}
# Get the current values ( ran only once at the start of the script )
CUR_GTK3=$(grep "$GTK3_OPTION" "$GTK3_SETTINGS" | grep -o "[0-1]$")
CUR_GTK4=$(gsettings get $GTK4_OPTION | tr -d "'" | cut -d'-' -f2)
while true ; do
# Executed every $INTERVAL second(s)
BRIGHTNESS="$($ADB shell cat /sys/class/sensors/light_sensor/lux | cut -d',' -f4)" || (sleep $INTERVAL_ERROR ; continue)
if [ "$BRIGHTNESS" -gt "$THRESHOLD" ] ; then
set_theme 0 # Light
else
set_theme 1 # Dark
fi
sleep $INTERVAL
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment