Class: String

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

Overview

is the value non-empty?

Direct Known Subclasses

AnsiString

Constant Summary collapse

FN_ID_LEN =
4
FN_MAX_LEN =
64
FN_PATTERN =

characters than can be used in a file name without quotes or escaping

%r{[^!#%\+\-0-9=@A-Z_a-z()\[\]{}]}.freeze
FN_REPLACEMENT =

except ‘.’, ‘,’, ‘~’ reserved for tokenization / !“#$%&‘()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ^_`abcdefghijklmnopqrstuvwxyz{|}~

'_'

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
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/colorize.rb', line 14

def method_missing(method_name, *args, &block)
  case method_name.to_s
  # when /^bg_rgb_/
  #   bytes = $'.split('_')
  #   bg_rgb_color(bytes[0..2].join(';'))
  when /^fg_bg_rgb_/
    pp [__LINE__, caller[0]]; binding.irb
    bytes = $'.split('_')
    fg_bg_rgb_color(bytes[0..2].join(';'), bytes[3..5].join(';'))
  when /^fg_bg_rgbh_/
    pp [__LINE__, caller[0]]; binding.irb
    hex_to_fg_bg_rgb($')
  when /^fg_rgb_/
    pp [__LINE__, caller[0]]; binding.irb
    fg_rgb_color($'.gsub('_', ';'))
  when /^fg_rgbh_/
    pp [__LINE__, caller[0]]; binding.irb
    hex_to_rgb($')

  when 'to_a', 'to_ary', 'to_hash', 'to_int', 'to_io', 'to_regexp'
    nil
  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.



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

def ansi_control_sequence
  pp [__LINE__, caller[0]]; binding.irb
  "\033[#{self}\033[0m"
end

#bgreenObject



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

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.



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

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

#blinkingObject



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

def blinking;         x; "\033[5m#{self}\033[25m"; end

#blueObject

More named colors using RGB hex values



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

def blue;    fg_rgbh_00_00_FF; end

#boldObject

graphics modes



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

def bold;             x; "\033[1m#{self}\033[22m"; end

#bold_italicObject



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

def bold_italic;      x; "\033[1m\033[3m#{self}\033[22m\033[23m"; end

#bold_underlineObject



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

def bold_underline;   x; "\033[1m\033[4m#{self}\033[22m\033[24m"; end

#bredObject



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

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

#bwhiteObject



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

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

#byellowObject



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

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

#cyanObject



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

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

#dimObject



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

def dim;              x; "\033[2m#{self}\033[22m"; end

#fg_bg_rgb_color(fg_rgb, bg_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.



60
61
62
63
# File 'lib/colorize.rb', line 60

def fg_bg_rgb_color(fg_rgb, bg_rgb)
  pp [__LINE__, caller[0]]; binding.irb
  "38;2;#{fg_rgb}m\033[48;2;#{bg_rgb}m#{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.



69
70
71
72
# File 'lib/colorize.rb', line 69

def fg_rgb_color(rgb)
  pp [__LINE__, caller[0]]; binding.irb
  "38;2;#{rgb}m#{self}".ansi_control_sequence
end

#greenObject



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

def green;   fg_rgbh_00_FF_00; end

#hex_to_fg_bg_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.



78
79
80
81
82
83
84
85
# File 'lib/colorize.rb', line 78

def hex_to_fg_bg_rgb(hex_str)
  pp [__LINE__, caller[0]]; binding.irb
  values = hex_str.split('_').map { |hex| hex.to_i(16).to_s }
  fg_bg_rgb_color(
    values[0..2].join(';'),
    values[3..5].join(';')
  )
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.



91
92
93
94
95
96
# File 'lib/colorize.rb', line 91

def hex_to_rgb(hex_str)
  pp [__LINE__, caller[0]]; binding.irb
  fg_rgb_color(
    hex_str.split('_').map { |hex| hex.to_i(16).to_s }.join(';')
  )
end

#hiddenObject



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

def hidden;           x; "\033[8m#{self}\033[28m"; end

#indigoObject



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

def indigo;  fg_rgbh_4B_00_82; end

#inverseObject



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

def inverse;          x; "\033[7m#{self}\033[27m"; end

#italicObject



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

def italic;           x; "\033[3m#{self}\033[23m"; end

#magentaObject



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

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.



51
52
53
# File 'lib/hash_delegator.rb', line 51

def non_empty?
  !empty?
end

#orangeObject



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

def orange;  fg_rgbh_FF_7F_00; end

#plainString

Provides a plain, unmodified version of the string.

Returns:

  • (String)

    The original string.



101
102
103
104
# File 'lib/colorize.rb', line 101

def plain
  pp [__LINE__, caller[0]]; binding.irb
  self
end

#present?Boolean

Returns:

  • (Boolean)


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

def present?
  !empty?
end

#pub_name(id_len: FN_ID_LEN, max_len: FN_MAX_LEN, pattern: FN_PATTERN, replacement: FN_REPLACEMENT) ⇒ Object

block name in commands and documents



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/namer.rb', line 28

def pub_name(
  id_len: FN_ID_LEN, max_len: FN_MAX_LEN,
  pattern: FN_PATTERN, replacement: FN_REPLACEMENT
)
  trimmed = if self[max_len]
              rand(((10**(id_len - 1)) + 1)..(10**id_len)).to_s
              dig = Digest::MD5.hexdigest(self)[0, id_len]
              self[0..max_len - id_len] + dig
            else
              self
            end

  trimmed.gsub(pattern, replacement).tap { |ret|
    pp [__LINE__, 'String.pub_name() ->', ret] if $pd
  }
end

#redObject



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

def red;     fg_rgbh_FF_00_00; end

#strikethroughObject



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

def strikethrough;    x; "\033[9m#{self}\033[29m"; end

#underlineObject



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

def underline;        x; "\033[4m#{self}\033[24m"; end

#underline_italicObject



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

def underline_italic; x; "\033[4m\033[3m#{self}\033[23m\033[24m"; end

#violetObject



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

def violet;  fg_rgbh_94_00_D3; end

#whiteObject



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

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

#xObject



129
130
131
# File 'lib/colorize.rb', line 129

def x
  pp [__LINE__, caller[1]]; binding.irb
end

#yellowObject



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

def yellow;  fg_rgbh_FF_FF_00; end