Module: Strings::ANSI

Defined in:
lib/strings/ansi.rb,
lib/strings/ansi/version.rb,
lib/strings/ansi/extensions.rb

Overview

Helper functions for handling ANSI escape sequences

Defined Under Namespace

Modules: Extensions

Constant Summary collapse

CSI =

The control sequence indicator

"\033"
RESET =

The code for reseting styling

"\e[0m"
ANSI_MATCHER =

The regex to match ANSI codes

'(\[)?\033(\[)?[;?\d]*[\dA-Za-z]([\];])?'
VERSION =
"0.2.0"

Class Method Summary collapse

Class Method Details

.ansi?(string) ⇒ Boolean

Check if string contains ANSI codes

Examples:

Strings::ANSI.ansi?("\e[33mfoo\[e0m")
# => true

Parameters:

  • string (String)

    the string to check

Returns:

  • (Boolean)


45
46
47
# File 'lib/strings/ansi.rb', line 45

def ansi?(string)
  !!(string =~ /#{ANSI_MATCHER}/)
end

.only_ansi?(string) ⇒ Boolean

Check if string contains only ANSI codes

Examples:

Strings::ANSI.only_ansi?("\e[33mfoo\[e0m")
# => false

Strings::ANSI.only_ansi?("\e[33m")
# => false

Parameters:

  • string (String)

    the string to check

Returns:

  • (Boolean)


65
66
67
# File 'lib/strings/ansi.rb', line 65

def only_ansi?(string)
  !!(string =~ /^(#{ANSI_MATCHER})+$/)
end

.sanitize(string) ⇒ String

Return a copy of string with ANSI characters removed

Examples:

Strings::ANSI.sanitize("\e[33mfoo\[e0m")
# => "foo"

Parameters:

  • string (String)

Returns:

  • (String)


28
29
30
# File 'lib/strings/ansi.rb', line 28

def sanitize(string)
  string.gsub(/#{ANSI_MATCHER}/, '')
end