Class: String

Inherits:
Object
  • Object
show all
Defined in:
lib/cli_class_tool/string.rb

Overview

Extension to the core String class to add colorization support

Constant Summary collapse

@@is_a_tty =

colorization

nil

Instance Method Summary collapse

Instance Method Details

#black ⇒ String

Make the string black

Returns:



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

def black
    colorize(30)
end

#blue ⇒ String

Make the string blue

Returns:



66
67
68
# File 'lib/cli_class_tool/string.rb', line 66

def blue
    colorize(34)
end

#bold ⇒ String

Make the string bold

Returns:



90
91
92
# File 'lib/cli_class_tool/string.rb', line 90

def bold
    colorize(1)
end

#brown ⇒ String

Make the string brown (yellow)

Returns:



54
55
56
# File 'lib/cli_class_tool/string.rb', line 54

def brown
    colorize(33)
end

#colorize(color_code) ⇒ String

Colorize the string using ANSI escape codes

Parameters:

  • color_code (Integer) —

    ANSI color code

Returns:

  • (String) —

    Colorized string if TTY, else original string



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/cli_class_tool/string.rb', line 10

def colorize(color_code)
    @@is_a_tty = $stdout.isatty() if @@is_a_tty == nil
    if @@is_a_tty then
        if self =~ /\A\e\[([\d;]+)m(.*)\e\[0m\z/
            codes = $1.split(';')
            content = $2
            new_codes = codes.dup
            if color_code.to_i == 1
                new_codes.unshift("1") unless new_codes.include?("1")
            else
                new_codes.reject! { |c| (30..37).include?(c.to_i) || (90..97).include?(c.to_i) }
                new_codes << color_code.to_s
            end
            new_codes.uniq!
            new_codes.sort_by! { |c| c.to_i }
            return "\e[#{new_codes.join(';')}m#{content}\e[0m"
        else
            return "\e[#{color_code}m#{self}\e[0m"
        end
    else
        return self
    end
end

#cyan ⇒ String

Make the string cyan

Returns:



78
79
80
# File 'lib/cli_class_tool/string.rb', line 78

def cyan
    colorize(36)
end

#gray ⇒ String Also known as: grey

Make the string gray/grey / light black

Returns:



96
97
98
# File 'lib/cli_class_tool/string.rb', line 96

def gray
    colorize(90)
end

#green ⇒ String

Make the string green

Returns:



48
49
50
# File 'lib/cli_class_tool/string.rb', line 48

def green
    colorize(32)
end

Wrap the string in an OSC 8 terminal hyperlink if stdout is a TTY.

Parameters:

  • url (String, #to_s, nil) —

    Target URL for the hyperlink.

Returns:

  • (String) —

    The formatted terminal hyperlink, or original string if not a TTY or URL is nil/empty.



147
148
149
150
151
152
153
154
# File 'lib/cli_class_tool/string.rb', line 147

def hyperlink(url)
    @@is_a_tty = $stdout.isatty() if @@is_a_tty == nil
    if @@is_a_tty && url && !url.to_s.empty?
        "\e]8;;#{url}\e\\#{self}\e]8;;\e\\"
    else
        self
    end
end

#light_blue ⇒ String

Make the string light blue

Returns:

  • (String) —

    Light blue string



121
122
123
# File 'lib/cli_class_tool/string.rb', line 121

def light_blue
    colorize(94)
end

#light_cyan ⇒ String

Make the string light cyan

Returns:

  • (String) —

    Light cyan string



133
134
135
# File 'lib/cli_class_tool/string.rb', line 133

def light_cyan
    colorize(96)
end

#light_green ⇒ String

Make the string light green

Returns:

  • (String) —

    Light green string



109
110
111
# File 'lib/cli_class_tool/string.rb', line 109

def light_green
    colorize(92)
end

#light_magenta ⇒ String

Make the string light magenta

Returns:

  • (String) —

    Light magenta string



127
128
129
# File 'lib/cli_class_tool/string.rb', line 127

def light_magenta
    colorize(95)
end

#light_red ⇒ String

Make the string light red

Returns:

  • (String) —

    Light red string



103
104
105
# File 'lib/cli_class_tool/string.rb', line 103

def light_red
    colorize(91)
end

#light_white ⇒ String

Make the string light white

Returns:

  • (String) —

    Light white string



139
140
141
# File 'lib/cli_class_tool/string.rb', line 139

def light_white
    colorize(97)
end

#light_yellow ⇒ String

Make the string light yellow

Returns:

  • (String) —

    Light yellow string



115
116
117
# File 'lib/cli_class_tool/string.rb', line 115

def light_yellow
    colorize(93)
end

#magenta ⇒ String

Make the string magenta

Returns:



72
73
74
# File 'lib/cli_class_tool/string.rb', line 72

def magenta
    colorize(35)
end

#red ⇒ String

Make the string red

Returns:



42
43
44
# File 'lib/cli_class_tool/string.rb', line 42

def red
    colorize(31)
end

#visible_length ⇒ Integer

Compute the visible length of the string Without any of ANSI/ASCII control characters nor embedded hyperlinks

Returns:

  • (Integer) —

    Visible string length



160
161
162
163
164
165
# File 'lib/cli_class_tool/string.rb', line 160

def visible_length()
    return self.gsub(/\e\]8;.*?(?:\e\\|\a)/, "") # Removes opening and closing hyperlink tags
               .gsub(/\e\[[0-9;?]*[a-zA-Z]/, "") # Removes ANSI escape codes (colors, cursor control)
               .gsub(/[[:cntrl:]]/, "")          # Removes any residual control characters
               .length
end

#white ⇒ String

Make the string white

Returns:



84
85
86
# File 'lib/cli_class_tool/string.rb', line 84

def white
    colorize(37)
end

#yellow ⇒ String

Make the string yellow

Returns:



60
61
62
# File 'lib/cli_class_tool/string.rb', line 60

def yellow
    colorize(33)
end