Add default color for each instance

This commit is contained in:
Nobuyoshi Nakada 2020-08-11 16:17:51 +09:00
Родитель 5af983af4f
Коммит 2e7fe3b687
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 7CD2805BFA3770C6
1 изменённых файлов: 8 добавлений и 4 удалений

Просмотреть файл

@ -1,8 +1,12 @@
# frozen-string-literal: true # frozen-string-literal: true
class Colorize class Colorize
# call-seq:
# Colorize.new(colorize = nil)
# Colorize.new(color: color, colors_file: colors_file)
def initialize(color = nil, opts = ((_, color = color, nil)[0] if Hash === color)) def initialize(color = nil, opts = ((_, color = color, nil)[0] if Hash === color))
@colors = @reset = nil @colors = @reset = nil
@color = (opts[:color] if opts)
if color or (color == nil && STDOUT.tty?) if color or (color == nil && STDOUT.tty?)
if (/\A\e\[.*m\z/ =~ IO.popen("tput smso", "r", :err => IO::NULL, &:read) rescue nil) if (/\A\e\[.*m\z/ =~ IO.popen("tput smso", "r", :err => IO::NULL, &:read) rescue nil)
@beg = "\e[" @beg = "\e["
@ -29,7 +33,8 @@ class Colorize
"bold"=>"1", "underline"=>"4", "reverse"=>"7", "bold"=>"1", "underline"=>"4", "reverse"=>"7",
} }
def decorate(str, name) # colorize.decorate(str, name = color_name)
def decorate(str, name = @color)
if @colors and color = (@colors[name] || DEFAULTS[name]) if @colors and color = (@colors[name] || DEFAULTS[name])
"#{@beg}#{color}m#{str}#{@reset}" "#{@beg}#{color}m#{str}#{@reset}"
else else
@ -45,7 +50,6 @@ class Colorize
end end
if $0 == __FILE__ if $0 == __FILE__
colorize = Colorize.new colorize = Colorize.new(ARGV.shift)
col = ARGV.shift ARGV.each {|str| puts colorize.decorate(str)}
ARGV.each {|str| puts colorize.decorate(str, col)}
end end