LoginSignup
3
4

More than 5 years have passed since last update.

pryでlsした際のオブジェクト名の色を変更する

Last updated at Posted at 2015-10-17

問題

  • Windowsでcmd.exeでpryを使っている
  • cmd.exeの色の設定などは標準のまま
  • たとえばls Loggerとした場合、constantsに表示されるクラス名が青色のため、ほとんど見えない

解決策

.pryrcファイルにて設定を実施

Pry.config.ls['class_constant_color'] = :bright_blue
Pry.config.ls['instance_var_color'] = :bright_cyan

詳細

pry-themeを使用したいので最初はそちらの設定かと思ったが、考えてみればpryのlsコマンドの出力結果であって。

それらしい下記を見ると
https://github.com/pry/pry/tree/master/lib/pry/commands/ls
constants.rbというのがあるので覗いてみた。

https://github.com/pry/pry/blob/master/lib/pry/commands/ls/constants.rb#L33
なんかcolor(:exception_constant, name)とか、それっぽいのを発見

#colorは下記に
https://github.com/pry/pry/blob/master/lib/pry/commands/ls/formatter.rb#L20

      def color(type, str)
        Pry::Helpers::Text.send _pry_.config.ls["#{type}_color"], str
      end

_pry_.config.lsにkey名投げれば色の名前が返ってくる

Pry.config.ls['class_constant_color'] # => :blue
Pry.config.ls['instance_var_color'] # => :blue

で、先頭に書いたように設定をしてみたら色が変わった。
なお、設定する色の名前は
https://github.com/pry/pry/blob/master/lib/pry/helpers/text.rb
COLORSに定義されていて、key名もしくはkey名にprefixでbright_を付けた9色 x 2パターン、
かと思いきやpurplemagentaは同じだったので8色 x 2パターン = 16種類。

補足

lsに設定されているxxx_colorなもの一覧は下記のようにして確認してみた

Pry.config.ls.keys.sort.grep(/color/).each do |key|
  puts "#{key.ljust(25)} = :#{Pry.config.ls[key]}"
end
# builtin_global_color     = cyan
# class_constant_color     = bright_blue
# class_var_color          = bright_blue
# constant_color           = default
# exception_constant_color = magenta
# global_var_color         = default
# heading_color            = bright_blue
# instance_var_color       = blue
# local_var_color          = yellow
# method_missing_color     = bright_red
# private_method_color     = blue
# protected_method_color   = blue
# pry_var_color            = default
# pseudo_global_color      = cyan
# public_method_color      = default
# unloaded_constant_color  = yellow

と思ったらもろ下記にかいてあります
https://github.com/pry/pry/blob/master/lib/pry/commands/ls.rb#L4

require 'pry/commands/ls/ls_entity'
class Pry
  class Command::Ls < Pry::ClassCommand
    DEFAULT_OPTIONS = {
      :heading_color            => :bright_blue,
      :public_method_color      => :default,
      :private_method_color     => :blue,
      :protected_method_color   => :blue,
      :method_missing_color     => :bright_red,
      :local_var_color          => :yellow,
      :pry_var_color            => :default,     # e.g. _, _pry_, _file_
      :instance_var_color       => :blue,        # e.g. @foo
      :class_var_color          => :bright_blue, # e.g. @@foo
      :global_var_color         => :default,     # e.g. $CODERAY_DEBUG, $eventmachine_library
      :builtin_global_color     => :cyan,        # e.g. $stdin, $-w, $PID
      :pseudo_global_color      => :cyan,        # e.g. $~, $1..$9, $LAST_MATCH_INFO
      :constant_color           => :default,     # e.g. VERSION, ARGF
      :class_constant_color     => :blue,        # e.g. Object, Kernel
      :exception_constant_color => :magenta,     # e.g. Exception, RuntimeError
      :unloaded_constant_color  => :yellow,      # Any constant that is still in .autoload? state
      :separator                => "  ",
      :ceiling                  => [Object, Module, Class]
    }

...
3
4
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
3
4