Class: Charming::UI::ANSISlicer

Inherits:
Object
  • Object
show all
Defined in:
lib/charming/presentation/ui/ansi_slicer.rb

Overview

ANSISlicer extracts a visible substring from a string that may contain ANSI escape sequences, preserving the styling that is active at the start of the slice and emitting a trailing reset if any styled content was copied.

Constant Summary collapse

TOKEN_PATTERN =

One ANSI escape sequence or one grapheme cluster (‘X`). The ANSI branch comes first so a valid escape is consumed whole rather than as graphemes.

/#{Width::ANSI_PATTERN}|\X/

Class Method Summary collapse

Class Method Details

.slice(line, start_column, width) ⇒ Object



13
14
15
16
17
# File 'lib/charming/presentation/ui/ansi_slicer.rb', line 13

def self.slice(line, start_column, width)
  return "" unless width.positive?

  slice_range(line.to_s, start_column, start_column + width)
end

.slice_range(line, start_column, end_column) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/charming/presentation/ui/ansi_slicer.rb', line 19

def self.slice_range(line, start_column, end_column)
  state = {column: 0, output: +"", active: [], started: false, styled: false}

  each_ansi_or_char(line) do |token, ansi|
    if ansi
      slice_ansi_token(token, state, start_column, end_column)
    else
      slice_char(token, state, start_column, end_column)
    end
  end

  terminate_slice(state)
end