Class: Exa::CLI::Formatters::SearchFormatter

Inherits:
Object
  • Object
show all
Defined in:
lib/exa/cli/formatters/search_formatter.rb

Class Method Summary collapse

Class Method Details

.format(result, format) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/exa/cli/formatters/search_formatter.rb', line 7

def self.format(result, format)
  case format
  when "json"
    JSON.pretty_generate(result.to_h)
  when "pretty"
    format_pretty(result)
  when "text"
    format_text(result)
  when "toon"
    Exa::CLI::Base.encode_as_toon(result.to_h)
  else
    JSON.pretty_generate(result.to_h)
  end
end

.format_pretty(result) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/exa/cli/formatters/search_formatter.rb', line 24

def self.format_pretty(result)
  output = []
  result.results.each_with_index do |item, idx|
    output << "--- Result #{idx + 1} ---"
    output << "Title:       #{item['title']}"
    output << "URL:         #{item['url']}"
    output << "Score:       #{item['score']}" if item['score']
    output << ""
  end
  output.join("\n")
end

.format_text(result) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/exa/cli/formatters/search_formatter.rb', line 36

def self.format_text(result)
  output = []
  result.results.each do |item|
    output << "#{item['title']}\n#{item['url']}"
  end
  output.join("\n\n")
end