Last active
November 10, 2019 11:48
-
-
Save pedropombeiro/a82271c4ca578b3ebe8237f53157b23b to your computer and use it in GitHub Desktop.
Smooth external monitor brightness control (assignable to keyboard shortcuts)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# frozen_string_literal: true | |
mon = 'DP-1' # Discover monitor name with: xrandr | grep " connected" | |
total_inc = 0.20 # Step Up/Down brightness by inc | |
steps = 10 # Total number of steps (for gradient) | |
# Avoid reentrance which would cause jerky behavior | |
exit 0 if Integer(`ps aux | grep "ruby #{__FILE__}" | wc -l`) > 3 | |
# Get brightness level with decimal place | |
curr_brightness = Float(`xrandr --verbose --current | grep ^"#{mon}" -A5 | tail -n1`.split(':').last) | |
case ARGV.first&.downcase | |
when '+', 'up' | |
factor = 1 | |
when '-', 'down' | |
factor = -1 | |
end | |
target_brightness = (curr_brightness + (factor * total_inc)).clamp(0.1, 1.0) if factor | |
if target_brightness | |
inc = ((target_brightness - curr_brightness) / steps).round(2) | |
(1..steps).each do | |
break if curr_brightness == target_brightness | |
curr_brightness = (curr_brightness + inc).clamp(0.1, 1.0).round(2) | |
# Set new brightness | |
`xrandr --output "#{mon}" --brightness "#{curr_brightness}"` | |
end | |
end | |
# Display current brightness | |
puts "Monitor #{mon} " | |
puts `xrandr --verbose --current | grep ^"#{mon}" -A5 | tail -n1` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Keyboard shortcut: