Class: String

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

Overview

is the value non-empty?

Direct Known Subclasses

AnsiString, TrackedString

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

def method_missing(method_name, *args, &block)
  case method_name.to_s
  when /^fg_bg_rgb_/
    bytes = $'.split('_')
    fg_bg_rgb_color(bytes[0..2].join(';'), bytes[3..5].join(';'))
  when /^fg_bg_rgbh_/
    hex_to_fg_bg_rgb($')
  when /^fg_rgb_/
    fg_rgb_color($'.gsub('_', ';'))
  when /^fg_rgbh_/
    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.



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

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

#bgreenObject



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

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.



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

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



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

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

#blueObject

More named colors using RGB hex values



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

def blue;    fg_rgbh_00_00_FF; end

#boldObject

graphics modes



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

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

#bold_italicObject



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

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

#bold_underlineObject



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

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

#bredObject



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

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

#bwhiteObject



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

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

#byellowObject



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

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

#cyanObject



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

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

#delete_even_charsObject



51
52
53
54
55
56
57
58
59
# File 'lib/string_util.rb', line 51

def delete_even_chars
  clusters = scan(/\X/)
  return dup if clusters.length <= 1

  clusters.each_with_index
          .select { |_, i| i.even? } # keep 1st, 3rd, 5th... (0-based even)
          .map(&:first)
          .join
end

#delete_even_chars!Object



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

def delete_even_chars!
  replace(delete_even_chars)
end

#derefObject

replace the app’s directory with a .



262
263
264
# File 'lib/ww.rb', line 262

def deref
  sub(%r{^#{Dir.pwd}}, '')
end

#dimObject



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

def dim;              "\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.



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

def fg_bg_rgb_color(fg_rgb, bg_rgb)
  "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.



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

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

#greenObject



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

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.



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

def hex_to_fg_bg_rgb(hex_str)
  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.



71
72
73
74
75
# File 'lib/colorize.rb', line 71

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

#hiddenObject



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

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

#indigoObject



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

def indigo;  fg_rgbh_4B_00_82; end

#inverseObject



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

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

#italicObject



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

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

#magentaObject



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

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

#orangeObject



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

def orange;  fg_rgbh_FF_7F_00; end

#plainString

Provides a plain, unmodified version of the string.

Returns:

  • (String)

    The original string.



80
81
82
# File 'lib/colorize.rb', line 80

def plain
  self
end

#present?Boolean

Checks if the string contains any non-whitespace characters. characters, false otherwise.

Returns:

  • (Boolean)

    Returns true if the string contains non-whitespace



27
28
29
# File 'lib/string_util.rb', line 27

def present?
  !strip.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



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/namer.rb', line 25

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 do |ret|
    pp [__LINE__, 'String.pub_name() ->', ret] if $pd
  end
end

#redObject



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

def red;     fg_rgbh_FF_00_00; end

#sort_chars(reverse: false, casefold: false, &block) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/string_util.rb', line 91

def sort_chars(reverse: false, casefold: false, &block)
  clusters = scan(/\X/)
  return self if clusters.length <= 1

  sorted =
    if block
      clusters.sort(&block)
    elsif casefold
      clusters.sort_by(&:downcase)
    else
      clusters.sort
    end

  sorted.reverse! if reverse
  sorted.join
end

#sort_chars!(reverse: false, casefold: false, &block) ⇒ Object



108
109
110
# File 'lib/string_util.rb', line 108

def sort_chars!(reverse: false, casefold: false, &block)
  replace(sort_chars(reverse: reverse, casefold: casefold, &block))
end

#strikethroughObject



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

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

#underlineObject



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

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

#underline_italicObject



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

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

#violetObject



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

def violet;  fg_rgbh_94_00_D3; end

#whiteObject



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

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

#yellowObject



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

def yellow;  fg_rgbh_FF_FF_00; end