Class: Querly::CLI::Console

Inherits:
Object
  • Object
show all
Includes:
Querly::Concerns::BacktraceFormatter
Defined in:
lib/querly/cli/console.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Querly::Concerns::BacktraceFormatter

#format_backtrace

Constructor Details

#initialize(paths:) ⇒ Console

Returns a new instance of Console.



10
11
12
# File 'lib/querly/cli/console.rb', line 10

def initialize(paths:)
  @paths = paths
end

Instance Attribute Details

#pathsObject (readonly)

Returns the value of attribute paths.



8
9
10
# File 'lib/querly/cli/console.rb', line 8

def paths
  @paths
end

Instance Method Details

#analyzerObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/querly/cli/console.rb', line 35

def analyzer
  return @analyzer if @analyzer

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

  ScriptEnumerator.new(paths: paths, config: nil).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

#loopObject



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/querly/cli/console.rb', line 53

def loop
  while line = Readline.readline("> ", true)
    case line
    when "quit"
      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 = 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-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}:#{start_col}\t#{src}"

          count += 1
        end

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

#puts_commandsObject



98
99
100
101
102
103
104
105
106
# File 'lib/querly/cli/console.rb', line 98

def puts_commands
  puts <<-Message
Commands:
  - find PATTERN   Find PATTERN from given paths
  - reload!        Reload program from paths
  - quit

  Message
end

#reload!Object



30
31
32
33
# File 'lib/querly/cli/console.rb', line 30

def reload!
  @analyzer = nil
  analyzer
end

#startObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/querly/cli/console.rb', line 14

def start
  puts <<-Message
Querly #{VERSION}, interactive console

  Message

  puts_commands

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

  loop
end