This commit is contained in:
@@ -0,0 +1,13 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require_relative 'rainbow/global'
|
||||
|
||||
module Rainbow
|
||||
def self.new
|
||||
Wrapper.new(global.enabled)
|
||||
end
|
||||
|
||||
self.enabled = false unless STDOUT.tty? && STDERR.tty?
|
||||
self.enabled = false if ENV['TERM'] == 'dumb'
|
||||
self.enabled = true if ENV['CLICOLOR_FORCE'] == '1'
|
||||
end
|
||||
@@ -0,0 +1,150 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module Rainbow
|
||||
class Color
|
||||
attr_reader :ground
|
||||
|
||||
def self.build(ground, values)
|
||||
unless [1, 3].include?(values.size)
|
||||
raise ArgumentError,
|
||||
"Wrong number of arguments for color definition, should be 1 or 3"
|
||||
end
|
||||
|
||||
color = values.size == 1 ? values.first : values
|
||||
|
||||
case color
|
||||
# NOTE: Properly handle versions before/after Ruby 2.4.0.
|
||||
# Ruby 2.4+ unifies Fixnum & Bignum into Integer.
|
||||
# However previous versions would still use Fixnum.
|
||||
# To avoid missing `Fixnum` input, call `0.class` which would
|
||||
# return either `Integer` or `Fixnum`.
|
||||
when 0.class
|
||||
Indexed.new(ground, color)
|
||||
when Symbol
|
||||
if Named.color_names.include?(color)
|
||||
Named.new(ground, color)
|
||||
elsif X11Named.color_names.include?(color)
|
||||
X11Named.new(ground, color)
|
||||
else
|
||||
raise ArgumentError,
|
||||
"Unknown color name, valid names: " +
|
||||
(Named.color_names + X11Named.color_names).join(', ')
|
||||
end
|
||||
when Array
|
||||
RGB.new(ground, *color)
|
||||
when String
|
||||
RGB.new(ground, *parse_hex_color(color))
|
||||
end
|
||||
end
|
||||
|
||||
def self.parse_hex_color(hex)
|
||||
unless hex =~ /^#?[a-f0-9]{6}/i
|
||||
raise ArgumentError,
|
||||
"Invalid hexadecimal RGB triplet. Valid format: /^#?[a-f0-9]{6}/i"
|
||||
end
|
||||
|
||||
hex = hex.sub(/^#/, '')
|
||||
r = hex[0..1].to_i(16)
|
||||
g = hex[2..3].to_i(16)
|
||||
b = hex[4..5].to_i(16)
|
||||
|
||||
[r, g, b]
|
||||
end
|
||||
|
||||
class Indexed < Color
|
||||
attr_reader :num
|
||||
|
||||
def initialize(ground, num)
|
||||
@ground = ground
|
||||
@num = num
|
||||
end
|
||||
|
||||
def codes
|
||||
code = num + (ground == :foreground ? 30 : 40)
|
||||
|
||||
[code]
|
||||
end
|
||||
end
|
||||
|
||||
class Named < Indexed
|
||||
NAMES = {
|
||||
black: 0,
|
||||
red: 1,
|
||||
green: 2,
|
||||
yellow: 3,
|
||||
blue: 4,
|
||||
magenta: 5,
|
||||
cyan: 6,
|
||||
white: 7,
|
||||
default: 9
|
||||
}.freeze
|
||||
|
||||
def self.color_names
|
||||
NAMES.keys
|
||||
end
|
||||
|
||||
def self.valid_names
|
||||
color_names.join(', ')
|
||||
end
|
||||
|
||||
def initialize(ground, name)
|
||||
unless Named.color_names.include?(name)
|
||||
raise ArgumentError,
|
||||
"Unknown color name, valid names: #{self.class.valid_names}"
|
||||
end
|
||||
|
||||
super(ground, NAMES[name])
|
||||
end
|
||||
end
|
||||
|
||||
class RGB < Indexed
|
||||
attr_reader :r, :g, :b
|
||||
|
||||
def self.to_ansi_domain(value)
|
||||
(6 * (value / 256.0)).to_i
|
||||
end
|
||||
|
||||
def initialize(ground, *values)
|
||||
if values.min.negative? || values.max > 255
|
||||
raise ArgumentError, "RGB value outside 0-255 range"
|
||||
end
|
||||
|
||||
super(ground, 8)
|
||||
@r, @g, @b = values
|
||||
end
|
||||
|
||||
def codes
|
||||
super + [5, code_from_rgb]
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def code_from_rgb
|
||||
16 + self.class.to_ansi_domain(r) * 36 +
|
||||
self.class.to_ansi_domain(g) * 6 +
|
||||
self.class.to_ansi_domain(b)
|
||||
end
|
||||
end
|
||||
|
||||
class X11Named < RGB
|
||||
include X11ColorNames
|
||||
|
||||
def self.color_names
|
||||
NAMES.keys
|
||||
end
|
||||
|
||||
def self.valid_names
|
||||
color_names.join(', ')
|
||||
end
|
||||
|
||||
def initialize(ground, name)
|
||||
unless X11Named.color_names.include?(name)
|
||||
raise ArgumentError,
|
||||
"Unknown color name, valid names: #{self.class.valid_names}"
|
||||
end
|
||||
|
||||
super(ground, *NAMES[name])
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,64 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require 'rainbow'
|
||||
|
||||
module Rainbow
|
||||
module Ext
|
||||
module String
|
||||
module InstanceMethods
|
||||
def foreground(*color)
|
||||
Rainbow(self).foreground(*color)
|
||||
end
|
||||
|
||||
alias color foreground
|
||||
alias colour foreground
|
||||
|
||||
def background(*color)
|
||||
Rainbow(self).background(*color)
|
||||
end
|
||||
|
||||
def reset
|
||||
Rainbow(self).reset
|
||||
end
|
||||
|
||||
def bright
|
||||
Rainbow(self).bright
|
||||
end
|
||||
|
||||
def faint
|
||||
Rainbow(self).faint
|
||||
end
|
||||
|
||||
def italic
|
||||
Rainbow(self).italic
|
||||
end
|
||||
|
||||
def underline
|
||||
Rainbow(self).underline
|
||||
end
|
||||
|
||||
def blink
|
||||
Rainbow(self).blink
|
||||
end
|
||||
|
||||
def inverse
|
||||
Rainbow(self).inverse
|
||||
end
|
||||
|
||||
def hide
|
||||
Rainbow(self).hide
|
||||
end
|
||||
|
||||
def cross_out
|
||||
Rainbow(self).cross_out
|
||||
end
|
||||
|
||||
alias strike cross_out
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
class String
|
||||
include Rainbow::Ext::String::InstanceMethods
|
||||
end
|
||||
@@ -0,0 +1,25 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require_relative 'wrapper'
|
||||
|
||||
module Rainbow
|
||||
def self.global
|
||||
@global ||= Wrapper.new
|
||||
end
|
||||
|
||||
def self.enabled
|
||||
global.enabled
|
||||
end
|
||||
|
||||
def self.enabled=(value)
|
||||
global.enabled = value
|
||||
end
|
||||
|
||||
def self.uncolor(string)
|
||||
StringUtils.uncolor(string)
|
||||
end
|
||||
end
|
||||
|
||||
def Rainbow(string)
|
||||
Rainbow.global.wrap(string.to_s)
|
||||
end
|
||||
@@ -0,0 +1,100 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module Rainbow
|
||||
class NullPresenter < ::String
|
||||
def color(*_values)
|
||||
self
|
||||
end
|
||||
|
||||
def background(*_values)
|
||||
self
|
||||
end
|
||||
|
||||
def reset
|
||||
self
|
||||
end
|
||||
|
||||
def bright
|
||||
self
|
||||
end
|
||||
|
||||
def faint
|
||||
self
|
||||
end
|
||||
|
||||
def italic
|
||||
self
|
||||
end
|
||||
|
||||
def underline
|
||||
self
|
||||
end
|
||||
|
||||
def blink
|
||||
self
|
||||
end
|
||||
|
||||
def inverse
|
||||
self
|
||||
end
|
||||
|
||||
def hide
|
||||
self
|
||||
end
|
||||
|
||||
def cross_out
|
||||
self
|
||||
end
|
||||
|
||||
def black
|
||||
self
|
||||
end
|
||||
|
||||
def red
|
||||
self
|
||||
end
|
||||
|
||||
def green
|
||||
self
|
||||
end
|
||||
|
||||
def yellow
|
||||
self
|
||||
end
|
||||
|
||||
def blue
|
||||
self
|
||||
end
|
||||
|
||||
def magenta
|
||||
self
|
||||
end
|
||||
|
||||
def cyan
|
||||
self
|
||||
end
|
||||
|
||||
def white
|
||||
self
|
||||
end
|
||||
|
||||
def method_missing(method_name, *args)
|
||||
if Color::X11Named.color_names.include?(method_name) && args.empty?
|
||||
self
|
||||
else
|
||||
super
|
||||
end
|
||||
end
|
||||
|
||||
def respond_to_missing?(method_name, *args)
|
||||
Color::X11Named.color_names.include?(method_name) && args.empty? || super
|
||||
end
|
||||
|
||||
alias foreground color
|
||||
alias fg color
|
||||
alias bg background
|
||||
alias bold bright
|
||||
alias dark faint
|
||||
alias strike cross_out
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,144 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require_relative 'string_utils'
|
||||
require_relative 'x11_color_names'
|
||||
require_relative 'color'
|
||||
|
||||
module Rainbow
|
||||
class Presenter < ::String
|
||||
TERM_EFFECTS = {
|
||||
reset: 0,
|
||||
bright: 1,
|
||||
faint: 2,
|
||||
italic: 3,
|
||||
underline: 4,
|
||||
blink: 5,
|
||||
inverse: 7,
|
||||
hide: 8,
|
||||
cross_out: 9
|
||||
}.freeze
|
||||
|
||||
# Sets color of this text.
|
||||
def color(*values)
|
||||
wrap_with_sgr(Color.build(:foreground, values).codes)
|
||||
end
|
||||
|
||||
alias foreground color
|
||||
alias fg color
|
||||
|
||||
# Sets background color of this text.
|
||||
def background(*values)
|
||||
wrap_with_sgr(Color.build(:background, values).codes)
|
||||
end
|
||||
|
||||
alias bg background
|
||||
|
||||
# Resets terminal to default colors/backgrounds.
|
||||
#
|
||||
# It shouldn't be needed to use this method because all methods
|
||||
# append terminal reset code to end of string.
|
||||
def reset
|
||||
wrap_with_sgr(TERM_EFFECTS[:reset])
|
||||
end
|
||||
|
||||
# Turns on bright/bold for this text.
|
||||
def bright
|
||||
wrap_with_sgr(TERM_EFFECTS[:bright])
|
||||
end
|
||||
|
||||
alias bold bright
|
||||
|
||||
# Turns on faint/dark for this text (not well supported by terminal
|
||||
# emulators).
|
||||
def faint
|
||||
wrap_with_sgr(TERM_EFFECTS[:faint])
|
||||
end
|
||||
|
||||
alias dark faint
|
||||
|
||||
# Turns on italic style for this text (not well supported by terminal
|
||||
# emulators).
|
||||
def italic
|
||||
wrap_with_sgr(TERM_EFFECTS[:italic])
|
||||
end
|
||||
|
||||
# Turns on underline decoration for this text.
|
||||
def underline
|
||||
wrap_with_sgr(TERM_EFFECTS[:underline])
|
||||
end
|
||||
|
||||
# Turns on blinking attribute for this text (not well supported by terminal
|
||||
# emulators).
|
||||
def blink
|
||||
wrap_with_sgr(TERM_EFFECTS[:blink])
|
||||
end
|
||||
|
||||
# Inverses current foreground/background colors.
|
||||
def inverse
|
||||
wrap_with_sgr(TERM_EFFECTS[:inverse])
|
||||
end
|
||||
|
||||
# Hides this text (set its color to the same as background).
|
||||
def hide
|
||||
wrap_with_sgr(TERM_EFFECTS[:hide])
|
||||
end
|
||||
|
||||
def cross_out
|
||||
wrap_with_sgr(TERM_EFFECTS[:cross_out])
|
||||
end
|
||||
|
||||
alias strike cross_out
|
||||
|
||||
def black
|
||||
color(:black)
|
||||
end
|
||||
|
||||
def red
|
||||
color(:red)
|
||||
end
|
||||
|
||||
def green
|
||||
color(:green)
|
||||
end
|
||||
|
||||
def yellow
|
||||
color(:yellow)
|
||||
end
|
||||
|
||||
def blue
|
||||
color(:blue)
|
||||
end
|
||||
|
||||
def magenta
|
||||
color(:magenta)
|
||||
end
|
||||
|
||||
def cyan
|
||||
color(:cyan)
|
||||
end
|
||||
|
||||
def white
|
||||
color(:white)
|
||||
end
|
||||
|
||||
# We take care of X11 color method call here.
|
||||
# Such as #aqua, #ghostwhite.
|
||||
def method_missing(method_name, *args)
|
||||
if Color::X11Named.color_names.include?(method_name) && args.empty?
|
||||
color(method_name)
|
||||
else
|
||||
super
|
||||
end
|
||||
end
|
||||
|
||||
def respond_to_missing?(method_name, *args)
|
||||
Color::X11Named.color_names.include?(method_name) && args.empty? || super
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def wrap_with_sgr(codes) #:nodoc:
|
||||
self.class.new(StringUtils.wrap_with_sgr(self, [*codes]))
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,14 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require_relative 'presenter'
|
||||
require_relative 'global'
|
||||
|
||||
module Rainbow
|
||||
refine String do
|
||||
Presenter.instance_methods(false).each do |method_name|
|
||||
define_method(method_name) do |*args|
|
||||
::Rainbow.global.wrap(self).send(method_name, *args)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,22 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module Rainbow
|
||||
class StringUtils
|
||||
def self.wrap_with_sgr(string, codes)
|
||||
return string if codes.empty?
|
||||
|
||||
seq = "\e[" + codes.join(";") + "m"
|
||||
|
||||
string = string.sub(/^(\e\[([\d;]+)m)*/) { |m| m + seq }
|
||||
|
||||
return string if string.end_with? "\e[0m"
|
||||
|
||||
string + "\e[0m"
|
||||
end
|
||||
|
||||
def self.uncolor(string)
|
||||
# See http://www.commandlinefu.com/commands/view/3584/remove-color-codes-special-characters-with-sed
|
||||
string.gsub(/\e\[[0-9;]*m/, '')
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,5 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module Rainbow
|
||||
VERSION = "3.1.1"
|
||||
end
|
||||
@@ -0,0 +1,22 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require_relative 'presenter'
|
||||
require_relative 'null_presenter'
|
||||
|
||||
module Rainbow
|
||||
class Wrapper
|
||||
attr_accessor :enabled
|
||||
|
||||
def initialize(enabled = true)
|
||||
@enabled = enabled
|
||||
end
|
||||
|
||||
def wrap(string)
|
||||
if enabled
|
||||
Presenter.new(string.to_s)
|
||||
else
|
||||
NullPresenter.new(string.to_s)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,153 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module Rainbow
|
||||
module X11ColorNames
|
||||
NAMES = {
|
||||
aqua: [0, 255, 255],
|
||||
aquamarine: [127, 255, 212],
|
||||
mediumaquamarine: [102, 205, 170],
|
||||
azure: [240, 255, 255],
|
||||
beige: [245, 245, 220],
|
||||
bisque: [255, 228, 196],
|
||||
black: [0, 0, 0],
|
||||
blanchedalmond: [255, 235, 205],
|
||||
blue: [0, 0, 255],
|
||||
darkblue: [0, 0, 139],
|
||||
lightblue: [173, 216, 230],
|
||||
mediumblue: [0, 0, 205],
|
||||
aliceblue: [240, 248, 255],
|
||||
cadetblue: [95, 158, 160],
|
||||
dodgerblue: [30, 144, 255],
|
||||
midnightblue: [25, 25, 112],
|
||||
navyblue: [0, 0, 128],
|
||||
powderblue: [176, 224, 230],
|
||||
royalblue: [65, 105, 225],
|
||||
skyblue: [135, 206, 235],
|
||||
deepskyblue: [0, 191, 255],
|
||||
lightskyblue: [135, 206, 250],
|
||||
slateblue: [106, 90, 205],
|
||||
darkslateblue: [72, 61, 139],
|
||||
mediumslateblue: [123, 104, 238],
|
||||
steelblue: [70, 130, 180],
|
||||
lightsteelblue: [176, 196, 222],
|
||||
brown: [165, 42, 42],
|
||||
rosybrown: [188, 143, 143],
|
||||
saddlebrown: [139, 69, 19],
|
||||
sandybrown: [244, 164, 96],
|
||||
burlywood: [222, 184, 135],
|
||||
chartreuse: [127, 255, 0],
|
||||
chocolate: [210, 105, 30],
|
||||
coral: [255, 127, 80],
|
||||
lightcoral: [240, 128, 128],
|
||||
cornflower: [100, 149, 237],
|
||||
cornsilk: [255, 248, 220],
|
||||
crimson: [220, 20, 60],
|
||||
cyan: [0, 255, 255],
|
||||
darkcyan: [0, 139, 139],
|
||||
lightcyan: [224, 255, 255],
|
||||
firebrick: [178, 34, 34],
|
||||
fuchsia: [255, 0, 255],
|
||||
gainsboro: [220, 220, 220],
|
||||
gold: [255, 215, 0],
|
||||
goldenrod: [218, 165, 32],
|
||||
darkgoldenrod: [184, 134, 11],
|
||||
lightgoldenrod: [250, 250, 210],
|
||||
palegoldenrod: [238, 232, 170],
|
||||
gray: [190, 190, 190],
|
||||
darkgray: [169, 169, 169],
|
||||
dimgray: [105, 105, 105],
|
||||
lightgray: [211, 211, 211],
|
||||
slategray: [112, 128, 144],
|
||||
lightslategray: [119, 136, 153],
|
||||
webgray: [128, 128, 128],
|
||||
green: [0, 255, 0],
|
||||
darkgreen: [0, 100, 0],
|
||||
lightgreen: [144, 238, 144],
|
||||
palegreen: [152, 251, 152],
|
||||
darkolivegreen: [85, 107, 47],
|
||||
yellowgreen: [154, 205, 50],
|
||||
forestgreen: [34, 139, 34],
|
||||
lawngreen: [124, 252, 0],
|
||||
limegreen: [50, 205, 50],
|
||||
seagreen: [46, 139, 87],
|
||||
darkseagreen: [143, 188, 143],
|
||||
lightseagreen: [32, 178, 170],
|
||||
mediumseagreen: [60, 179, 113],
|
||||
springgreen: [0, 255, 127],
|
||||
mediumspringgreen: [0, 250, 154],
|
||||
webgreen: [0, 128, 0],
|
||||
honeydew: [240, 255, 240],
|
||||
indianred: [205, 92, 92],
|
||||
indigo: [75, 0, 130],
|
||||
ivory: [255, 255, 240],
|
||||
khaki: [240, 230, 140],
|
||||
darkkhaki: [189, 183, 107],
|
||||
lavender: [230, 230, 250],
|
||||
lavenderblush: [255, 240, 245],
|
||||
lemonchiffon: [255, 250, 205],
|
||||
lime: [0, 255, 0],
|
||||
linen: [250, 240, 230],
|
||||
magenta: [255, 0, 255],
|
||||
darkmagenta: [139, 0, 139],
|
||||
maroon: [176, 48, 96],
|
||||
webmaroon: [127, 0, 0],
|
||||
mintcream: [245, 255, 250],
|
||||
mistyrose: [255, 228, 225],
|
||||
moccasin: [255, 228, 181],
|
||||
oldlace: [253, 245, 230],
|
||||
olive: [128, 128, 0],
|
||||
olivedrab: [107, 142, 35],
|
||||
orange: [255, 165, 0],
|
||||
darkorange: [255, 140, 0],
|
||||
orchid: [218, 112, 214],
|
||||
darkorchid: [153, 50, 204],
|
||||
mediumorchid: [186, 85, 211],
|
||||
papayawhip: [255, 239, 213],
|
||||
peachpuff: [255, 218, 185],
|
||||
peru: [205, 133, 63],
|
||||
pink: [255, 192, 203],
|
||||
deeppink: [255, 20, 147],
|
||||
lightpink: [255, 182, 193],
|
||||
hotpink: [255, 105, 180],
|
||||
plum: [221, 160, 221],
|
||||
purple: [160, 32, 240],
|
||||
mediumpurple: [147, 112, 219],
|
||||
rebeccapurple: [102, 51, 153],
|
||||
webpurple: [127, 0, 127],
|
||||
red: [255, 0, 0],
|
||||
darkred: [139, 0, 0],
|
||||
orangered: [255, 69, 0],
|
||||
mediumvioletred: [199, 21, 133],
|
||||
palevioletred: [219, 112, 147],
|
||||
salmon: [250, 128, 114],
|
||||
darksalmon: [233, 150, 122],
|
||||
lightsalmon: [255, 160, 122],
|
||||
seashell: [255, 245, 238],
|
||||
sienna: [160, 82, 45],
|
||||
silver: [192, 192, 192],
|
||||
darkslategray: [47, 79, 79],
|
||||
snow: [255, 250, 250],
|
||||
tan: [210, 180, 140],
|
||||
teal: [0, 128, 128],
|
||||
thistle: [216, 191, 216],
|
||||
tomato: [255, 99, 71],
|
||||
turquoise: [64, 224, 208],
|
||||
darkturquoise: [0, 206, 209],
|
||||
mediumturquoise: [72, 209, 204],
|
||||
paleturquoise: [175, 238, 238],
|
||||
violet: [238, 130, 238],
|
||||
darkviolet: [148, 0, 211],
|
||||
blueviolet: [138, 43, 226],
|
||||
wheat: [245, 222, 179],
|
||||
white: [255, 255, 255],
|
||||
antiquewhite: [250, 235, 215],
|
||||
floralwhite: [255, 250, 240],
|
||||
ghostwhite: [248, 248, 255],
|
||||
navajowhite: [255, 222, 173],
|
||||
whitesmoke: [245, 245, 245],
|
||||
yellow: [255, 255, 0],
|
||||
lightyellow: [255, 255, 224],
|
||||
greenyellow: [173, 255, 47]
|
||||
}.freeze
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user