Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for intermittent exception while parsing Xrandr modes #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 24 additions & 17 deletions lib/xrandr.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module Xrandr
VERSION = '0.0.6'
VERSION = '0.0.7'

class Control
attr_reader :screens, :outputs, :command
Expand Down Expand Up @@ -104,23 +104,15 @@ def parse_output(data)
end

def parse_modes(modes)
modes.map do |data|
parse_mode data
modes.map! do |m|
begin
Mode.from_s(m)
rescue => e
puts e
nil
end
end
end

def parse_mode(data)
matches = data.lstrip.match(/^(?<resolution>\d+x\d+i?) +(?<rate>[\d\.]+)(?<current>[\* ])(?<preferred>[\+ ]).*/)

resolution = matches[:resolution].gsub 'i', '' if matches[:resolution]
args = {
resolution: resolution,
rate: matches[:rate],
current: matches[:current] == '*',
preferred: matches[:preferred] == '+',
}

Mode.new args
modes.compact
end
end

Expand Down Expand Up @@ -161,6 +153,21 @@ def status

class Mode
attr_reader :resolution, :rate, :current, :preferred
MATCHER = /^\s*(?<resolution>\d+x\d+i?) +(?<rate>[\d\.]+)(?<current>[\* ])(?<preferred>[\+ ]).*/

def self.from_s(mode_string)
matches = mode_string.match(MATCHER)
raise "Failed to parse mode #{data}" if matches.nil?
resolution = matches[:resolution].gsub 'i', '' if matches[:resolution]
args = {
resolution: resolution,
rate: matches[:rate],
current: matches[:current] == '*',
preferred: matches[:preferred] == '+',
}

Mode.new args
end

def initialize(args={})
@resolution = Resolution.new(args.fetch :resolution)
Expand Down