Module: Bridgetown::Foundation::Packages::Ansi

Extended by:
Ansi
Included in:
Ansi
Defined in:
bridgetown-foundation/lib/bridgetown/foundation/packages/ansi.rb

Constant Summary collapse

ESCAPE =
format("%c", 27)
MATCH =
%r!#{ESCAPE}\[(?:\d+)(?:;\d+)*(j|k|m|s|u|A|B|G)|\e\(B\e\[m!ix
COLORS =
{
  bold: 1,
  black: 30,
  red: 31,
  green: 32,
  yellow: 33,
  blue: 34,
  magenta: 35,
  cyan: 36,
  white: 37,
}.freeze

Instance Method Summary collapse

Instance Method Details

#colorsObject



22
# File 'bridgetown-foundation/lib/bridgetown/foundation/packages/ansi.rb', line 22

def colors = COLORS

#has?(str) ⇒ Boolean

Does the string include ANSI color codes?

Returns:

  • (Boolean)


32
33
34
# File 'bridgetown-foundation/lib/bridgetown/foundation/packages/ansi.rb', line 32

def has?(str)
  !!(str =~ MATCH)
end

#reset(str = "") ⇒ Object

Reset the color back to the default color so that you do not leak any colors when you move onto the next line. This is probably normally used as part of a wrapper so that we don’t leak colors.



39
40
41
42
# File 'bridgetown-foundation/lib/bridgetown/foundation/packages/ansi.rb', line 39

def reset(str = "")
  @ansi_reset ||= format("%c[0m", 27)
  "#{@ansi_reset}#{str}"
end

#strip(str) ⇒ Object

Strip ANSI from the current string. It also strips cursor stuff, well some of it, and it also strips some other stuff that a lot of the other ANSI strippers don’t.



27
28
29
# File 'bridgetown-foundation/lib/bridgetown/foundation/packages/ansi.rb', line 27

def strip(str)
  str.gsub MATCH, ""
end