1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 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
|