prettytable
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
Repository
prettytable
Owner
Statistic
- 2
- 0
- 0
- 0
- 0
- over 3 years ago
- May 2, 2021
License
MIT License
Links
Synced at
Fri, 08 Nov 2024 09:13:14 GMT
Languages