GarishPrint
An opinioned pretty printing package for Julia objects.
Installation
GarishPrint is a
Julia Language
package. To install GarishPrint,
please open
Julia's interactive session (known as REPL) and press ] key in the REPL to use the package mode, then type the following command
pkg> add GarishPrint
Usage
there is only one function exported that is pprint
,
GarishPrint.pprint
— Functionpprint([io::IO=stdout, ]xs...; kw...)
Pretty print given objects xs
to io
, default io is stdout
.
Keyword Arguments
indent::Int
: indent size, default is2
.compact::Bool
: whether print withint one line, default isget(io, :compact, false)
.width::Int
: the width hint of printed string, note this is not stricted obeyed,
default is displaysize(io)[2].
show_indent::Bool
: whether print indentation hint, default istrue
.color::Bool
: whether print with color, default istrue
.
Color Preference
color preference is available as keyword arguments to override the default color scheme. These arguments may take any of the values :normal
, :default
, :bold
, :black
, :blink
, :blue
, :cyan
, :green
, :hidden
, :light_black
, :light_blue
, :light_cyan
, :light_green
, :light_magenta
, :light_red
, :light_yellow
, :magenta
, :nothing
, :red
, :reverse
, :underline
, :white
, or :yellow
or an integer between 0 and 255 inclusive. Note that not all terminals support 256 colors.
The default color scheme can be checked via GarishPrint.default_colors_256()
for 256 color, and GarishPrint.default_colors_ansi()
for ANSI color. The 256 color will be used when the terminal is detected to support 256 color.
fieldname
: field name of a struct.type
: the color of a type.operator
: the color of an operator, e.g+
,=>
.literal
: the color of literals.constant
: the color of constants, e.gπ
.number
: the color of numbers, e.g1.2
,1
.string
: the color of string.comment
: comments, e.g# some comments
undef
: the const binding toUndefInitializer
linenumber
: line numbers.
Notes
The color print and compact print can also be turned on/off by setting IOContext
, e.g IOContext(io, :color=>false)
will print without color, and IOContext(io, :compact=>true)
will print within one line. This is also what the standard Julia IO
objects follows in printing by default.
pprint(io::IO, mime::MIME, x; kw...)
Pretty print an object x with given MIME
type.
currently only supports MIME"text/plain"
, the implementation of MIME"text/html"
is coming soon. Please also feel free to file an issue if you have a desired format wants to support.
here is a quick example
using GarishPrint
struct ABC{T1, T2, T3}
hee::T1
haa::T2
hoo::T3
end
struct Example{T1, T2}
field_a::T1
field_b::T2
abc::ABC
end
x = Example(
Dict(
"a"=>Example(
[1, 2, 3],
2.0,
ABC(1, 2.0im, 3.12f0),
),
"str" => Set([1, 2, 3]),
),
undef,
ABC(nothing, 1.2+2.1im, π),
)
pprint(x)
it will print the following
License
MIT License