prettytable
Version, currently 0.1.07 versions
github.com/henrikac/prettytable
Crystal library that makes it easy to build simple text tables
Nothing has been indexed for 0.1.0 yet. The tag is recorded, its shard.yml has not been read, so the manifest and dependency list below are empty because they are unknown rather than because they are absent.
Installation
# Add this to your shard.yml
dependencies:
prettytable:
github: henrikac/prettytable
version: ~> 0.1.0Then run:
shards installshard.yml
No shard.yml has been indexed for 0.1.0. You can read it on the repository.
Dependencies
Unknown: the shard.yml for this version has not been read yet.
README
This README is the one indexed from the repository at its latest ref, not from the tag for this version.
prettytable
Crystal library that makes it easy to build simple text tables.
Installation
-
Add the dependency to your
shard.yml:dependencies: prettytable: github: henrikac/prettytable -
Run
shards install
Usage
Basic example
require "prettytable"
# default initialize
# table = PrettyTable::Table.new
# table.set_headers(["id", "name", "age"])
# initialize + set_headers
table = PrettyTable::Table.new(["id", "name", "age"])
table << [
["1", "Melody Connolly", "42"],
["2", "Leslie Hutchinson", "1"],
["3", "Codey French", "58"]
]
puts table
will output
+----+-------------------+-----+
| id | name | age |
+----+-------------------+-----+
| 1 | Melody Connolly | 42 |
| 2 | Leslie Hutchinson | 1 |
| 3 | Codey French | 58 |
+----+-------------------+-----+
Notes:
- Once headers has been set they cannot be re-set / updated.
- Rows cannot be added to the table before the headers has been set.
Ways to add rows to table
#add_row(row : Array(String))#add_rows(rows : Array(Array(String)))#<<(row : Array(String))#<<(rows : Array(Array(String)))
Delete row
require "prettytable"
table = PrettyTable::Table.new(["id", "name", "age"])
# headers can be set as above or later with #set_headers(headers : Array(String))
table << [
["1", "Melody Connolly", "42"],
["2", "Leslie Hutchinson", "1"],
["3", "Codey French", "58"]
]
table.delete_row(1) # => ["2", "Leslie Hutchinson", "1"]
Get row/column
require "prettytable"
table = PrettyTable::Table.new(["id", "name", "age"])
table << [
["1", "Melody Connolly", "42"],
["2", "Leslie Hutchinson", "1"],
["3", "Codey French", "58"],
["4", "Lulu Sparkles", "23"]
]
# get row
table[0] # => ["1", "Melody Connolly", "42"]
# get rows
table[1..3] # => [
# ["2", "Leslie Hutchinson", "1"],
# ["3", "Codey French", "58"],
# ["4", "Lulu Sparkles", "23"]
# ]
# get column
table["name"] # => ["Melody Connolly", "Leslie Hutchinson", "Codey French"]
Add/remove a column
require "prettytable"
table = PrettyTable::Table.new(["id", "name", "age"])
table << [
["1", "Melody Connolly", "42"],
["2", "Leslie Hutchinson", "1"],
["3", "Codey French", "58"],
]
table.add_column("height", ["158", "163", "189"])
table.remove_column("id") # => ["1", "2", "3"]
Select multiple columns
require "prettytable"
table = PrettyTable::Table.new(["id", "name", "age"])
table << [
["1", "Melody Connolly", "42"],
["2", "Leslie Hutchinson", "1"],
["3", "Codey French", "58"]
]
puts table.select(["name", "age"])
will output
+-------------------+-----+
| name | age |
+-------------------+-----+
| Melody Connolly | 42 |
| Leslie Hutchinson | 1 |
| Codey French | 58 |
+-------------------+-----+
Difference between two tables
require "prettytable"
table = PrettyTable::Table.new(["id", "name", "age"])
table << [
["1", "John Doe", "31"],
["2", "Kelly Strong", "20"],
["3", "James Hightower", "58"],
["4", "Brian Muscle", "3"],
["5", "Lulu Sparkles", "28"]
]
other = PrettyTable::Table.new(["id", "name", "age"])
other << [
["1", "John Doe", "31"],
["2", "Kelly Strong", "20"],
["3", "James Hightower", "58"]
]
puts table - other
will output
+----+---------------+-----+
| id | name | age |
+----+---------------+-----+
| 4 | Brian Muscle | 42 |
| 5 | Lulu Sparkles | 58 |
+----+---------------+-----+
Sort a table
require "prettytable"
table = PrettyTable::Table.new(["id", "name", "age"])
table << [
["1", "John Doe", "31"],
["2", "Kelly Strong", "20"],
["3", "James Hightower", "58"],
["4", "Brian Muscle", "3"],
["5", "Lulu Sparkles", "28"]
]
sorted_table_asc = table.sort("name")
sorted_table_desc = table.sort("name", false)
custom_sort = table.sort { |a, b| a[2] <=> b[2] }
Sorting a table will always return a new table.
To hash
require "prettytable"
table = PrettyTable::Table.new(["id", "name", "age"])
table << [
["1", "Melody Connolly", "42"],
["2", "Leslie Hutchinson", "1"],
["3", "Codey French", "58"]
]
table.to_h # => {
# "id" => ["1", "2", "3"],
# "name" => ["Melody Connoly", "Leslie Hutchinson", "Codey French"],
# "age" => ["42", "1", "58"]
# }
To JSON
require "prettytable"
table = PrettyTable::Table.new(["id", "name", "age"])
table << [
["1", "Melody Connolly", "42"],
]
table.to_json # => [{"id":"1","name":"Melody Connolly","age":"42"}]
To/From CSV
require "prettytable"
table = PrettyTable::Table.new(["id", "name", "age"])
table << [
["1", "Melody Connolly", "42"],
["2", "Leslie Hutchinson", "1"],
["3", "Codey French", "58"]
]
table.to_csv("./table.csv") # => saves the table to table.csv
# Load table from .csv
loaded_table = PrettyTable::Table.from_csv("./table.csv")
loaded_table.headers # => ["id", "name", "age"]
Contributing
- Fork it (https://github.com/henrikac/prettytable/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 a new Pull Request
Contributors
- Henrik Christensen - creator and maintainer
Documentation
Built from the current release. The first visit to a release nobody has asked for starts its build.
Links
This release
- Version
0.1.0- Tagged
- May 6, 2021
- Commit
04d57c24cc14- Indexed
- not yet
Dependents
No indexed shard depends on this one yet.
Repository
github.com/henrikac/prettytable
Metadata
- Created
- Aug 12, 2026
- Updated
- Aug 16, 2026
- Synced
- Aug 16, 2026
- Versions
- 7