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:, history_path:, history_size:) ⇒ Console

Returns a new instance of Console.



13
14
15
16
17
18
# File 'lib/querly/cli/console.rb', line 13

def initialize(paths:, history_path:, history_size:)
  @paths = paths
  @history_path = history_path
  @history_size = history_size
  @history = []
end

Instance Attribute Details

#historyObject (readonly)

Returns the value of attribute history.



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

def history
  @history
end

#history_pathObject (readonly)

Returns the value of attribute history_path.



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

def history_path
  @history_path
end

#history_sizeObject (readonly)

Returns the value of attribute history_size.



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

def history_size
  @history_size
end

#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



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/querly/cli/console.rb', line 42

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

#load_historyObject



107
108
109
110
111
112
113
114
115
# File 'lib/querly/cli/console.rb', line 107

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_commandsObject



125
126
127
128
129
130
131
132
133
# File 'lib/querly/cli/console.rb', line 125

def puts_commands
  puts "Commands:\n  - find PATTERN   Find PATTERN from given paths\n  - reload!        Reload program from paths\n  - quit\n\n  Message\nend\n"

#reload!Object



37
38
39
40
# File 'lib/querly/cli/console.rb', line 37

def reload!
  @analyzer = nil
  analyzer
end

#save_history(line) ⇒ Object



117
118
119
120
121
122
123
# File 'lib/querly/cli/console.rb', line 117

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

#startObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/querly/cli/console.rb', line 20

def start
  puts "Querly \#{VERSION}, interactive console\n\n  Message\n\n  puts_commands\n\n  STDOUT.print \"Loading...\"\n  STDOUT.flush\n  reload!\n  STDOUT.puts \" ready!\"\n\n  load_history\n  start_loop\nend\n"

#start_loopObject



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
98
99
100
101
102
103
104
105
# File 'lib/querly/cli/console.rb', line 60

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