Skip to content

Instantly share code, notes, and snippets.

@itayadler
Created April 1, 2014 10:42
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 itayadler/9911670 to your computer and use it in GitHub Desktop.
Save itayadler/9911670 to your computer and use it in GitHub Desktop.
A ruby module that retrieves the resolution details of your monitors - based on https://github.com/jhford/screenresolution
require 'active_support/core_ext/object/try'
class ScreenResolution
#ONLY FOR MAC
#Has a dependency on the screenresolution plugin. Install with it with `brew install screenresolution`
def self.get
#The screenresolution script output is going to STDERR (uses NSLog), so we redirect to STDOUT
raw_output = %x(screenresolution get 2>&1)
output = raw_output.split("\n")
output.map.with_index do |line, i|
next if i == 0
raw_resolution = line.split("Display #{i-1}: ")[1]
resolution = raw_resolution.split('x')
color_refresh_rate = resolution[2].try(:split, '@')
color = color_refresh_rate.try(:[], 0)
refresh_rate = color_refresh_rate.try(:[], 1)
{
width: resolution[0].to_i,
height: resolution[1].to_i,
color: color.to_i,
refresh_rate: refresh_rate.to_i
}
end.compact
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment