0.0
Low commit activity in last 3 years
A long-lived project that still receives updates
TableFormat shows text table like emacs org-table for easy reading.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

Runtime

 Project Readme
Gem Version

TableFormat

TableFormat 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          |
# >> |---------------------+------------------------------------------------------------|

In the case of Rails

class ApplicationController < ActionController::Base
  if Rails.env.development?
    before_action do
      logger.debug params.to_unsafe_h.to_t(truncate: 40)
    end
  end
end
% cat log/development.log
Started POST "/api/xy_master/time_records" for ::1 at 2021-11-18 13:52:25 +0900
Processing by Api::XyMaster::TimeRecordsController#create as JSON
  Parameters: (snip)
|-------------+---------------------------------------------|
|   scope_key | scope_today                                 |
| time_record | {"rule_key"=>"rule100t", "spent_sec"=>0,... |
|      format | json                                        |
|  controller | api/xy_master/time_records                  |
|      action | create                                      |
|-------------+---------------------------------------------|

Installation

Install as a standalone gem

$ gem install table_format

Or install within application using Gemfile

$ bundle add table_format
$ 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 TableFormat.default_options
# >> |-------------------+-------|
# >> |          markdown | false |
# >> |            header |       |
# >> |             cover | true  |
# >> |          vertical | |     |
# >> |      intersection | +     |
# >> | intersection_both | |     |
# >> |           horizon | -     |
# >> |           padding |       |
# >> |           in_code | UTF-8 |
# >> |-------------------+-------|

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

TableFormat.default_options[:intersection_both] = '+'

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