Stupid_and_clever

I actually don’t know what to think about this code. It’s stupid and clever.

It’s stupid where it needs to be clever and clever where it doesn’t matter.

Took 1:44 to write.

require 'open3'

#     This requires 'xgamma' and 'xsct' to be installed.
#     arch: sudo pacman -S xgamma
#           sudo pacman -S xsct

module Get
  def Get.xc(s)
    res, ec = Open3.capture2e s
    ec.exitstatus.zero? ? res.chomp : nil
  end

  def Get.data
    data = xc('xgamma')
      .scan(/(\w+)\s+(\d\.\d+)/)
      .to_h
      .transform_keys {|k| k.downcase.to_sym }
    data[:temp], data[:brightness] =
      xc('xsct')[/(\d+)\s+(\d\.\d+)/].split
    data
  end
end

class Screen
  def initialize = ( @val = Get.data )

  def method_missing(name, *args, **kw)
    if %i(gamma temp bright tb).include?(name)
      send('_%s' % name, *args, **kw); @val = Get.data
    else super; end
  end

  private
  def _gamma(**kw)
    kw.each {|k,v|
      Get.xc('xgamma -%sgamma %s' % [k[0], v]) if
        %i(red green blue).include?(k)
    }
  end

  def _temp(tmp) = ( Get.xc('xsct %s' % tmp) )
  def _bright(val) = ( Get.xc('xsct %s %s' % [@val[:temp], val]) )
  def _tb(tb) = ( Get.xc('xsct %s' % tb) )

end


# Usage =================================================================

scr = Screen.new
scr.gamma(blue: 0.7, red: 0.2)
sleep(2)

scr.gamma(blue: 1)
sleep(1)
scr.gamma(red: 1)

scr.temp(4000)
sleep(1)

scr.bright(0.5)
sleep(1)

scr.tb '0'