Class: LanguageOperator::CLI::Formatters::CodeFormatter

Inherits:
Object
  • Object
show all
Extended by:
Helpers::UxHelper
Defined in:
lib/language_operator/cli/formatters/code_formatter.rb

Overview

Formatter for displaying syntax-highlighted code in the terminal

Class Method Summary collapse

Methods included from Helpers::UxHelper

box, highlight_ruby_code, logo, pastel, prompt, spinner, table

Class Method Details

.display_ruby_code(code_content, title: nil, max_lines: nil) ⇒ Object

Display Ruby code with syntax highlighting

Parameters:

  • code_content (String)

    The Ruby code to display

  • title (String, nil) (defaults to: nil)

    Optional title to display above the code

  • max_lines (Integer, nil) (defaults to: nil)

    Maximum number of lines to display (nil for all)



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/language_operator/cli/formatters/code_formatter.rb', line 18

def display_ruby_code(code_content, title: nil, max_lines: nil)
  # Truncate if max_lines specified
  lines = code_content.lines
  truncated = false
  if max_lines && lines.length > max_lines
    code_to_display = lines[0...max_lines].join
    truncated = true
    remaining_lines = lines.length - max_lines
  else
    code_to_display = code_content
  end

  # Highlight and print the code
  highlighted = highlight_ruby_code(code_to_display)
  puts highlighted

  # Show truncation notice if applicable
  return unless truncated

  puts
  puts pastel.dim("... #{remaining_lines} more lines ...")
end

.display_snippet(code_content, description: nil) ⇒ Object

Display a code snippet with context

Parameters:

  • code_content (String)

    The Ruby code

  • description (String) (defaults to: nil)

    Description of what the code does



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/language_operator/cli/formatters/code_formatter.rb', line 45

def display_snippet(code_content, description: nil)
  puts
  puts pastel.cyan('Generated Code Preview:') if description.nil?
  puts pastel.dim(description) if description
  puts

  highlighted = highlight_ruby_code(code_content)

  puts highlighted
  puts
end