<commit_before># frozen_string_literal: true

require 'fast'
require 'fast/version'
require 'coderay'
require 'optparse'
require 'ostruct'

# Fast is a powerful tool to search through the command line for specific Ruby code.
# It defines #report and #highlight functions that can be used to pretty print
# code and results from the search.
module Fast
  module_function

  # Highligh some source code based on the node.
  # Useful for printing code with syntax highlight.
  def highlight(node, show_sexp: false, colorize: true)
    output =
      if node.respond_to?(:loc) && !show_sexp
        node.loc.expression.source
      else
        node
      end

    code = CodeRay.scan(output, :ruby)
    colorize ? code.term : code.plain
  end

  # Combines {.highlight} with files printing file name in the head with the
  # source line.
  # @param result [Astrolabe::Node]
  # @param show_sexp [Boolean] Show string expression instead of source
  # @param file [String] Show the file name and result line before content
  # @param headless [Boolean] Skip printing the file name and line before content
  # @example
  #   Fast.highlight(Fast.search(...))
  def report(result, show_sexp: false, file: nil, headless: false, colorize: true)
    if file
      line = result.loc.expression.line if result.is_a?(Parser::AST::Node)
      puts(highlight("# #{file}:#{line}", colorize: colorize)) unless headless
    end
    puts highlight(result, show_sexp: show_sexp, colorize: colorize)
  end

  # Command Line Interface for Fast
  class Cli # rubocop:disable Metrics/ClassLength
    attr_reader :pattern, :show_sexp, :pry, :from_code, :similar, :help
    def initialize(args)
      args = replace_args_with_shortcut(args) if args.first&.start_with?('.')

      @pattern, *@files = args.reject { |arg| arg.start_with? '-' }
      @colorize = STDOUT.isatty

      option_parser.parse! args
<commit_msg>Skip processing CodeRay if do not colorize<commit_after># frozen_string_literal: true

require 'fast'
require 'fast/version'
require 'coderay'
require 'optparse'
require 'ostruct'

# Fast is a powerful tool to search through the command line for specific Ruby code.
# It defines #report and #highlight functions that can be used to pretty print
# code and results from the search.
module Fast
  module_function

  # Highligh some source code based on the node.
  # Useful for printing code with syntax highlight.
  # @param show_sexp [Boolean] prints node expression instead of code
  # @param colorize [Boolean] skips `CodeRay` processing when false.
  def highlight(node, show_sexp: false, colorize: true)
    output =
      if node.respond_to?(:loc) && !show_sexp
        node.loc.expression.source
      else
        node
      end
    return output unless colorize

    CodeRay.scan(output, :ruby).term
  end

  # Combines {.highlight} with files printing file name in the head with the
  # source line.
  # @param result [Astrolabe::Node]
  # @param show_sexp [Boolean] Show string expression instead of source
  # @param file [String] Show the file name and result line before content
  # @param headless [Boolean] Skip printing the file name and line before content
  # @example
  #   Fast.highlight(Fast.search(...))
  def report(result, show_sexp: false, file: nil, headless: false, colorize: true)
    if file
      line = result.loc.expression.line if result.is_a?(Parser::AST::Node)
      puts(highlight("# #{file}:#{line}", colorize: colorize)) unless headless
    end
    puts highlight(result, show_sexp: show_sexp, colorize: colorize)
  end

  # Command Line Interface for Fast
  class Cli # rubocop:disable Metrics/ClassLength
    attr_reader :pattern, :show_sexp, :pry, :from_code, :similar, :help
    def initialize(args)
      args = replace_args_with_shortcut(args) if args.first&.start_with?('.')

      @pattern, *@files = args.reject { |arg| arg.start_with? '-' }
      @colorize = STDOUT.isatty

      option_parser.parse! args

{'fraction_non_alphanumeric': 0.09503294475418145, 'fraction_numerical': 0.0, 'mean_word_length': 3.726946107784431, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '6387826', 'n_tokens_mistral': 1203, 'n_tokens_neox': 1182, 'n_words': 421}