<commit_before># frozen_string_literal: true

require "cli/parser"

module Homebrew
  module_function

  def tap_info_args
    Homebrew::CLI::Parser.new do
      usage_banner <<~EOS
        `tap-info` [<options>] [<tap>]

        Display detailed information about one or more provided <tap>.
        Display a brief summary of all installed taps if no <tap> are passed.
      EOS
      switch "--installed",
             description: "Display information on all installed taps."
      flag "--json",
           description: "Print a JSON representation of <taps>. Currently the default and only accepted "\
                        "value for <version> is `v1`. See the docs for examples of using the JSON "\
                        "output: <https://docs.brew.sh/Querying-Brew>"
      switch :debug
    end
  end

  def tap_info
    tap_info_args.parse

    if args.installed?
      taps = Tap
    else
      taps = ARGV.named.sort.map do |name|
        Tap.fetch(name)
      end
    end

    if args.json
      raise UsageError, "invalid JSON version: #{args.json}" unless ["v1", true].include? args.json

      print_tap_json(taps.sort_by(&:to_s))
    else
      print_tap_info(taps.sort_by(&:to_s))
    end
  end

  def print_tap_info(taps)
    if taps.none?
      tap_count = 0
      formula_count = 0
      command_count = 0
      pinned_count = 0
      private_count = 0
      Tap.each do |tap|
        tap_count += 1
        formula_count += tap.formula_files.size
        command_count += tap.command_files.size
        pinned_count += 1 if tap.pinned?
        private_count += 1 if tap.private?
      end
      info = "#{tap_count} #{"tap".pluralize(tap_count)}"
      info += ", #{pinned_count} pinned"
      info += ", #{private_count} private"
      info += ", #{formula_count} #{"formula".pluralize(formula_count)}"
      info += ", #{command_count} #{"command".pluralize(command_count)}"
      info += ", #{Tap::TAP_DIRECTORY.abv}" if Tap::TAP_DIRECTORY.directory?
      puts info
    else
      taps.each_with_index do |tap, i|
        puts unless i.zero?
        info = "#{tap}: "
        if tap.installed?
          info += tap.pinned? ? "pinned" : "unpinned"
          info += ", private" if tap.private?
          info += if (contents = tap.contents).empty?
            ", no commands/casks/formulae"
          else<commit_msg>cmd/tap-info: fix frozen pathname usage<commit_after># frozen_string_literal: true

require "cli/parser"

module Homebrew
  module_function

  def tap_info_args
    Homebrew::CLI::Parser.new do
      usage_banner <<~EOS
        `tap-info` [<options>] [<tap>]

        Display detailed information about one or more provided <tap>.
        Display a brief summary of all installed taps if no <tap> are passed.
      EOS
      switch "--installed",
             description: "Display information on all installed taps."
      flag "--json",
           description: "Print a JSON representation of <taps>. Currently the default and only accepted "\
                        "value for <version> is `v1`. See the docs for examples of using the JSON "\
                        "output: <https://docs.brew.sh/Querying-Brew>"
      switch :debug
    end
  end

  def tap_info
    tap_info_args.parse

    if args.installed?
      taps = Tap
    else
      taps = ARGV.named.sort.map do |name|
        Tap.fetch(name)
      end
    end

    if args.json
      raise UsageError, "invalid JSON version: #{args.json}" unless ["v1", true].include? args.json

      print_tap_json(taps.sort_by(&:to_s))
    else
      print_tap_info(taps.sort_by(&:to_s))
    end
  end

  def print_tap_info(taps)
    if taps.none?
      tap_count = 0
      formula_count = 0
      command_count = 0
      pinned_count = 0
      private_count = 0
      Tap.each do |tap|
        tap_count += 1
        formula_count += tap.formula_files.size
        command_count += tap.command_files.size
        pinned_count += 1 if tap.pinned?
        private_count += 1 if tap.private?
      end
      info = "#{tap_count} #{"tap".pluralize(tap_count)}"
      info += ", #{pinned_count} pinned"
      info += ", #{private_count} private"
      info += ", #{formula_count} #{"formula".pluralize(formula_count)}"
      info += ", #{command_count} #{"command".pluralize(command_count)}"
      info += ", #{Tap::TAP_DIRECTORY.dup.abv}" if Tap::TAP_DIRECTORY.directory?
      puts info
    else
      taps.each_with_index do |tap, i|
        puts unless i.zero?
        info = "#{tap}: "
        if tap.installed?
          info += tap.pinned? ? "pinned" : "unpinned"
          info += ", private" if tap.private?
          info += if (contents = tap.contents).empty?
            ", no commands/casks/formulae"
          else
{'fraction_non_alphanumeric': 0.11558001693480102, 'fraction_numerical': 0.004233700254022015, 'mean_word_length': 2.240740740740741, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 17, 'https://': 2, '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': '3199862', 'n_tokens_mistral': 1548, 'n_tokens_neox': 1448, 'n_words': 395}