冒险村08 - Preitter output in rails console

08 - Preitter output in rails console

Rails 的 default console 显示看起来有点不好阅读、不好看、也不美丽,在开发上对於资料的显示相对於来说就是一路到底的感觉。当显示的资料表中的栏位比较多时,对於开发来说又更不容易些,这篇将介绍一些优化 console UI 的两个用过的 gems。

defualt console UI

  # bundle exec rails console
  User.all
  User Load (0.3ms)  SELECT "users".* FROM "users" /* loading for inspect */ LIMIT $1  [["LIMIT", 11]]
  => #<ActiveRecord::Relation [#<User id: 1, name: "chester", email: "[email protected]", tel: "0123456789", created_at: "2021-08-30 05:00:22.542415000 +0000", updated_at: "2021-08-30 05:00:22.542415000 +0000">, #<User id: 2, name: "chester_2", email: "[email protected]", tel: "9876543210", created_at: "2021-08-30 08:00:36.836992000 +0000", updated_at: "2021-08-30 08:00:36.836992000 +0000">, #<User id: 3, name: "chester_3", email: "[email protected]", tel: "543219876540", created_at: "2021-08-30 08:00:53.073957000 +0000", updated_at: "2021-08-30 08:00:53.073957000 +0000">]>

  User.pluck(:name)
  => ["chester", "chester_2", "chester_3"]

也可以透过 yaml 格式来让 format 更好看些

  # bundle exec rails console
  y User.first.attributes
  # or
  puts User.first.attributes.to_yaml

  User Load (0.3ms)  SELECT "users".* FROM "users" ORDER BY "users"."id" ASC LIMIT $1  [["LIMIT", 1]]
  ---
  id: 1
  name: chester
  email: [email protected]
  tel: '0123456789'
  created_at: !ruby/object:ActiveSupport::TimeWithZone
    utc: 2021-08-30 05:00:22.542415000 Z
    zone: &1 !ruby/object:ActiveSupport::TimeZone
      name: Etc/UTC
    time: 2021-08-30 05:00:22.542415000 Z
  updated_at: !ruby/object:ActiveSupport::TimeWithZone
    utc: 2021-08-30 05:00:22.542415000 Z
    zone: *1
    time: 2021-08-30 05:00:22.542415000 Z
  => nil

注: 各人觉得相较来看安装底下两个 gems 还是比较好看,使用经验觉得都满好用的,看团队决定要安装哪个都好~


pry-rails

gem install

  # Gemfile
  group :development, :test do
    gem "pry-rails"
  end

pry-rails console UI

  # bundle exec rails console
  User Load (0.4ms)  SELECT "users".* FROM "users"
  => [#<User:0x00007fb5a1bf1770
    id: 1,
    name: "chester",
    email: "[email protected]",
    tel: "0123456789",
    created_at: Mon, 30 Aug 2021 05:00:22.542415000 UTC +00:00,
    updated_at: Mon, 30 Aug 2021 05:00:22.542415000 UTC +00:00>,
  #<User:0x00007fb5611bb188
    id: 2,
    name: "chester_2",
    email: "[email protected]",
    tel: "9876543210",
    created_at: Mon, 30 Aug 2021 08:00:36.836992000 UTC +00:00,
    updated_at: Mon, 30 Aug 2021 08:00:36.836992000 UTC +00:00>,
  #<User:0x00007fb5611ba850
    id: 3,
    name: "chester_3",
    email: "[email protected]",
    tel: "543219876540",
    created_at: Mon, 30 Aug 2021 08:00:53.073957000 UTC +00:00,
    updated_at: Mon, 30 Aug 2021 08:00:53.073957000 UTC +00:00>]
  
  User.pluck(:name)
  => ["chester",
 "chester_2",
 "chester_3"]

awesome_print

gem install

  # Gemfile
  group :development, :test do
    gem "awesome_print"
  end

Setting ~/.irbrc

  # ~/.irbrc

  # require
  require "awesome_print"
  # set format in rails console
  AwesomePrint.irb!

  # put hacky convenience methods here

awesome_print console UI

  # bundle exec rails console
  User.all
  User Load (0.4ms)  SELECT "users".* FROM "users"
  [
    [0] #<User:0x00007fd4c407c5b0> {
                :id => 1,
              :name => "chester",
             :email => "[email protected]",
               :tel => "0123456789",
        :created_at => Mon, 30 Aug 2021 05:00:22.542415000 UTC +00:00,
        :updated_at => Mon, 30 Aug 2021 05:00:22.542415000 UTC +00:00
    },
    [1] #<User:0x00007fd4c407c4e8> {
                :id => 2,
              :name => "chester_2",
             :email => "[email protected]",
               :tel => "9876543210",
        :created_at => Mon, 30 Aug 2021 08:00:36.836992000 UTC +00:00,
        :updated_at => Mon, 30 Aug 2021 08:00:36.836992000 UTC +00:00
    },
    [2] #<User:0x00007fd4c407c420> {
                :id => 3,
              :name => "chester_3",
             :email => "[email protected]",
               :tel => "543219876540",
        :created_at => Mon, 30 Aug 2021 08:00:53.073957000 UTC +00:00,
        :updated_at => Mon, 30 Aug 2021 08:00:53.073957000 UTC +00:00
    }
  ]

  User.pluck(:name)
  [
    [0] "chester",
    [1] "chester_2",
    [2] "chester_3"
  ]

延伸阅读

满推这篇 rails console 设定来做达到更好的效果,不过有时候又觉得太麻烦了啊~还是用单一一种来处理就好,提供参考。

https://www.bruceli.net/tw/2014/08/13/awesome-rails-console-customization-using-pry.html

参考来源

My blog


<<:  Day 10:新手杀,谁来教我离开 vim 阿~~

>>:  让按钮来个酷动态! 操纵DOM事件:CSS 篇 (一)

Microsoft Azure Machine Learning - Day 1

Chap.O 基础 & 简介: Prat1. Azure Machine Learning ...

永远不回头的离职档案管理

或许有人会说:「离职前就给他摆烂,什麽都不要做,以後主管打电话来讨档案,也不要接,反正我不在这间公司...

Final Day

整体感言: 第一次参与这样的活动,非常的好玩,但是因为之前累积的技术的存底明显不够,变成内容会有点太...

Day 20 To Do List - 封装

第 20 天~ 走到了三分之二了~!!! 好激动阿!! 再接再厉~ 加油! 昨天把 To Do Li...

#16 No-code 之旅 — Project Setup

嗨嗨~ 今天比较晚下班,还没写文章也还没写专案XD 今天先建立专案好了~ 周末再赶进度! Setup...