Class: String

Inherits:
Object show all
Defined in:
lib/colorize.rb,
lib/hash_delegator.rb,
lib/object_present.rb,
lib/object_present.rb

Overview

is the value non-empty?

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ String

Handles dynamic method calls to create RGB colors.

Parameters:

  • method_name (Symbol)

    The name of the method being called.

  • args (Array)

    The arguments passed to the method.

  • block (Proc)

    An optional block.

Returns:

  • (String)

    The formatted string.



14
15
16
17
18
19
20
21
22
23
# File 'lib/colorize.rb', line 14

def method_missing(method_name, *args, &block)
  case method_name.to_s
  when /^fg_rgb_/
    fg_rgb_color($'.gsub('_', ';'))
  when /^fg_rgbh_/
    hex_to_rgb($')
  else
    super
  end
end

Instance Method Details

#ansi_control_sequenceString

Generates an ANSI control sequence for the string.

Returns:

  • (String)

    The string wrapped in an ANSI control sequence.



28
29
30
# File 'lib/colorize.rb', line 28

def ansi_control_sequence
  "\033[#{self}\033[0m"
end

#bgreenObject



64
# File 'lib/colorize.rb', line 64

def bgreen;  "1;32m#{self}".ansi_control_sequence; end

#blackObject

A collection of methods for applying named colors.

For example, #black applies a black foreground color to the string. These are provided for convenience and easy readability.



62
# File 'lib/colorize.rb', line 62

def black;   "30m#{self}".ansi_control_sequence; end

#blank?Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/object_present.rb', line 20

def blank?
  empty? || /\A[[:space:]]*\z/.freeze.match?(self)
end

#blueObject

More named colors using RGB hex values



72
# File 'lib/colorize.rb', line 72

def blue;    fg_rgbh_00_00_FF; end

#bredObject



63
# File 'lib/colorize.rb', line 63

def bred;    "1;31m#{self}".ansi_control_sequence; end

#bwhiteObject



69
# File 'lib/colorize.rb', line 69

def bwhite;  "1;37m#{self}".ansi_control_sequence; end

#byellowObject



65
# File 'lib/colorize.rb', line 65

def byellow; "1;33m#{self}".ansi_control_sequence; end

#cyanObject



67
# File 'lib/colorize.rb', line 67

def cyan;    "36m#{self}".ansi_control_sequence; end

#fg_rgb_color(rgb) ⇒ String

Applies a 24-bit RGB foreground color to the string.

Parameters:

  • rgb (String)

    The RGB color, expressed as a string like “1;2;3”.

Returns:

  • (String)

    The string with the applied RGB foreground color.



36
37
38
# File 'lib/colorize.rb', line 36

def fg_rgb_color(rgb)
  "38;2;#{rgb}m#{self}".ansi_control_sequence
end

#greenObject



73
# File 'lib/colorize.rb', line 73

def green;   fg_rgbh_00_FF_00; end

#hex_to_rgb(hex_str) ⇒ String

Converts hex color codes to RGB and applies them to the string.

Parameters:

  • hex_str (String)

    The RGB color, expressed as a hex string like “FF00FF”.

Returns:

  • (String)

    The string with the applied RGB foreground color.



44
45
46
47
48
# File 'lib/colorize.rb', line 44

def hex_to_rgb(hex_str)
  fg_rgb_color(
    hex_str.split('_').map { |hex| hex.to_i(16).to_s }.join(';')
  )
end

#indigoObject



74
# File 'lib/colorize.rb', line 74

def indigo;  fg_rgbh_4B_00_82; end

#magentaObject



66
# File 'lib/colorize.rb', line 66

def magenta; "35m#{self}".ansi_control_sequence; end

#non_empty?Boolean

Checks if the string is not empty.

Returns:

  • (Boolean)

    Returns true if the string is not empty, false otherwise.



38
39
40
# File 'lib/hash_delegator.rb', line 38

def non_empty?
  !empty?
end

#orangeObject



75
# File 'lib/colorize.rb', line 75

def orange;  fg_rgbh_FF_7F_00; end

#plainString

Provides a plain, unmodified version of the string.

Returns:

  • (String)

    The original string.



53
54
55
# File 'lib/colorize.rb', line 53

def plain
  self
end

#present?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/object_present.rb', line 30

def present?
  !empty?
end

#redObject



76
# File 'lib/colorize.rb', line 76

def red;     fg_rgbh_FF_00_00; end

#violetObject



77
# File 'lib/colorize.rb', line 77

def violet;  fg_rgbh_94_00_D3; end

#whiteObject



68
# File 'lib/colorize.rb', line 68

def white;   "37m#{self}".ansi_control_sequence; end

#yellowObject



78
# File 'lib/colorize.rb', line 78

def yellow;  fg_rgbh_FF_FF_00; end