Coder Social home page Coder Social logo

marcelmay / kformat Goto Github PK

View Code? Open in Web Editor NEW
37.0 3.0 4.0 250 KB

KFormat is a tiny Kotlin library for formatted text output such as printing values in a structured table

License: Apache License 2.0

Kotlin 100.00%
kotlin kotlin-jvm formatter formatted-text kotlin-dsl

kformat's People

Contributors

dependabot[bot] avatar marcelmay avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

kformat's Issues

Support left margin

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

Support to print NULL values

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"
  }

Support rendering unformatted line

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 header lines

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 prefix for column values

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

Recommend targeting older Kotlin bytecode

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?

Support providing additional formatting flags

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()

Make table header immutable

Avoids issues with inconsistent index etc.

Breaking change: table.header does not return internal, mutable header collection.

Rows with different content causes table failure

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.

Fix empty table (line but no data rows) rendering

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.