Skip to content

Commit

Permalink
Merge pull request #1 from scottrhoyt/custom_fences
Browse files Browse the repository at this point in the history
Added custom fences
  • Loading branch information
scottrhoyt committed Feb 6, 2016
2 parents 9c7db78 + c6867a1 commit 13ff957
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 10 deletions.
11 changes: 7 additions & 4 deletions Source/SwiftyTextTable/TextTable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ public struct TextTableColumn {

public struct TextTable {
private var columns: [TextTableColumn]
public var columnFence = "|"
public var rowFence = "-"
public var cornerFence = "+"

public init(columns: [TextTableColumn]) {
self.columns = columns
Expand All @@ -62,11 +65,11 @@ public struct TextTable {

public func render() -> String {
let separator = fence(columns.map({ column in
Repeat(count: column.width + 2, repeatedValue: "-").joinWithSeparator("")
}), separator: "+")
let header = fence(columns.map({ " \($0.header.withPadding($0.width)) " }), separator: "|")
Repeat(count: column.width + 2, repeatedValue: rowFence).joinWithSeparator("")
}), separator: cornerFence)
let header = fence(columns.map({ " \($0.header.withPadding($0.width)) " }), separator: columnFence)
let values = (0..<columns.first!.values.count).map({ rowIndex in
fence(columns.map({ " \($0.values[rowIndex].withPadding($0.width)) " }), separator: "|")
fence(columns.map({ " \($0.values[rowIndex].withPadding($0.width)) " }), separator: columnFence)
}).joinWithSeparator("\n")
return [separator, header, separator, values, separator].joinWithSeparator("\n")
}
Expand Down
35 changes: 29 additions & 6 deletions Source/SwiftyTextTableTests/SwiftyTextTableTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,21 @@ import XCTest
import SwiftyTextTable

class SwiftyTextTableTests: XCTestCase {
// protocol XCTestCaseProvider
lazy var allTests: [(String, () throws -> Void)] = [
("testRender", self.testRender)
]

func testRender() {
var table: TextTable!

override func setUp() {
super.setUp()
let foo = TextTableColumn(header: "foo")
let bar = TextTableColumn(header: "bar")
let baz = TextTableColumn(header: "baz")
var table = TextTable(columns: [foo, bar, baz])
table = TextTable(columns: [foo, bar, baz])
table.addRow("1", "2")
table.addRow(11, 22, 33)
table.addRow(111, 222, 333, 444)
}

func testRenderDefault() {
let output = table.render()
let expected = "+-----+-----+-----+\n" +
"| foo | bar | baz |\n" +
Expand All @@ -33,4 +35,25 @@ class SwiftyTextTableTests: XCTestCase {
"+-----+-----+-----+"
XCTAssertEqual(output, expected)
}

func testRenderCustom() {
table.columnFence = "!"
table.rowFence = "*"
table.cornerFence = "."
let output = table.render()
let expected = ".*****.*****.*****.\n" +
"! foo ! bar ! baz !\n" +
".*****.*****.*****.\n" +
"! 1 ! 2 ! !\n" +
"! 11 ! 22 ! 33 !\n" +
"! 111 ! 222 ! 333 !\n" +
".*****.*****.*****."
XCTAssertEqual(output, expected)
}

// MARK: - protocol XCTestCaseProvider for SPM
lazy var allTests: [(String, () throws -> Void)] = [
("testRenderDefault", self.testRenderDefault),
("testRenderCustom", self.testRenderCustom)
]
}

0 comments on commit 13ff957

Please sign in to comment.