Class: Querly::CLI::Console
- Inherits:
-
Object
- Object
- Querly::CLI::Console
- Includes:
- Querly::Concerns::BacktraceFormatter
- Defined in:
- lib/querly/cli/console.rb
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#history ⇒ Object
readonly
Returns the value of attribute history.
-
#history_path ⇒ Object
readonly
Returns the value of attribute history_path.
-
#history_size ⇒ Object
readonly
Returns the value of attribute history_size.
-
#paths ⇒ Object
readonly
Returns the value of attribute paths.
-
#threads ⇒ Object
readonly
Returns the value of attribute threads.
Instance Method Summary collapse
- #analyzer ⇒ Object
-
#initialize(paths:, history_path:, history_size:, config: nil, threads:) ⇒ Console
constructor
A new instance of Console.
- #load_history ⇒ Object
- #puts_commands ⇒ Object
- #reload! ⇒ Object
- #save_history(line) ⇒ Object
- #start ⇒ Object
- #start_loop ⇒ Object
Methods included from Querly::Concerns::BacktraceFormatter
Constructor Details
#initialize(paths:, history_path:, history_size:, config: nil, threads:) ⇒ Console
Returns a new instance of Console.
15 16 17 18 19 20 21 22 |
# 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 |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
11 12 13 |
# File 'lib/querly/cli/console.rb', line 11 def config @config end |
#history ⇒ Object (readonly)
Returns the value of attribute history.
12 13 14 |
# File 'lib/querly/cli/console.rb', line 12 def history @history end |
#history_path ⇒ Object (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_size ⇒ Object (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 |
#paths ⇒ Object (readonly)
Returns the value of attribute paths.
8 9 10 |
# File 'lib/querly/cli/console.rb', line 8 def paths @paths end |
#threads ⇒ Object (readonly)
Returns the value of attribute threads.
13 14 15 |
# File 'lib/querly/cli/console.rb', line 13 def threads @threads end |
Instance Method Details
#analyzer ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# 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 ⇒ Object
111 112 113 114 115 116 117 118 119 |
# 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 ⇒ Object
129 130 131 132 133 134 135 136 137 |
# 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! ⇒ Object
41 42 43 44 |
# File 'lib/querly/cli/console.rb', line 41 def reload! @analyzer = nil analyzer end |
#save_history(line) ⇒ Object
121 122 123 124 125 126 127 |
# 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 ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# 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 ⇒ Object
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 106 107 108 109 |
# 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 |