File: show_csv_table.rb

package info (click to toggle)
ruby-terminal-table 4.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 232 kB
  • sloc: ruby: 875; makefile: 3
file content (34 lines) | stat: -rwxr-xr-x 817 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#!/usr/bin/env ruby

require "csv"
$LOAD_PATH << "#{__dir__}/../lib"
require "terminal-table"

#
# Usage:
#   ./show_csv_table.rb data.csv
#   cat data.csv | ./show_csv_table.rb
#   cat data.csv | ./show_csv_table.rb -
#
#
# Reads a CSV from $stdin if no argument given, or argument is '-'
# otherwise interprets first cmdline argument as the CSV filename
#
use_stdin = ARGV[0].nil? || (ARGV[0] == '-')
io_object = use_stdin ? $stdin : File.open(ARGV[0], 'r')
csv = CSV.new(io_object)

#
# Convert to an array for use w/ terminal-table
# The assumption is that this is a pretty small spreadsheet.
#
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..-1]
end

puts user_table