Class: Synthra::REPL::Formatter

Inherits:
Object
  • Object
show all
Defined in:
lib/synthra/repl/formatter.rb

Overview

Visual formatter for REPL output

Class Method Summary collapse

Class Method Details

.format_array_table(array) ⇒ String

Format array as a table

Parameters:

  • array (Array)

    array to format

Returns:

  • (String)

    formatted table



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/synthra/repl/formatter.rb', line 62

def self.format_array_table(array)
  return "[]" if array.empty?

  # If array of hashes, show as table
  if array.first.is_a?(Hash)
    keys = array.first.keys
    max_widths = keys.map { |k| [k.to_s.length, array.map { |h| format_value(h[k], 100).length }.max].max }
    max_widths = max_widths.map { |w| [w, 30].min } # Cap column widths

    # Header
    header = "  " + keys.map.with_index { |k, i| k.to_s.ljust(max_widths[i]) }.join("")
    separator = "  " + max_widths.map { |w| "" * w }.join("─┼─")

    rows = array.map do |hash|
      "  " + keys.map.with_index { |k, i| format_value(hash[k], max_widths[i]).ljust(max_widths[i]) }.join("")
    end

    [header, separator, *rows].join("\n")
  else
    array.map.with_index { |item, i| "  [#{i}] #{format_value(item, 60)}" }.join("\n")
  end
end

.format_context(context) ⇒ String

Format context state for debugging

Parameters:

  • context (Context)

    context to format

Returns:

  • (String)

    formatted context state



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/synthra/repl/formatter.rb', line 124

def self.format_context(context)
  lines = []
  lines << "  Context State:"
  lines << "  ──────────────"
  lines << "  Depth: #{context.depth}"
  lines << "  Registry: #{context.registry ? 'available' : 'none'}"
  lines << "  Parent Context: #{context.parent_context ? 'yes' : 'no'}"
  lines << "  Shared Context: #{context.shared_context ? "#{context.shared_context.size} entries" : 'none'}"
  lines << ""
  lines << "  Generated Fields:"
  lines << "  ────────────────"
  
  if context.empty?
    lines << "    (none yet)"
  else
    context.each do |key, value|
      value_str = format_value(value, 50)
      lines << "    #{key.to_s.ljust(20)} = #{value_str}"
    end
  end

  lines.join("\n")
end

.format_hash_table(hash) ⇒ String

Format hash as a table

Parameters:

  • hash (Hash)

    hash to format

Returns:

  • (String)

    formatted table



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/synthra/repl/formatter.rb', line 39

def self.format_hash_table(hash)
  return "{}" if hash.empty?

  max_key_width = hash.keys.map { |k| k.to_s.length }.max
  max_key_width = [max_key_width, 20].min # Cap at 20 chars
  max_value_width = 60

  lines = []
  hash.each do |key, value|
    key_str = key.to_s.ljust(max_key_width)
    value_str = format_value(value, max_value_width)
    lines << "  #{key_str}#{value_str}"
  end

  header = "  #{'' * max_key_width}─┼─#{'' * max_value_width}"
  [header, *lines].join("\n")
end

.format_value(value, max_width = 60) ⇒ String

Format a single value

Parameters:

  • value (Object)

    value to format

  • max_width (Integer) (defaults to: 60)

    maximum width

Returns:

  • (String)

    formatted value



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/synthra/repl/formatter.rb', line 91

def self.format_value(value, max_width = 60)
  case value
  when nil
    "nil"
  when String
    if value.length > max_width
      "#{value[0, max_width - 3]}..."
    else
      value.inspect
    end
  when Hash
    if value.empty?
      "{}"
    else
      "{#{value.keys.length} keys}"
    end
  when Array
    if value.empty?
      "[]"
    else
      "[#{value.length} items]"
    end
  else
    str = value.inspect
    str.length > max_width ? "#{str[0, max_width - 3]}..." : str
  end
end

.table(data) ⇒ String

Format data as a table

Parameters:

  • data (Hash, Array)

    data to format

Returns:

  • (String)

    formatted table string



23
24
25
26
27
28
29
30
31
32
# File 'lib/synthra/repl/formatter.rb', line 23

def self.table(data)
  case data
  when Hash
    format_hash_table(data)
  when Array
    format_array_table(data)
  else
    data.inspect
  end
end