Class: Querly::CLI::Console

Inherits:
Object
  • Object
show all
Defined in:
lib/querly/cli/console.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(paths:) ⇒ Console

Returns a new instance of Console.



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

def initialize(paths:)
  @paths = paths
end

Instance Attribute Details

#pathsObject (readonly)

Returns the value of attribute paths.



6
7
8
# File 'lib/querly/cli/console.rb', line 6

def paths
  @paths
end

Instance Method Details

#analyzerObject



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

def analyzer
  return @analyzer if @analyzer

  @analyzer = Analyzer.new(taggings: [])

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

  @analyzer
end

#loopObject



50
51
52
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
97
# File 'lib/querly/cli/console.rb', line 50

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)

        count = 0

        analyzer.find(pattern) do |script, pair|
          path = script.path.to_s
          line = pair.node.loc.first_line

          while true
            parent = pair.parent

            if parent && parent.node.loc.first_line == line
              pair = pair.parent
            else
              break
            end
          end

          src = Rainbow(pair.node.loc.expression.source.split(/\n/).first).blue
          col = pair.node.loc.column

          puts "  #{path}:#{line}:#{col}\t#{src}"

          count += 1
        end

        puts "#{count} results"
      rescue => exn
        STDOUT.puts exn.inspect
        STDOUT.puts exn.backtrace
      end
    else
      puts_commands
    end
  end
end

#puts_commandsObject



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

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

  Message
end

#reload!Object



28
29
30
31
# File 'lib/querly/cli/console.rb', line 28

def reload!
  @analyzer = nil
  analyzer
end

#startObject



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

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

  Message

  puts_commands

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

  loop
end