Tabled
Tabled is a versatile gem designed for rendering tabular data in a console. It offers various features to make data presentation simple and customizable.
Table of Contents
- Installation
- Usage
- Options
- Examples
- Exporting Data
- Printing CSV or JSON Files
- Contributing
Installation
bundle add tabled
# or
gem install tabled
Usage
To use Tabled in your application, follow these steps:
- Require the gem in your code:
require 'tabled'
- Pass an array of rows to the application. Each row can contain any number of columns and optional footer text.
Options
Tabled accepts the following options:
-
framed
(optional, default: true): Specify if the output should have frames. -
row_separator
(optional, default: "-"): Set the character for row separators or set it to nil if you don't want to separate rows. -
titles
(optional): Provide a list of column titles.
Examples
Here are some examples to help you get started:
Simple Data Structure
data = [
["Helena", "20 years", "Female"],
["John", "18 years", "Male"],
["Alan", "23 years", "Male"],
]
Tabled.new(data, framed: false, row_separator: nil).print_to_console
Using Footer Inside a Row
data = [
["Helena", "20 years", "Female"],
["John", "18 years", "Male", { footer: "Legendary assassin John Wick (Keanu Reeves)." }],
["Alan", "23 years", "Male"],
]
Tabled.new(data, row_separator: nil).print_to_console
Print Ruby CSV records
csv = CSV.parse(<<~ROWS, headers: true)
Bob,Engineering,1000
Jane,Sales,2000
John,Management,5000
ROWS
Tabled.from_csv(csv: csv, framed: true).print_to_console
Exporting Data
You can export the data in CSV or JSON formats. By default, the file is saved in the current directory with the file name "tabled.csv" (CSV format).
# Export to CSV
Tabled.new(data).export_to_file
# Export to JSON
Tabled.new(data, titles: ['Name', 'Age', 'Gender']).export_to_file(format: :json)
# Customize file name
Tabled.new(data, titles: ['Name', 'Age', 'Gender']).export_to_file(file_name: 'my_file.json')
Printing CSV or JSON Files
As part of the gem, the tabled binary is included. You can run it like this:
tabled print path_to_[csv|json]_file
Contributing
- Fork it ( http://github.com/rukomoynikov/tabled/fork )
- Create your feature branch (git checkout -b my-new-feature)
- Commit your changes (git commit -am 'Add some feature')
- Push to the branch (git push origin my-new-feature)
- Create new Pull Request
Development process in Docker
Run docker compose run --build --rm app bash
to get full packed dev environment for the gem developing.