Giter VIP home page Giter VIP logo

swiftytexttable's Introduction

SwiftyTextTable

A lightweight Swift library for generating text tables.

Build Status codecov.io Carthage compatible Swift Package Manager compatible CocoaPods Platform OS X + Linux Language Swift 4.0

Example

Swift Language Support

SwiftyTextTable is now Swift 4.0 compatible! The last release to support Swift 3.1 was 0.7.1. The last release to support Swift 2.3 was 0.3.1.

Installation

Carthage (OS X)

You can use Carthage to install SwiftyTextTable by adding it to your Cartfile:

github "scottrhoyt/SwiftyTextTable"

Swift Package Manager (OS X + Linux)

You can use The Swift Package Manager to install SwiftyTextTable by adding the proper description to your Package.swift file:

import PackageDescription

let package = Package(
    name: "<YOUR_PROJECT_NAME>",
    dependencies: [
        .package(url: "https://github.com/scottrhoyt/SwiftyTextTable.git", from: "0.5.0")
    ]
)

CocoaPods (OS X)

You can use CocoaPods to install SwiftyTextTable by adding it to your Podfile:

pod 'SwiftyTextTable'

Manual

Simply copy the *.swift files from the Source/SwiftyTextTable directory into your project.

Usage

import SwiftyTextTable

// First create some columns
let foo = TextTableColumn(header: "foo")
let bar = TextTableColumn(header: "bar")
let baz = TextTableColumn(header: "baz")

// Then create a table with the columns
var table = TextTable(columns: [foo, bar, baz])

// Then add some rows
table.addRow([1, 2, 3])
table.addRow([11, 22, 33])

// Then render the table and use
let tableString = table.render()
print(tableString)

/*
+-----+-----+-----+
| foo | bar | baz |
+-----+-----+-----+
| 1   | 2   | 3   |
| 11  | 22  | 33  |
+-----+-----+-----+
*/

// Put a header on the table if you'd like
table.header = "my foo table"
print(table.render())

/*
+-----------------+
| my foo table    |
+-----------------+
| foo | bar | baz |
+-----+-----+-----+
| 1   | 2   | 3   |
| 11  | 22  | 33  |
+-----+-----+-----+
*/

Any CustomStringConvertible can be used for row values.

Creating Tables from Arrays of Objects with TextTableRepresentable

Let's say you have an array of objects that looks this:

enum AnimalType: String, CustomStringConvertible {
    case dog = "Dog"
    case cat = "Cat"
    case gorilla = "Gorilla"

    var description: String {
        return self.rawValue
    }
}

struct Pet {
    let type: AnimalType
    let name: String
    let canHazPizza: Bool
}

let furball = Pet(type: .cat, name: "Furball", canHazPizza: false)
let bestFriend = Pet(type: .dog, name: "Best Friend", canHazPizza: true)
let scary = Pet(type: .gorilla, name: "Scary", canHazPizza: true)
let pets = [furball, bestFriend, scary]

Now you want to print a table containing your pets. You can accomplish this by having Pet conform to TextTableRepresentable:

extension Pet: TextTableRepresentable {
    static var columnHeaders: [String] {
        return ["Name", "Animal", "Can Haz Pizza?"]
    }

    var tableValues: [CustomStringConvertible] {
        return [name, type, canHazPizza ? "yes" : "no"]
    }

    // Optional
    static var tableHeader: String? {
      return "My Pets"
    }
}

You can now print a table of your pets simply:

print(pets.renderTextTable())

/*
+----------------------------------------+
| My Pets                                |
+----------------------------------------+
| Name        | Animal  | Can Haz Pizza? |
+-------------+---------+----------------+
| Furball     | Cat     | no             |
| Best Friend | Dog     | yes            |
| Scary       | Gorilla | yes            |
+-------------+---------+----------------+
*/

Fence Custimization

You can also customize the output of TextTable.render() by using different values for columnFence, rowFence, and cornerFence.

table.columnFence = ":"
table.rowFence = "."
table.cornerFence = "."

print(table.render())

/*
...................
: foo : bar : baz :
...................
: 1   : 2   :     :
: 11  : 22  : 33  :
...................
*/

Row Padding/Truncation

When adding rows, TextTable will automatically pad the rows with empty strings when there are fewer values than columns. TextTable will also disregard all values over the column count.

let foo = TextTableColumn(header: "foo")
let bar = TextTableColumn(header: "bar")
let baz = TextTableColumn(header: "baz")

var table = TextTable(columns: [foo, bar, baz])

table.addRow([1, 2])
table.addRow([11, 22, 33])
table.addRow([111, 222, 333, 444])

let tableString = table.render()
print(tableString)

/*
+-----+-----+-----+
| foo | bar | baz |
+-----+-----+-----+
| 1   | 2   |     |
| 11  | 22  | 33  |
| 111 | 222 | 333 |
+-----+-----+-----+
*/

Console Formatting Support

SwiftyTextTable will recognize many console escape sequences used to format output (e.g. Rainbow) and account for them in constructing the table.

API Reference

Check out the full API reference here.

License

SwiftyTextTable is released under the MIT License.

swiftytexttable's People

Contributors

eneko avatar jpsim avatar norio-nomura avatar roslund avatar scottrhoyt 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  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  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  avatar  avatar  avatar  avatar  avatar  avatar  avatar

swiftytexttable's Issues

Multi-Byte Unicode Characters not counted correctly

I found that multi-byte unicode characters are counted incorrectly resulting in wrong column widths:

+----------------------------------------+-------------+-----------------+
| FILENAME                               | STATUS      | TIMESTAMP       |
+----------------------------------------+-------------+-----------------+
| 234212640FW20_0716_C_FRONT_WHL_096.jpg | ❇️ DELIVERED | 17.06.20, 01:57 |
| 230257732FW20_0013_C_FRONT_BLL_019.jpg | ❇️ DELIVERED | 22.06.20, 19:54 |
| 230306780BLAA_038.jpg                  | ❇️ DELIVERED | 16.06.20, 12:00 |
| 230306780FW20_0290_C_FRONT_BKER.jpg    | ❇️ DELIVERED | 17.06.20, 01:58 |
+----------------------------------------+-------------+-----------------+

Improve support for escape sequences to support Rainbow.

Hi, just stumbled across SwiftyTextTable and wanted to use it for a project. Thanks for building it!

Unfortunately it seems SwiftTextTable is unable to calculate the correct size for a column when trying to apply additional information to the strings via something like onevcat/Rainbow.

I'm getting this for example when applying the color red to the string foo:

+--------------+-----+-----+
| foo | bar | baz |
+--------------+-----+-----+
| 1            | 2   | 3   |
| 11           |     | 33  |
| 111          | 222 | 333 |
+--------------+-----+-----+

I'm guessing it's because of this, seeing how color information in the terminal is represented through escape sequences that are counted as additional length in this case. Is there any way one could work around this?

Updated Version

Hi Team,

Is there any support available for this library.Any upcoming releases planned?

Right alignment?

All the values are left aligned. Does it support right alignment?

Thanks!

Carthage failing on CI builds (Xcode 9.4 required)

Just noticed CI builds failing on Travis CI because of Carthage now requiring Xcode 9.4.

Carthage installed. Upgrading if neccessary.
carthage (0.25.0) < 0.31.1
==> Upgrading 1 outdated package:
carthage 0.25.0 -> 0.31.1
carthage: A full installation of Xcode.app 9.4 is required to compile this software.
Installing just the Command Line Tools is not sufficient.
Xcode 9.4 cannot be installed on macOS 10.12.
You must upgrade your version of macOS.
Error: carthage: An unsatisfied requirement failed this build.
The command "./scripts/upstall-carthage.sh" failed and exited with 1 during .

I believe this would be fixed by requiring Xcode 9.4 on the .travis.yml file. @scottrhoyt let me know your thoughts, I can submit a PR for that.

Swift 4.2 Carthage

I'm trying to use SwiftyTextTable using Carthage on macOS Mojave, with Xcode 10 that uses Swift 4.2, and I'm getting the following error:

*** Cloning SwiftyTextTable
*** Checking out SwiftyTextTable at "0.8.2"
*** xcodebuild output can be found in /var/folders/9t/kjmwgng518vcjnczl_0j9h780000gn/T/carthage-xcodebuild.R3VNUw.log
*** Downloading SwiftyTextTable.framework binary at "0.8.2"
***  Skipped installing SwiftyTextTable.framework binary due to the error:
	"Incompatible Swift version - framework was built with 4.1.2 (swiftlang-902.0.54 clang-902.0.39.2) and the local version is 4.2.1 (swiftlang-1000.11.42 clang-1000.11.45.1)."

    Falling back to building from the source
*** Building scheme "SwiftyTextTable" in SwiftyTextTable.xcodeproj

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.