Class: RubynCode::Output::Formatter
- Inherits:
-
Object
- Object
- RubynCode::Output::Formatter
- 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
-
#pastel ⇒ Object
readonly
Returns the value of attribute pastel.
Instance Method Summary collapse
- #agent_message(message) ⇒ Object
- #bold(message) ⇒ Object
- #code_block(code, language: 'ruby') ⇒ Object
- #colorize_diff_line(line) ⇒ Object
- #diff(text) ⇒ Object
- #dim(message) ⇒ Object
- #error(message) ⇒ Object
- #info(message) ⇒ Object
-
#initialize(enabled: $stdout.tty?) ⇒ Formatter
constructor
A new instance of Formatter.
- #success(message) ⇒ Object
- #tool_call(tool_name, arguments = {}) ⇒ Object
- #tool_result(tool_name, result, success: true) ⇒ Object
- #warning(message) ⇒ Object
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
#pastel ⇒ Object (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 () prefix = pastel.blue.bold("#{AGENT_ICON} Assistant") output "#{prefix}\n#{}" end |
#bold(message) ⇒ Object
38 39 40 |
# File 'lib/rubyn_code/output/formatter.rb', line 38 def bold() output pastel.bold() 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() output pastel.dim() end |
#error(message) ⇒ Object
22 23 24 |
# File 'lib/rubyn_code/output/formatter.rb', line 22 def error() output pastel.red("\u2718 #{}") end |
#info(message) ⇒ Object
30 31 32 |
# File 'lib/rubyn_code/output/formatter.rb', line 30 def info() output pastel.cyan("\u2139 #{}") end |
#success(message) ⇒ Object
18 19 20 |
# File 'lib/rubyn_code/output/formatter.rb', line 18 def success() output pastel.green("\u2714 #{}") 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() output pastel.yellow("\u26A0 #{}") end |