From b2a642c0f1d2e90e0562a15bd2fb43a9007a710f Mon Sep 17 00:00:00 2001 From: Martin Hradil Date: Wed, 28 Jul 2021 02:06:25 +0000 Subject: [PATCH] hsr-outputs.rb: use xrandr gem to get proper list of outputs with names Xinerama doesn't seem to be able to return an output name --- hsr-outputs.rb | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100755 hsr-outputs.rb diff --git a/hsr-outputs.rb b/hsr-outputs.rb new file mode 100755 index 0000000..d566975 --- /dev/null +++ b/hsr-outputs.rb @@ -0,0 +1,42 @@ +#!/usr/bin/env ruby +# gem install xrandr + +require 'json' +require 'xrandr' + +outputs = Xrandr::Control.new.outputs.select(&:connected).map do |o| + x, y = o.position.split('x') + width, height = o.resolution.split('x') + { :name => o.name, :x => x, :y => y, :width => width, :height => height } +end + +def print_csv(outputs) + puts("name,x,y,width,height") + outputs.each { |o| puts("#{o[:name]},#{o[:x]},#{o[:y]},#{o[:width]},#{o[:height]}") } +end + +def print_json(outputs) + puts outputs.to_json +end + +def print_text(outputs) + outputs.each_with_index do |o, i| + puts('') if i > 0 + puts("name: #{o[:name]}") + puts("\tx:\t#{o[:x]}") + puts("\ty:\t#{o[:y]}") + puts("\twidth:\t#{o[:width]}") + puts("\theight:\t#{o[:height]}") + end +end + +if ARGV.first == '--csv' + print_csv(outputs) +elsif ARGV.first == '--json' + print_json(outputs) +elsif ARGV.first.nil? or ARGV.first == '--text' + print_text(outputs) +else + $stderr.puts("syntax: #$0 [<--text|--json|--csv>]") +end +