Skip to content

Easily generate fancy ascii tables in lua. Supports multiple styles (Unicode, Markdown)

License

Notifications You must be signed in to change notification settings

sapphyrus/table_gen.lua

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 

Repository files navigation

table_gen.lua

⚡️ Easily generate human-readable ASCII tables from lua

Example

-- require the file
local table_gen = require "table_gen"

local headings = {"Country", "Capital", "Population", "Language"}
local rows = {
	{"USA", "Washington, D.C.", "237 million", "English"},
	{"Sweden", "Stockholm", "10 million",	"Swedish"},
	{"Germany", "Berlin", "82 million", "German"}
}

-- generate the table. Last argument are the options, or if a string, the style option
local table_out = table_gen(rows, headings, {
	style = "Markdown (Github)"
})

-- Print it to console
print(table_out)

-- output:
-- | Country |     Capital      | Population  | Language |
-- |---------|------------------|-------------|----------|
-- | USA     | Washington, D.C. | 237 million | English  |
-- | Sweden  | Stockholm        | 10 million  | Swedish  |
-- | Germany | Berlin           | 82 million  | German   |

Result:

Country Capital Population Language
USA Washington, D.C. 237 million English
Sweden Stockholm 10 million Swedish
Germany Berlin 82 million German

Documentation

table_gen.generate_table(rows, headings, options)

Syntax:

  • rows: A table of rows
  • (optional) headings: A headings row
  • (optional) options: A table of options, or a style string
    • style (string): The general style. Check the supported styles below
    • top_line (boolean): Defaults to true for all styles but Github Markdown
    • bottom_line (boolean): Defaults to true for all styles but Github Markdown
    • header_seperator_line: Defaults to true

Instead of table_gen.generate_table(...), you can also directly call table_gen(...).


Styles

ASCII (Default):

+-----------+----------+-----+
| Firstname | Lastname | Age |
+-----------+----------+-----+
| Jill      | Smith    | 51  |
| Eve       | Jackson  | 94  |
| John      | Doe      | 80  |
+-----------+----------+-----+

Unicode:

╔═══════════╦══════════╦═════╗
║ Firstname ║ Lastname ║ Age ║
╠═══════════╬══════════╬═════╣
║ Jill      ║ Smith    ║ 51  ║
║ Eve       ║ Jackson  ║ 94  ║
║ John      ║ Doe      ║ 80  ║
╚═══════════╩══════════╩═════╝

Unicode (Single Line):

┌───────────┬──────────┬─────┐
│ Firstname │ Lastname │ Age │
├───────────┼──────────┼─────┤
│ Jill      │ Smith    │ 51  │
│ Eve       │ Jackson  │ 94  │
│ John      │ Doe      │ 80  │
└───────────┴──────────┴─────┘

Markdown (Github):

| Firstname | Lastname | Age |
|-----------|----------|-----|
| Jill      | Smith    | 51  |
| Eve       | Jackson  | 94  |
| John      | Doe      | 80  |

Compact:

 ----------- ---------- -----
  Firstname   Lastname   Age
 ----------- ---------- -----
  Jill        Smith      51
  Eve         Jackson    94
  John        Doe        80

ASCII (Girder):

//===========[]==========[]=====\\
|| Firstname || Lastname || Age ||
|]===========[]==========[]=====[|
|| Jill      || Smith    || 51  ||
|| Eve       || Jackson  || 94  ||
|| John      || Doe      || 80  ||
\\===========[]==========[]=====//

Credits

This script was based on https://github.com/ozh/ascii-tables (https://ozh.github.io/ascii-tables/)

About

Easily generate fancy ascii tables in lua. Supports multiple styles (Unicode, Markdown)

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages