Class: SpecForge::Forge::Display

Inherits:
Object
  • Object
show all
Defined in:
lib/spec_forge/forge/display.rb

Overview

Handles formatted output for forge execution

Display manages the console output during test runs, adapting its verbosity based on the configured level (0-3). It formats step headers, action indicators, expectation results, and failure summaries.

Constant Summary collapse

LINE_LENGTH =

Maximum line length for output formatting

Returns:

  • (Integer)
120

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(verbosity_level: 0) ⇒ Display

Creates a new display handler with the specified verbosity level

Parameters:

  • verbosity_level (Integer) (defaults to: 0)

    Output verbosity (0=minimal, 1=verbose, 2=debug, 3=trace)



29
30
31
32
# File 'lib/spec_forge/forge/display.rb', line 29

def initialize(verbosity_level: 0)
  @verbosity_level = verbosity_level || 0
  @color = Pastel.new
end

Instance Attribute Details

#verbosity_levelInteger (readonly)

Returns Current verbosity level (0-3).

Returns:

  • (Integer)

    Current verbosity level (0-3)



20
21
22
# File 'lib/spec_forge/forge/display.rb', line 20

def verbosity_level
  @verbosity_level
end

Instance Method Details

#action(message, **options) ⇒ void

This method returns an undefined value.

Displays an action message with optional symbol indicator

Only shown when verbosity is above default (0). Used to indicate step actions like callbacks, requests, and store operations.

Parameters:

  • message (String)

    The action message to display

  • options (Hash)

    Formatting options passed to #format



90
91
92
93
94
# File 'lib/spec_forge/forge/display.rb', line 90

def action(message, **options)
  return if default_mode?

  puts format(message, indent: 1, **options)
end

#blueprint_end(blueprint, success: true) ⇒ void

This method returns an undefined value.

Called when a blueprint finishes execution

Parameters:

  • blueprint (Blueprint)

    The blueprint that finished

  • success (Boolean) (defaults to: true)

    Whether all steps passed



286
287
288
289
290
291
292
293
294
295
296
297
298
# File 'lib/spec_forge/forge/display.rb', line 286

def blueprint_end(blueprint, success: true)
  return if default_mode?

  style = success ? :bright_green : :bright_red

  visual_length = "[#{blueprint.name}] Cleanup".size
  line = "#{@color.decorate("[#{blueprint.name}]", style)} Cleanup"
  length = LINE_LENGTH - visual_length

  filler = @color.decorate("" * length, style)

  puts "#{line} #{filler}"
end

#blueprint_start(blueprint) ⇒ void

This method returns an undefined value.

Called when a blueprint begins execution

Displays a header line with the blueprint name. Only shown in verbose modes.

Parameters:

  • blueprint (Blueprint)

    The blueprint starting



200
201
202
203
204
205
206
207
208
# File 'lib/spec_forge/forge/display.rb', line 200

def blueprint_start(blueprint)
  return if default_mode?

  visual_length = "[#{blueprint.name}] Setup".size
  line = "#{@color.bright_blue("[#{blueprint.name}]")} Setup"
  filler = @color.bright_blue("" * (LINE_LENGTH - visual_length))

  puts "#{line} #{filler}"
end

#default_mode?Boolean

Returns whether display is in default (minimal) mode

Returns:

  • (Boolean)

    True if verbosity is 0



39
40
41
# File 'lib/spec_forge/forge/display.rb', line 39

def default_mode?
  verbosity_level == 0
end

#empty_linevoid

This method returns an undefined value.

Outputs a blank line for visual separation



75
76
77
# File 'lib/spec_forge/forge/display.rb', line 75

def empty_line
  puts ""
end

#expectation_failed(message, indent: 0) ⇒ void

This method returns an undefined value.

Displays a failing expectation indicator (red F)

Parameters:

  • message (String)

    The expectation message (unused in default mode)

  • indent (Integer) (defaults to: 0)

    Indentation level



118
119
120
121
122
# File 'lib/spec_forge/forge/display.rb', line 118

def expectation_failed(message, indent: 0)
  return if verbose?

  print @color.red("F")
end

#expectation_finished(failed_examples:, total_count:, index: 0, show_index: false) ⇒ void

This method returns an undefined value.

Displays the result summary for a completed expectation

Parameters:

  • failed_examples (Array)

    List of failed RSpec examples

  • total_count (Integer)

    Total number of assertions in the expectation

  • index (Integer) (defaults to: 0)

    The expectation index (1-based)

  • show_index (Boolean) (defaults to: false)

    Whether to display the index prefix



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/spec_forge/forge/display.rb', line 134

def expectation_finished(failed_examples:, total_count:, index: 0, show_index: false)
  return if default_mode?

  failed_count = failed_examples.size

  print format_with_indent("#{index}:  ", indent: 1) if show_index

  if failed_count == 0
    action(
      "(#{total_count}/#{total_count} passed)",
      symbol: :success,
      symbol_styles: :green,
      indent: show_index ? 0 : 1
    )
  else
    action(
      "(#{failed_count}/#{total_count} failed)",
      symbol: :error,
      symbol_styles: :red,
      indent: show_index ? 0 : 1
    )
  end

  return if failed_examples.blank?

  puts ""

  failed_examples.each do |example|
    message = example[:exception][:message].strip.prepend("\n")

    puts format_with_indent("#{example[:description]} #{@color.red(message)}", indent: 3)
    # puts JSON.pretty_generate(example[:exception][:backtrace]) # DEBUG

    puts ""
  end
end

#expectation_passed(message, indent: 0) ⇒ void

This method returns an undefined value.

Displays a passing expectation indicator (green dot)

Parameters:

  • message (String)

    The expectation message (unused in default mode)

  • indent (Integer) (defaults to: 0)

    Indentation level



104
105
106
107
108
# File 'lib/spec_forge/forge/display.rb', line 104

def expectation_passed(message, indent: 0)
  return if verbose?

  print @color.green(".")
end

#forge_end(forge) ⇒ void

This method returns an undefined value.

Called when the entire forge run completes

Parameters:

  • forge (Forge)

    The forge instance



307
308
309
310
311
312
313
314
# File 'lib/spec_forge/forge/display.rb', line 307

def forge_end(forge)
  return if default_mode?

  line = "#{@color.magenta("[forge]")} Quenched"
  filler = @color.magenta("" * (LINE_LENGTH - 16))

  puts "#{line} #{filler}"
end

#forge_start(forge) ⇒ void

This method returns an undefined value.

Called when the forge run begins

Displays a header line indicating the forge has started. Only shown in verbose modes.

Parameters:

  • forge (Forge)

    The forge instance starting



181
182
183
184
185
186
187
188
# File 'lib/spec_forge/forge/display.rb', line 181

def forge_start(forge)
  return if default_mode?

  line = "#{@color.magenta("[forge]")} Ignited"
  filler = @color.magenta("" * (LINE_LENGTH - 15)) # [forge] ignited

  puts "#{line} #{filler}"
end

#max_verbose?Boolean

Returns whether display is in maximum verbose (trace) mode

Returns:

  • (Boolean)

    True if verbosity is 3 or higher



66
67
68
# File 'lib/spec_forge/forge/display.rb', line 66

def max_verbose?
  verbosity_level >= 3
end

#stats(forge) ⇒ void

This method returns an undefined value.

Displays final statistics and any failures from the forge run

Shows failure details (if any), summary counts for blueprints, steps, expectations, and failures, plus total execution time.

Parameters:

  • forge (Forge)

    The completed forge instance



326
327
328
329
330
331
332
333
334
335
336
337
# File 'lib/spec_forge/forge/display.rb', line 326

def stats(forge)
  puts ""

  if forge.failures.size > 0
    puts "Failures:\n\n"
    puts format_failures(forge.failures)
  end

  puts format_stats(forge)
  puts ""
  puts @color.dim("Finished in #{sprintf("%.2g", forge.timer.time_elapsed)}s")
end

#step_end(forge, step, error: nil) ⇒ void

This method returns an undefined value.

Called when a step finishes execution

Parameters:

  • forge (Forge)

    The forge instance

  • step (Step)

    The step that finished

  • error (Exception, nil) (defaults to: nil)

    Any error that occurred



256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
# File 'lib/spec_forge/forge/display.rb', line 256

def step_end(forge, step, error: nil)
  return unless verbose?

  puts ""

  if max_verbose? || (very_verbose? && error)
    indent = error ? 3 : 1

    details = format_debug_details(forge, step, indent:)
    return if details.blank?

    if error
      puts format_with_indent(@color.red(error.message), indent:) unless error.is_a?(Error::ExpectationFailure)
      puts ""
      puts format_with_indent(@color.dim("" * (LINE_LENGTH * 0.75)), indent:)
      puts ""
    end

    puts details
  end
end

#step_start(step) ⇒ void

This method returns an undefined value.

Called when a step begins execution

Parameters:

  • step (Step)

    The step starting



217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# File 'lib/spec_forge/forge/display.rb', line 217

def step_start(step)
  return if default_mode?

  line_number = step.source.line_number.to_s.rjust(2, "0")
  line = "#{step.source.file_name}:#{line_number}"

  if step.included_by.present?
    line_number = step.included_by.line_number.to_s.rjust(2, "0")
    line = "#{step.included_by.file_name}:#{line_number}#{line}"
  end

  visual_length = line.size + 2
  line = @color.cyan("[#{line}]")

  filler_size = LINE_LENGTH - visual_length

  # +1 offset to match forge/blueprint headers
  if step.name.present?
    name = step.name
    filler_size -= (name.size + 1)
  else
    name = @color.dim("(unnamed)")
    filler_size -= 10 # (unnamed) + 1
  end

  filler = @color.cyan("" * filler_size)

  puts "#{line} #{name} #{filler}"
end

#verbose?Boolean

Returns whether display is in verbose mode or higher

Returns:

  • (Boolean)

    True if verbosity is 1 or higher



48
49
50
# File 'lib/spec_forge/forge/display.rb', line 48

def verbose?
  verbosity_level >= 1
end

#very_verbose?Boolean

Returns whether display is in very verbose (debug) mode or higher

Returns:

  • (Boolean)

    True if verbosity is 2 or higher



57
58
59
# File 'lib/spec_forge/forge/display.rb', line 57

def very_verbose?
  verbosity_level >= 2
end