Skip to content

Commit

Permalink
Refactor config more examples
Browse files Browse the repository at this point in the history
  • Loading branch information
deusaquilus committed Jan 23, 2024
1 parent ff28c3a commit ae95a60
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 91 deletions.
5 changes: 1 addition & 4 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,7 @@ dependencies {
implementation(kotlin("reflect"))
}

//tasks.test {
// useJUnitPlatform()
//}

// Needed for Kotest
tasks.withType<Test>().configureEach {
useJUnitPlatform()
}
Expand Down
19 changes: 11 additions & 8 deletions src/main/kotlin/io/exoquery/Base.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package io.exoquery
import io.exoquery.fansi.Attrs
import io.exoquery.fansi.Color
import io.exoquery.pprint.PPrinter
import io.exoquery.pprint.PPrinterConfig

fun pprint(
value: Any?,
Expand All @@ -15,12 +16,14 @@ fun pprint(
colorApplyPrefix: Attrs = Color.Yellow,
showGenericForCollections: Boolean = true
) = PPrinter(
defaultWidth,
defaultHeight,
defaultIndent,
defaultEscapeUnicode,
defaultShowFieldNames,
colorLiteral,
colorApplyPrefix,
showGenericForCollections
PPrinterConfig(
defaultWidth,
defaultHeight,
defaultIndent,
defaultEscapeUnicode,
defaultShowFieldNames,
colorLiteral,
colorApplyPrefix,
showGenericForCollections
)
)(value)
69 changes: 37 additions & 32 deletions src/main/kotlin/io/exoquery/pprint/PPrinter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,17 @@ import io.exoquery.fansi.Attrs
import io.exoquery.fansi.Color as FansiColor
import io.exoquery.fansi.Str

data class PPrinterConfig(
val defaultWidth: Int = 100,
val defaultHeight: Int = 500,
val defaultIndent: Int = 2,
val defaultEscapeUnicode: Boolean = false,
val defaultShowFieldNames: Boolean = true,
val colorLiteral: Attrs = FansiColor.Green,
val colorApplyPrefix: Attrs = FansiColor.Yellow,
val showGenericForCollections: Boolean = true
)

/**
*
* @param defaultWidth How wide to allow a pretty-printed value to become
Expand All @@ -16,28 +27,20 @@ import io.exoquery.fansi.Str
* @param additionalHandlers Provide this to override how certain types are
* pretty-printed at runtime
*/
data class PPrinter(
val defaultWidth: Int = 100,
val defaultHeight: Int = 500,
val defaultIndent: Int = 2,
val defaultEscapeUnicode: Boolean = false,
val defaultShowFieldNames: Boolean = true,
val colorLiteral: Attrs = FansiColor.Green,
val colorApplyPrefix: Attrs = FansiColor.Yellow,
override val showGenericForCollections: Boolean = true
): Walker() {
open class PPrinter(val config: PPrinterConfig): Walker {
override val showGenericForCollections get() = config.showGenericForCollections

/**
* Converts an [[Any]] into a large colored `Str`
*/
operator fun invoke(
open operator fun invoke(
x: Any?,
width: Int = defaultWidth,
height: Int = defaultHeight,
indent: Int = defaultIndent,
width: Int = config.defaultWidth,
height: Int = config.defaultHeight,
indent: Int = config.defaultIndent,
initialOffset: Int = 0,
escapeUnicode: Boolean = defaultEscapeUnicode,
showFieldNames: Boolean = defaultShowFieldNames
escapeUnicode: Boolean = config.defaultEscapeUnicode,
showFieldNames: Boolean = config.defaultShowFieldNames
): Str =
Str.join(
this.tokenize(
Expand All @@ -55,13 +58,13 @@ data class PPrinter(
/**
* Converts an [[Any]] into a large colored `Str`
*/
fun <T> pprintln(x: T,
width: Int = defaultWidth,
height: Int = defaultHeight,
indent: Int = defaultIndent,
open fun <T> pprintln(x: T,
width: Int = config.defaultWidth,
height: Int = config.defaultHeight,
indent: Int = config.defaultIndent,
initialOffset: Int = 0,
escapeUnicode: Boolean = defaultEscapeUnicode,
showFieldNames: Boolean = defaultShowFieldNames): Unit {
escapeUnicode: Boolean = config.defaultEscapeUnicode,
showFieldNames: Boolean = config.defaultShowFieldNames): Unit {
tokenize(
x as Any,
width,
Expand All @@ -78,22 +81,22 @@ data class PPrinter(
* Converts an [[Any]] into an iterator of colored chunks, wrapped at a
* certain width and truncated at a certain height
*/
fun tokenize(
open fun tokenize(
x: Any?,
width: Int = defaultWidth,
height: Int = defaultHeight,
indent: Int = defaultIndent,
width: Int = config.defaultWidth,
height: Int = config.defaultHeight,
indent: Int = config.defaultIndent,
initialOffset: Int = 0,
escapeUnicode: Boolean = defaultEscapeUnicode,
showFieldNames: Boolean = defaultShowFieldNames): Iterator<Str> {
escapeUnicode: Boolean = config.defaultEscapeUnicode,
showFieldNames: Boolean = config.defaultShowFieldNames): Iterator<Str> {

// The three stages within the pretty-printing process:

// Convert the Any into a lazy Tree of `Apply`, `Infix` and `Lazy`/`Strict` literals
val tree = this.treeify(x, escapeUnicode, showFieldNames)
// Render the `Any` into a stream of tokens, properly indented and wrapped
// at the given width
val renderer = Renderer(width, colorApplyPrefix, colorLiteral, indent)
val renderer = Renderer(width, config.colorApplyPrefix, config.colorLiteral, indent)
val rendered = renderer.rec(tree, initialOffset, 0).iter
// Truncate the output stream once it's wrapped-at-width height goes
// beyond the desired height
Expand All @@ -102,10 +105,12 @@ data class PPrinter(
}

companion object {
val Color = PPrinter()
val Color = PPrinter(PPrinterConfig())
val BlackWhite = PPrinter(
colorLiteral = Attrs.Empty,
colorApplyPrefix = Attrs.Empty
PPrinterConfig().copy(
colorLiteral = Attrs.Empty,
colorApplyPrefix = Attrs.Empty
)
)
}
}
Expand Down
5 changes: 2 additions & 3 deletions src/main/kotlin/io/exoquery/pprint/Walker.kt
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,8 @@ sealed interface Tree {
)
}

abstract class Walker {
val tuplePrefix = "scala.Tuple"
abstract val showGenericForCollections: Boolean
interface Walker {
val showGenericForCollections: Boolean

// No additional handlers. To extend, override the treeify function
// fun additionalHandlers: PartialFunction<Any, Tree>
Expand Down
8 changes: 4 additions & 4 deletions src/test/kotlin/io/exoquery/pprint/Check.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ class Check(val width: Int = 100, val height: Int = 99999, val renderTwice: Bool
}

companion object {
val blackWhite = PPrinter(defaultShowFieldNames = false)
val color = PPrinter(defaultShowFieldNames = false)
val blackWhiteFields = PPrinter()
val colorFields = PPrinter()
val blackWhite = PPrinter(PPrinterConfig(defaultShowFieldNames = false))
val color = PPrinter(PPrinterConfig(defaultShowFieldNames = false))
val blackWhiteFields = PPrinter(PPrinterConfig())
val colorFields = PPrinter(PPrinterConfig())
}
}
97 changes: 57 additions & 40 deletions src/test/kotlin/io/exoquery/pprint/Examples.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package io.exoquery.pprint

import io.exoquery.fansi.Str
import io.exoquery.pprint
import java.time.format.DateTimeFormatter.ISO_LOCAL_DATE

data class Name(val first: String, val last: String)
data class Person(val name: Name, val age: Int)
Expand All @@ -16,47 +17,63 @@ data class VeryComplexCustomer(val name: Name, val addressAliases: Map<Alias, Li

data class SequenceHolder(val seq: Sequence<String>)

fun ex1() = run {
val p = Person(Name("Joe", "Bloggs"), 42)
println(pprint(p))
}

fun ex2() = run {
val p = Customer(Name("Joe", "Bloggs"), listOf(Address("foo", 123), Address("bar", 456), Address("baz", 789)))
println(pprint(p))
}

fun ex3() = run {
val p =
ComplexCustomer(
Name("Joe", "Bloggs"),
mapOf(Alias("Primary") to Address("foo", 123), Alias("Secondary") to Address("bar", 456), Alias("Tertiary") to Address("baz", 789))
)
println(pprint(p))
}

fun ex4() = run {
val p =
VeryComplexCustomer(
Name("Joe", "Bloggs"),
mapOf(
Alias("Primary") to
listOf(Address("foo", 123), Address("foo1", 123), Address("foo2", 123)),
Alias("Secondary") to
listOf(Address("bar", 456), Address("bar1", 456), Address("bar2", 456)),
Alias("Tertiary") to
listOf(Address("baz", 789), Address("baz1", 789), Address("baz2", 789))
)
)
println(pprint(p))
}

fun ex5() = run {
var i = 0
val p = SequenceHolder(generateSequence { "foo-${i++}" })
println(pprint(p, defaultHeight = 10).plainText)
}

class CustomPPrinter1(config: PPrinterConfig) : PPrinter(config) {
override fun treeify(x: Any?, escapeUnicode: Boolean, showFieldNames: Boolean): Tree =
when (x) {
is java.time.LocalDate -> Tree.Literal(x.format(ISO_LOCAL_DATE))
else -> super.treeify(x, escapeUnicode, showFieldNames)
}
}

fun ex7() = run {
val pp = CustomPPrinter1(PPrinterConfig())
println(pp.invoke(java.time.LocalDate.now()))
}

fun main() {
// run {
// val p = Person(Name("Joe", "Bloggs"), 42)
// println(pprint(p))
// }

// run {
// val p = Customer(Name("Joe", "Bloggs"), listOf(Address("foo", 123), Address("bar", 456), Address("baz", 789)))
// println(pprint(p))
// }

// run {
// val p =
// ComplexCustomer(
// Name("Joe", "Bloggs"),
// mapOf(Alias("Primary") to Address("foo", 123), Alias("Secondary") to Address("bar", 456), Alias("Tertiary") to Address("baz", 789))
// )
// println(pprint(p))
// }

// run {
// val p =
// VeryComplexCustomer(
// Name("Joe", "Bloggs"),
// mapOf(
// Alias("Primary") to
// listOf(Address("foo", 123), Address("foo1", 123), Address("foo2", 123)),
// Alias("Secondary") to
// listOf(Address("bar", 456), Address("bar1", 456), Address("bar2", 456)),
// Alias("Tertiary") to
// listOf(Address("baz", 789), Address("baz1", 789), Address("baz2", 789))
// )
// )
// println(pprint(p))
// }

run {
var i = 0
val p = SequenceHolder(generateSequence { "foo-${i++}" })
println(pprint(p, defaultHeight = 10).plainText)
}
ex7()


//val seq = generateSequence { "foo" }
////val joined = Str.join(PPrinter.BlackWhite.tokenize(seq, height = 5).iterator())
Expand Down

0 comments on commit ae95a60

Please sign in to comment.