Module: OllamaChat::Utils::StripANSI

Defined in:
lib/ollama_chat/utils/strip_ansi.rb

Overview

Strips ANSI escape sequences from a string.

Handles both SGR (Select Graphic Rendition) sequences like \e[38;2;255;128;0m and OSC (Operating System Command) sequences like \e]0;title\a or \e]8;;url\e\\.

Examples:

OllamaChat::Utils::StripANSI.strip_ansi("\e[31mred\e[0m")
# => "red"

Constant Summary collapse

ANSI_RE =
/\e\[[0-9;?]*[A-Za-z]|\e\].*?(\e\\|\a)/m

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.strip_ansi(line) ⇒ String

Removes all ANSI escape sequences from the given string.

Parameters:

  • line (String)

    the string potentially containing ANSI escapes

Returns:

  • (String)

    the string with all ANSI escape sequences removed



19
20
21
# File 'lib/ollama_chat/utils/strip_ansi.rb', line 19

def strip_ansi(line)
  line.to_s.gsub(ANSI_RE, '')
end

Instance Method Details

#strip_ansi(line) ⇒ String (private)

Removes all ANSI escape sequences from the given string.

Parameters:

  • line (String)

    the string potentially containing ANSI escapes

Returns:

  • (String)

    the string with all ANSI escape sequences removed



19
20
21
# File 'lib/ollama_chat/utils/strip_ansi.rb', line 19

def strip_ansi(line)
  line.to_s.gsub(ANSI_RE, '')
end