Skip to content

Instantly share code, notes, and snippets.

@penguin2716
Created February 9, 2014 06:27
Show Gist options
  • Save penguin2716/8895157 to your computer and use it in GitHub Desktop.
Save penguin2716/8895157 to your computer and use it in GitHub Desktop.
update screen resolution by setting scale ratio automatically
#!/usr/bin/env ruby
=begin
Copyright (c) 2014 Takuma Nakajima
This software is released under the MIT License.
http://opensource.org/licenses/mit-license.php
=end
if ARGV.size < 1
puts "usage: $0 <x>x<y>"
exit 1
elsif not ARGV[0] =~ /\d+x\d+/
puts "usage: $0 <x>x<y>"
exit 1
end
# get current screen information
screen_info = `xrandr -q | grep '+' | awk '{print $1}'`.split("\n")
current_screen = screen_info[0]
current_size = screen_info[1].split("x").map{|size| size.to_i}
newsize = ARGV[0].split("x").map{|size| size.to_i}
# calculate scale ratio
current_x = current_size[0]
current_y = current_size[1]
new_x = newsize[0]
new_y = newsize[1]
scale_x = ((new_x.to_f / current_x).round(4) * 1000).to_i / 1000.0
scale_y = ((new_y.to_f / current_y).round(4) * 1000).to_i / 1000.0
# update resolution
xrandr_command = "xrandr --output #{current_screen} --mode #{current_size.join('x')} --fb #{newsize.join('x')} --panning #{newsize.join('x')} --scale #{[scale_x, scale_y].join('x')}"
puts xrandr_command
puts "you may have to reset your wallpaper."
system xrandr_command
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment