marcelmay / kformat Goto Github PK
View Code? Open in Web Editor NEWKFormat is a tiny Kotlin library for formatted text output such as printing values in a structured table
License: Apache License 2.0
KFormat is a tiny Kotlin library for formatted text output such as printing values in a structured table
License: Apache License 2.0
Center alignment is nice for certain types of fields, e.g. "y/n" for booleans.
Support left margin for indenting.
Example (margin of '. '):
table {
header("A", "B", "C")
row(10, "b1", "c1")
row(20, "b2", "c2")
hints {
borderStyle = Table.BorderStyle.SINGLE_LINE
leftMargin(". ")
}
}.render()
produces
. A | B | C
. ---|----|---
. 10 | b1 | c1
. 20 | b2 | c2
I wanted to use this library to print database tables.
But the issue is that it crashes when the column value is NULL.
It would be great to have the possibility to define null replacement as string defined by hints, like
hints {
nullString = "NULL"
}
Example:
table {
header("A", "B" /* Final label skipped */)
row(10, "b1", 1.5F)
row(20 /* Only one value instead of three */)
}
Introduce a rendering hint key enum for type safety.
Support rendering an unformatted line as a row.
Example:
table {
header("A", "B", "C")
row(10, "b1", "c1")
row(20, "b2", "c2")
line("An unformatted line between")
row(30, "b3", "c3")
hints {
borderStyle = Table.BorderStyle.SINGLE_LINE
}
}.render()
renders
A | B | C
---|----|---
10 | b1 | c1
20 | b2 | c2
An unformatted line between
30 | b3 | c3
Support multiple headers of variable length
table {
header("A1", "B1")
row("a1", "b1")
line()
header("A3", "B3", "C3")
row("a4", "b4", "c3")
hints {
borderStyle = Table.BorderStyle.SINGLE_LINE
}
}
renders
A1 | B1 |
---|----|---
a1 | b1 |
A3 | B3 | C3
---|----|---
a4 | b4 | c3
Support auto prefixing cell values for a column.
Example:
table {
header("A", "B", "C", "D")
row(10, "b1", "c1", "d1")
row(20, "b2", "c2", "d2")
hints {
borderStyle = Table.BorderStyle.SINGLE_LINE
prefix(3, "±")
}
}.render()
renders
A | B | C | D
---|----|----|----
10 | b1 | c1 | ±d1
20 | b2 | c2 | ±d2
The library currently targets Kotlin 1.9 metadata.
Unfortunately for those of us stuck on older versions of Kotlin, this means we cannot use this library. Can you set the Kotlin metadata target to 1.6 for the next version?
Example with new formatFlag(...):
table {
header("A", "B", "C")
row(10, "b1", 1.5F)
row(1, "b1", -1.3333F)
hints {
borderStyle = Table.BorderStyle.SINGLE_LINE
alignment(0, Hints.Alignment.LEFT)
precision(2, 2)
formatFlag(2, "+") // New!
}
}.render()
produces floats with sign prefix:
"""
A | B | C
---|----|------
10 | b1 | +1.50
1 | b1 | -1.33
""".trimIndent()
Update kotlin 1.5.30 -> 1.6.10
Avoids issues with inconsistent index etc.
Breaking change: table.header does not return internal, mutable header collection.
Table without header labels renders an empty line.
Support injecting (predefined) rendering hints.
Example setting rendering hint for leftMargin to '>':
table {
hints(mapOf("*.leftMargin" to ">")) {
...
}
}
I have a case in which I have rows with different content. For example, some rows use a dash for a value if it is not applicable, while other rows may have a numeric value.
It seems like ktformat uses an "example row" to decide what formatting to apply to every row, but this then results in an exception like this:
java.lang.IllegalArgumentException: Can not format value '—' of type 'class java.lang.String 'using format spec '%-8d'
This seems like a common use case that it would be nice to handle elegantly. Perhaps one way to do it is to provide value converters as hints. If such a hint was provided, it would be used to transform the value for display rather than the example row.
The workaround is to ensure all values added to a row are consistent i.e. numeric values are converted to Strings in advance, so that the example row has consistent types with every other row.
java.lang.IllegalStateException: No row found in [Row(values=[Purging oldest snapshot reports (maxSnapshotVersions=3):])] that is not an unformatted 'line'
at de.m3y.kformat.Table.findExampleRow(Table.kt:466)
at de.m3y.kformat.Table.computeColFormats(Table.kt:419)
at de.m3y.kformat.Table.render(Table.kt:307)
at de.m3y.kformat.Table.render$default(Table.kt:305)
table.hasRows()
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.