Class: RubynCode::Output::Formatter

Inherits:
Object
  • Object
show all
Defined in:
lib/rubyn_code/output/formatter.rb

Constant Summary collapse

TOOL_ICON =

wrench

"\u{1F527}"
AGENT_ICON =

robot

"\u{1F916}"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(enabled: $stdout.tty?) ⇒ Formatter

Returns a new instance of Formatter.



14
15
16
# File 'lib/rubyn_code/output/formatter.rb', line 14

def initialize(enabled: $stdout.tty?)
  @pastel = Pastel.new(enabled: enabled)
end

Instance Attribute Details

#pastelObject (readonly)

Returns the value of attribute pastel.



12
13
14
# File 'lib/rubyn_code/output/formatter.rb', line 12

def pastel
  @pastel
end

Instance Method Details

#agent_message(message) ⇒ Object



95
96
97
98
# File 'lib/rubyn_code/output/formatter.rb', line 95

def agent_message(message)
  prefix = pastel.blue.bold("#{AGENT_ICON} Assistant")
  output "#{prefix}\n#{message}"
end

#bold(message) ⇒ Object



38
39
40
# File 'lib/rubyn_code/output/formatter.rb', line 38

def bold(message)
  output pastel.bold(message)
end

#code_block(code, language: 'ruby') ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/rubyn_code/output/formatter.rb', line 42

def code_block(code, language: 'ruby')
  lexer = find_lexer(language)
  formatter = Rouge::Formatters::Terminal256.new(theme: Rouge::Themes::Monokai.new)

  highlighted = formatter.format(lexer.lex(code))

  lines = [
    pastel.dim("\u2500" * 40),
    highlighted,
    pastel.dim("\u2500" * 40)
  ]

  output lines.join("\n")
end

#colorize_diff_line(line) ⇒ Object



62
63
64
65
66
67
68
69
70
# File 'lib/rubyn_code/output/formatter.rb', line 62

def colorize_diff_line(line)
  case line
  when /\A[+-]{3}\s/ then pastel.bold(line)
  when /\A@@/        then pastel.cyan(line)
  when /\A\+/        then pastel.green(line)
  when /\A-/         then pastel.red(line)
  else                    pastel.dim(line)
  end
end

#diff(text) ⇒ Object



57
58
59
60
# File 'lib/rubyn_code/output/formatter.rb', line 57

def diff(text)
  lines = text.each_line.map { |line| colorize_diff_line(line) }
  output lines.join
end

#dim(message) ⇒ Object



34
35
36
# File 'lib/rubyn_code/output/formatter.rb', line 34

def dim(message)
  output pastel.dim(message)
end

#error(message) ⇒ Object



22
23
24
# File 'lib/rubyn_code/output/formatter.rb', line 22

def error(message)
  output pastel.red("\u2718 #{message}")
end

#info(message) ⇒ Object



30
31
32
# File 'lib/rubyn_code/output/formatter.rb', line 30

def info(message)
  output pastel.cyan("\u2139 #{message}")
end

#success(message) ⇒ Object



18
19
20
# File 'lib/rubyn_code/output/formatter.rb', line 18

def success(message)
  output pastel.green("\u2714 #{message}")
end

#tool_call(tool_name, arguments = {}) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/rubyn_code/output/formatter.rb', line 72

def tool_call(tool_name, arguments = {})
  header = pastel.magenta.bold("#{TOOL_ICON} #{tool_name}")
  parts = [header]

  unless arguments.empty?
    args_display = arguments.map do |key, value|
      display_value = truncate(value.to_s, 120)
      "  #{pastel.dim("#{key}:")} #{display_value}"
    end
    parts.concat(args_display)
  end

  output parts.join("\n")
end

#tool_result(tool_name, result, success: true) ⇒ Object



87
88
89
90
91
92
93
# File 'lib/rubyn_code/output/formatter.rb', line 87

def tool_result(tool_name, result, success: true)
  status = success ? pastel.green("\u2714") : pastel.red("\u2718")
  header = pastel.magenta("#{status} #{tool_name}")
  result_text = truncate(result.to_s, 500)

  output "#{header}\n#{pastel.dim(result_text)}"
end

#warning(message) ⇒ Object



26
27
28
# File 'lib/rubyn_code/output/formatter.rb', line 26

def warning(message)
  output pastel.yellow("\u26A0 #{message}")
end