Class: SwarmCLI::UI::Formatters::Cost

Inherits:
Object
  • Object
show all
Defined in:
lib/swarm_cli/ui/formatters/cost.rb

Overview

Cost formatting with color coding

Class Method Summary collapse

Class Method Details

.format(cost, pastel:) ⇒ Object

Format cost with appropriate precision and color Small costs: green, $0.001234 Medium costs: yellow, $0.1234 Large costs: red, $12.34



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/swarm_cli/ui/formatters/cost.rb', line 13

def format(cost, pastel:)
  return pastel.dim("$0.0000") if cost.nil? || cost.zero?

  formatted = if cost < 0.01
    Kernel.format("%.6f", cost)
  elsif cost < 1.0
    Kernel.format("%.4f", cost)
  else
    Kernel.format("%.2f", cost)
  end

  if cost < 0.01
    pastel.green("$#{formatted}")
  elsif cost < 1.0
    pastel.yellow("$#{formatted}")
  else
    pastel.red("$#{formatted}")
  end
end

.format_plain(cost) ⇒ Object

Format cost without color (for plain text)



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/swarm_cli/ui/formatters/cost.rb', line 34

def format_plain(cost)
  return "$0.0000" if cost.nil? || cost.zero?

  if cost < 0.01
    Kernel.format("$%.6f", cost)
  elsif cost < 1.0
    Kernel.format("$%.4f", cost)
  else
    Kernel.format("$%.2f", cost)
  end
end