Class: Charming::Presentation::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.

Class Method Summary collapse

Class Method Details

.slice(line, start_column, width) ⇒ Object



10
11
12
13
14
# File 'lib/charming/presentation/ui/ansi_slicer.rb', line 10

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



16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/charming/presentation/ui/ansi_slicer.rb', line 16

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