Skip to content

Commit

Permalink
Refactor and add ascii delimiter option
Browse files Browse the repository at this point in the history
  • Loading branch information
snowphone committed Jun 5, 2021
1 parent 626ce9f commit a3ee59b
Show file tree
Hide file tree
Showing 10 changed files with 94 additions and 32 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ That is why I started this project.
<dependency>
<groupId>com.github.snowphone</groupId>
<artifactId>cjk-table</artifactId>
<version>0.3</version>
<version>0.4</version>
</dependency>
```

Expand All @@ -31,7 +31,7 @@ repositories {
maven { url="https://jitpack.io".let(::uri) }
}
dependencies {
implementation("com.github.snowphone:cjk-table:0.3")
implementation("com.github.snowphone:cjk-table:0.4")
}
```

Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>kr.sixtyfive</groupId>
<artifactId>cjk-table</artifactId>
<version>0.3</version>
<version>0.4</version>

<licenses>
<license>
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package kr.sixtyfive

object AlignLeft : Alignment {
object LeftAlign : Alignment {
override fun pad(): (String, Int, Char) -> String {
return String::padEnd
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package kr.sixtyfive

object AlignRight : Alignment {
object RightAlign : Alignment {
override fun pad(): (String, Int, Char) -> String {
return String::padStart
}
Expand Down
10 changes: 10 additions & 0 deletions src/main/kotlin/kr/sixtyfive/Delimiter/AsciiDelimiter.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package kr.sixtyfive

object AsciiDelimiter : Delimiter {
override val vertical = '|'
override val horizontal = '-'
override val cross = '+'
override val topCross = '+'
override val bottomCross = '+'
}

10 changes: 10 additions & 0 deletions src/main/kotlin/kr/sixtyfive/Delimiter/Delimiter.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package kr.sixtyfive

interface Delimiter {
val vertical: Char
val horizontal: Char
val cross: Char
val topCross: Char
val bottomCross: Char
}

10 changes: 10 additions & 0 deletions src/main/kotlin/kr/sixtyfive/Delimiter/FancyDelimiter.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package kr.sixtyfive

object FancyDelimiter : Delimiter {
override val vertical = ''
override val horizontal = ''
override val cross = ''
override val topCross = ''
override val bottomCross = ''
}

31 changes: 16 additions & 15 deletions src/main/kotlin/kr/sixtyfive/Table.kt
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
package kr.sixtyfive


/**
* @param alignment A rule for an alignment. Either of `AlignLeft` or `AlignRight` is allowed.
*/
class Table(
alignment: Alignment = AlignRight
alignment: Alignment = RightAlign,
delimiterSet: Delimiter = FancyDelimiter
) {
private val delimiter = ''
private val horizontalLine = ''
private val cross = ''
private val topCross = ''
private val bottomCross = ''
private val delimiter = delimiterSet.vertical
private val horizontalLine = delimiterSet.horizontal
private val cross = delimiterSet.cross
private val topCross = delimiterSet.topCross
private val bottomCross = delimiterSet.bottomCross

private val table = mutableListOf<List<String>>()
private val lineIndices = mutableSetOf<Int>()
Expand All @@ -38,7 +38,7 @@ class Table(
*/
fun render(): String {
val colLen = table.map { it.size }.maxOrNull() ?: 0
val widths = (0 until colLen).map(this::calcWidth).toList()
val widths = (0 until colLen).map(this::calculateWidth).toList()
val builder = StringBuilder()
(0..table.size).forEach { r ->
if (r in lineIndices) {
Expand Down Expand Up @@ -79,17 +79,18 @@ class Table(
return builder.toString()
}

private fun calcWidth(colIndex: Int): Int {
/**
* Return maximum length in `colIndex`th column.
* Note that the length is in half-width.
*/
private fun calculateWidth(colIndex: Int): Int {
val vertLine = table.map { if (colIndex in it.indices) it[colIndex] else "" }
return (vertLine.map(this::len).maxOrNull() ?: 0)
return (vertLine.map(this::strLenInHalfWidth).maxOrNull() ?: 0)
}

/**
* Return length by bytes (half-width)
*/
private fun len(s: String): Int {
private fun strLenInHalfWidth(s: String): Int {
return s.map {
if (it.isFullWidth()) 2 else 1
}.sum()
}
}
}
55 changes: 43 additions & 12 deletions src/test/kotlin/kr/sixtyfive/TableTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,15 @@ import kotlin.test.Test
import kotlin.test.assertEquals

internal class TableTest {
@Test
fun testLength() {
val obj = Table()
val method = obj.javaClass.getDeclaredMethod("strLenInHalfWidth", String::class.java)
method.isAccessible = true

assertEquals(7, method.invoke(obj, "안녕 hi"))
}

@Test
fun chainCall() {
val expected =
Expand All @@ -29,11 +38,11 @@ internal class TableTest {
}

@Test
fun twoColumns() {
fun twoColumnsWithLeftAlignment() {
val expected =
"ID│ Name\n" +
"10│한국어\n"
val table = Table()
"ID│Name \n" +
"10│한국어\n"
val table = Table(LeftAlign)
table.addRow("ID", "Name")
table.addRow(10, "한국어")

Expand All @@ -44,32 +53,54 @@ internal class TableTest {
fun oneColumnHorizontalLine() {
val expected =
"Language\n" +
"────────\n" +
" 한국어\n"
"────────\n" +
" 한국어\n"

val table = Table()
val table = Table(RightAlign)
table.addRow("Language")
table.addLine()
table.addRow("한국어")

assertEquals(expected, table.render())
}

@Test
fun asciiDelimiter() {
val expected =
"--+------\n" +
"ID| Name\n" +
"--+------\n" +
"10|한국어\n" +
"--+------\n"
val table = Table(delimiterSet = AsciiDelimiter)
table.addLine()
table.addRow("ID", "Name")
table.addLine()
table.addRow("10", "한국어")
table.addLine()

print(table.render())

assertEquals(expected, table.render())
}

@Test
fun twoColumnsHorizontalLine() {
val expected =
"──┬──────\n" +
"ID│ Name\n" +
"──┼──────\n" +
"10│한국어\n" +
"──┴──────\n"
"ID│ Name\n" +
"──┼──────\n" +
"10│한국어\n" +
"──┴──────\n"
val table = Table()
table.addLine()
table.addRow("ID", "Name")
table.addLine()
table.addRow("10", "한국어")
table.addLine()

print(table.render())

assertEquals(expected, table.render())
}

Expand All @@ -84,4 +115,4 @@ internal class TableTest {

assertEquals(expected, table.render())
}
}
}

0 comments on commit a3ee59b

Please sign in to comment.