tj / terminal-table

Ruby ASCII Table Generator, simple and feature rich.
MIT License
1.53k stars 121 forks source link

CLI / shell usage? #116

Closed luckman212 closed 3 years ago

luckman212 commented 3 years ago

Not strictly an issue, but I am wondering if it's possible to use this nice-looking tool to process data from a shell pipe (stdin) or maybe csv/tsv file on disk? Has anyone done this?

nanobowers commented 3 years ago

@luckman212

I threw this together quickly, should work on a CSV applied to stdin or a file-name argument. Using new unicode styling from v3.0.0.

#!/usr/bin/env ruby

require "csv"
require "terminal-table"

io_object = ARGV[0] ? File.open(ARGV[0], 'r') : $stdin
csv = CSV.new(io_object)

csv_array = csv.to_a

user_table = Terminal::Table.new do |v|
  v.style = { :border => :unicode_round } # >= v3.0.0
  v.title = "Some Title"
  v.headings = csv_array[0]
  v.rows = csv_array[1..]
end
puts user_table
nateberkopec commented 3 years ago

@nanobowers That's too good to leave in an issue... maybe add to README?

nanobowers commented 3 years ago

@nanobowers Will do, I found some other factual errors in the readme that I need to fix and will add this example as part of that PR when i get some time.

nateberkopec commented 3 years ago

When you do, mark this issue as closed 👍

nanobowers commented 3 years ago

Addressed (documented) in #120 @nateberkopec would close, but don't have the ability to close it since I didn't open the issue.

nateberkopec commented 3 years ago

@nanobowers in the future, you can link using a keyword which is what I was trying to refer to above. Saves me a click.