https://github.com/fjebaker/tisch
Table printing utilities.
https://github.com/fjebaker/tisch
Last synced: 9 months ago
JSON representation
Table printing utilities.
- Host: GitHub
- URL: https://github.com/fjebaker/tisch
- Owner: fjebaker
- License: mit
- Created: 2023-04-08T14:58:13.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2023-04-08T19:53:55.000Z (about 3 years ago)
- Last Synced: 2025-08-25T11:10:02.136Z (9 months ago)
- Language: Zig
- Size: 10.7 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Tisch
> Using bad code saves you from writing bad code.
Table printing utilities for command line tools.
## Examples
Default with `tisch.Table`:
```zig
var str = try table.toString(.{});
```
```
┌─────────┬─────┬─────────┐
│Heading 1│ 2 │Heading 3│
├─────────┼─────┼─────────┤
│a │b │c │
│1 │2 │3 │
│hello │world│! │
└─────────┴─────┴─────────┘
```
With padding:
```zig
var str = try table.toString(.{
.padding = .{ .l = 3, .r = 2 },
});
```
```
┌──────────────┬──────────┬──────────────┐
│ Heading 1 │ 2 │ Heading 3 │
├──────────────┼──────────┼──────────────┤
│ a │ b │ c │
│ 1 │ 2 │ 3 │
│ hello │ world │ ! │
└──────────────┴──────────┴──────────────┘
```
Alignment:
```zig
var str = try table.toString(.{
.padding = .{ .l = 1, .r = 1 },
.alignment = .Right,
});
```
```
┌───────────┬───────┬───────────┐
│ Heading 1 │ 2 │ Heading 3 │
├───────────┼───────┼───────────┤
│ a │ b │ c │
│ 1 │ 2 │ 3 │
│ hello │ world │ ! │
└───────────┴───────┴───────────┘
```
Equal widths:
```zig
var str = try table.toString(.{
.padding = .{ .l = 1, .r = 1 },
.even = true,
});
```
```
┌───────────┬───────────┬───────────┐
│ Heading 1 │ 2 │ Heading 3 │
├───────────┼───────────┼───────────┤
│ a │ b │ c │
│ 1 │ 2 │ 3 │
│ hello │ world │ ! │
└───────────┴───────────┴───────────┘
```
Outlines:
```zig
var str = try table.toString(.{
.padding = .{ .l = 1, .r = 1 },
.even = true,
.outline = false,
});
```
```
─────────────────────────────────────
Heading 1 2 Heading 3
─────────────────────────────────────
a b c
1 2 3
hello world !
─────────────────────────────────────
```
No rule lines at all:
```zig
var str = try table.toString(.{
.padding = .{ .l = 1, .r = 1 },
.outline = false,
.rule = false,
});
```
```
Heading 1 2 Heading 3
a b c
1 2 3
hello world !
```
Add indexes:
```zig
var str = try table.toString(.{
.padding = .{ .l = 1, .r = 1 },
.index = true,
.index_str = "Index",
});
```
```
┌───────┬───────────┬───────┬───────────┐
│ Index │ Heading 1 │ 2 │ Heading 3 │
├───────┼───────────┼───────┼───────────┤
│ 1 │ a │ b │ c │
│ 2 │ 1 │ 2 │ 3 │
│ 3 │ hello │ world │ ! │
└───────┴───────────┴───────┴───────────┘
```
Pure ASCII:
```zig
var table = try AsciiTable.initWithHeadings(...)
// ...
var str = try table.toString(.{
.padding = .{ .l = 1, .r = 1 },
});
```
```
+-------+-----------+-------+-----------+
| Index | Heading 1 | 2 | Heading 3 |
+-------+-----------+-------+-----------+
| 1 | a | b | c |
| 2 | 1 | 2 | 3 |
| 3 | hello | world | ! |
+-------+-----------+-------+-----------+
```
## Usage
Add the project as a zig dependency in your `build.zig.zon` and add the module to your project.
Initialize a table with some headings, and then add as many rows as you need. Rows are simply `[][]const u8`, which are copied by the table.
```zig
const tisch = @import("tisch");
const Table = tisch.Table;
var headings = [_][]const u8{ "Heading 1", "2", "Heading 3" };
var table = try Table.initWithHeadings(allocator, &headings);
defer table.deinit();
var row1 = [_][]const u8{ "a", "b", "c" };
try table.addRow(&row1);
var row2 = [_][]const u8{ "1", "2", "3" };
try table.addRow(&row2);
var row3 = [_][]const u8{ "hello", "world", "!" };
try table.addRow(&row3);
// print options
var str = try table.toString(
.{
.alignment = .Right,
.even = false,
.outline = true,
.rule = true,
.padding = .{ .r = 2, .l = 2 },
.index = false,
},
);
defer allocator.free(str);
```