Skip to content

Commit

Permalink
Showing 28 changed files with 1,014 additions and 13 deletions.
451 changes: 451 additions & 0 deletions spec/std/pretty_print_spec.cr

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions src/array.cr
Original file line number Diff line number Diff line change
@@ -1531,6 +1531,13 @@ class Array(T)
io << "[...]" unless executed
end

def pretty_print(pp) : Nil
executed = exec_recursive(:pretty_print) do
pp.list("[", self, "]")
end
pp.text "[...]" unless executed
end

# Returns a pointer to the internal buffer where `self`'s elements are stored.
#
# This method is **unsafe** because it returns a pointer, and the pointed might eventually
4 changes: 4 additions & 0 deletions src/compiler/crystal/syntax/ast.cr
Original file line number Diff line number Diff line change
@@ -84,6 +84,10 @@ module Crystal
def class_desc : String
{{@type.name.split("::").last.id.stringify}}
end

def pretty_print(pp)
pp.text to_s
end
end

class Nop < ASTNode
4 changes: 4 additions & 0 deletions src/compiler/crystal/syntax/location.cr
Original file line number Diff line number Diff line change
@@ -48,6 +48,10 @@ class Crystal::Location
io << filename << ":" << line_number << ":" << column_number
end

def pretty_print(pp)
pp.text to_s
end

def <=>(other)
self_file = @filename
other_file = other.filename
8 changes: 8 additions & 0 deletions src/compiler/crystal/syntax/virtual_file.cr
Original file line number Diff line number Diff line change
@@ -18,4 +18,12 @@ class Crystal::VirtualFile
def to_s(io)
io << "expanded macro: " << @macro.name
end

def inspect(io)
to_s(io)
end

def pretty_print(pp)
pp.text inspect
end
end
4 changes: 4 additions & 0 deletions src/compiler/crystal/types.cr
Original file line number Diff line number Diff line change
@@ -542,6 +542,10 @@ module Crystal
end

abstract def to_s_with_options(io : IO, skip_union_parens : Bool = false, generic_args : Bool = true, codegen = false)

def pretty_print(pp)
pp.text to_s
end
end

# A type that has a name and can be inside a namespace.
7 changes: 7 additions & 0 deletions src/deque.cr
Original file line number Diff line number Diff line change
@@ -338,6 +338,13 @@ class Deque(T)
io << "Deque{...}" unless executed
end

def pretty_print(pp)
executed = exec_recursive(:inspect) do
pp.list("Deque{", self, "}")
end
pp.text "Deque{...}" unless executed
end

# Returns the number of elements in the deque.
#
# ```
8 changes: 8 additions & 0 deletions src/dir.cr
Original file line number Diff line number Diff line change
@@ -203,6 +203,14 @@ class Dir
io << "#<Dir:" << @path << ">"
end

def inspect(io)
to_s(io)
end

def pretty_print(pp)
pp.text inspect
end

private struct EntryIterator
include Iterator(String)

13 changes: 13 additions & 0 deletions src/env.cr
Original file line number Diff line number Diff line change
@@ -133,4 +133,17 @@ module ENV
end
io << "}"
end

def self.pretty_print(pp)
pp.list("{", keys.sort, "}") do |key|
pp.group do
key.pretty_print(pp)
pp.text " =>"
pp.nest do
pp.breakable
self[key].pretty_print(pp)
end
end
end
end
end
30 changes: 30 additions & 0 deletions src/file/stat.cr
Original file line number Diff line number Diff line change
@@ -101,6 +101,36 @@ class File
io << ">"
end

def pretty_print(pp)
pp.surround("#<File::Stat", ">", left_break: " ", right_break: nil) do
pp.text "dev=0x#{dev.to_s(16)}"
pp.comma
pp.text "ino=#{ino}"
pp.comma
pp.text "mode=0o#{mode.to_s(8)}"
pp.comma
pp.text "nlink=#{nlink}"
pp.comma
pp.text "uid=#{uid}"
pp.comma
pp.text "gid=#{gid}"
pp.comma
pp.text "rdev=0x#{rdev.to_s(16)}"
pp.comma
pp.text "size=#{size}"
pp.comma
pp.text "blksize=#{blksize}"
pp.comma
pp.text "blocks=#{blocks}"
pp.comma
pp.text "atime=#{atime}"
pp.comma
pp.text "mtime=#{mtime}"
pp.comma
pp.text "ctime=#{ctime}"
end
end

def blockdev?
(@stat.st_mode & LibC::S_IFMT) == LibC::S_IFBLK
end
16 changes: 16 additions & 0 deletions src/hash.cr
Original file line number Diff line number Diff line change
@@ -749,6 +749,22 @@ class Hash(K, V)
io << "{...}" unless executed
end

def pretty_print(pp) : Nil
executed = exec_recursive(:pretty_print) do
pp.list("{", self, "}") do |key, value|
pp.group do
key.pretty_print(pp)
pp.text " =>"
pp.nest do
pp.breakable
value.pretty_print(pp)
end
end
end
end
pp.text "{...}" unless executed
end

# Returns self.
def to_h
self
18 changes: 18 additions & 0 deletions src/http/headers.cr
Original file line number Diff line number Diff line change
@@ -227,6 +227,24 @@ struct HTTP::Headers
to_s(io)
end

def pretty_print(pp)
pp.list("HTTP::Headers{", @hash.keys.sort_by(&.name), "}") do |key|
pp.group do
key.name.pretty_print(pp)
pp.text " =>"
pp.nest do
pp.breakable
values = get(key)
if values.size == 1
values.first.pretty_print(pp)
else
values.pretty_print(pp)
end
end
end
end
end

forward_missing_to @hash

private def wrap(key)
4 changes: 4 additions & 0 deletions src/io/file_descriptor.cr
Original file line number Diff line number Diff line change
@@ -225,6 +225,10 @@ class IO::FileDescriptor
io
end

def pretty_print(pp)
pp.text inspect
end

private def unbuffered_read(slice : Slice(UInt8))
count = slice.size
loop do
5 changes: 5 additions & 0 deletions src/json/any.cr
Original file line number Diff line number Diff line change
@@ -230,6 +230,11 @@ struct JSON::Any
@raw.to_s(io)
end

# :nodoc:
def pretty_print(pp)
@raw.pretty_print(pp)
end

# Returns true if both `self` and *other*'s raw object are equal.
def ==(other : JSON::Any)
raw == other.raw
13 changes: 8 additions & 5 deletions src/kernel.cr
Original file line number Diff line number Diff line change
@@ -71,20 +71,23 @@ def puts(*objects)
STDOUT.puts *objects
end

# Prints *object* to STDOUT by invoking `inspect(io)` on it, followed
# Pretty prints *object* to STDOUT followed
# by a newline. Returns *object*.
#
# See `Object#pretty_print(pp)`
def p(object)
object.inspect(STDOUT)
PrettyPrint.format(object, STDOUT, 79)
puts
object
end

# Prints each object in *objects* to STDOUT by invoking `inspect(io)` on it, followed
# Pretty prints each object in *objects* to STDOUT, followed
# by a newline. Returns *objects*.
#
# See `Object#pretty_print(pp)`
def p(*objects)
objects.each do |obj|
obj.inspect(STDOUT)
puts
p obj
end
objects
end
19 changes: 11 additions & 8 deletions src/macros.cr
Original file line number Diff line number Diff line change
@@ -97,16 +97,19 @@ macro pp(*exps)
# Nothing
{% elsif exps.size == 1 %}
{% exp = exps.first %}
::puts "#{ {{exp.stringify}} } # => #{ ({{exp}}).inspect }"
%prefix = "#{{{ exp.stringify }}} # => "
::print %prefix
PrettyPrint.format({{exp}}, STDOUT, width: 80 - %prefix.size, indent: %prefix.size)
::puts
{% else %}
%strings = [] of {String, String}
{% for exp in exps %}
%strings.push({ {{exp.stringify}}, ({{exp}}).inspect })
%names = { {{*exps.map(&.stringify)}} }
%max_size = %names.max_of &.size
{% for exp, i in exps %}
%prefix = "#{%names[{{i}}].ljust(%max_size)} # => "
::print %prefix
PrettyPrint.format({{exp}}, STDOUT, width: 80 - %prefix.size, indent: %prefix.size)
::puts
{% end %}
%max_size = %strings.max_of &.[0].size
%strings.each do |%left, %right|
::puts "#{%left.ljust(%max_size)} # => #{%right}"
end
{% end %}
end

23 changes: 23 additions & 0 deletions src/named_tuple.cr
Original file line number Diff line number Diff line change
@@ -241,6 +241,29 @@ struct NamedTuple
io << "}"
end

def pretty_print(pp)
pp.surround("{", "}", left_break: nil, right_break: nil) do
{% for key, value, i in T %}
{% if i > 0 %}
pp.comma
{% end %}
pp.group do
key = {{key.stringify}}
if Symbol.needs_quotes?(key)
pp.text key.inspect
else
pp.text key
end
pp.text ": "
pp.nest do
pp.breakable ""
self[{{key.symbolize}}].pretty_print(pp)
end
end
{% end %}
end
end

# Yields each key and value in this named tuple.
#
# ```
16 changes: 16 additions & 0 deletions src/object.cr
Original file line number Diff line number Diff line change
@@ -107,6 +107,22 @@ class Object
to_s io
end

# Pretty prints `self` into the given printer.
#
# By default appends a text that is the result of invoking
# `#inspect` on self. Subclasses should override
# for custom pretty printing.
def pretty_print(pp : PrettyPrint) : Nil
pp.text(inspect)
end

# Returns a pretty printed version of `self`.
def pretty_inspect(width = 79, newline = "\n", indent = 0) : String
String.build do |io|
PrettyPrint.format(self, io, width, newline, indent)
end
end

# Yields self to the block, and then returns self.
#
# The primary purpose of this method is to "tap into" a method chain,
1 change: 1 addition & 0 deletions src/prelude.cr
Original file line number Diff line number Diff line change
@@ -52,6 +52,7 @@ require "named_tuple"
require "nil"
require "number"
require "pointer"
require "pretty_print"
require "primitives"
require "proc"
require "process"
Loading

0 comments on commit 84b0c6d

Please sign in to comment.