Skip to content

Instantly share code, notes, and snippets.

@pedrogglima
Last active June 16, 2019 15:35
Show Gist options
  • Select an option

  • Save pedrogglima/11159a19c2b8fe5d7853139e0bb8da9d to your computer and use it in GitHub Desktop.

Select an option

Save pedrogglima/11159a19c2b8fe5d7853139e0bb8da9d to your computer and use it in GitHub Desktop.
Script in ruby for managing monitors brightness... I set it on keyboard shortcuts so I can manage the brightness for my two monitors from the keyboard.
#!/usr/bin/env ruby
=begin
Script for setting displays's brightness, changes from 0.1
from the total of (0...1).
The script is set to works on two display, but you can set
the number_of_displays to change it.
The order for the displays comes from the order given
by the 'xrandr --verbose' output.
usage:
# for display 1, up brightness in 0.1
- script.rb d1 up
# for display 2, down brightness in 0.1
- script.rb d2 down
requirements:
randr (linux package for working with displays)
ruby
randr commands
xrandr --listmonitors
* list the displays (monitors) availables
* e.g HDMI-1 or DVI-I-1
xrandr --verbose
* shows all configs for the displays connected
* as brightness and display's name.
xrandr --output DISPLAY_NAME --brightness VALUE
* change brightness on the given display
* values from 0 to 1 for brightness
* e.g xrandr --output HDMI-1 --brightness 0.5
=end
brightness_update = 0.1
number_of_displays = 2
if ARGV[0].match /\A"d[1-9]{1}"\z/
$stdout.puts 'ARGV[0] should be (d1, d2, ..., d9)'
$stdout.flush
return 1
end
if ARGV[1].match /\A("up"|"down")\z/
$stdout.puts 'ARGV[1] should be (up, down)'
$stdout.flush
return 1
end
# Arguments for the function display_brightness
selected_display = ARGV[0];
# Set brightness up or down
selected_set = ARGV[1];
displays = []
Display = Struct.new(:id, :name, :brightness)
# display.id starts from 1.
number_of_displays.times do |i|
displays << Display.new("d#{i+1}", "", "")
end
# get display's information from the system
# lines() split command ouput by lines
cmd_output = `xrandr --verbose`.lines
if cmd_output.nil?
$stdout.puts 'error xrandr --verbose'
$stdout.flush
return 1
end
# extract the settings from the command's output
cmd_output.each do |string|
case string
# whitespace to target only the 'connected' word
when / connected /
displays.each do |display|
if display.name.empty?
display.name = string.strip.split(" ")[0]
# match display's name
unless display.name.match /\A(HDMI-1|DVI-I-1)\z/
$stdout.puts "error while retrieving settings \
from xrandr: display's name #{display.name}"
$stdout.flush
return 1
end
break
end
end
when /Brightness/
displays.each do |display|
if display.brightness.empty?
display.brightness = string.strip.split(" ")[1]
# match 0 || 0.\d{1} || 1 || 1.0
unless display.brightness.match /\A((0|0\.\d{1,2})|(1|1\.0))\z/
$stdout.puts "error while retrieving settings \
from xrandr: brightness #{display.brightness}"
$stdout.flush
return 1
end
break
end
end
else
end
end
# set the brightness according with settings and the display
displays.each do |display|
if display.id == selected_display
brightness = 1
if selected_set == "up"
brightness = (display.brightness.to_f + brightness_update)
else # selected_set == down
brightness = (display.brightness.to_f - brightness_update)
end
if brightness > 1 || brightness < 0
$stdout.puts "Display: hit brightness limit (#{brightness})"
$stdout.flush
else
`xrandr --output #{display.name} --brightness #{brightness}`
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment