module Chamomile::ANSI
Constants
- ESCAPE_RE
-
Matches CSI sequences, OSC sequences, ESC charset/other sequences
Public Class Methods
Source
# File lib/chamomile/styling/ansi.rb, line 34 def extract_escape(chars, start) return nil unless chars[start] == "\e" i = start + 1 return nil if i >= chars.length if chars[i] == "[" seq = +"\e[" i += 1 while i < chars.length && chars[i].match?(/[0-9;]/) seq << chars[i] i += 1 end if i < chars.length && chars[i].match?(/[A-Za-z]/) seq << chars[i] return seq end end nil end
Source
# File lib/chamomile/styling/ansi.rb, line 22 def height(str) str.count("\n") + 1 end
Source
# File lib/chamomile/styling/ansi.rb, line 13 def printable_width(str) stripped = strip(str) width = 0 stripped.each_char do |ch| width += char_width(ch) end width end
Source
# File lib/chamomile/styling/ansi.rb, line 68 def sgr_open_after?(was_open, seq) return false if ["\e[0m", "\e[m"].include?(seq) return true if seq.match?(/\A\e\[\d/) was_open end
Source
# File lib/chamomile/styling/ansi.rb, line 26 def size(str) return [0, 1] if str.empty? lines = str.split("\n", -1) w = lines.map { |line| printable_width(line) }.max || 0 [w, lines.length] end
Source
# File lib/chamomile/styling/ansi.rb, line 9 def strip(str) str.gsub(ESCAPE_RE, "") end
Source
# File lib/chamomile/styling/ansi.rb, line 56 def track_sgr(active_sgr, seq) if ["\e[0m", "\e[m"].include?(seq) active_sgr.clear elsif seq.match?(/\A\e\[\d/) if active_sgr.empty? active_sgr.replace(seq) else active_sgr.replace("#{active_sgr.delete_suffix("\e[0m")}#{seq}") end end end