Skip to content

Commit

Permalink
Reorganize file.
Browse files Browse the repository at this point in the history
  • Loading branch information
scottrhoyt committed Jan 1, 2017
1 parent 69c0d15 commit 6c8a149
Showing 1 changed file with 96 additions and 94 deletions.
190 changes: 96 additions & 94 deletions Source/SwiftyTextTable/TextTable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,78 +31,7 @@ private extension String {
}
}

// MARK: Helper Extensions

private extension String {
func withPadding(count: Int) -> String {
let length = characters.count
if length < count {
return self +
repeatElement(" ", count: count - length).joined()
}
return self
}

func strippedLength() -> Int {
return stripped().characters.count
}
}

private extension Array where Element: CustomStringConvertible {
func paragraph() -> String {
return self.map({ $0.description }).joined(separator: "\n")
}
}

// MARK: - TextTable Protocols

/// A protocol used to create a `TextTable` from an object.
public protocol TextTableObject {

/// The text table header.
static var tableHeader: String? { get }

/// An array column headers to represent this object's data.
static var columnHeaders: [String] { get }

/// The values to render in the text table. Should have the same count as `columnHeaders`.
var tableValues: [CustomStringConvertible] { get }
}

public extension TextTableObject {
/// Returns `nil`.
static var tableHeader: String? {
return nil
}
}

private func fence(strings: [String], separator: String) -> String {
return separator + strings.joined(separator: separator) + separator
}

/// Represents a column in a `TextTable`.
public struct TextTableColumn {

/// The header for the column.
public var header: String

/// The values contained in this column. Each value represents another row.
fileprivate var values: [String] = []

/// Initialize a new column for inserting into a `TextTable`.
public init(header: String) {
self.header = header
}

/**
The minimum width of the column needed to accomodate all values in this column.
- Complexity: O(n)
*/
public var width: Int {
// FIXME: This should probably be a function because of it's O(n) complexity.
return max(header.strippedLength(), values.reduce(0) { max($0, $1.strippedLength()) })
}
}
// MARK: - TextTable structures

/// Used to create a tabular representation of data.
public struct TextTable {
Expand All @@ -123,36 +52,36 @@ public struct TextTable {
public var header: String? = nil

/**
Create a new `TextTable` from `TextTableColumn`s.
Create a new `TextTable` from `TextTableColumn`s.

- parameters:
- columns: An `Array` of `TextTableColumn`s.
- header: The table header. Defaults to `nil`.
*/
- parameters:
- columns: An `Array` of `TextTableColumn`s.
- header: The table header. Defaults to `nil`.
*/
public init(columns: [TextTableColumn], header: String? = nil) {
self.columns = columns
self.header = header
}

/**
Create a new `TextTable` from an `TextTableObject`s.
Create a new `TextTable` from an `TextTableObject`s.

- parameters:
- objects: An `Array` of `TextTableObject`s.
- header: The table header. Defaults to `nil`.
*/
- parameters:
- objects: An `Array` of `TextTableObject`s.
- header: The table header. Defaults to `nil`.
*/
public init<T: TextTableObject>(objects: [T], header: String? = nil) {
self.header = header ?? T.tableHeader
columns = T.columnHeaders.map { TextTableColumn(header: $0) }
objects.forEach { addRow(values: $0.tableValues) }
}

/**
Add a row to the table.
Add a row to the table.

- parameters:
- values: The values contained in the new row.
*/
- parameters:
- values: The values contained in the new row.
*/
public mutating func addRow(values: [CustomStringConvertible]) {
let values = values.count >= columns.count ? values :
values + [CustomStringConvertible](repeating: "", count: columns.count - values.count)
Expand All @@ -165,10 +94,10 @@ public struct TextTable {
}

/**
Render the table to a `String`.
Render the table to a `String`.

- returns: The `String` representation of the table.
*/
- returns: The `String` representation of the table.
*/
public func render() -> String {
let separator = fence(strings: columns.map({ column in
return repeatElement(rowFence, count: column.width + 2).joined()
Expand All @@ -186,10 +115,10 @@ public struct TextTable {
}

/**
Render the table's header to a `String`.
Render the table's header to a `String`.

- returns: The `String` representation of the table header. `nil` if `header` is `nil`.
*/
- returns: The `String` representation of the table header. `nil` if `header` is `nil`.
*/
private func renderTableHeader() -> String? {
guard let header = header else {
return nil
Expand All @@ -198,9 +127,82 @@ public struct TextTable {
let calculateWidth: (Int, TextTableColumn) -> Int = { $0 + $1.width + 2 }
let separator = cornerFence +
repeatElement(rowFence, count: columns.reduce(0, calculateWidth) + columns.count - 1).joined() +
cornerFence
cornerFence
let title = fence(strings: [" \(header.withPadding(count: separator.characters.count - 4)) "], separator: columnFence)

return [separator, title, separator].paragraph()
}
}

/// Represents a column in a `TextTable`.
public struct TextTableColumn {

/// The header for the column.
public var header: String

/// The values contained in this column. Each value represents another row.
fileprivate var values: [String] = []

/// Initialize a new column for inserting into a `TextTable`.
public init(header: String) {
self.header = header
}

/**
The minimum width of the column needed to accomodate all values in this column.
- Complexity: O(n)
*/
public var width: Int {
// FIXME: This should probably be a function because of it's O(n) complexity.
return max(header.strippedLength(), values.reduce(0) { max($0, $1.strippedLength()) })
}
}

// MARK: - TextTableObject

/// A protocol used to create a `TextTable` from an object.
public protocol TextTableObject {

/// The text table header.
static var tableHeader: String? { get }

/// An array column headers to represent this object's data.
static var columnHeaders: [String] { get }

/// The values to render in the text table. Should have the same count as `columnHeaders`.
var tableValues: [CustomStringConvertible] { get }
}

public extension TextTableObject {
/// Returns `nil`.
static var tableHeader: String? {
return nil
}
}

private func fence(strings: [String], separator: String) -> String {
return separator + strings.joined(separator: separator) + separator
}

// MARK: - Helper Extensions

private extension String {
func withPadding(count: Int) -> String {
let length = characters.count
if length < count {
return self +
repeatElement(" ", count: count - length).joined()
}
return self
}

func strippedLength() -> Int {
return stripped().characters.count
}
}

private extension Array where Element: CustomStringConvertible {
func paragraph() -> String {
return self.map({ $0.description }).joined(separator: "\n")
}
}

0 comments on commit 6c8a149

Please sign in to comment.