Skip to content

Commit

Permalink
WIP refactor of formatting and printers
Browse files Browse the repository at this point in the history
  • Loading branch information
icy-arctic-fox committed Aug 10, 2024
1 parent c06506b commit 181595a
Show file tree
Hide file tree
Showing 7 changed files with 147 additions and 131 deletions.
18 changes: 10 additions & 8 deletions src/spectator/formatters/common_text_output.cr
Original file line number Diff line number Diff line change
Expand Up @@ -53,19 +53,19 @@ module Spectator::Formatters
private def print_failure(result, number, padding) : Nil
digit_count = number.to_s.size
padding -= digit_count
printer.print_inline_label("#{number})", padding: padding) do
printer.puts result.example.full_description, style: :error
printer.label("#{number})", padding: padding) do
printer.with_style(:error, &.puts result.example.full_description)

error = result.exception
if error.is_a?(AssertionFailed)
printer.print_label(:error, &.<< "Failure: ")
printer.label(:error, "Failure: ")
if location = error.location
if source_code = Spectator.source_cache.get(location.file, location.line, location.end_line)
if location.line == location.end_line
printer.print_code(source_code.strip)
printer.code(source_code.strip)
else
printer.puts
printer.indent &.print_code(source_code)
printer.indent &.code(source_code)
end
end
end
Expand All @@ -79,7 +79,8 @@ module Spectator::Formatters
printer.puts
if location = error.location
relative_path = location.relative_to(Spectator.working_path)
printer.puts "# #{relative_path}", style: :info
printer.with_style(:info, &.print "# ", relative_path)
printer.puts
printer.puts
end
else
Expand All @@ -89,7 +90,7 @@ module Spectator::Formatters
end

private def print_trace(error) : Nil
printer.print_label(:error, &.<< "#{error.class}: ")
printer.label(:error, &.<< "#{error.class}: ")
printer.puts "#{error.message}"

printer.indent do
Expand Down Expand Up @@ -141,7 +142,7 @@ module Spectator::Formatters
printer << " (" << humanize(summary.test_time) << " in tests)"
printer.puts

printer.puts(summary.style) do |io|
printer.print(summary.style) do |io|
io << summary.total << " examples, "
io << summary.failed << " failures"
if summary.errors > 0
Expand All @@ -151,6 +152,7 @@ module Spectator::Formatters
io << ", " << summary.skipped << " skipped"
end
end
printer.puts
end

# TODO: Move to a utility module.
Expand Down
53 changes: 30 additions & 23 deletions src/spectator/formatters/indent.cr
Original file line number Diff line number Diff line change
@@ -1,48 +1,55 @@
module Spectator::Formatters
module Indent
DEFAULT_INDENT_AMOUNT = 2
INDENT_STRING = " "

abstract def io : IO

private property indent_amount = 0
private property indent = ""
private property indent_size = 0
private property? newline = true

def indent(amount : Int = DEFAULT_INDENT_AMOUNT, & : self ->) : Nil
self.indent_amount += amount
self.indent_size += amount
self.indent = INDENT_STRING * indent_size
begin
yield self
ensure
self.indent_amount -= amount
self.indent_size -= amount
end
end

def <<(object) : self
print_indent
super
private def print_indented(io : IO, *objects) : Nil
objects.each do |object|
maybe_print_indent(io)
string = insert_indent(object.to_s)
io.print string
end
end

def print(*objects) : Nil
print_indent
io.print(*objects)
end
private def puts_indented(io : IO, *objects) : Nil
if objects.empty?
io.puts
self.newline = true
return
end

def puts(*objects) : Nil
objects.each do |object|
print_indent
io.puts(object)
@newline = true
maybe_print_indent(io)
string = object.to_s.chomp
string = insert_indent(string)
io.puts string
self.newline = true
end
end

def puts : Nil
io.puts
self.newline = true
end

private def print_indent
private def maybe_print_indent(io : IO) : Nil
return unless newline?
io << indent
self.newline = false
indent_amount.times { io << ' ' }
end

private def insert_indent(text : String) : String
return text unless text.includes?('\n')
text.gsub('\n', "\n#{indent}")
end
end
end
55 changes: 27 additions & 28 deletions src/spectator/formatters/plain_printer.cr
Original file line number Diff line number Diff line change
Expand Up @@ -5,48 +5,47 @@ module Spectator::Formatters
class PlainPrinter < Printer
include Indent

def puts(style : Style, & : IO ->) : Nil
print_indent
yield io
def <<(object) : self
print_indented(io, object)
self
end

def print(style : Style, & : IO ->) : Nil
print_indent
yield io
def print(*objects) : Nil
print_indented(io, *objects)
end

def print_value(& : IO ->) : Nil
print_indent
yield io
def puts(*objects) : Nil
puts_indented(io, *objects)
end

def print_type(& : IO ->) : Nil
print_indent
yield io
def title(text : String) : Nil
puts_indented(io)
puts_indented(io, text)
puts_indented(io)
end

def print_title(style : Style = :none, & : IO ->) : Nil
print_indent
yield io
def label(label : String, *, padding : Int = 0, & : self ->) : Nil
puts_indented(io)
print_indented(io, " " * padding, label, ' ')
indent(label.size + 1) do
yield self
end
end

def print_label(style : Style = :none, & : IO ->) : Nil
print_indent
yield io
def value(value) : Nil
print_indented(io, value)
end

def print_inline_label(label : String, style : Style = :none, padding : Int = 0, &) : Nil
indent(padding) do
print_indent
io << label
io << ' '
indent(label.size + padding + 1) { yield }
end
def type(type) : Nil
print_indented(io, type)
end

def code(code : String) : Nil
puts_indented(io, code)
end

def print_code(code : String) : Nil
print_indent
io.puts code
def with_style(style : Style, & : self ->) : Nil
yield self
end
end
end
33 changes: 12 additions & 21 deletions src/spectator/formatters/printer.cr
Original file line number Diff line number Diff line change
Expand Up @@ -13,37 +13,28 @@ module Spectator::Formatters
def initialize(@io = STDOUT)
end

def <<(object) : self
io << object
self
end
abstract def <<(object) : self

abstract def puts(style : Style, & : IO ->) : Nil
abstract def print(*objects) : Nil

def puts(*objects, style : Style) : Nil
objects.each do |object|
puts style, &.puts(object)
end
end
abstract def puts(*objects) : Nil

abstract def print(style : Style, & : IO ->) : Nil
abstract def title(text : String) : Nil

def print(*objects, style : Style) : Nil
objects.each do |object|
print style, &.print(object)
def label(label : String, text : String, *, padding : Int = 0) : Nil
labeled(label, padding: padding) do |printer|
printer << text
end
end

abstract def print_value(& : IO ->) : Nil

abstract def print_type(& : IO ->) : Nil
abstract def label(label : String, *, padding : Int = 0, & : self ->) : Nil

abstract def print_title(style : Style = :none, & : IO ->) : Nil
abstract def value(value) : Nil

abstract def print_label(style : Style = :none, & : IO ->) : Nil
abstract def type(type) : Nil

abstract def print_inline_label(label : String, style : Style = :none, padding : Int = 0, &) : Nil
abstract def code(code : String) : Nil

abstract def print_code(code : String) : Nil
abstract def with_style(style : Style, & : self ->) : Nil
end
end
92 changes: 46 additions & 46 deletions src/spectator/formatters/terminal_printer.cr
Original file line number Diff line number Diff line change
Expand Up @@ -7,60 +7,68 @@ module Spectator::Formatters
class TerminalPrinter < Printer
include Indent

def puts(style : Style, &) : Nil
print_indent
colorize_style(style).surround(io) do
yield io
end
puts
private getter style : Style = :none

def <<(object) : self
print_indented(io, object)
self
end

def print(style : Style, & : IO ->) : Nil
print_indent
colorize_style(style).surround(io) do
yield io
end
def print(*objects) : Nil
print_indented(io, *objects)
end

def print_value(& : IO ->) : Nil
print_indent
Colorize.with.bold.surround(io) do
yield io
end
def puts(*objects) : Nil
puts_indented(io, *objects)
end

def print_type(& : IO ->) : Nil
print_indent
Colorize.with.bold.cyan.surround(io) do
yield io
def title(text : String) : Nil
puts_indented(io)
emphasized_colorize_style(style).surround(io) do
print_indented(io, ' ', text, ' ')
end
puts_indented(io)
puts_indented(io)
end

def print_title(style : Style = :none, & : IO ->) : Nil
print_indent
def label(label : String, *, padding : Int = 0, & : self ->) : Nil
puts_indented(io)
emphasized_colorize_style(style).surround(io) do
io << ' '
yield io
io << ' '
print_indented(io, " " * padding, label, ' ')
end
indent(label.size + 1) do
yield self
end
puts
end

def print_label(style : Style = :none, & : IO ->) : Nil
print_indent
colorize_style(style).surround(io) do
yield io
def value(value) : Nil
Colorize.with.bold.surround(io) do
print_indented(io, value)
end
end

def print_inline_label(label : String, style : Style = :none, padding : Int = 0, &) : Nil
indent(padding) do
emphasized_colorize_style(style, label).surround(io) do
print_indent
io << label
end
io << ' '
indent(label.size + 1) { yield }
def type(type) : Nil
Colorize.with.bold.cyan.surround(io) do
print_indented(io, type)
end
end

def code(code : String) : Nil
lines = code.lines(false)
min_code_indent = lines.min_of { |line| indent_size(line) }
lines.map! { |line| line[min_code_indent..] }
code = lines.join
highlighted = syntax_highlighter.highlight(code)
puts_indented(io, highlighted)
end

def with_style(style : Style, & : self ->) : Nil
previous_style = self.style
begin
self.style = style
yield self
ensure
self.style = previous_style
end
end

Expand All @@ -84,14 +92,6 @@ module Spectator::Formatters
end
end

def print_code(code : String) : Nil
print_indent
indent = " " * indent_amount
indented_code = code.gsub('\n', "\n#{indent}")
syntax_highlighter.highlight(indented_code)
io.puts unless code.ends_with?("\n")
end

private def colorize_style(style : Style, object = nil)
base = object.colorize
case style
Expand Down
Loading

0 comments on commit 181595a

Please sign in to comment.