Giter VIP home page Giter VIP logo

tablua's Introduction

tablua

A lua module for print tables nicely.

Instalation

Add the file tablua.lua to your project or install using LuaRocks: luarocks install tablua

Usage

local tablua = require("tablua")

local data = {
    { "username", "password" },
    { "maria",    "pass123" }
}

-- basic usage
local t1 = tablua(data)
print(t1)
-- ╭──────────┬──────────╮
-- │ username │ password │
-- ├──────────┼──────────┤
-- │  maria   │ pass123  │
-- ╰──────────┴──────────╯
--

-- options
t1:padding(3)
t1:first_line_data()
print(t1)
-- ╭──────────────┬──────────────╮
-- │   username   │   password   │
-- │    maria     │   pass123    │
-- ╰──────────────┴──────────────╯

-- chaining methods
t1:padding(0):first_line_header()
print(t1)
-- ╭────────┬────────╮
-- │username│password│
-- ├────────┼────────┤
-- │ maria  │pass123 │
-- ╰────────┴────────╯

-- adding lines
t1:add_line({ "heloisa", "verylongpassword" }) -- defaults to last line
t1:add_line({ "NEW_HEADER", "NEW_HEADER" }, 1)
print(t1)
-- ╭──────────┬────────────────╮
-- │NEW_HEADER│   NEW_HEADER   │
-- ├──────────┼────────────────┤
-- │ username │    password    │
-- │  maria   │    pass123     │
-- │ heloisa  │verylongpassword│
-- ╰──────────┴────────────────╯

-- removing lines
t1:remove_line() -- defaults to last line
t1:remove_line(1)
print(t1)
-- ╭────────┬────────╮
-- │username│password│
-- ├────────┼────────┤
-- │ maria  │pass123 │
-- ╰────────┴────────╯

-- anything that implements __tostring can be used
local x = {}
setmetatable(x, { __tostring = function(v) return "it works!" end })
t1:add_line({ x, 3.1415 })
print(t1)
-- ╭─────────┬────────╮
-- │username │password│
-- ├─────────┼────────┤
-- │  maria  │pass123 │
-- │it works!│ 3.1415 │
-- ╰─────────┴────────╯

-- the first line defines the table columns
t1 = tablua({ { 10, 20, 30 } }) -- the table has 3 columns
t1:add_line({ 10, })            -- missing elements defaults to nil
t1:add_line({ 10, 20, 30, 40 }) -- extra elements are ignored
t1:remove_line(1)               -- now the table has just 1 column
print(t1)
-- ╭────╮
-- │ 10 │
-- ├────┤
-- │ 10 │
-- ╰────╯


-- calling with the wrong type of arguments will cause undefined behavior
local t2 = tablua({ 10 }) -- very bad: should be {{10}}. This will crash the program when trying to print.
t2 = tablua({ { 10 } })   -- good
t2:add_line({ { 5 } })    -- maybe bad: lines should be {col1, col2, ...}. This will print the table address.

-- WARN: The next line will crash the program
-- t2:add_line({ "Why would you", "do that?" }, -99) -- very bad: index must be positive and greater than zero

print(t2)
-- ╭───────────────────────╮
-- │          10           │
-- ├───────────────────────┤
-- │ table: 0x55b10ffc3190 │
-- ╰───────────────────────╯

tablua's People

Contributors

luiz734 avatar

Stargazers

 avatar  avatar

Watchers

 avatar

tablua's Issues

tablua supports only Lua 5.4

I tested tablua using lua 5.3 and it works fine.

The rockspec makes it only available for lua 5.4, It should be at least 5.3. A lot of lua code still running in that version.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.