Skip to content

snowe2010/pretty-print

Repository files navigation

GitHub Workflow Status Bintray Codecov

pretty-print - pp

Adds a pp(Any?) and T.pp() method to pretty print any Java or Kotlin object.

pp(Any?) takes any object and prints it in a pretty format.

T.pp() pretty prints inside a method chain when called on an object inline.

Approach Instruction

Gradle

testImplementation "com.tylerthrailkill.helpers:pretty-print:{version}"

Gradle Kotlin DSL

testImplementation("com.tylerthrailkill.helpers:pretty-print:$version")

Maven

<dependency>
    <groupId>com.tylerthrailkill.helpers</groupId>
    <artifactId>pretty-print</artifactId>
    <version>{version}</version>
</dependency>

API

`pp(obj: Any?, indent: Int = 2, writeTo: Appendable = System.out, wrappedLineWidth: Int = 80)

Parameter Description Default

obj

object to print

indent

changes the number of spaces used to indent each level of the output

2

writeTo

changes the Appendable you are printing to

System.out

wrappedLineWidth

changes the number of characters allowed before wrapping in a multiline string

80

Examples

Main API

Top Level Method
data class TinyObject(var int: Int)
pp(TinyObject(1))
TinyObject(
  int = 1
)
Inline method
data class TinyObject(var int: Int)
fun callSomething(obj: Any?) {
    println("inline wrapper function entered")
}
callSomething(TinyObject(1).pp())
TinyObject(
  int = 1
)
inline wrapper function entered

Other uses

List
pp(listOf("1", 2, 3.0, true))
[
  "1",
  2,
  3.0,
  true
]
Object with list
data class OL(val list: List<String>)
pp(OL(listOf("1")))
OL(
  list = [
           "1"
         ]
)
Map
pp(mapOf("key1" to "value1", "key2" to "value2"))
{
  "key1" -> "value1",
  "key2" -> "value2"
}
Object with map
data class OM(val map: Map<Any, Any>)
pp(OM(mapOf(1 to "value", "key" to 1)))
OM(
  map = {
          1 -> "value",
          "key" -> 1
        }
)
Multiline strings
pp("Goodbye, cruel world. Goodbye, cruel lamp.", wrappedLineWidth = 22)
"""
Goodbye, cruel world.
Goodbye, cruel lamp.
"""
Multiline strings with unicode line breaking
pp("Goodbye, cruel world. Good­bye, cruel lamp.", wrappedLineWidth = 27)
"""
Goodbye, cruel world. Good­
bye, cruel lamp.
"""
pp("😍️🥞😍️", wrappedLineWidth = 3)
"""
😍️
🥞
😍️
"""
Multiple fields
pp(SmallObject("Goodbye, cruel world. Goodbye, cruel lamp.", 1))
SmallObject(
  field1 = "Goodbye, cruel world. Goodbye, cruel lamp."
  field2 = 1
)
Different indent size
data class TinyObject(var int: Int)
pp(TinyObject(1), tabSize = 0)
TinyObject(
int = 1
)
data class TinyObject(var int: Int)
pp(TinyObject(1), tabSize = 10)
TinyObject(
          int = 1
)
Different output stream
val stream = ByteArrayOutputStream()
pp(TinyObject(1), printStream = PrintStream(stream))
println(":::")
print(stream.toString())
println(":::")
:::
TinyObject(
  int = 1
)
:::
Cyclic references
data class O1(var c: O2? = null)
data class O2(var c: O1? = null)
val sco1 = O1()
val sco2 = O2(sco1)
sco1.c = sco2
pp(sco1)
O1(
  c = O2(
    c = cyclic reference detected for 50699452
  )
)[$id=50699452]

ToDo

  • Test nullability cases

  • implement pretty print for java* classes

  • fix unicode line breaking with icu4j library characters

  • multiplatform