Skip to content
Kward Search API index

Class: Kward::PtyTranscriptNormalizer

Inherits:
Object
  • Object
show all
Defined in:
lib/kward/pty/transcript_normalizer.rb

Overview

Reduces safe, line-oriented PTY redraws to transcript-friendly text.

Defined Under Namespace

Classes: Line

Constant Summary collapse

HORIZONTAL_REDRAW_PATTERN =
/[\r\b]|\e\[[0-9;]*[CDGK`]/.freeze
MAX_SCREEN_COLUMNS =
4096

Class Method Summary collapse

Class Method Details

.normalize(text, columns: 80, max_bytes: nil) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/kward/pty/transcript_normalizer.rb', line 12

def self.normalize(text, columns: 80, max_bytes: nil)
  columns = columns.to_i.clamp(1, MAX_SCREEN_COLUMNS)
  output = +""
  text.each_line do |line|
    body = line.delete_suffix("\n")
    output << if body.match?(HORIZONTAL_REDRAW_PATTERN)
      Line.new(body, columns).render
    else
      ANSI.sanitize_transcript(body)
    end
    output << "\n" if line.end_with?("\n")
    return nil if max_bytes && output.bytesize > max_bytes
  end
  output
end