<commit_before># typed: false
# frozen_string_literal: true

require "metafiles"
require "formula"
require "cli/parser"
require "cask/cmd"

module Homebrew
  extend T::Sig

  module_function

  sig { returns(CLI::Parser) }
  def list_args
    Homebrew::CLI::Parser.new do
      description <<~EOS
        List all installed formulae and casks.

        If <formula> is provided, summarise the paths within its current keg.
        If <cask> is provided, list its artifacts.
      EOS
      switch "--formula", "--formulae",
             description: "List only formulae, or treat all named arguments as formulae."
      switch "--cask", "--casks",
             description: "List only casks, or treat all named arguments as casks."
      switch "--unbrewed",
             description: "List files in Homebrew's prefix not installed by Homebrew.",
             replacement: "`brew --prefix --unbrewed`"
      switch "--full-name",
             description: "Print formulae with fully-qualified names. Unless `--full-name`, `--versions` "\
                          "or `--pinned` are passed, other options (i.e. `-1`, `-l`, `-r` and `-t`) are "\
                          "passed to `ls`(1) which produces the actual output."
      switch "--versions",
             description: "Show the version number for installed formulae, or only the specified "\
                          "formulae if <formula> are provided."
      switch "--multiple",
             depends_on:  "--versions",
             description: "Only show formulae with multiple versions installed."
      switch "--pinned",
             description: "List only pinned formulae, or only the specified (pinned) "\
                          "formulae if <formula> are provided. See also `pin`, `unpin`."
      # passed through to ls
      switch "-1",
             description: "Force output to be one entry per line. " \
                          "This is the default when output is not to a terminal."
      switch "-l",
             depends_on:  "--formula",
             description: "List formulae in long format."
      switch "-r",
             depends_on:  "--formula",
             description: "Reverse the order of the formulae sort to list the oldest entries first."
      switch "-t",
             depends_on:  "--formula",
             description: "Sort formulae by time modified, listing most recently modified first."

      conflicts "--formula", "--cask"
      conflicts "--full-name", "--versions"
      conflicts "--pinned", "--multiple"
      conflicts "--cask", "--multiple"
      ["--formula", "--cask", "--full-name", "--versions", "--pinned"].each do |flag|
        conflicts "--unbrewed", flag
      end
      ["-1", "-l", "-r", "-t"].each do |flag|
        conflicts "--unbrewed", flag
        conflicts "--versions", flag
        conflicts "--pinned", flag
      end
      ["--pinned", "-l", "-r", "-t"].each do |flag|
        conflicts "--full-name", flag
        conflicts "--cask", flag
      end

      named_args [:installed_formula, :installed_cask]
    end
  end

  def list
    args = list_args.parse

    # Unbrewed uses the PREFIX, which will exist
    # Things below use the CELLAR, which doesn't until the first formula is installed.
    unless HOMEBREW_CELLAR.exist?
      raise NoSuchKegError, args.named.first if args.named.present? && !args.cask?

      return
    end

    if args.full_name?
      unless args.cask?
        formula_names = args.no_named? ? Formula.installed : args.named.to_resolved_formulae
        full_formula_names = formula_names.map(&:full_name).sort(&tap_and_name_comparison)
        full_formula_names = Formatter.columns(full_formula_names) unless args.public_send(:'1?')
        puts full_formula_names if full_formula_names.present?
      end
      if args.cask? || (!args.formula? && args.no_named?)
        cask_names = if args.no_named?
          Cask::Caskroom.casks
        else
          args.named.to_formulae_and_casks(only: :cask, method: :resolve)
        end
        full_cask_names = cask_names.map(&:full_name).sort(&tap_and_name_comparison)
        full_cask_names = Formatter.columns(full_cask_names) unless args.public_send(:'1?')
        puts full_cask_names if full_cask_names.present?
      end
    elsif args.cask?
      list_casks(args: args)
    elsif args.pinned? || args.versions?
      filtered_list args: args
    elsif args.no_named?
      ENV["CLICOLOR"] = nil

      ls_args = []
      ls_args << "-1" if args.public_send(:'1?')
      ls_args << "-l" if args.l?
      ls_args << "-r" if args.r?
      ls_args << "-t" if args.t?

      ohai "Formulae" if $stdout.tty? && !args.formula?
      safe_system "ls", *ls_args, HOMEBREW_CELLAR

      unless args.formula?
        if $stdout.tty?
          puts
          ohai "Casks"
        end
        list_casks(args: args)
      end
    elsif args.verbose? || !$stdout.tty?
      system_command! "find", args: args.named.to_kegs.map(&:to_s) + %w[-not -type d -print], print_stdout: true<commit_msg>cmd/list: list casks only if one is installed<commit_after># typed: false
# frozen_string_literal: true

require "metafiles"
require "formula"
require "cli/parser"
require "cask/cmd"

module Homebrew
  extend T::Sig

  module_function

  sig { returns(CLI::Parser) }
  def list_args
    Homebrew::CLI::Parser.new do
      description <<~EOS
        List all installed formulae and casks.

        If <formula> is provided, summarise the paths within its current keg.
        If <cask> is provided, list its artifacts.
      EOS
      switch "--formula", "--formulae",
             description: "List only formulae, or treat all named arguments as formulae."
      switch "--cask", "--casks",
             description: "List only casks, or treat all named arguments as casks."
      switch "--unbrewed",
             description: "List files in Homebrew's prefix not installed by Homebrew.",
             replacement: "`brew --prefix --unbrewed`"
      switch "--full-name",
             description: "Print formulae with fully-qualified names. Unless `--full-name`, `--versions` "\
                          "or `--pinned` are passed, other options (i.e. `-1`, `-l`, `-r` and `-t`) are "\
                          "passed to `ls`(1) which produces the actual output."
      switch "--versions",
             description: "Show the version number for installed formulae, or only the specified "\
                          "formulae if <formula> are provided."
      switch "--multiple",
             depends_on:  "--versions",
             description: "Only show formulae with multiple versions installed."
      switch "--pinned",
             description: "List only pinned formulae, or only the specified (pinned) "\
                          "formulae if <formula> are provided. See also `pin`, `unpin`."
      # passed through to ls
      switch "-1",
             description: "Force output to be one entry per line. " \
                          "This is the default when output is not to a terminal."
      switch "-l",
             depends_on:  "--formula",
             description: "List formulae in long format."
      switch "-r",
             depends_on:  "--formula",
             description: "Reverse the order of the formulae sort to list the oldest entries first."
      switch "-t",
             depends_on:  "--formula",
             description: "Sort formulae by time modified, listing most recently modified first."

      conflicts "--formula", "--cask"
      conflicts "--full-name", "--versions"
      conflicts "--pinned", "--multiple"
      conflicts "--cask", "--multiple"
      ["--formula", "--cask", "--full-name", "--versions", "--pinned"].each do |flag|
        conflicts "--unbrewed", flag
      end
      ["-1", "-l", "-r", "-t"].each do |flag|
        conflicts "--unbrewed", flag
        conflicts "--versions", flag
        conflicts "--pinned", flag
      end
      ["--pinned", "-l", "-r", "-t"].each do |flag|
        conflicts "--full-name", flag
        conflicts "--cask", flag
      end

      named_args [:installed_formula, :installed_cask]
    end
  end

  def list
    args = list_args.parse

    # Unbrewed uses the PREFIX, which will exist
    # Things below use the CELLAR, which doesn't until the first formula is installed.
    unless HOMEBREW_CELLAR.exist?
      raise NoSuchKegError, args.named.first if args.named.present? && !args.cask?

      return
    end

    if args.full_name?
      unless args.cask?
        formula_names = args.no_named? ? Formula.installed : args.named.to_resolved_formulae
        full_formula_names = formula_names.map(&:full_name).sort(&tap_and_name_comparison)
        full_formula_names = Formatter.columns(full_formula_names) unless args.public_send(:'1?')
        puts full_formula_names if full_formula_names.present?
      end
      if args.cask? || (!args.formula? && args.no_named?)
        cask_names = if args.no_named?
          Cask::Caskroom.casks
        else
          args.named.to_formulae_and_casks(only: :cask, method: :resolve)
        end
        full_cask_names = cask_names.map(&:full_name).sort(&tap_and_name_comparison)
        full_cask_names = Formatter.columns(full_cask_names) unless args.public_send(:'1?')
        puts full_cask_names if full_cask_names.present?
      end
    elsif args.cask?
      list_casks(args: args)
    elsif args.pinned? || args.versions?
      filtered_list args: args
    elsif args.no_named?
      ENV["CLICOLOR"] = nil

      ls_args = []
      ls_args << "-1" if args.public_send(:'1?')
      ls_args << "-l" if args.l?
      ls_args << "-r" if args.r?
      ls_args << "-t" if args.t?

      ohai "Formulae" if $stdout.tty? && !args.formula?
      safe_system "ls", *ls_args, HOMEBREW_CELLAR

      if !args.formula? && Cask::Caskroom.casks.any?
        if $stdout.tty?
          puts
          ohai "Casks"
        end
        list_casks(args: args)
      end
    elsif args.verbose? || !$stdout.tty?
      system_command! "find", args: args.named.to_kegs.map(&:to_s) + %w[-not -type d -print], print_stdout: true
{'fraction_non_alphanumeric': 0.1232876712328767, 'fraction_numerical': 0.0015882469724042088, 'mean_word_length': 2.217821782178218, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 11, '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': '5973879', 'n_tokens_mistral': 3068, 'n_tokens_neox': 2945, 'n_words': 901}