{"id":15713734,"url":"https://github.com/henrikac/prettytable","last_synced_at":"2025-03-30T18:47:15.764Z","repository":{"id":140271165,"uuid":"363716294","full_name":"henrikac/prettytable","owner":"henrikac","description":"Crystal library that makes it easy to build simple text tables","archived":false,"fork":false,"pushed_at":"2021-05-06T15:16:02.000Z","size":34,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-02-05T21:27:12.518Z","etag":null,"topics":["crystal","crystal-lang","crystal-language"],"latest_commit_sha":null,"homepage":"","language":"Crystal","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/henrikac.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-05-02T17:58:54.000Z","updated_at":"2022-10-12T23:41:07.000Z","dependencies_parsed_at":null,"dependency_job_id":"f8697070-b16a-4317-8f72-171d624a0351","html_url":"https://github.com/henrikac/prettytable","commit_stats":null,"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/henrikac%2Fprettytable","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/henrikac%2Fprettytable/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/henrikac%2Fprettytable/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/henrikac%2Fprettytable/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/henrikac","download_url":"https://codeload.github.com/henrikac/prettytable/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246365640,"owners_count":20765546,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["crystal","crystal-lang","crystal-language"],"created_at":"2024-10-03T21:33:07.988Z","updated_at":"2025-03-30T18:47:15.469Z","avatar_url":"https://github.com/henrikac.png","language":"Crystal","funding_links":[],"categories":[],"sub_categories":[],"readme":"# prettytable\n\nCrystal library that makes it easy to build simple text tables.\n\n## Installation\n\n1. Add the dependency to your `shard.yml`:\n\n   ```yaml\n   dependencies:\n     prettytable:\n       github: henrikac/prettytable\n   ```\n\n2. Run `shards install`\n\n## Usage\n\n#### Basic example\n\n```crystal\nrequire \"prettytable\"\n\n# default initialize\n# table = PrettyTable::Table.new\n# table.set_headers([\"id\", \"name\", \"age\"])\n\n# initialize + set_headers\ntable = PrettyTable::Table.new([\"id\", \"name\", \"age\"])\n\ntable \u003c\u003c [\n  [\"1\", \"Melody Connolly\", \"42\"],\n  [\"2\", \"Leslie Hutchinson\", \"1\"],\n  [\"3\", \"Codey French\", \"58\"]\n]\n\nputs table\n```\nwill output\n```\n\n+----+-------------------+-----+\n| id | name              | age |\n+----+-------------------+-----+\n|  1 | Melody Connolly   |  42 |\n|  2 | Leslie Hutchinson |   1 |\n|  3 | Codey French      |  58 |\n+----+-------------------+-----+\n\n```\n*Notes:*\n+ Once headers has been set they cannot be re-set / updated.\n+ Rows cannot be added to the table before the headers has been set.\n\n#### Ways to add rows to table\n\n+ `#add_row(row : Array(String))`\n+ `#add_rows(rows : Array(Array(String)))`\n+ `#\u003c\u003c(row : Array(String))`\n+ `#\u003c\u003c(rows : Array(Array(String)))`\n\n#### Delete row\n\n```crystal\nrequire \"prettytable\"\n\ntable = PrettyTable::Table.new([\"id\", \"name\", \"age\"])\n# headers can be set as above or later with #set_headers(headers : Array(String))\ntable \u003c\u003c [\n  [\"1\", \"Melody Connolly\", \"42\"],\n  [\"2\", \"Leslie Hutchinson\", \"1\"],\n  [\"3\", \"Codey French\", \"58\"]\n]\n\ntable.delete_row(1) # =\u003e [\"2\", \"Leslie Hutchinson\", \"1\"]\n```\n\n#### Get row/column\n\n```crystal\nrequire \"prettytable\"\n\ntable = PrettyTable::Table.new([\"id\", \"name\", \"age\"])\ntable \u003c\u003c [\n  [\"1\", \"Melody Connolly\", \"42\"],\n  [\"2\", \"Leslie Hutchinson\", \"1\"],\n  [\"3\", \"Codey French\", \"58\"],\n  [\"4\", \"Lulu Sparkles\", \"23\"]\n]\n\n# get row\ntable[0] # =\u003e [\"1\", \"Melody Connolly\", \"42\"]\n\n# get rows\ntable[1..3] # =\u003e [\n            #      [\"2\", \"Leslie Hutchinson\", \"1\"],\n            #      [\"3\", \"Codey French\", \"58\"],\n            #      [\"4\", \"Lulu Sparkles\", \"23\"]\n            #    ]\n\n# get column\ntable[\"name\"] # =\u003e [\"Melody Connolly\", \"Leslie Hutchinson\", \"Codey French\"]\n```\n\n#### Add/remove a column\n\n```crystal\nrequire \"prettytable\"\n\ntable = PrettyTable::Table.new([\"id\", \"name\", \"age\"])\ntable \u003c\u003c [\n  [\"1\", \"Melody Connolly\", \"42\"],\n  [\"2\", \"Leslie Hutchinson\", \"1\"],\n  [\"3\", \"Codey French\", \"58\"],\n]\n\ntable.add_column(\"height\", [\"158\", \"163\", \"189\"])\n\ntable.remove_column(\"id\") # =\u003e [\"1\", \"2\", \"3\"]\n```\n\n#### Select multiple columns\n\n```crystal\nrequire \"prettytable\"\n\ntable = PrettyTable::Table.new([\"id\", \"name\", \"age\"])\ntable \u003c\u003c [\n  [\"1\", \"Melody Connolly\", \"42\"],\n  [\"2\", \"Leslie Hutchinson\", \"1\"],\n  [\"3\", \"Codey French\", \"58\"]\n]\n\nputs table.select([\"name\", \"age\"])\n```\nwill output\n```\n+-------------------+-----+\n| name              | age |\n+-------------------+-----+\n| Melody Connolly   |  42 |\n| Leslie Hutchinson |   1 |\n| Codey French      |  58 |\n+-------------------+-----+\n```\n\n#### Difference between two tables\n\n```crystal\nrequire \"prettytable\"\n\ntable = PrettyTable::Table.new([\"id\", \"name\", \"age\"])\ntable \u003c\u003c [\n  [\"1\", \"John Doe\", \"31\"],\n  [\"2\", \"Kelly Strong\", \"20\"],\n  [\"3\", \"James Hightower\", \"58\"],\n  [\"4\", \"Brian Muscle\", \"3\"],\n  [\"5\", \"Lulu Sparkles\", \"28\"]\n]\n\nother = PrettyTable::Table.new([\"id\", \"name\", \"age\"])\nother \u003c\u003c [\n  [\"1\", \"John Doe\", \"31\"],\n  [\"2\", \"Kelly Strong\", \"20\"],\n  [\"3\", \"James Hightower\", \"58\"]\n]\n\nputs table - other\n```\nwill output\n```\n+----+---------------+-----+\n| id | name          | age |\n+----+---------------+-----+\n|  4 | Brian Muscle  |  42 |\n|  5 | Lulu Sparkles |  58 |\n+----+---------------+-----+\n```\n\n#### Sort a table\n\n```crystal\nrequire \"prettytable\"\n\ntable = PrettyTable::Table.new([\"id\", \"name\", \"age\"])\ntable \u003c\u003c [\n  [\"1\", \"John Doe\", \"31\"],\n  [\"2\", \"Kelly Strong\", \"20\"],\n  [\"3\", \"James Hightower\", \"58\"],\n  [\"4\", \"Brian Muscle\", \"3\"],\n  [\"5\", \"Lulu Sparkles\", \"28\"]\n]\n\nsorted_table_asc = table.sort(\"name\")\nsorted_table_desc = table.sort(\"name\", false)\ncustom_sort = table.sort { |a, b| a[2] \u003c=\u003e b[2] }\n```\nSorting a table will always return a new table.\n\n#### To hash\n\n```crystal\nrequire \"prettytable\"\n\ntable = PrettyTable::Table.new([\"id\", \"name\", \"age\"])\ntable \u003c\u003c [\n  [\"1\", \"Melody Connolly\", \"42\"],\n  [\"2\", \"Leslie Hutchinson\", \"1\"],\n  [\"3\", \"Codey French\", \"58\"]\n]\n\ntable.to_h # =\u003e {\n           #      \"id\" =\u003e [\"1\", \"2\", \"3\"],\n           #      \"name\" =\u003e [\"Melody Connoly\", \"Leslie Hutchinson\", \"Codey French\"],\n           #      \"age\" =\u003e [\"42\", \"1\", \"58\"]\n           #    }\n```\n\n#### To JSON\n\n```crystal\nrequire \"prettytable\"\n\ntable = PrettyTable::Table.new([\"id\", \"name\", \"age\"])\ntable \u003c\u003c [\n  [\"1\", \"Melody Connolly\", \"42\"],\n]\n\ntable.to_json # =\u003e [{\"id\":\"1\",\"name\":\"Melody Connolly\",\"age\":\"42\"}]\n```\n\n#### To/From CSV\n\n```crystal\nrequire \"prettytable\"\n\ntable = PrettyTable::Table.new([\"id\", \"name\", \"age\"])\ntable \u003c\u003c [\n  [\"1\", \"Melody Connolly\", \"42\"],\n  [\"2\", \"Leslie Hutchinson\", \"1\"],\n  [\"3\", \"Codey French\", \"58\"]\n]\n\ntable.to_csv(\"./table.csv\") # =\u003e saves the table to table.csv\n\n# Load table from .csv\nloaded_table = PrettyTable::Table.from_csv(\"./table.csv\")\nloaded_table.headers # =\u003e [\"id\", \"name\", \"age\"]\n```\n\n## Contributing\n\n1. Fork it (\u003chttps://github.com/henrikac/prettytable/fork\u003e)\n2. Create your feature branch (`git checkout -b my-new-feature`)\n3. Commit your changes (`git commit -am 'Add some feature'`)\n4. Push to the branch (`git push origin my-new-feature`)\n5. Create a new Pull Request\n\n## Contributors\n\n- [Henrik Christensen](https://github.com/henrikac) - creator and maintainer\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhenrikac%2Fprettytable","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhenrikac%2Fprettytable","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhenrikac%2Fprettytable/lists"}