Module: Sus::Output::Variable

Defined in:
lib/sus/output/variable.rb

Overview

Provides a compact, truncated representation of values for output.

Rather than building a full ‘inspect` string and then truncating it, we walk the value and stream tokens directly into an output, aborting as soon as a character budget is exhausted. This means we never materialize the full representation of a large subject (e.g. a big array or richly inspected instance).

Containers (arrays and hashes) are formatted directly so we can recurse with the budget; leaf values delegate to native ‘inspect` so their formatting exactly matches Ruby. The value itself is emitted in a single style (matching the rest of sus’s output), and only the truncation ellipsis is highlighted distinctly so it’s clear where output was cut.

Defined Under Namespace

Classes: Formatter

Constant Summary collapse

TRUNCATION_LIMIT =

The maximum length of an inspected value before it is truncated. Override it with the ‘SUS_OUTPUT_VARIABLE_TRUNCATION_LIMIT` environment variable; a value of `0` (or `nil`) disables truncation entirely.

ENV.fetch("SUS_OUTPUT_VARIABLE_TRUNCATION_LIMIT", 100).then do |value|
	value = Integer(value)
	value.zero? ? nil : value
end
ELLIPSIS =

The string appended to a truncated value.

""

Class Method Summary collapse

Class Method Details

.buffer(value, limit: TRUNCATION_LIMIT) ⇒ Object

Capture a value’s representation into a buffer, resolving the value immediately, but deferring colour resolution until the buffer is replayed into a real output.



171
172
173
174
175
# File 'lib/sus/output/variable.rb', line 171

def self.buffer(value, limit: TRUNCATION_LIMIT)
	buffer = Buffered.new
	self.format(buffer, value, limit: limit)
	return buffer
end

.format(output, value, limit: TRUNCATION_LIMIT) ⇒ Object

Format a value into the given output, truncating at the limit.



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/sus/output/variable.rb', line 149

def self.format(output, value, limit: TRUNCATION_LIMIT)
	# With no limit, the formatter would produce exactly `value.inspect`, so
	# we can skip walking the value and emit it directly:
	unless limit
		return output.write(:variable, value.inspect, :reset)
	end
	
	formatter = Formatter.new(output, limit: limit)
	
	begin
		formatter.format(value)
	rescue Formatter::Truncated
		output.write(:ellipsis, ELLIPSIS, :reset)
	end
end