Module: Colored2

Defined in:
lib/colored2.rb,
lib/colored2/codes.rb,
lib/colored2/numbers.rb,
lib/colored2/version.rb,
lib/colored2/ascii_decorator.rb

Defined Under Namespace

Classes: AsciiDecorator, BackgroundColor, Code, Effect, TextColor

Constant Summary collapse

COLORS =
{
  black: 30,
  red: 31,
  green: 32,
  yellow: 33,
  blue: 34,
  magenta: 35,
  cyan: 36,
  white: 37
}.freeze
EFFECTS =
{
  no_color: 0,
  bold: 1,
  dark: 2,
  italic: 3,
  underlined: 4,
  reversed: 7,
  plain: 21, # non-bold
  normal: 22
}.freeze
VERSION =
'4.0.0'

Class Method Summary collapse

Class Method Details

.background_next!Object



15
16
17
# File 'lib/colored2/ascii_decorator.rb', line 15

def self.background_next!
  Colored2::AsciiDecorator.background_next!
end

.background_next?Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/colored2/ascii_decorator.rb', line 23

def self.background_next?
  Colored2::AsciiDecorator.background_next?
end

.decorate(a_class) ⇒ Object



7
8
9
# File 'lib/colored2.rb', line 7

def self.decorate(a_class)
  a_class.send(:include, Colored2)
end

.disable!Object



11
12
13
# File 'lib/colored2/ascii_decorator.rb', line 11

def self.disable!
  Colored2::AsciiDecorator.disable!
end

.enable!Object



7
8
9
# File 'lib/colored2/ascii_decorator.rb', line 7

def self.enable!
  Colored2::AsciiDecorator.enable!
end

.foreground_next!Object



19
20
21
# File 'lib/colored2/ascii_decorator.rb', line 19

def self.foreground_next!
  Colored2::AsciiDecorator.foreground_next!
end

.included(from_class) ⇒ Object



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
# File 'lib/colored2.rb', line 11

def self.included(from_class)
  from_class.class_eval do
    def surround_with_color(color: nil, effect: nil, color_self: nil, string: nil, &block)
      color_type = if Colored2.background_next? && effect.nil?
                     Colored2.foreground_next!
                     :background
                   else
                     :foreground
                   end

      opts = {}
      opts.merge!(color_type => color) if color
      opts.merge!(effect:) if effect

      if color_self
        opts.merge!( beginning: :on, end: :off)
        colored = Colored2::AsciiDecorator.new(self).decorate(opts)
        if string || block
          arg = "#{string}#{block&.call}"
          colored << Colored2::AsciiDecorator.new(arg).decorate(opts) if !arg.empty?
        end
      else
        opts.merge!( end: :on )
        colored = Colored2::AsciiDecorator.new(self).decorate(opts)
        if string || block
          arg = "#{string}#{block&.call}"
          colored << Colored2::AsciiDecorator.new(arg).decorate(opts.merge(end: :off)) if !arg.empty?
        end
      end
      colored
    end

    def on
      Colored2.background_next!
      self
    end
  end

  from_class.instance_eval do
    COLORS.each_key do |color|
      define_method(color) do |string = nil, &block|
        surround_with_color(color:, color_self: true, string:, &block)
      end

      define_method("#{color}!".to_sym) do |string = nil, &block|
        surround_with_color(color:, color_self: false, string:, &block)
      end
    end

    EFFECTS.each_key do |effect|
      next if effect == 'no_color'

      define_method(effect) do |string = nil, &block|
        surround_with_color(effect:, color_self: true, string:, &block)
      end

      define_method("#{effect}!".to_sym) do |string = nil, &block|
        surround_with_color(effect:, color_self: false, string:, &block)
      end
    end

    define_method(:to_eol) do
      tmp = sub(/^(\e\[[\[\e0-9;m]+m)/, "\\1\e[2K")
      if tmp == self
        return "\e[2K" << self
      end

      tmp
    end

    define_method(:to_bol) do
      "#{self}\033[#{length}D\033[0D"
    end
  end
end

.integer_classObject



6
7
8
9
# File 'lib/colored2/numbers.rb', line 6

def self.integer_class
  major, minor = RUBY_VERSION.split(".").map(&:to_i)
  major >= 2 && minor >= 4 ? Integer : Numeric
end