tty-table icon indicating copy to clipboard operation
tty-table copied to clipboard

Create table directly from Hash

Open vaibhavatul47 opened this issue 8 years ago • 4 comments

It would be great to add support for data saved in array of hash. Eg:

users = [
  { name: "Foo", city: "XYZ", admin: true },
  { name: "Bar", zip: 987654, city: "ABC"}
]

table = TTY::Table.new(users)
renderer = TTY::Table::Renderer::Unicode.new(table)
renderer.render

Output:

#  ┌─────┬───────┬─────┬─────┐
#  │name │zip    │city │admin│
#  ├─────┼───────┼─────┼─────┤
#  │Foo  │       │XYZ  │✔    │
#  │Bar  │987654 │ABC  │     │
#  └─────┴───────┴─────┴─────┘

Current implementation expects each header to have value defined for each row. If values are not available then it is to be replaced by nil. For above example sample input would be:

users = [ {
  name:  ["Foo", "Bar"],
  zip:   [nil, 987654],
  city:  ["XYZ", "ABC"],
  admin: [true, nil]
} ]

I can submit a PR if required.

vaibhavatul47 avatar Apr 05 '18 05:04 vaibhavatul47

Hi @vaibhavatul47; if you have a patch for this completed already, let me know - otherwise I may tackle this.

slowbro avatar Oct 07 '19 18:10 slowbro

I had it somewhere on my machine, I'll share the patch shortly.

vaibhavatul47 avatar Oct 08 '19 15:10 vaibhavatul47

I know this is old, but @vaibhavatul47 this would be super nice to have :)

dishcandanty avatar Mar 31 '21 22:03 dishcandanty