A MoonBit library for pretty-printing tables in terminal. Ported from Rust tabled.
moon add hustcer/tabularlet table = @tabular.Table::from_rows([
["Name", "Language", "Stars"],
["Deno", "Rust/TS", "101k"],
["Bun", "Zig/C++", "76k"],
["Node.js", "C++/JS", "110k"],
])
table.apply(@tabular.Style::modern()) |> ignore
println(table.to_string())┌─────────┬──────────┬───────┐
│ Name │ Language │ Stars │
├─────────┼──────────┼───────┤
│ Deno │ Rust/TS │ 101k │
├─────────┼──────────┼───────┤
│ Bun │ Zig/C++ │ 76k │
├─────────┼──────────┼───────┤
│ Node.js │ C++/JS │ 110k │
└─────────┴──────────┴───────┘table.apply(@tabular.Style::ascii()) |> ignore
table.apply(@tabular.Style::modern()) |> ignore
table.apply(@tabular.Style::rounded()) |> ignore
table.apply(@tabular.Style::extended()) |> ignore
table.apply(@tabular.Style::dots()) |> ignore
table.apply(@tabular.Style::empty()) |> ignore
table.apply(@tabular.Style::blank()) |> ignore
table.apply(@tabular.Style::re_structured_text()) |> ignore
table.apply(@tabular.Style::ascii_rounded()) |> ignore
table.apply(@tabular.Style::markdown()) |> ignore
table.apply(@tabular.Style::psql()) |> ignore+----------+------+
| language | year |
+----------+------+
| Rust | 2010 |
+----------+------+
| MoonBit | 2022 |
+----------+------+┌──────────┬──────┐
│ language │ year │
├──────────┼──────┤
│ Rust │ 2010 │
├──────────┼──────┤
│ MoonBit │ 2022 │
└──────────┴──────┘╭──────────┬──────╮
│ language │ year │
├──────────┼──────┤
│ Rust │ 2010 │
├──────────┼──────┤
│ MoonBit │ 2022 │
╰──────────┴──────╯| language | year |
|----------|------|
| Rust | 2010 |
| MoonBit | 2022 | language | year
----------+------
Rust | 2010
MoonBit | 2022:.................:
: language : year :
:..........:......:
: Rust : 2010 :
:..........:......:
: MoonBit : 2022 :
:.................:╔══════════╦══════╗
║ language ║ year ║
╠══════════╬══════╣
║ Rust ║ 2010 ║
╠══════════╬══════╣
║ MoonBit ║ 2022 ║
╚══════════╩══════╝ language year
Rust 2010
MoonBit 2022========== ======
language year
========== ======
Rust 2010
MoonBit 2022
========== ======.-----------------.
| language | year |
| Rust | 2010 |
| MoonBit | 2022 |
'-----------------'let style = @tabular.Style::ascii()
.top('─')
.bottom('─')
.left('│')
.right('│')
.horizontal('─')
.vertical('│')
.corner_top_left('┌')
.corner_top_right('┐')
.corner_bottom_left('└')
.corner_bottom_right('┘')
table.apply(style) |> ignorelet style = @tabular.Style::ascii()
.remove_horizontal()
.horizontals([
(1, @tabular.HorizontalLine::full('-', '+', '+', '+')),
])
table.apply(style) |> ignorelet builder = @tabular.Builder::default()
builder.push_record(["Name", "Age", "City"])
builder.push_record(["Alice", "30", "NYC"])
builder.push_record(["Bob", "25", "LA"])
let table = builder.build()
println(table.to_string())///|
let builder = @tabular.Builder::from_rows([
["Name", "Age"],
["Alice", "30"],
["Bob", "25"],
])
///|
let table1 = builder.index().build()
///|
let table2 = builder.index().transpose().build()
///|
let table3 = builder.index().hide().build()table.set_align(@tabular.Align::right()) |> ignore
table.set_padding(@tabular.Padding::new(1, 1, 0, 0)) |> ignore
table.set_width(@tabular.Width::wrap(10)) |> ignore
table.set_height(@tabular.Height::increase(2)) |> ignoretable.modify_rows(
@tabular.Rows::first(),
@tabular.Setting::align(@tabular.Align::center()),
) |> ignore
table.modify_cols(
@tabular.Cols::one(1),
@tabular.Setting::width(@tabular.Width::wrap(8)),
) |> ignore
table.modify_cell(
@tabular.Cell::new(1, 2),
@tabular.Setting::format(@tabular.Format::surround("[", "]")),
) |> ignorelet table = @tabular.Table::from_rows([
["Team", "Q1", "Q2", "Q3"],
["A", "12", "18", "22"],
["B", "10", "16", "21"],
])
table.apply(@tabular.Style::modern()) |> ignore
table.modify_cell(@tabular.Cell::new(0, 0), @tabular.Span::col(2)) |> ignore
table.correct_borders() |> ignore
println(table.to_string())┌─────────────┬────┬────┐
│ Team │ Q2 │ Q3 │
├──────┬──────┼────┼────┤
│ A │ 12 │ 18 │ 22 │
├──────┼──────┼────┼────┤
│ B │ 10 │ 16 │ 21 │
└──────┴──────┴────┴────┘let table = @tabular.Table::from_rows([
["0", "1", "2"],
["0", "1", "1"],
["1", "1", "2"],
["1", "1", "1"],
])
table.panel(@tabular.Panel::header("Runtime Comparison")) |> ignore
table.merge(@tabular.Merge::horizontal()) |> ignore
println(table.to_string())table.highlight(
@tabular.Highlight::outline_cell(@tabular.Cell::new(1, 1), '*'),
) |> ignore
table.shadow(@tabular.Shadow::new(1)) |> ignoretable.extract_rows(@tabular.Rows::new(0, 2)) |> ignore
table.remove_cols(@tabular.Cols::one(0)) |> ignore
table.split(@tabular.Split::col(2).concat()) |> ignoremoon testpub enum Align {
Left
Right
Center
}pub struct ByColName {
name : String
}pub struct Cell {
row : Int
col : Int
}pub enum Format {
Surround(String, String, Bool)
Value(String, Bool)
Content((String) -> String, Bool)
Positioned((String, Int, Int) -> String, Bool)
}pub enum Height {
Increase(Int)
Limit(Int)
}fn HorizontalLine::full(main : Char, left? : Char, intersection? : Char, right? : Char) -> HorizontalLinepub enum Merge {
Horizontal
Vertical
}pub struct Padding {
left : Int
right : Int
top : Int
bottom : Int
}pub enum Panel {
}pub struct Shadow {
size : Int
offset : Int
fill : Char
top : Bool
bottom : Bool
left : Bool
right : Bool
}pub enum Span {
}pub struct Split {
// private fields
}pub struct Style {
borders : Borders
horizontal_lines : HorizontalLines
custom_horizontal_lines : Array[(Int, HorizontalLine)]
}pub enum VAlign {
Top
Bottom
Center
}A MoonBit library for pretty-printing tables in terminal. Ported from Rust tabled.