Module: Foruiman::TUI::Text

Defined in:
lib/foruiman/tui/text.rb

Class Method Summary collapse

Class Method Details

.clean(text) ⇒ Object



41
42
43
# File 'lib/foruiman/tui/text.rb', line 41

def clean(text)
  Foruiman::ANSI::Decoder.new.feed(text, eof: true).gsub(Foruiman::ANSI::SGR, "").tr("\n", " ")
end

.clip(text, columns, ellipsis: false) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/foruiman/tui/text.rb', line 14

def clip(text, columns, ellipsis: false)
  return "" unless columns.positive?
  return text if width(text) <= columns

  limit = ellipsis ? columns - 1 : columns
  used = 0
  output = +""
  text.scan(/\e\[[0-9;:]*m|\X/).each do |token|
    if token.start_with?("\e[")
      output << token
      next
    end
    cells = Unicode::DisplayWidth.of(token, emoji: :all)
    break if used + cells > limit

    output << token
    used += cells
  end
  output << "…" if ellipsis
  output
end

.pad(text, columns) ⇒ Object



36
37
38
39
# File 'lib/foruiman/tui/text.rb', line 36

def pad(text, columns)
  clipped = clip(text, columns)
  clipped + (" " * [columns - width(clipped), 0].max)
end

.width(text) ⇒ Object



10
11
12
# File 'lib/foruiman/tui/text.rb', line 10

def width(text)
  Unicode::DisplayWidth.of(text.gsub(Foruiman::ANSI::SGR, ""), emoji: :all)
end