Skip to content

Commit

Permalink
Center align, perf & fix stuck width
Browse files Browse the repository at this point in the history
  • Loading branch information
cfilipov committed Aug 13, 2016
1 parent cd6fd6f commit 0fdbf90
Show file tree
Hide file tree
Showing 24 changed files with 202 additions and 180 deletions.
16 changes: 14 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
# Change Log

## [1.0.0-alpha.1](https://github.com/cfilipov/TextTable/releases/tag/v1.0.0-alpha.1)

* Support for center alignment.
* If all columns have explicit width, then width calculations are skipped.
* Escape strings in certain formats (HTML & Latex, for example).
* Fixed: `TextTable` config persists calculated column widths. This results in widths getting stuck from the first call to `print` or `string(for:)`.

##### Known Issues in 1.0.0-alpha.1

* Very little effort has but put into performance optimizations.
* It should be possible to create columns without headers, but this hasn't been tested and likely doesn't work yet.

## [1.0.0-alpha.0](https://github.com/cfilipov/TextTable/releases/tag/v1.0.0-alpha.0)

* Breaking change: completely re-written API. No more conforming to a protocol, instead a `TextTable` class is used in similar to `NSFormatter`. This offers much more flexibility.
* Breaking change: completely re-written API. No more conforming to a protocol, instead a `TextTable` class is used in a similar way to `NSFormatter`. This offers much more flexibility.
* Many more output formats. Similar to what you get from Python's [tabulate](https://pypi.python.org/pypi/tabulate) lib.

### Known Issues in 1.0.0-alpha.0
##### Known Issues in 1.0.0-alpha.0

* No attempt at optimizing performance has been made yet. Even when widths are provided, an expensive calculation is still performed.
* No escaping is being done. None of the formatters even attempt to sanitize the input strings.
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Easily print textual tables in Swift. Inspired by the Python [tabulate](https://

Latest Version |
---------------- |
[1.0.0-alpha.0](https://github.com/cfilipov/TextTable/releases/tag/v1.0.0-alpha.0) |
[1.0.0-alpha.1](https://github.com/cfilipov/TextTable/releases/tag/v1.0.0-alpha.1) |

## Features

Expand Down Expand Up @@ -128,7 +128,7 @@ For a full list of supported formats see [Supported Formats](#supported-formats)

### Column-Specific Formatters

You can specify a [Formatter](https://developer.apple.com/reference/foundation/nsformatter) on a per-column basis. In this example a `DateFormatter` is used to customize the display of the `Birthday` column.
You can specify a [Formatter](https://developer.apple.com/reference/foundation/nsformatter) on a per-column basis. In this example a [DateFormatter](https://developer.apple.com/reference/foundation/nsdateformatter) is used to customize the display of the `Birthday` column.

```Swift
let dateFormatter = DateFormatter()
Expand Down Expand Up @@ -216,7 +216,6 @@ TextTable supports most of the formats as [tabulate](https://pypi.python.org/pyp
### Plain

```
> table.print(data) // or..
> table.print(data, format: Format.Plain())
Name Age Birthday
Expand All @@ -230,6 +229,7 @@ Eve 142 8/13/16
`Simple` is the default format. It corresponds to `simple_tables` in [Pandoc](http://pandoc.org/) Markdown extensions.

```
> table.print(data) // or..
> table.print(data, format: Format.Simple())
Name Age Birthday
Expand Down
17 changes: 7 additions & 10 deletions Sources/FancyGridFormat.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,15 @@ extension Format {

public var string: String = ""
public var width: Int? = nil
public var alignment: Alignment? = nil
public var row: Int = 0
public var align: Alignment = .left

private var row: Int = 0
private var contentStack: [String] = []

public static func escape(_ s: String) -> String {
return s
}

public func beginTable() { }

public func endTable() {
Expand Down Expand Up @@ -79,15 +83,8 @@ extension Format {
public func endColumn() { }

public func content(_ s: String) {
let pad: PaddingFunction
switch alignment {
case .some(.left): pad = s.rightpad
case .some(.right): pad = s.leftpad
case .some(.center): fatalError()
case .none: pad = s.leftpad
}
if let width = width {
contentStack.append(pad(length: width, character: " "))
contentStack.append(s.pad(align, length: width))
} else {
contentStack.append(s)
}
Expand Down
15 changes: 6 additions & 9 deletions Sources/GridFormat.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,14 @@ extension Format {

public var string: String = ""
public var width: Int? = nil
public var alignment: Alignment? = nil
public var align: Alignment = .left

private var contentStack: [String] = []

public static func escape(_ s: String) -> String {
return s
}

public func beginTable() { }
public func endTable() { }
public func beginHeaderRow() { }
Expand Down Expand Up @@ -64,15 +68,8 @@ extension Format {
public func endColumn() { }

public func content(_ s: String) {
let pad: PaddingFunction
switch alignment {
case .some(.left): pad = s.rightpad
case .some(.right): pad = s.leftpad
case .some(.center): fatalError()
case .none: pad = s.leftpad
}
if let width = width {
contentStack.append(pad(length: width, character: " "))
contentStack.append(s.pad(align, length: width))
} else {
contentStack.append(s)
}
Expand Down
21 changes: 12 additions & 9 deletions Sources/HtmlFormat.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,14 @@ extension Format {

public var string: String = ""
public var width: Int? = nil
public var alignment: Alignment? = nil
public var align: Alignment = .left

public static func escape(_ s: String) -> String {
return s
.replacingOccurrences(of: "&", with: "&")
.replacingOccurrences(of: "<", with: "&lt;")
.replacingOccurrences(of: "<", with: "&gt;")
}

public func beginTable() {
string.append("<table>\n")
Expand Down Expand Up @@ -43,14 +50,10 @@ extension Format {
public func beginHeaderColumn() {
string.append("<")
string.append("th")
if alignment != nil || width != nil {
string.append(" ")
string.append("style=\"")
if let alignment = alignment {
string.append("text-align:\(alignment.rawValue);")
}
string.append("\"")
}
string.append(" ")
string.append("style=\"")
string.append("text-align:\(align.rawValue);")
string.append("\"")
string.append(">")
}

Expand Down
29 changes: 16 additions & 13 deletions Sources/LatexFormat.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,24 @@ extension Format {

public var string: String = ""
public var width: Int? = nil
public var alignment: Alignment? = nil
public var align: Alignment = .left

private var contentStack: [String] = []

public static func escape(_ s: String) -> String {
return s
.replacingOccurrences(of: "#", with: "\\#")
.replacingOccurrences(of: "$", with: "\\$")
.replacingOccurrences(of: "%", with: "\\%")
.replacingOccurrences(of: "&", with: "\\&")
.replacingOccurrences(of: "\\", with: "\\textbackslash{}")
.replacingOccurrences(of: "^", with: "\\textasciicircum{}")
.replacingOccurrences(of: "_", with: "\\_")
.replacingOccurrences(of: "{", with: "\\{")
.replacingOccurrences(of: "}", with: "\\}")
.replacingOccurrences(of: "~", with: "\\textasciitilde{}")
}

public func beginTable() {
string.append("\\begin{tabular}{lr}\n")
}
Expand Down Expand Up @@ -54,18 +68,7 @@ extension Format {
public func endColumn() { }

public func content(_ s: String) {
let pad: PaddingFunction
switch alignment {
case .some(.left): pad = s.rightpad
case .some(.right): pad = s.leftpad
case .some(.center): fatalError()
case .none: pad = s.leftpad
}
if let width = width {
contentStack.append(pad(length: width, character: " "))
} else {
contentStack.append(s)
}
contentStack.append(s)
}
}
}
15 changes: 6 additions & 9 deletions Sources/OrgFormat.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,14 @@ extension Format {

public var string: String = ""
public var width: Int? = nil
public var alignment: Alignment? = nil
public var align: Alignment = .left

private var contentStack: [String] = []

public static func escape(_ s: String) -> String {
return s
}

public func beginTable() { }
public func endTable() { }
public func beginHeaderRow() { }
Expand Down Expand Up @@ -52,15 +56,8 @@ extension Format {
public func endColumn() { }

public func content(_ s: String) {
let pad: PaddingFunction
switch alignment {
case .some(.left): pad = s.rightpad
case .some(.right): pad = s.leftpad
case .some(.center): fatalError()
case .none: pad = s.leftpad
}
if let width = width {
contentStack.append(pad(length: width, character: " "))
contentStack.append(s.pad(align, length: width))
} else {
contentStack.append(s)
}
Expand Down
26 changes: 11 additions & 15 deletions Sources/PipeFormat.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,16 @@ extension Format {

public var string: String = ""
public var width: Int? = nil
public var alignment: Alignment? = nil
public var align: Alignment = .left

private var isHeader: Bool = false
private var contentStack: [String] = []
private var headerStack: [String] = []

public static func escape(_ s: String) -> String {
return s
}

public func beginTable() { }
public func endTable() { }

Expand Down Expand Up @@ -58,24 +62,16 @@ extension Format {
public func endColumn() { }

public func content(_ s: String) {
let pad: PaddingFunction
switch alignment {
case .some(.left): pad = s.rightpad
case .some(.right): pad = s.leftpad
case .some(.center): fatalError()
case .none: pad = s.leftpad
}
let w = max(width!, 3)
contentStack.append(pad(length: w, character: " "))
contentStack.append(s.pad(align, length: w))
if isHeader {
switch alignment {
case .some(.left):
switch align {
case .left:
headerStack.append(":" + String(repeating: Character("-"), count: w+1))
case .some(.right):
case .right:
headerStack.append(String(repeating: Character("-"), count: w+1) + ":")
case .some(.center):
headerStack.append(":" + String(repeating: Character("-"), count: w-2) + ":")
case .none: fatalError()
case .center:
headerStack.append(":" + String(repeating: Character("-"), count: w) + ":")
}
}
}
Expand Down
15 changes: 6 additions & 9 deletions Sources/PlainFormat.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,14 @@ extension Format {

public var string: String = ""
public var width: Int? = nil
public var alignment: Alignment? = nil
public var align: Alignment = .left

private var contentStack: [String] = []

public static func escape(_ s: String) -> String {
return s
}

public func beginTable() { }
public func endTable() { }
public func beginHeaderRow() { }
Expand All @@ -42,15 +46,8 @@ extension Format {
public func endColumn() { }

public func content(_ s: String) {
let pad: PaddingFunction
switch alignment {
case .some(.left): pad = s.rightpad
case .some(.right): pad = s.leftpad
case .some(.center): fatalError()
case .none: pad = s.leftpad
}
if let width = width {
contentStack.append(pad(length: width, character: " "))
contentStack.append(s.pad(align, length: width))
} else {
contentStack.append(s)
}
Expand Down
15 changes: 6 additions & 9 deletions Sources/PsqlFormat.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,14 @@ extension Format {

public var string: String = ""
public var width: Int? = nil
public var alignment: Alignment? = nil
public var align: Alignment = .left

private var contentStack: [String] = []

public static func escape(_ s: String) -> String {
return s
}

public func beginTable() { }

public func endTable() {
Expand Down Expand Up @@ -68,15 +72,8 @@ extension Format {
public func endColumn() { }

public func content(_ s: String) {
let pad: PaddingFunction
switch alignment {
case .some(.left): pad = s.rightpad
case .some(.right): pad = s.leftpad
case .some(.center): fatalError()
case .none: pad = s.leftpad
}
if let width = width {
contentStack.append(pad(length: width, character: " "))
contentStack.append(s.pad(align, length: width))
} else {
contentStack.append(s)
}
Expand Down
15 changes: 6 additions & 9 deletions Sources/RstFormat.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,14 @@ extension Format {

public var string: String = ""
public var width: Int? = nil
public var alignment: Alignment? = nil
public var align: Alignment = .left

private var contentStack: [String] = []

public static func escape(_ s: String) -> String {
return s
}

public func beginTable() { }

public func endTable() {
Expand Down Expand Up @@ -59,15 +63,8 @@ extension Format {
public func endColumn() { }

public func content(_ s: String) {
let pad: PaddingFunction
switch alignment {
case .some(.left): pad = s.rightpad
case .some(.right): pad = s.leftpad
case .some(.center): fatalError()
case .none: pad = s.leftpad
}
if let width = width {
contentStack.append(pad(length: width, character: " "))
contentStack.append(s.pad(align, length: width))
} else {
contentStack.append(s)
}
Expand Down
Loading

0 comments on commit 0fdbf90

Please sign in to comment.