Sha256: 62c558d3cd10ad1070883b69af8cfc7241c645d6485b9610dbdc60accec25696

Contents?: true

Size: 1.14 KB

Versions: 1

Compression:

Stored size: 1.14 KB

Contents

require 'lerna/strategy_selector'
require 'lerna/state'

module Lerna
  class Runner
    def initialize(logger:, strategies:, system:, state: State.new,
                   strategy_selector: StrategySelector.new)
      @logger = logger
      @strategies = strategies
      @system = system
      @state = state
      @strategy_selector = strategy_selector
    end

    def run
      state.scan!
      return unless state.changed?

      log state_summary
      strategy = find_strategy
      if strategy
        log "Using #{strategy.class}"
        apply_strategy strategy
      else
        log 'No applicable strategy found'
      end
    end

  private

    attr_reader :state, :strategy_selector, :strategies

    def log(s)
      @logger.call(s)
    end

    def state_summary
      state.displays.
        map { |d| "#{d.name}#{d.connected? ? '*' : ''}" }.
        join(' ')
    end

    def find_strategy
      strategy_selector.call(strategies, state.displays)
    end

    def apply_strategy(strategy)
      system 'xrandr', *strategy.configuration
      system 'xset dpms force on'
    end

    def system(*args)
      @system.call(*args)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
lerna-0.1.0 lib/lerna/runner.rb