Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/akicho8/table_format

TableFormat shows text table like emacs org-table for easy reading.
https://github.com/akicho8/table_format

data-visualization ruby table

Last synced: about 2 months ago
JSON representation

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

Awesome Lists containing this project

README

        

#+html: Gem Version
#+html:
#+html:

* TableFormat

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

#+BEGIN_SRC ruby
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 |
# >> |---------------------+------------------------------------------------------------|
#+END_SRC

In the case of Rails

#+BEGIN_SRC ruby
class ApplicationController < ActionController::Base
if Rails.env.development?
before_action do
logger.debug params.to_unsafe_h.to_t(truncate: 40)
end
end
end
#+END_SRC

#+BEGIN_SRC shell
% 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 |
|-------------+---------------------------------------------|
#+END_SRC

** Installation

Install as a standalone gem

#+BEGIN_SRC shell-script
$ gem install table_format
#+END_SRC

Or install within application using Gemfile

#+BEGIN_SRC shell-script
$ bundle add table_format
$ bundle install
#+END_SRC

** Examples

*** Array of hash

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

*** Hash

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

*** Array

#+BEGIN_SRC ruby
tp [:alice, :bob]
# >> |-------|
# >> | alice |
# >> | bob |
# >> |-------|
#+END_SRC

*** ActiveRecord or Mongoid

#+BEGIN_SRC ruby
['alice', 'bob'].each { |e| User.create!(name: e) }
#+END_SRC

#+BEGIN_SRC ruby
tp User
# >> |----+-------|
# >> | id | name |
# >> |----+-------|
# >> | 1 | alice |
# >> | 2 | bob |
# >> |----+-------|
#+END_SRC

#+BEGIN_SRC ruby
tp User.limit(1)
# >> |----+-------|
# >> | id | name |
# >> |----+-------|
# >> | 1 | alice |
# >> |----+-------|
#+END_SRC

#+BEGIN_SRC ruby
tp User.first
# >> |------+-------|
# >> | id | 1 |
# >> | name | alice |
# >> |------+-------|
#+END_SRC

**** ActiveRecord::Result

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

** How to table as string

Use to_t method.

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

** Options

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

#+BEGIN_SRC ruby
tp 1
# >> |---|
# >> | 1 |
# >> |---|

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

*** Markdown format example

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

#+BEGIN_SRC ruby
tp [{id: 1, name: 'alice'}, {id: 2, name: 'bob'}], markdown: true
# >> | id | name |
# >> |----|-------|
# >> | 1 | alice |
# >> | 2 | bob |
#+END_SRC

#+BEGIN_SRC ruby
tp [{id: 1, name: 'alice'}, {id: 2, name: 'bob'}], intersection: '|', cover: false
# >> | id | name |
# >> |----|-------|
# >> | 1 | alice |
# >> | 2 | bob |
#+END_SRC

** Global Options

#+BEGIN_SRC ruby
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 |
# >> +---+
#+END_SRC