Skip to content

tomieq/AsciiTable

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

AsciiTable

This is simple Swift library for formatting text tables.

Usage

let table = AsciiTable(labels: ["endpoint", "allowed methods", "content-type"])
table.add(["/warhouse", "POST", "application/json"])
table.add(["/shop", "GET", "text/html"])
table.add(["/order", "POST", "application/json"])
table.add(["/admin/auth/v1/token", "GET", "application/json"])
print(table)

Output:

╒════════════════════════╤══════════════════╤═══════════════════╕
│  endpoint              │ allowed methods  │ content-type      │
╞════════════════════════╪══════════════════╪═══════════════════╡
│  /warhouse             │ POST             │ application/json  │
│  /shop                 │ GET              │ text/html         │
│  /order                │ POST             │ application/json  │
│  /admin/auth/v1/token  │ GET              │ application/json  │
└────────────────────────┴──────────────────┴───────────────────┘

Styles

You can configure border style and header divider style.

StyleName.popular

With double header divider:

╒════════════════════════╤══════════════════╤═══════════════════╕
│  endpoint              │ allowed methods  │ content-type      │
╞════════════════════════╪══════════════════╪═══════════════════╡
│  /warhouse             │ POST             │ application/json  │
│  /shop                 │ GET              │ text/html         │
│  /admin/auth/v1/token  │ GET              │ application/json  │
└────────────────────────┴──────────────────┴───────────────────┘

With single header divider:

╒════════════════════════╤══════════════════╤═══════════════════╕
│  endpoint              │ allowed methods  │ content-type      │
├────────────────────────┼──────────────────┼───────────────────┤
│  /warhouse             │ POST             │ application/json  │
│  /shop                 │ GET              │ text/html         │
│  /admin/auth/v1/token  │ GET              │ application/json  │
└────────────────────────┴──────────────────┴───────────────────┘

StyleName.singleBorder

With double header divider:

┌────────────────────────┬──────────────────┬───────────────────┐
│  endpoint              │ allowed methods  │ content-type      │
╞════════════════════════╪══════════════════╪═══════════════════╡
│  /warhouse             │ POST             │ application/json  │
│  /shop                 │ GET              │ text/html         │
│  /admin/auth/v1/token  │ GET              │ application/json  │
└────────────────────────┴──────────────────┴───────────────────┘

With single header divider:

┌────────────────────────┬──────────────────┬───────────────────┐
│  endpoint              │ allowed methods  │ content-type      │
├────────────────────────┼──────────────────┼───────────────────┤
│  /warhouse             │ POST             │ application/json  │
│  /shop                 │ GET              │ text/html         │
│  /admin/auth/v1/token  │ GET              │ application/json  │
└────────────────────────┴──────────────────┴───────────────────┘

StyleName.doubleBorder

With double header divider:

╔════════════════════════╤══════════════════╤═══════════════════╗
║  endpoint              │ allowed methods  │ content-type      ║
╠════════════════════════╪══════════════════╪═══════════════════╣
║  /warhouse             │ POST             │ application/json  ║
║  /shop                 │ GET              │ text/html         ║
║  /admin/auth/v1/token  │ GET              │ application/json  ║
╚════════════════════════╧══════════════════╧═══════════════════╝

With single header divider:

╔════════════════════════╤══════════════════╤═══════════════════╗
║  endpoint              │ allowed methods  │ content-type      ║
╟────────────────────────┼──────────────────┼───────────────────╢
║  /warhouse             │ POST             │ application/json  ║
║  /shop                 │ GET              │ text/html         ║
║  /admin/auth/v1/token  │ GET              │ application/json  ║
╚════════════════════════╧══════════════════╧═══════════════════╝

StyleName.noBorder

With double header divider:

   endpoint              │ allowed methods  │ content-type       
 ════════════════════════╪══════════════════╪═══════════════════ 
   /warhouse             │ POST             │ application/json   
   /shop                 │ GET              │ text/html         
   /admin/auth/v1/token  │ GET              │ application/json   

With single header divider:

   endpoint              │ allowed methods  │ content-type       
 ────────────────────────┼──────────────────┼─────────────────── 
   /warhouse             │ POST             │ application/json   
   /shop                 │ GET              │ text/html          
   /admin/auth/v1/token  │ GET              │ application/json

Dividers

You can add horizontal lines between rows with .addDivider() or .addDoubleDivider()

Sample code:

let table = AsciiTable(labels: ["endpoint", "allowed methods", "content-type"],
                       borderStyle: .popular,
                       headerDivider: .double)
table.add(["/warhouse", "POST", "application/json"])
table.addDivider()
table.add(["/shop", "GET", "text/html"])
table.addDivider()
table.add(["/admin/auth/v1/token", "GET", "application/json"])
print(table)

Output:

╒════════════════════════╤══════════════════╤═══════════════════╕
│  endpoint              │ allowed methods  │ content-type      │
╞════════════════════════╪══════════════════╪═══════════════════╡
│  /warhouse             │ POST             │ application/json  │
├────────────────────────┼──────────────────┼───────────────────┤
│  /shop                 │ GET              │ text/html         │
├────────────────────────┼──────────────────┼───────────────────┤
│  /admin/auth/v1/token  │ GET              │ application/json  │
└────────────────────────┴──────────────────┴───────────────────┘

Swift Package Manager.

import PackageDescription

let package = Package(
    name: "MyServer",
    dependencies: [
        .package(url: "https://github.com/tomieq/AsciiTable", from: "2.0.0")
    ]
)

in the target:

    targets: [
        .executableTarget(
            name: "AppName",
            dependencies: [
                .product(name: "AsciiTable", package: "AsciiTable")
            ])
    ]

About

Swift library for creating table in plain text format

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages