1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Ruby: pry の起動時に色付きバナーを出すスクリプト

Last updated at Posted at 2014-12-11

pry 用バナー

pry の起動時に色付きバナーを出すスクリプトです。(irb でも動作します)
バナーはスクロールアップしながら表示されます。

color-banner.gif

バナーは RUBY のバージョンによって色を変えています。

  • 2.3 の場合 : 黄色 (※未確認...ruby2.3 がでたらチェックしてみます)
  • 2.2 の場合 : 水色 (※未確認...ruby2.2 がでたらチェックしてみます)
  • 2.1 の場合 : 赤 (上の画像)
  • 2.0 の場合 : 青 (下の画像)
  • 1.9 の場合 : マゼンタ
  • 1.8 の場合 : 緑

(※下の画像はスクリプト単体で起動したところです)

ruby20.png

使い方

  1. 下のスクリプト 'banner.rb' を $RUBYLIB で指定されるディレクトリに置きます。
  2. ~/.pryrc または ~/.irbrc に以下の1行を追加します。
require 'banner'

以上です。

banner.rb
# !/usr/bin/env ruby

instance_eval do
  #
  # configuration
  #
  version_color = RUBY_VERSION >= '2.3' ? :YELLOW  :
                  RUBY_VERSION >= '2.2' ? :CYAN    :
                  RUBY_VERSION >= '2.1' ? :RED     :
                  RUBY_VERSION >= '2.0' ? :BLUE    :
                  RUBY_VERSION >= '1.9' ? :MAGENTA :
                  RUBY_VERSION >= '1.8' ? :GREEN   : false

  #
  # procedure
  #
  seq = proc {|n| $stdout.isatty ? "\e[#{n}m" : "" }

  color = proc do |s|
    n = {
      :BLACK    => 30,
      :RED      => 31,
      :GREEN    => 32,
      :YELLOW   => 33,
      :BLUE     => 34,
      :MAGENTA  => 35,
      :CYAN     => 36,
      :WHITE    => 37,
    }[version_color]

    "#{seq[n]}#{s}#{seq[0]}"
  end

  cls  = "\e[2J"
  move = proc {|y| "\e[#{y}H" }

  #
  # banner / description
  #
  banner = <<-'BANNER_DEFAULT'
      ######  #     # ######  #     #
      #     # #     # #     #  #   #
      #     # #     # #     #   # #
      ######  #     # ######     #
      #   #   #     # #     #    #
      #    #  #     # #     #    #
      #     #  #####  ######     #
  BANNER_DEFAULT

  descr = <<-DESCR
  #{RUBY_DESCRIPTION.sub(/\Aruby \S+/){ color[$&] }}
  #{RUBY_COPYRIGHT}
  DESCR

  #
  # print banner
  #
  if $stdout.isatty
    lines = banner.split $/
    top = 2

    [*0...lines.size].reverse.each_with_index do |y, i|
      puts cls
      puts move[top + y             ] + color[lines[0..i] * $/]
      puts move[top + lines.size + 1] + descr
      puts

      sleep 0.1
    end

  else
    puts $/ + banner + $/ + descr + $/

  end
end

# vim:set ts=2 sw=2 et:

Gem化

(※2014.12.14追記)
このスクリプトを Gem 化しました。
Gem版は名前が変更されています。また、モジュール化されています。

$ gem install color_banner

使い方も異なります。
[使い方] ~/.pryrc に以下の行を追加する

require 'color_banner'
ColorBanner.banner

今後は Gem 化版をメインに保守するつもりです。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?