/org_tp

OrgTp shows text table like emacs org-table for easy reading.

Primary LanguageRuby

Gem Version

OrgTp

OrgTp shows text table like emacs org-table for easy reading.

tp Object.constants.grep(/RUBY_/).map { |e| [e, Object.const_get(e)] }.to_h

# >> |---------------------+------------------------------------------------------------|
# >> |        RUBY_VERSION | 2.5.0                                                      |
# >> |   RUBY_RELEASE_DATE | 2017-12-25                                                 |
# >> |       RUBY_PLATFORM | x86_64-darwin16                                            |
# >> |     RUBY_PATCHLEVEL | 0                                                          |
# >> |    RUBY_DESCRIPTION | ruby 2.5.0p0 (2017-12-25 revision 61468) [x86_64-darwin16] |
# >> |         RUBY_ENGINE | ruby                                                       |
# >> |       RUBY_REVISION | 61468                                                      |
# >> | RUBY_ENGINE_VERSION | 2.5.0                                                      |
# >> |      RUBY_COPYRIGHT | ruby - Copyright (C) 1993-2017 Yukihiro Matsumoto          |
# >> |---------------------+------------------------------------------------------------|

Installation

Install as a standalone gem

$ gem install org_tp

Or install within application using Gemfile

$ bundle add org_tp
$ bundle install

Examples

Array of hash

tp [{id: 1, name: 'alice'}, {id: 2, name: 'bob'}]
# >> |----+-------|
# >> | id | name  |
# >> |----+-------|
# >> |  1 | alice |
# >> |  2 | bob   |
# >> |----+-------|

Hash

tp({id: 1, name: 'alice'})
# >> |------+-------|
# >> |   id | 1     |
# >> | name | alice |
# >> |------+-------|

Array

tp [:alice, :bob]
# >> |-------|
# >> | alice |
# >> | bob   |
# >> |-------|

ActiveRecord or Mongoid

['alice', 'bob'].each { |e| User.create!(name: e) }
tp User
# >> |----+-------|
# >> | id | name  |
# >> |----+-------|
# >> |  1 | alice |
# >> |  2 | bob   |
# >> |----+-------|
tp User.limit(1)
# >> |----+-------|
# >> | id | name  |
# >> |----+-------|
# >> |  1 | alice |
# >> |----+-------|
tp User.first
# >> |------+-------|
# >> |   id | 1     |
# >> | name | alice |
# >> |------+-------|

ActiveRecord::Result

tp ActiveRecord::Base.connection.select_all('SELECT * FROM users')
# >> |----+-------|
# >> | id | name  |
# >> |----+-------|
# >> |  1 | alice |
# >> |  2 | bob   |
# >> |----+-------|

How to table as string

Use to_t method.

puts [{id: 1, name: 'alice'}, {id: 2, name: 'bob'}].to_t
# >> |----+-------|
# >> | id | name  |
# >> |----+-------|
# >> |  1 | alice |
# >> |  2 | bob   |
# >> |----+-------|

Options

Pass as the second argument to tp or the first argument to to_t.

tp 1
# >> |---|
# >> | 1 |
# >> |---|

tp 1, intersection_both: '+'
# >> +---+
# >> | 1 |
# >> +---+

Markdown format example

markdown: true has the same meaning as intersection: '|', cover: false

tp [{id: 1, name: 'alice'}, {id: 2, name: 'bob'}], markdown: true
# >> | id | name  |
# >> |----|-------|
# >> |  1 | alice |
# >> |  2 | bob   |
tp [{id: 1, name: 'alice'}, {id: 2, name: 'bob'}], intersection: '|', cover: false
# >> | id | name  |
# >> |----|-------|
# >> |  1 | alice |
# >> |  2 | bob   |

Global Options

tp OrgTp.default_options
# >> |-------------------+-------|
# >> |          markdown | false |
# >> |            header |       |
# >> |             cover | true  |
# >> |          vertical | |     |
# >> |      intersection | +     |
# >> | intersection_both | |     |
# >> |           horizon | -     |
# >> |           padding |       |
# >> |           in_code | UTF-8 |
# >> |-------------------+-------|

tp 1
# >> |---|
# >> | 1 |
# >> |---|

OrgTp.default_options[:intersection_both] = '+'

tp 1
# >> +---+
# >> | 1 |
# >> +---+