Table

Table

Module table
Author Luma Contributors
Version 1.0.0
License MIT

A pure Luma module for formatting [[str]] data as terminal tables. Four built-in styles, automatic column width detection, and a helper to convert maps into table data. No external dependencies.

Features

  • Four styles: box-drawing (Unicode), ASCII, compact, and markdown
  • Auto-sized columns based on content width
  • Print or render to string for further processing
  • Convert maps to table format with from_maps()
  • Works with CSV data out of the box ([[str]] is the shared format)

Quick Start

table = import "table"

data: [[str]] = [
    ["Name", "Age", "City"],
    ["Alice", "30", "Tallinn"],
    ["Bob", "25", "Riga"]
]

// Print with default box-drawing style
table.print_table(data)

Output:

+-------+-----+---------+
| Name  | Age | City    |
+-------+-----+---------+
| Alice | 30  | Tallinn |
| Bob   | 25  | Riga    |
+-------+-----+---------+

Installation

  1. Download the module file: table.luma
  2. Place it in your project directory (or a lib/ folder)
  3. Import it in your code:
table = import "table"

Styles

Default (box-drawing)

table.print_table(data)
+----------+-----+----------+
| Name     | Age | City     |
+----------+-----+----------+
| Alice    | 30  | Tallinn  |
| Bob      | 25  | Riga     |
+----------+-----+----------+

Simple (ASCII)

table.print_styled(data, "simple")
+----------+-----+----------+
| Name     | Age | City     |
+----------+-----+----------+
| Alice    | 30  | Tallinn  |
| Bob      | 25  | Riga     |
+----------+-----+----------+

Compact (no borders)

table.print_styled(data, "compact")
Name   Age  City
-----  ---  -------
Alice  30   Tallinn
Bob    25   Riga

Markdown

table.print_styled(data, "markdown")
| Name  | Age | City    |
|-------|-----|---------|
| Alice | 30  | Tallinn |
| Bob   | 25  | Riga    |

Paste the output directly into GitHub issues, READMEs, or documentation.

API Reference

Function Description
print_table(data: [[str]]) Print table with default style
print_styled(data: [[str]], style: str) Print table with a named style
render(data: [[str]]) -> str Return table string (default style)
render_styled(data: [[str]], style: str) -> str Return table string with a named style
from_maps(maps: [{str: str}], columns: [str]) -> [[str]] Convert a list of maps to table data

Styles: "default", "simple", "compact", "markdown"

Usage

Print vs Render

print_table and print_styled output directly to the terminal. render and render_styled return the table as a string for further processing:

output: str = table.render_styled(data, "markdown")
file("report.md").write(output)

Convert maps to a table

from_maps takes a list of {str: str} maps and a list of column names, and produces [[str]] with the column names as the header row:

maps: [{str: str}] = [
    {"name": "Luma", "version": "1.0"},
    {"name": "CSV", "version": "1.0"}
]
data: [[str]] = table.from_maps(maps, ["name", "version"])
table.print_table(data)

Works with CSV

The CSV module and Table module share the same [[str]] data format. Parse a CSV file and display it immediately:

csv   = import "csv"
table = import "table"

data: [[str]] = csv.read("employees.csv")
table.print_table(data)

Filter first, then display:

engineers: [[str]] = csv.where(data, "role", "engineer")
table.print_styled(engineers, "compact")

Complete Example

table = import "table"

// Build data
data: [[str]] = [
    ["Product", "Price", "Stock"],
    ["Laptop", "1299.99", "15"],
    ["Mouse", "24.99", "200"],
    ["Keyboard", "79.99", "85"]
]

// Terminal display
print("=== Inventory ===")
table.print_table(data)

// Markdown for documentation
print("")
print("=== Markdown ===")
table.print_styled(data, "markdown")

// Save compact report to file
report: str = table.render_styled(data, "compact")
file("inventory.txt").write(report)

Design

  • The first row of [[str]] is always the header
  • Column widths are computed automatically from the widest cell in each column
  • All rendering is pure string operations (pad_right, pad_left, repeat, concatenation)
  • No external dependencies — uses only Luma’s built-in string methods
  • The module is a single .luma file with no setup required