Skip to content

Commit

Permalink
fixing misstatement in README, adding csv-to-terminal-table recipe in…
Browse files Browse the repository at this point in the history
… the examples dir and README
  • Loading branch information
nanobowers committed Feb 5, 2021
1 parent 37f13c1 commit 4bb0a7e
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 1 deletion.
22 changes: 21 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ table.style.border[:nw] = '*' # override the north-west corner of the table

### Customizing row separators

Row-separators can now be customized in a variety of ways. The default separator's border_type is referred to as `:mid`. Additional `:strong` / `:strong_a` and `:strong_b` separator styles can be applied to separate sections (e.g. header/footer/title).
Row-separators can now be customized in a variety of ways. The default separator's border_type is referred to as `:div`. Additional separator border types (e.g. `:double`, `:heavy`, `:dash` - see full list below) can be applied to separate the sections (e.g. header/footer/title).

The separator border_type may be specified when a user-defined separator added. Alternatively, borders may be adjusted after the table's rows are elaborated, but before the table is rendered.

Expand Down Expand Up @@ -379,6 +379,26 @@ rows[2].border_type = :heavy # modify separator row: emphasize below title
puts table.render
```

## Example: Displaying a small CSV spreadsheet

See `examples/show_csv_table.rb` in source distribution

```ruby
#!/usr/bin/env ruby
require "csv"
require "terminal-table"
use_stdin = ARGV[0].nil? || (ARGV[0] == '-')
io_object = use_stdin ? $stdin : File.open(ARGV[0], 'r')
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..-1]
end
puts user_table
```
## More examples

For more examples, please see the `examples` directory included in the
Expand Down
4 changes: 4 additions & 0 deletions examples/data.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
First Name,Last Name,Email
TJ,Holowaychuk,tj@vision-media.ca
Bob,Someone,bob@vision-media.ca
Joe,Whatever,joe@vision-media.ca
34 changes: 34 additions & 0 deletions examples/show_csv_table.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,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

0 comments on commit 4bb0a7e

Please sign in to comment.