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

#blueString

Make the string blue

Returns:



39
40
41
# File 'lib/cli_class_tool/string.rb', line 39

def blue
    colorize(34)
end

#brownString

Make the string brown (yellow)

Returns:



33
34
35
# File 'lib/cli_class_tool/string.rb', line 33

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
# 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
        return "\e[#{color_code}m#{self}\e[0m"
    else
        return self
    end
end

#greenString

Make the string green

Returns:



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

def green
    colorize(32)
end

#magentaString

Make the string magenta

Returns:



45
46
47
# File 'lib/cli_class_tool/string.rb', line 45

def magenta
    colorize(35)
end

#redString

Make the string red

Returns:



21
22
23
# File 'lib/cli_class_tool/string.rb', line 21

def red
    colorize(31)
end