class Querly::CLI::Console

Attributes

config[R]
history[R]
history_path[R]
history_size[R]
paths[R]
threads[R]

Public Class Methods

new(paths:, history_path:, history_size:, config: nil, threads:) click to toggle source
# File lib/querly/cli/console.rb, line 15
def initialize(paths:, history_path:, history_size:, config: nil, threads:)
  @paths = paths
  @history_path = history_path
  @history_size = history_size
  @config = config
  @history = []
  @threads = threads
end

Public Instance Methods

analyzer() click to toggle source
# File lib/querly/cli/console.rb, line 46
def analyzer
  return @analyzer if @analyzer

  @analyzer = Analyzer.new(config: config, rule: nil)

  ScriptEnumerator.new(paths: paths, config: config, threads: threads).each do |path, script|
    case script
    when Script
      @analyzer.scripts << script
    when StandardError
      p path: path, script: script.inspect
      puts script.backtrace
    end
  end

  @analyzer
end
load_history() click to toggle source
# File lib/querly/cli/console.rb, line 111
def load_history
  history_path.readlines.each do |line|
    line.chomp!
    Readline::HISTORY.push(line)
    history.push line
  end
rescue Errno::ENOENT
  # in the first time
end
puts_commands() click to toggle source
# File lib/querly/cli/console.rb, line 129
      def puts_commands
        puts <<-Message
Commands:
  - find PATTERN   Find PATTERN from given paths
  - reload!        Reload program from paths
  - quit

        Message
      end
reload!() click to toggle source
# File lib/querly/cli/console.rb, line 41
def reload!
  @analyzer = nil
  analyzer
end
save_history(line) click to toggle source
# File lib/querly/cli/console.rb, line 121
def save_history(line)
  history.push line
  if history.size > history_size
    @history = history.drop(history.size - history_size)
  end
  history_path.write(history.join("\n") + "\n")
end
start() click to toggle source
# File lib/querly/cli/console.rb, line 24
      def start
        puts <<-Message
Querly #{VERSION}, interactive console

        Message

        puts_commands

        STDOUT.print "Loading..."
        STDOUT.flush
        reload!
        STDOUT.puts " ready!"

        load_history
        start_loop
      end
start_loop() click to toggle source
# File lib/querly/cli/console.rb, line 64
def start_loop
  while line = Readline.readline("> ", true)
    case line
    when "quit", "exit"
      exit
    when "reload!"
      STDOUT.print "reloading..."
      STDOUT.flush
      reload!
      STDOUT.puts " done"
    when /^find (.+)/
      begin
        pattern = Pattern::Parser.parse($1, where: {})

        count = 0

        analyzer.find(pattern) do |script, pair|
          path = script.path.to_s
          line_no = pair.node.loc.first_line
          range = pair.node.loc.expression
          start_col = range.column
          end_col = range.last_column

          src = range.source_buffer.source_lines[line_no-1]
          src = Rainbow(src[0...start_col]).blue +
            Rainbow(src[start_col...end_col]).bright.blue.bold +
            Rainbow(src[end_col..-1]).blue

          puts "  #{path}:#{line_no}:#{start_col}\t#{src}"

          count += 1
        end

        puts "#{count} results"

        save_history line
      rescue => exn
        STDOUT.puts Rainbow("Error: #{exn}").red
        STDOUT.puts "Backtrace:"
        STDOUT.puts format_backtrace(exn.backtrace)
      end
    else
      puts_commands
    end
  end
end