-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP refactor of formatting and printers
- Loading branch information
1 parent
c06506b
commit 181595a
Showing
7 changed files
with
147 additions
and
131 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.