<commit_before>require 'terminal-table'

module Hue
  class Cli < CliBase
    desc 'bridges', 'Find all the bridges on your network'
    def bridges
      # TODO: Extended output form that includes proxy_address, proxy_port,
      # TODO: known_clients, network_mask, gateway, dhcp, etc...
      headings = ["ID", "Name", "IP", "MAC", "Channel", "API Version", "Software Version", "Update Info"]
      rows = client(options).bridges.each_with_object([]) do |bridge, r|
        bridge.refresh
        r << [
          bridge.id,
          bridge.name,
          bridge.ip,
          bridge.mac_address,
          bridge.zigbee_channel,
          bridge.api_version,
          bridge.software_version,
          (bridge.software_update["text"] rescue nil)
        ]
      end
      puts Terminal::Table.new(rows: rows, headings: headings)
    end

    desc 'lights', 'Find all of the lights on your network'
    shared_options
    method_option :sort, :type => :string, :aliases => '--order'
    def lights
      headings = [
        "ID",
        "Type",
        "Model",
        "Name",
        "Status",
        "Mode",
        "Hue",
        "Saturation",
        "Brightness",
        "X/Y",
        "Temp",
        "Alert",
        "Effect",
        "Software Version",
        "Reachable?",
      ]
      rows = client(options).lights.each_with_object([]) do |light, r|
        r << [
          light.id,
          light.type,
          light.model,
          light.name,
          (light.off? ? "Off" : "On"),
          light.color_mode,
          light.hue,
          light.saturation,
          light.brightness,
          [light.x, light.y].compact.join(", "),
          light.color_temperature,
          light.alert,
          light.effect,
          light.software_version,
          (light.reachable? ? "Yes" : "No"),
        ]
      end
      if options[:sort]
        sorting = options[:sort].strip.split(/\s*,\s*/).map(&:to_i)
        rows.sort! do |a, b|
          sorting
            .map { |k| a[k] <=> b[k] }
            .find { |n| n != 0 } || 0
        end
      end
      puts Terminal::Table.new(rows: rows, headings: headings)
    end

    desc 'add', 'Search for new lights'
    shared_options
    def add
      client(options).add_lights
    end

    desc 'all [on|off] [--hue=X] [--brightness=X] [--saturation=X]', 'Send commands to all lights'
    shared_options
    shared_light_options
    long_desc <<-LONGDESC
    Examples: \n
      hue all on --hue 12345\n
      hue all --bri 25\n
      hue all --alert lselect\n
      hue all off\n
    LONGDESC
    def all(state = nil)
      body = clean_body(options, state: state)
      client(options).lights.each do |light|
        puts light.name
        puts light.set_state body
      end
    end

    desc 'light <id> [on|off] [--hue=X] [--brightness=X] [--saturation=X]', 'Access or update a light'
    long_desc <<-LONGDESC
    Examples: \n
      hue light 1 on --hue 12345  \n
      hue light 1 --bri 25 \n
      hue light 1 --alert lselect \n
      hue light 1 off
    LONGDESC
    shared_options
    shared_light_options
    def light(id, state = nil)
      light = client(options).light(id)
      puts light.name

      body = clean_body(options, state: state)
      puts light.set_state(body) if body.length > 0
    end

    desc 'groups', 'Find all light groups on your network'
    shared_options
    def groups
      headings = ["ID", "Name", "Light IDs", "Lights"]
      rows = client(options).groups.each_with_object([]) do |group, r|
        r << [
          group.id,
          group.name,
          group.lights.map { |light| light.id.to_i }.sort.join(", "),
          group.lights.map { |light| light.name }.sort.join("\n"),
        ]
      end
      puts Terminal::Table.new(rows: rows, headings: headings)
    end

    desc 'group <id> [on|off] [--hue=X] [--brightness=X] [--saturation=X] [--name=X] [--lights=X,Y,Z...]', 'Update a group of lights'
    long_desc <<-LONGDESC
    Examples: \n
      hue group 1 on --hue 12345
      hue group 1 --bri 25
      hue group 1 --name "My Group"
      hue group 1 --alert lselect
      hue group 1 --lights "1, 2, 3"
      hue group 1 off
    LONGDESC
    shared_options
    shared_light_options
    method_option :name, :type => :string
    method_option :lights, :type => :string<commit_msg>Make output for groups subcommand a bit more self-consistent.<commit_after>require 'terminal-table'

module Hue
  class Cli < CliBase
    desc 'bridges', 'Find all the bridges on your network'
    def bridges
      # TODO: Extended output form that includes proxy_address, proxy_port,
      # TODO: known_clients, network_mask, gateway, dhcp, etc...
      headings = ["ID", "Name", "IP", "MAC", "Channel", "API Version", "Software Version", "Update Info"]
      rows = client(options).bridges.each_with_object([]) do |bridge, r|
        bridge.refresh
        r << [
          bridge.id,
          bridge.name,
          bridge.ip,
          bridge.mac_address,
          bridge.zigbee_channel,
          bridge.api_version,
          bridge.software_version,
          (bridge.software_update["text"] rescue nil)
        ]
      end
      puts Terminal::Table.new(rows: rows, headings: headings)
    end

    desc 'lights', 'Find all of the lights on your network'
    shared_options
    method_option :sort, :type => :string, :aliases => '--order'
    def lights
      headings = [
        "ID",
        "Type",
        "Model",
        "Name",
        "Status",
        "Mode",
        "Hue",
        "Saturation",
        "Brightness",
        "X/Y",
        "Temp",
        "Alert",
        "Effect",
        "Software Version",
        "Reachable?",
      ]
      rows = client(options).lights.each_with_object([]) do |light, r|
        r << [
          light.id,
          light.type,
          light.model,
          light.name,
          (light.off? ? "Off" : "On"),
          light.color_mode,
          light.hue,
          light.saturation,
          light.brightness,
          [light.x, light.y].compact.join(", "),
          light.color_temperature,
          light.alert,
          light.effect,
          light.software_version,
          (light.reachable? ? "Yes" : "No"),
        ]
      end
      if options[:sort]
        sorting = options[:sort].strip.split(/\s*,\s*/).map(&:to_i)
        rows.sort! do |a, b|
          sorting
            .map { |k| a[k] <=> b[k] }
            .find { |n| n != 0 } || 0
        end
      end
      puts Terminal::Table.new(rows: rows, headings: headings)
    end

    desc 'add', 'Search for new lights'
    shared_options
    def add
      client(options).add_lights
    end

    desc 'all [on|off] [--hue=X] [--brightness=X] [--saturation=X]', 'Send commands to all lights'
    shared_options
    shared_light_options
    long_desc <<-LONGDESC
    Examples: \n
      hue all on --hue 12345\n
      hue all --bri 25\n
      hue all --alert lselect\n
      hue all off\n
    LONGDESC
    def all(state = nil)
      body = clean_body(options, state: state)
      client(options).lights.each do |light|
        puts light.name
        puts light.set_state body
      end
    end

    desc 'light <id> [on|off] [--hue=X] [--brightness=X] [--saturation=X]', 'Access or update a light'
    long_desc <<-LONGDESC
    Examples: \n
      hue light 1 on --hue 12345  \n
      hue light 1 --bri 25 \n
      hue light 1 --alert lselect \n
      hue light 1 off
    LONGDESC
    shared_options
    shared_light_options
    def light(id, state = nil)
      light = client(options).light(id)
      puts light.name

      body = clean_body(options, state: state)
      puts light.set_state(body) if body.length > 0
    end

    desc 'groups', 'Find all light groups on your network'
    shared_options
    def groups
      headings = ["ID", "Name", "Light IDs", "Lights"]
      rows = client(options).groups.each_with_object([]) do |group, r|
        r << [
          group.id,
          group.name,
          group.lights.map { |light| light.id.to_i }.sort.join("\n"),
          group.lights.map { |light| light.name }.sort.join("\n"),
        ]
      end
      puts Terminal::Table.new(rows: rows, headings: headings)
    end

    desc 'group <id> [on|off] [--hue=X] [--brightness=X] [--saturation=X] [--name=X] [--lights=X,Y,Z...]', 'Update a group of lights'
    long_desc <<-LONGDESC
    Examples: \n
      hue group 1 on --hue 12345
      hue group 1 --bri 25
      hue group 1 --name "My Group"
      hue group 1 --alert lselect
      hue group 1 --lights "1, 2, 3"
      hue group 1 off
    LONGDESC
    shared_options
    shared_light_options
    method_option :name, :type => :string
    method_option :lights, :type => :string
{'fraction_non_alphanumeric': 0.13134328358208955, 'fraction_numerical': 0.008495981630309988, 'mean_word_length': 1.9163039839303648, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1946459', 'n_tokens_mistral': 2917, 'n_tokens_neox': 2794, 'n_words': 771}