<commit_before>module RMonitor
  module Profile
    def self.invokable?(profile)
      profile.all? do |device|
        DEVICES.has_key?(device[:name]) and
        !first_matching_configuration(DEVICES[device[:name]],
                                      device[:mode],
                                      device[:rate]).nil?
      end
    end

    def self.to_xrandr(profile)
      xrandr = 'xrandr'

      profile.each do |device|
        configuration = first_matching_configuration(DEVICES[device[:name]],
                                                     device[:mode],
                                                     device[:rate])

        xrandr << ' --output %s --mode %s --rate %s' % [
            device[:name],
            configuration[:mode],
            configuration[:rate],
        ]

        if device[:pos]
          xrandr << " --pos #{device[:pos]}"
        elsif device[:left_of]
          xrandr << " --left-of #{device[:left_of]}"
        elsif device[:right_of]
          xrandr << " --right-of #{device[:right_of]}"
        elsif device[:above]
          xrandr << " --above #{device[:above]}"
        elsif device[:below]
          xrandr << " --below #{device[:below]}"
        end
      end

      (DEVICES.keys - profile.map { |d| d[:name] }).each do |name|
        xrandr << ' --output %{name} --off' % { :name => name }
      end

      xrandr
    end

    private

    def self.first_matching_configuration(device, mode, rate)
      device[:configurations].find do |configuration|
        !mode or configuration[:mode] == mode &&
        !rate or configuration[:rate] == rate
      end<commit_msg>Devices are turned off before being turned on.<commit_after>module RMonitor
  module Profile
    def self.invokable?(profile)
      profile.all? do |device|
        DEVICES.has_key?(device[:name]) and
        !first_matching_configuration(DEVICES[device[:name]],
                                      device[:mode],
                                      device[:rate]).nil?
      end
    end

    def self.to_xrandr(profile)
      xrandr = 'xrandr'

      off = (DEVICES.keys - profile.map { |d| d[:name] })

      unless off.empty?
        (DEVICES.keys - profile.map { |d| d[:name] }).each do |name|
          xrandr << ' --output %{name} --off' % { :name => name }
        end

        xrandr << ' && xrandr'
      end

      profile.each do |device|
        configuration = first_matching_configuration(DEVICES[device[:name]],
                                                     device[:mode],
                                                     device[:rate])

        xrandr << ' --output %s --mode %s --rate %s' % [
            device[:name],
            configuration[:mode],
            configuration[:rate],
        ]

        if device[:pos]
          xrandr << " --pos #{device[:pos]}"
        elsif device[:left_of]
          xrandr << " --left-of #{device[:left_of]}"
        elsif device[:right_of]
          xrandr << " --right-of #{device[:right_of]}"
        elsif device[:above]
          xrandr << " --above #{device[:above]}"
        elsif device[:below]
          xrandr << " --below #{device[:below]}"
        end
      end

      xrandr
    end

    private

    def self.first_matching_configuration(device, mode, rate)
      device[:configurations].find do |configuration|
        !mode or configuration[:mode] == mode &&
        !rate or configuration[:rate] == rate
      end
{'fraction_non_alphanumeric': 0.13580963435867674, 'fraction_numerical': 0.0, 'mean_word_length': 1.595632530120482, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6763639', 'n_tokens_mistral': 975, 'n_tokens_neox': 934, 'n_words': 210}