Module: TTY::Command::Window::ANSI

Defined in:
lib/tty/command/window/ansi.rb

Overview

ANSI helpers: SGR attribute tracking and escape-sequence stripping.

Defined Under Namespace

Classes: SGRState

Constant Summary collapse

STRIP_PATTERN =
Regexp.union(
  /\e\][^\a\e]*(?:\a|\e\\)?/, # OSC ... BEL/ST
  /\eP.*?(?:\e\\|\a)/m,       # DCS ... ST
  %r{\e\[[\d;:?<=>!]*[ -/]*[@-~]}, # CSI sequences
  /\e[()*+][@-~]/,            # charset designation
  /\e[@-_]/,                  # other C1-introducing escapes
  /[\x00-\x08\x0b\x0c\x0e-\x1f\x7f]/ # stray control bytes (keep \t \n \r)
).freeze

Class Method Summary collapse

Class Method Details

.strip(text) ⇒ String

Remove escape sequences and normalize CRLF, leaving printable text.

Parameters:

  • text (String)

Returns:

  • (String)


20
21
22
# File 'lib/tty/command/window/ansi.rb', line 20

def self.strip(text)
  text.gsub(STRIP_PATTERN, "").gsub("\r\n", "\n").delete("\r")
end