Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
f1c375d
add start of tabulate module
Jul 1, 2022
7b2f680
multiline alias for luacheck
Jul 1, 2022
ab0775c
change nil to empty string
Jul 1, 2022
9099696
add footer features
Jul 1, 2022
9fd1481
allow row separator to be an integer
Jul 1, 2022
1018b73
add tabulate.grid convenience func
Jul 1, 2022
19c883e
allow for table of list data
Jul 1, 2022
0b7aea2
allow for list of lists
Jul 1, 2022
b68b0dd
allow user to use arbitrary keys
Jul 1, 2022
34b2f61
add dict2d converter
Jul 1, 2022
420fbfb
fix for sumneko lua call
Jul 1, 2022
ea95c5d
ignore param mismatch error
Jul 1, 2022
0bf78d8
enable row index showing
Jul 1, 2022
5959bf5
enforce that keys are strings to make it easier
Jul 1, 2022
79bc807
drop show_index for now
Jul 2, 2022
ee44e8d
cleanup some names
Jul 2, 2022
1847de5
drop unused func
Jul 2, 2022
f5dd5fc
remove diagnostic ignore with proper type check
Jul 2, 2022
23c57f8
check row_separator is > 0
Jul 2, 2022
61084a1
add sort method
Jul 2, 2022
9e136d9
add basic tabulate spec
Jul 3, 2022
92ef1f3
add filtering option
Jul 3, 2022
0345d77
reorder callbacks to value first
Jul 3, 2022
d8558d0
allow formatter to take of column table
Jul 4, 2022
f2286df
return fort table instead of string to allow escape hatch
Jul 4, 2022
8ac7c49
use alias to switch string to tabulate.ColumnKey
Jul 4, 2022
9a6dde9
more switching to tabulate.ColumnKey alias
Jul 4, 2022
f6978a6
if keys of same type use that as default columns
Jul 4, 2022
4a11d7d
fix tabulate tests with changin return value
Jul 4, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ prefixes.
- String splitting on column separator is done in lua
- all functions are UTF-8 compatible

## tabulate

A mix of python [tabulate](https://pypi.org/project/tabulate/) and
[tprint](https://github.com/smi11/tprint-lua) to enable quick (but powerful)
ascii table generation. It alleviates the tedious code format generation of the
`fort` module and allows for separation of data generation from table
formatting.

## Roadmap

There are a few things that I would like to add with lua fort. This list is in
Expand Down
2 changes: 1 addition & 1 deletion lua-fort-dev-1.rockspec
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ build = {
defines = {"FT_CONGIG_DISABLE_WCHAR"}
}
},
install = {lua = {fort = "src/fort.lua"}},
install = {lua = {fort = "src/fort.lua", tabulate = "src/tabulate.lua"}},
-- Override default build options (per platform)
platforms = {},
copy_directories = {"examples", "spec", "docs"}
Expand Down
207 changes: 207 additions & 0 deletions spec/tabulate_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,207 @@
local tabulate = require "tabulate"

describe("data types", function()
it("list list", function()
local value = tabulate({
{"cool", "testing"}, {"more", "third", "fourth"}
})
assert.equals(tostring(value), [[
+------+---------+--------+
| 1 | 2 | 3 |
+------+---------+--------+
| cool | testing | |
| more | third | fourth |
+------+---------+--------+
]])
end)
it("dict list", function()
-- column has to be set to be deterministic
local value = tabulate({
{a = "cool", b = "running"}, {a = "534", b = "sdfsdf"},
{a = "534", b = "sdfsdf", c = "testing"}
}, {column = {"a", "b", 'c'}})
assert.equals(tostring(value), [[
+------+---------+---------+
| a | b | c |
+------+---------+---------+
| cool | running | |
| 534 | sdfsdf | |
| 534 | sdfsdf | testing |
+------+---------+---------+
]])
end)
it("list dict", function()
-- column has to be set to be deterministic
local value = tabulate({
a = {1, 2, 3, 4, 5, 6, 8, "csdfs", "23"},
c = {"wow this is a longer text", 2, 4, "sdf", ""},
d = {"this is a good test", 2, 3434}
}, {column = {"a", "d", "c"}})
assert.equals(tostring(value), [[
+-------+---------------------+---------------------------+
| a | d | c |
+-------+---------------------+---------------------------+
| 1 | this is a good test | wow this is a longer text |
| 2 | 2 | 2 |
| 3 | 3434 | 4 |
| 4 | | sdf |
| 5 | | |
| 6 | | |
| 8 | | |
| csdfs | | |
| 23 | | |
+-------+---------------------+---------------------------+
]])
end)
end)

describe("frame", function()

local data = {cool = {1, 2, 3, 4}, testing = {"text", "2", 5, 5.2}}
it("basic", function()
local value = tabulate(data,
{column = {"cool", "testing"}, frame = "basic"})
assert.equals(tostring(value), [[
+------+---------+
| cool | testing |
+------+---------+
| 1 | text |
| 2 | 2 |
| 3 | 5 |
| 4 | 5.2 |
+------+---------+
]])
end)

it("bold", function()
local value = tabulate(data,
{column = {"cool", "testing"}, frame = "bold"})
assert.equals(tostring(value), [[
┏━━━━━━┳━━━━━━━━━┓
┃ cool ┃ testing ┃
┣━━━━━━╋━━━━━━━━━┫
┃ 1 ┃ text ┃
┃ 2 ┃ 2 ┃
┃ 3 ┃ 5 ┃
┃ 4 ┃ 5.2 ┃
┗━━━━━━┻━━━━━━━━━┛
]])
end)
it("solid_round", function()
local value = tabulate(data, {
column = {"cool", "testing"},
frame = "solid_round"
})
assert.equals(tostring(value), [[
╭──────┬─────────╮
│ cool │ testing │
├──────┼─────────┤
│ 1 │ text │
│ 2 │ 2 │
│ 3 │ 5 │
│ 4 │ 5.2 │
╰──────┴─────────╯
]])
end)
end)

describe("header", function()

local data_ll = {{"2", "4", "5"}, {"2", 343, 234.32}}
local data_dl = {{a = "2", b = "4", "5"}, {a = "2", b = 343, 234.32}}
local data_ld = {a = {"2", "2"}, b = {"4", 343}, c = {"5", 234.32}}
it("list-list", function()
local value = tabulate(data_ll, {
column = {1, 2, 3},
header = {[1] = "first", [2] = "second", [3] = "third"}
})
assert.equals(tostring(value), [[
+-------+--------+--------+
| first | second | third |
+-------+--------+--------+
| 2 | 4 | 5 |
| 2 | 343 | 234.32 |
+-------+--------+--------+
]])
end)
it("dict-list", function()
local value = tabulate(data_dl, {
column = {"a", "b", 1},
header = {a = "first", b = "second", [1] = "third"}
})
assert.equals(tostring(value), [[
+-------+--------+--------+
| first | second | third |
+-------+--------+--------+
| 2 | 4 | 5 |
| 2 | 343 | 234.32 |
+-------+--------+--------+
]])
end)
it("list-dict", function()
local value = tabulate(data_ld, {
column = {"a", "b", "c"},
header = {a = "first", b = "second", c = "third"}
})
assert.equals(tostring(value), [[
+-------+--------+--------+
| first | second | third |
+-------+--------+--------+
| 2 | 4 | 5 |
| 2 | 343 | 234.32 |
+-------+--------+--------+
]])
end)
end)

describe("align", function()

local data_dl = {
{a = "2", b = "4", c = "5"}, {a = "2", b = 343, c = 234.32}
}
it("left", function()
local value = tabulate(data_dl, {
column = {"a", "b", "c"},
header = {a = "first", b = "second", c = "third"},
align = {a = "left", b = "left"}
})
assert.equals(tostring(value), [[
+-------+--------+--------+
| first | second | third |
+-------+--------+--------+
| 2 | 4 | 5 |
| 2 | 343 | 234.32 |
+-------+--------+--------+
]])
end)
it("center", function()
local value = tabulate(data_dl, {
column = {"a", "b", "c"},
header = {a = "first", b = "second", c = "third"},
align = {a = "center", b = "center"}
})
assert.equals(tostring(value), [[
+-------+--------+--------+
| first | second | third |
+-------+--------+--------+
| 2 | 4 | 5 |
| 2 | 343 | 234.32 |
+-------+--------+--------+
]])
end)
it("right", function()
local value = tabulate(data_dl, {
column = {"a", "b", "c"},
header = {a = "first", b = "second", c = "third"},
align = {a = "right", b = "right"}
})
assert.equals(tostring(value), [[
+-------+--------+--------+
| first | second | third |
+-------+--------+--------+
| 2 | 4 | 5 |
| 2 | 343 | 234.32 |
+-------+--------+--------+
]])
end)
end)
Loading