-
Notifications
You must be signed in to change notification settings - Fork 0
/
night_light.sh
executable file
·324 lines (289 loc) · 10.2 KB
/
night_light.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
#!/usr/bin/env bash
# shellcheck disable=SC2004,SC2317,SC2053
## Author: Tommy Miland (@tmiland) - Copyright (c) 2023
######################################################################
#### night_light.sh ####
#### Automatic night light script for gnome ####
#### Script to adjust night light in gnome ####
#### Maintained by @tmiland ####
######################################################################
# VERSION='1.0.0' # Must stay on line 14 for updater to fetch the numbers
#------------------------------------------------------------------------------#
#
# MIT License
#
# Copyright (c) 2023 Tommy Miland
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#
#------------------------------------------------------------------------------#
## Uncomment for debugging purpose
if [[ $2 == "debug" ]]; then
set -o errexit
set -o pipefail
set -o nounset
set -o xtrace
fi
# Symlink: ln -sfn ~/.scripts/night_light.sh ~/.local/bin/night_light.sh
# Crontab: 1 1 * * * bash ~/.scripts/night_light.sh > /dev/null 2>&1
# Based on source: https://discussion.fedoraproject.org/t/can-i-manipulate-night-mode-from-command-line/72853/2
yr="1-68562/Norway/Telemark/Tinn/Rjukan"
yr_url=https://www.yr.no/en/other-conditions/$yr
pkg=lynx
if ! dpkg -s $pkg >/dev/null 2>&1; then
apt install $pkg
fi
sun() {
$pkg --dump $yr_url | grep -oE "Sun$1 [[:digit:]]+:[[:digit:]]+" | sed -n "s/.*Sun$1 *\([^ ]*.*\)/\1/p"
}
sunrise=$(sun rise)
sunset=$(sun set)
sunrise-transition() {
date -d"$1$2 minutes $sunrise" '+%H:%M'
}
sunset-transition() {
date -d"$1$2 minutes $sunset" '+%H:%M'
}
cfg_file=/home/tommy/.github/night_light/.night_light_config
# Read hidden configuration file with entries separated by " " into array
IFS=' ' read -ra CfgArr < $cfg_file
max_bright="${CfgArr[0]}"
after_sunrise="${CfgArr[1]}"
min_bright="${CfgArr[2]}"
before_sunset="${CfgArr[3]}"
# Source: https://www.omgubuntu.co.uk/2017/07/adjust-color-temperature-gnome-night-light
# 1000 — Lowest value (super warm/red)
# 4000 — Default night light on temperature
# 5500 — Balanced night light temperature
# 6500 — Default night light off temperature
# 10000 — Highest value (super cool/blue)
temperature_morning="4500"
temperature_noon="$max_bright"
temperature_evening="3500"
temperature_night="$min_bright"
config() {
. ./night_light_config.sh
}
night-light-temperature() {
gsettings set org.gnome.settings-daemon.plugins.color night-light-temperature "$1"
}
dark_toggle=$([[ $(gsettings get org.gnome.desktop.interface color-scheme) =~ "dark" ]] && echo dark || echo light)
toogle_dark() {
if [[ $DT == 1 ]]; then
if command -v dark-toggle &> /dev/null; then
if ! [[ $dark_toggle == "dark" ]]; then
dark-toggle
else
echo "Color-scheme is already set to dark"
exit 1
fi
else
echo "dark-toggle could not be found"
exit 1
fi
fi
}
toggle_light() {
if [[ $DT == 1 ]]; then
if command -v dark-toggle &> /dev/null; then
if ! [[ $dark_toggle == "light" ]]; then
dark-toggle
else
echo "Color-scheme is already set to light"
exit 1
fi
else
echo "dark-toggle could not be found"
exit 1
fi
fi
}
# Source: https://askubuntu.com/a/894470
# global variable
LastSetting=$(gsettings get org.gnome.settings-daemon.plugins.color night-light-temperature | sed 's|uint32 ||g')
auto-run() {
while true
do
# Current seconds
secNow=$(date +"%s")
secSunrise=$(date --date="$sunrise today" +%s)
secSunset=$(date --date="$sunset today" +%s)
set-and-sleep() {
if [[ $1 != $LastSetting ]]
then
# sudo sh -c "echo $1 | sudo tee $backlight"
# echo "$1" > "/tmp/display-current-brightness"
LastSetting="$1"
night-light-temperature "$1"
echo "Temperature set to ($1)"
fi
sleep 60
}
re='^[0-9]+$' # regex for valid numbers
calc-level-and-sleep() {
# Parms $1 = number of minutes for total transition
# $2 = number of seconds into transition
# Daytime - nightime = transition brightness levels
transition_spread=$(( $max_bright - $min_bright ))
secTotal=$(( $1 * 60 )) # Convert total transition minutes to seconds
Adjust=$( bc <<< "scale=6; $transition_spread * ( $2 / $secTotal )" )
Adjust=$( echo "$Adjust" | cut -f1 -d"." ) # Truncate number to integer
if ! [[ $Adjust =~ $re ]] ; then
Adjust=0 # When we get to last minute $Adjust can be non-numeric
fi
calc_bright=$(( $min_bright + $Adjust ))
set-and-sleep "$calc_bright"
}
# We're somewhere between sunrise and sunset
secMaxCutoff=$(( $secSunrise + ( $after_sunrise * 60 ) ))
secMinStart=$(( $secSunset - ( $before_sunset * 60 ) ))
# Are we between sunrise and full brightness?
if [[ "$secNow" -gt "$secSunrise" ]] && [[ "$secNow" -lt "$secMaxCutoff" ]]
then
# Current time - Sunrise = progress through transition
secPast=$(( $secNow - $secSunrise ))
calc-level-and-sleep "$after_sunrise" $secPast
PastDuration=$(date +%H:%M:%S -ud @${secPast})
echo "Transitioning $PastDuration minutes after sunrise (Currently set to: $after_sunrise)."
continue
fi
# Is it full bright day time?
if [[ "$secNow" -gt "$secMaxCutoff" ]] && [[ "$secNow" -lt "$secMinStart" ]]
then
# MAXIMUM: after sunrise transition AND before nightime transition
set-and-sleep "$max_bright"
continue
fi
# Are we between beginning to dim and sunset (full dim)?
if [[ "$secNow" -gt "$secMinStart" ]] && [[ "$secNow" -lt "$secSunset" ]]
then
# Sunset - Current time = progress through transition
secBefore=$(( $secSunset - $secNow ))
calc-level-and-sleep "$before_sunset" $secBefore
BeforeDuration=$(date +%H:%M:%S -ud @${secBefore})
echo "Transitioning $BeforeDuration minutes before sunset (Currently set to: $before_sunset)."
continue
fi
# Is it night time?
if [[ "$secNow" -gt "$secSunset" ]] || [[ "$secNow" -lt "$secSunrise" ]]
then
# MINIMUM: after sunset or before sunrise nightime setting
set-and-sleep "$min_bright"
toogle_dark
continue
fi
# At this stage brightness was set with manual override outside this program
# or exactly at a testpoint, then it will change next minute so no big deal.
sleep 60 # reset brightness once / minute.
done # End of forever loop
}
usage() {
# shellcheck disable=SC2046
printf "Usage: %s %s [options]\\n" "" $(basename "$0")
echo
echo " If called without arguments, uses 24 hour clock."
echo
printf " --24hour | -24 use 24 hour clock\\n"
printf " --12hour | -12 use 12 hour clock\\n"
printf " --light-enabled | -le turn on/off (true/false)\\n"
printf " --light-temperature | -lt show light-temperature\\n"
printf " --dark-toggle | -dt toggle dark/light color scheme\\n"
printf " --auto-run | -ar auto run\\n"
printf " --config | -c run config dialog"
printf "\\n"
printf " Crontab: 1 * * * * bash ~/.scripts/night_light.sh > /dev/null 2>&1\\n"
echo
}
DT=
AR=
ARGS=()
while [[ $# -gt 0 ]]; do
case $1 in
--help | -h)
usage
exit 0
;;
--light-enabled | -le) # Bash Space-Separated (e.g., --option argument)
gsettings set org.gnome.settings-daemon.plugins.color night-light-enabled "$2" # Source: https://stackoverflow.com/a/14203146
shift # past argument
shift # past value
;;
--light-temperature | -lt)
echo "$LastSetting"
exit 0
;;
--dark-toggle | -dt)
DT=1
shift
;;
--auto-run | -ar)
AR=1
auto-run
shift
;;
--config | -c)
config
exit 0
;;
-*|--*)
printf "Unrecognized option: $1\\n\\n"
usage
exit 1
;;
*)
ARGS+=("$1")
shift
;;
esac
done
set -- "${ARGS[@]}"
currenttime=$(date +%H:%M)
# morning="$sunrise"
morning=$(sunrise-transition + "$after_sunrise")
noon="12:00"
evening=$(sunset-transition - "$before_sunset")
night=$(sunset-transition + "$before_sunset")
night_light() {
if ! [[ $AR == "1" ]] && [[ -n "${1}" ]]; then
night-light-temperature "${1}"
return
elif [[ ! ( "$currenttime" < "$morning" || "$currenttime" > "$noon" ) ]]; then
night-light-temperature $temperature_morning
echo "Temperature set to morning ($temperature_morning)"
return
toggle_light
elif [[ ! ( "$currenttime" < "$noon" || "$currenttime" > "$evening" ) ]]; then
night-light-temperature $temperature_noon
echo "Temperature set to noon ($temperature_noon)"
return
toggle_light
elif [[ ! ( "$currenttime" < "$evening" || "$currenttime" > "$night" ) ]]; then
night-light-temperature $temperature_evening
echo "Temperature set to evening ($temperature_evening)"
return
toogle_dark
elif [[ ! ( "$currenttime" < "$night" ) ]]; then
night-light-temperature $temperature_night
echo "Temperature set to night ($temperature_night)"
return
toogle_dark
fi
}
night_light "$@"
exit 0