Skip to content

Instantly share code, notes, and snippets.

@rlogwood
Created January 16, 2023 18:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rlogwood/dba18a60748ff2f074721530aace6a37 to your computer and use it in GitHub Desktop.
Save rlogwood/dba18a60748ff2f074721530aace6a37 to your computer and use it in GitHub Desktop.
utility to quickly dim or brighten all monitors on linux with xrandr
#!/usr/bin/env ruby
# frozen_string_literal: true
LOWEST=0.3
HIGHEST=4
def sys(cmd, *args, **kwargs)
puts("*** running: \e[1m\e[33m#{cmd} #{args}\e[0m\e[22m")
system(cmd, *args, exception: true, **kwargs)
#return $?.exitstatus
$?.success?
end
# use Integer(str) and see if it raises:
def is_float?(str)
!!Float(str)
rescue ArgumentError, TypeError
false
end
def show_usage(error_msg)
puts "\n***\n*** ERROR: #{error_msg}\n***"
puts "*** brighteness of (#{ARGV[0]}) is not valid\n\n" unless ARGV.empty?
exe_name = File.basename(__FILE__)
puts "usage #{exe_name} (brightness)"
puts " (brightness) must be in range #{LOWEST} - #{HIGHEST}"
puts ""
puts " for example: ruby #{exe_name} 1.2"
exit 1;
end
def get_brightness
show_usage('invalid argument: brightness must be a decimal number') unless is_float?(ARGV[0])
brightness = ARGV[0].to_f
show_usage("brightness out of allowed range [#{LOWEST} - #{HIGHEST}]") unless brightness.between?(LOWEST, HIGHEST)
brightness
end
def set_monitor_brightness(brightness)
puts "setting brightness to #{brightness}"
monitor_list = `xrandr -q | grep " connected" | cut -d ' ' -f 1`
monitors = monitor_list.split("\n")
puts "updating brightness for monitors: #{monitors}"
monitors.each do |monitor|
cmd="xrandr --output #{monitor} --brightness #{brightness}"
sys(cmd)
end
end
set_monitor_brightness(get_brightness)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment