Class: Exa::CLI::Formatters::ResearchFormatter

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

Class Method Summary collapse

Class Method Details

.format_list(list, format) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/exa/cli/formatters/research_formatter.rb', line 20

def self.format_list(list, format)
  case format
  when "json"
    JSON.pretty_generate(list.to_h)
  when "pretty"
    format_list_pretty(list)
  when "text"
    format_list_text(list)
  when "toon"
    Exa::CLI::Base.encode_as_toon(list.to_h)
  else
    JSON.pretty_generate(list.to_h)
  end
end

.format_list_pretty(list) ⇒ Object



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
# File 'lib/exa/cli/formatters/research_formatter.rb', line 74

def self.format_list_pretty(list)
  output = []
  output << "Research Tasks (#{list.data.length}):"
  output << ""

  if list.data.empty?
    output << "No tasks found."
  else
    # Simple table format
    output << "%-40s %-15s %s" % ["Task ID", "Status", "Created"]
    output << "-" * 70

    list.data.each do |task|
      task_id = task.research_id.to_s[0..38]
      status = task.status.upcase[0..14]
      created = task.created_at.to_s[0..19]
      output << "%-40s %-15s %s" % [task_id, status, created]
    end
  end

  output << ""
  if list.has_more
    output << "More results available. Use --cursor #{list.next_cursor} for next page."
  else
    output << "End of results."
  end

  output.join("\n")
end

.format_list_text(list) ⇒ Object



115
116
117
118
119
120
# File 'lib/exa/cli/formatters/research_formatter.rb', line 115

def self.format_list_text(list)
  output = list.data.map do |task|
    "#{task.research_id} #{task.status.upcase} #{task.created_at}"
  end
  output.join("\n")
end

.format_task(task, format, show_events: false) ⇒ Object



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

def self.format_task(task, format, show_events: false)
  case format
  when "json"
    JSON.pretty_generate(task.to_h)
  when "pretty"
    format_task_pretty(task, show_events: show_events)
  when "text"
    format_task_text(task, show_events: show_events)
  when "toon"
    Exa::CLI::Base.encode_as_toon(task.to_h)
  else
    JSON.pretty_generate(task.to_h)
  end
end

.format_task_pretty(task, show_events: false) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/exa/cli/formatters/research_formatter.rb', line 37

def self.format_task_pretty(task, show_events: false)
  output = []
  output << "Research Task: #{task.research_id}"
  output << "Status: #{task.status.upcase}"
  output << "Created: #{task.created_at}"
  output << ""

  case task.status
  when "pending"
    output << "Task is pending execution..."
  when "running"
    output << "Task is running... ⚙️"
  when "completed"
    output << "Output:"
    output << "--------"
    output << task.output.to_s
    output << ""
    output << "Cost: $#{task.cost_dollars}" if task.cost_dollars
  when "failed"
    output << "Error: #{task.error}"
  when "canceled"
    output << "Task was canceled"
    output << "Finished: #{task.finished_at}" if task.finished_at
  end

  if show_events && task.events && !task.events.empty?
    output << ""
    output << "Events:"
    output << "-------"
    task.events.each do |event|
      output << "- #{event}"
    end
  end

  output.join("\n")
end

.format_task_text(task, show_events: false) ⇒ Object



104
105
106
107
108
109
110
111
112
113
# File 'lib/exa/cli/formatters/research_formatter.rb', line 104

def self.format_task_text(task, show_events: false)
  output = []
  output << "#{task.research_id} #{task.status.upcase} #{task.created_at}"
  if task.status == "completed"
    output << task.output.to_s
  elsif task.status == "failed"
    output << "Error: #{task.error}"
  end
  output.join("\n")
end