Module: Ronin::Support::CLI::ANSI

Defined in:
lib/ronin/support/cli/ansi.rb

Overview

Defines ANSI colors.

Since:

  • 1.0.0

Constant Summary collapse

RESET =

ANSI reset code

Since:

  • 1.0.0

"\e[0m"
BOLD_ON =

ANSI code for bold text

Since:

  • 1.0.0

"\e[1m"
BOLD_OFF =

ANSI code to disable boldness

Since:

  • 1.0.0

"\e[22m"
BLACK =

ANSI color code for black

Since:

  • 1.0.0

"\e[30m"
RED =

ANSI color code for red

Since:

  • 1.0.0

"\e[31m"
GREEN =

ANSI color code for green

Since:

  • 1.0.0

"\e[32m"
YELLOW =

ANSI color code for yellow

Since:

  • 1.0.0

"\e[33m"
BLUE =

ANSI color code for blue

Since:

  • 1.0.0

"\e[34m"
MAGENTA =

ANSI color code for magenta

Since:

  • 1.0.0

"\e[35m"
CYAN =

ANSI color code for cyan

Since:

  • 1.0.0

"\e[36m"
WHITE =

ANSI color code for white

Since:

  • 1.0.0

"\e[37m"
RESET_COLOR =

ANSI color for the default foreground color

Since:

  • 1.0.0

"\e[39m"

Foreground Color Methods collapse

Class Method Summary collapse

Class Method Details

.black(string = nil) ⇒ String

Sets the text color to black.

Parameters:

  • string (String, nil) (defaults to: nil)

    An optional string.

Returns:

  • (String)

    The colorized string or just BLACK if no arguments were given.

See Also:

Since:

  • 1.0.0



140
141
142
143
144
145
146
147
148
149
150
# File 'lib/ronin/support/cli/ansi.rb', line 140

def black(string=nil)
  if string
    if $stdout.tty? then "#{BLACK}#{string}#{RESET_COLOR}"
    else                 string
    end
  else
    if $stdout.tty? then BLACK
    else                 ''
    end
  end
end

.blue(string = nil) ⇒ String

Sets the text color to blue.

Parameters:

  • string (String, nil) (defaults to: nil)

    An optional string.

Returns:

  • (String)

    The colorized string or just BLUE if no arguments were given.

See Also:

Since:

  • 1.0.0



240
241
242
243
244
245
246
247
248
249
250
# File 'lib/ronin/support/cli/ansi.rb', line 240

def blue(string=nil)
  if string
    if $stdout.tty? then "#{BLUE}#{string}#{RESET_COLOR}"
    else                 string
    end
  else
    if $stdout.tty? then BLUE
    else                 ''
    end
  end
end

.bold(string = nil) ⇒ String

Bolds the text.

Parameters:

  • string (String, nil) (defaults to: nil)

    An optional string.

Returns:

  • (String)

    The bolded string or just BOLD_ON if no arguments were given.

See Also:

Since:

  • 1.0.0



111
112
113
114
115
116
117
118
119
120
121
# File 'lib/ronin/support/cli/ansi.rb', line 111

def bold(string=nil)
  if string
    if $stdout.tty? then "#{BOLD_ON}#{string}#{BOLD_OFF}"
    else                 string
    end
  else
    if $stdout.tty? then BOLD_ON
    else                 ''
    end
  end
end

.clearString?

Alias for reset.

Returns:

  • (String, nil)

    The ANSI reset String or nil if STDOUT is not a TTY.

See Also:

Since:

  • 1.0.0



94
95
96
# File 'lib/ronin/support/cli/ansi.rb', line 94

def clear
  reset
end

.cyan(string = nil) ⇒ String

Sets the text color to cyan.

Parameters:

  • string (String, nil) (defaults to: nil)

    An optional string.

Returns:

  • (String)

    The colorized string or just CYAN if no arguments were given.

See Also:

Since:

  • 1.0.0



290
291
292
293
294
295
296
297
298
299
300
# File 'lib/ronin/support/cli/ansi.rb', line 290

def cyan(string=nil)
  if string
    if $stdout.tty? then "#{CYAN}#{string}#{RESET_COLOR}"
    else                 string
    end
  else
    if $stdout.tty? then CYAN
    else                 ''
    end
  end
end

.green(string = nil) ⇒ String

Sets the text color to green.

Parameters:

  • string (String, nil) (defaults to: nil)

    An optional string.

Returns:

  • (String)

    The colorized string or just GREEN if no arguments were given.

See Also:

Since:

  • 1.0.0



190
191
192
193
194
195
196
197
198
199
200
# File 'lib/ronin/support/cli/ansi.rb', line 190

def green(string=nil)
  if string
    if $stdout.tty? then "#{GREEN}#{string}#{RESET_COLOR}"
    else                 string
    end
  else
    if $stdout.tty? then GREEN
    else                 ''
    end
  end
end

.magenta(string = nil) ⇒ String

Sets the text color to magenta.

Parameters:

  • string (String, nil) (defaults to: nil)

    An optional string.

Returns:

  • (String)

    The colorized string or just MAGENTA if no arguments were given.

See Also:

Since:

  • 1.0.0



265
266
267
268
269
270
271
272
273
274
275
# File 'lib/ronin/support/cli/ansi.rb', line 265

def magenta(string=nil)
  if string
    if $stdout.tty? then "#{MAGENTA}#{string}#{RESET_COLOR}"
    else                 string
    end
  else
    if $stdout.tty? then MAGENTA
    else                 ''
    end
  end
end

.red(string = nil) ⇒ String

Sets the text color to red.

Parameters:

  • string (String, nil) (defaults to: nil)

    An optional string.

Returns:

  • (String)

    The colorized string or just RED if no arguments were given.

See Also:

Since:

  • 1.0.0



165
166
167
168
169
170
171
172
173
174
175
# File 'lib/ronin/support/cli/ansi.rb', line 165

def red(string=nil)
  if string
    if $stdout.tty? then "#{RED}#{string}#{RESET_COLOR}"
    else                 string
    end
  else
    if $stdout.tty? then RED
    else                 ''
    end
  end
end

.resetString?

Resets text formatting.

Returns:

  • (String, nil)

    The ANSI reset String or nil if STDOUT is not a TTY.

See Also:

Since:

  • 1.0.0



78
79
80
81
82
# File 'lib/ronin/support/cli/ansi.rb', line 78

def reset
  if $stdout.tty? then RESET
  else                 ''
  end
end

.white(string = nil) ⇒ String

Sets the text color to white.

Parameters:

  • string (String, nil) (defaults to: nil)

    An optional string.

Returns:

  • (String)

    The colorized string or just WHITE if no arguments were given.

See Also:

Since:

  • 1.0.0



315
316
317
318
319
320
321
322
323
324
325
# File 'lib/ronin/support/cli/ansi.rb', line 315

def white(string=nil)
  if string
    if $stdout.tty? then "#{WHITE}#{string}#{RESET_COLOR}"
    else                 string
    end
  else
    if $stdout.tty? then WHITE
    else                 ''
    end
  end
end

.yellow(string = nil) ⇒ String

Sets the text color to yellow.

Parameters:

  • string (String, nil) (defaults to: nil)

    An optional string.

Returns:

  • (String)

    The colorized string or just YELLOW if no arguments were given.

See Also:

Since:

  • 1.0.0



215
216
217
218
219
220
221
222
223
224
225
# File 'lib/ronin/support/cli/ansi.rb', line 215

def yellow(string=nil)
  if string
    if $stdout.tty? then "#{YELLOW}#{string}#{RESET_COLOR}"
    else                 string
    end
  else
    if $stdout.tty? then YELLOW
    else                 ''
    end
  end
end