Swiftpack.co - gallinapassus/Table as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by gallinapassus.
gallinapassus/Table 0.1.9
Simple table
โญ๏ธ 1
๐Ÿ•“ 31 weeks ago
.package(url: "https://github.com/gallinapassus/Table.git", from: "0.1.9")

Tests

Table

Simple table

import Table

let cells:[[Txt]] = [
    ["123",
     Txt("x", alignment: .topLeft),
     Txt("x", alignment: .topCenter),
     Txt("x", alignment: .topRight)],
    ["123",
     Txt("x", alignment: .middleLeft),
     Txt("x", alignment: .middleCenter),
     Txt("x", alignment: .middleRight)],
    ["123",
     Txt("x", alignment: .bottomLeft),
     Txt("x", alignment: .bottomCenter),
     Txt("x", alignment: .bottomRight)],
]
let width:Width = 5

let cols = [
    Col("#", width: 1, defaultAlignment: .topLeft),
    Col("Col 1", width: width, defaultAlignment: .bottomCenter),
    Col("Col 2", width: width, defaultAlignment: .bottomCenter),
    Col("Col 3", width: width, defaultAlignment: .bottomCenter),
]
let table = Tbl("Table title",
                columns: cols,
                cells: cells)

print(table.render(style: .roundedPadded))
// Produces ->
//โ•ญโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฎ
//โ”‚        Table title        โ”‚
//โ”œโ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
//โ”‚ # โ”‚ Col 1 โ”‚ Col 2 โ”‚ Col 3 โ”‚
//โ”œโ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
//โ”‚ 1 โ”‚ x     โ”‚   x   โ”‚     x โ”‚
//โ”‚ 2 โ”‚       โ”‚       โ”‚       โ”‚
//โ”‚ 3 โ”‚       โ”‚       โ”‚       โ”‚
//โ”œโ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
//โ”‚ 1 โ”‚       โ”‚       โ”‚       โ”‚
//โ”‚ 2 โ”‚ x     โ”‚   x   โ”‚     x โ”‚
//โ”‚ 3 โ”‚       โ”‚       โ”‚       โ”‚
//โ”œโ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
//โ”‚ 1 โ”‚       โ”‚       โ”‚       โ”‚
//โ”‚ 2 โ”‚       โ”‚       โ”‚       โ”‚
//โ”‚ 3 โ”‚ x     โ”‚   x   โ”‚     x โ”‚
//โ•ฐโ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฏ

Another simple table using DSL.

import Table

let table = Tbl("Summer Olympics") {
    
    Columns {
        Col("Year", width: 4)
        Col("Host", width: .in(5...25), defaultWrapping: .word)
        Col("Country")
    }
    
    Rows {
        ["1952", "Helsinki", "Finland"]
        ["1956", "Stockholm", "Sweden"]
        ["1960", "Rome", "Italy"]
    }
}
print(table.render(style: .rounded))
//โ•ญโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฎ
//โ”‚   Summer Olympics    โ”‚
//โ”œโ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
//โ”‚Yearโ”‚Host     โ”‚Countryโ”‚
//โ”œโ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
//โ”‚1952โ”‚Helsinki โ”‚Finlandโ”‚
//โ”œโ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
//โ”‚1956โ”‚Stockholmโ”‚Sweden โ”‚
//โ”œโ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
//โ”‚1960โ”‚Rome     โ”‚Italy  โ”‚
//โ•ฐโ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฏ

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics