Class: Ace::Search::Organisms::ResultFormatter

Inherits:
Object
  • Object
show all
Defined in:
lib/ace/search/organisms/result_formatter.rb

Overview

Formats search results for various output formats This is an organism - business logic for result formatting

Class Method Summary collapse

Class Method Details

.format(results, format: :text, options: {}) ⇒ String

Format results based on output format

Parameters:

  • results (Array<Hash>)

    Search results

  • format (Symbol) (defaults to: :text)

    Output format (:text, :json, :yaml)

  • options (Hash) (defaults to: {})

    Formatting options

Returns:

  • (String)

    Formatted output



17
18
19
20
21
22
23
24
25
26
# File 'lib/ace/search/organisms/result_formatter.rb', line 17

def self.format(results, format: :text, options: {})
  case format
  when :json
    format_json(results)
  when :yaml
    format_yaml(results)
  else
    format_text(results, options)
  end
end

.format_count_result(result) ⇒ Object



64
65
66
# File 'lib/ace/search/organisms/result_formatter.rb', line 64

def self.format_count_result(result)
  "  #{result[:path]}: #{result[:count]}"
end

.format_file_result(result) ⇒ Object

Format file result



49
50
51
# File 'lib/ace/search/organisms/result_formatter.rb', line 49

def self.format_file_result(result)
  "  #{result[:path]}"
end

.format_json(results) ⇒ Object

Format results as JSON



69
70
71
72
73
74
# File 'lib/ace/search/organisms/result_formatter.rb', line 69

def self.format_json(results)
  JSON.pretty_generate({
    count: results.size,
    results: results
  })
end

.format_match_result(result, options = {}) ⇒ Object

Format match result with clickable link



54
55
56
57
58
59
60
61
62
# File 'lib/ace/search/organisms/result_formatter.rb', line 54

def self.format_match_result(result, options = {})
  # Create clickable terminal link: file:line:column
  path = result[:path]
  line = result[:line] || result[:line_number] || 1
  column = result[:column] || 0
  text = result[:text] || result[:content] || ""

  "  #{path}:#{line}:#{column}: #{text}"
end

.format_summary(results, options = {}) ⇒ Object

Format summary header



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/ace/search/organisms/result_formatter.rb', line 85

def self.format_summary(results, options = {})
  return "" if options[:count]

  count = results.size
  mode = options[:mode] || "search"
  pattern = options[:pattern] || ""

  filters = []
  # Show search path first if available
  if options[:search_path]
    # Convert to absolute path for display
    display_path = File.expand_path(options[:search_path])
    filters << "path: #{display_path}"
  end
  filters << "mode: #{mode}" if mode
  filters << "pattern: \"#{pattern}\"" if pattern && !pattern.empty?
  filters << "glob: #{options[:glob]}" if options[:glob]
  filters << "scope: #{options[:scope]}" if options[:scope]

  filter_str = filters.empty? ? "" : " | #{filters.join(" | ")}"

  "Search context:#{filter_str}\nFound #{count} results\n"
end

.format_text(results, options = {}) ⇒ Object

Format results as text with clickable terminal links



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/ace/search/organisms/result_formatter.rb', line 29

def self.format_text(results, options = {})
  return "No results found" if results.empty?

  lines = []

  results.each do |result|
    case result[:type]
    when :file
      lines << format_file_result(result)
    when :count
      lines << format_count_result(result)
    when :match
      lines << format_match_result(result, options)
    end
  end

  lines.join("\n")
end

.format_yaml(results) ⇒ Object

Format results as YAML



77
78
79
80
81
82
# File 'lib/ace/search/organisms/result_formatter.rb', line 77

def self.format_yaml(results)
  YAML.dump({
    "count" => results.size,
    "results" => results
  })
end