Skip to content

Instantly share code, notes, and snippets.

@dgunay
Last active October 16, 2022 23:06
Show Gist options
  • Save dgunay/851d3c8b85fa2f562ce5892645d5d459 to your computer and use it in GitHub Desktop.
Save dgunay/851d3c8b85fa2f562ce5892645d5d459 to your computer and use it in GitHub Desktop.
GNOME nightlight script
#!/usr/bin/env perl
# Turns the GNOME display night light feature on/off. If you don't supply
# an on/off argument, it will toggle the current state.
use strict;
use warnings;
my $setting = $ARGV[0] // '';
die "usage: nightlight [on|off]\n" if $setting =~ /^(-h|--help)$/;
$setting = opposite_state(bool_to_state(get_state())) if $setting eq "";
do_nightlight($setting);
exit 0;
sub do_nightlight {
my $state = shift;
my $true_or_false = state_to_bool($state);
my $cmd = "gsettings set org.gnome.settings-daemon.plugins.color night-light-enabled $true_or_false";
print `$cmd`;
}
# Turns "on" or "off" into "true" or "false"
sub state_to_bool {
my $state = shift;
return "true" if $state eq "on";
return "false" if $state eq "off";
die "state_to_bool: must be one of [on, off]";
}
# Turns "true" or "false" into "on" or "off"
sub bool_to_state {
my $bool = shift;
return "on" if $bool eq "true";
return "off" if $bool eq "false";
die "bool_to_state: must be one of [true, false]";
}
# Returns true/false for the current state of the night light.
sub get_state {
my $cmd = "gsettings get org.gnome.settings-daemon.plugins.color night-light-enabled";
my $state = `$cmd`;
chomp $state;
return $state;
}
# Returns on if the state is blank
sub opposite_state {
my $state = shift;
my $opposite = "on";
$opposite = "on" if $state eq 'off';
$opposite = "off" if $state eq 'on';
return $opposite;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment