Skip to content
Kward Search API index

Module: Kward::TerminalText

Defined in:
lib/kward/terminal/text.rb

Overview

Terminal-cell and grapheme helpers for cursor-aware text layout.

Class Method Summary collapse

Class Method Details

.next_grapheme_boundary(text, index) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/kward/terminal/text.rb', line 89

def next_grapheme_boundary(text, index)
  string = text.to_s
  cursor = [[index.to_i, 0].max, string.length].min
  return [cursor + 1, string.length].min if string.ascii_only?

  offset = 0
  string.each_grapheme_cluster do |grapheme|
    offset += grapheme.length
    return offset if offset > cursor
  end
  string.length
end

.previous_grapheme_boundary(text, index) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/kward/terminal/text.rb', line 73

def previous_grapheme_boundary(text, index)
  string = text.to_s
  cursor = [[index.to_i, 0].max, string.length].min
  return [cursor - 1, 0].max if string.ascii_only?

  previous = 0
  offset = 0
  string.each_grapheme_cluster do |grapheme|
    offset += grapheme.length
    return previous if offset >= cursor

    previous = offset
  end
  previous
end

.truncate(text, max_width) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/kward/terminal/text.rb', line 13

def truncate(text, max_width)
  string = text.to_s
  limit = [max_width.to_i, 0].max
  return string[0, limit].to_s if string.ascii_only?

  remaining = limit
  truncated = +""
  string.each_grapheme_cluster do |grapheme|
    grapheme_width = width(grapheme)
    break if grapheme_width > remaining

    truncated << grapheme
    remaining -= grapheme_width
  end
  truncated
end

.width(text) ⇒ Object



9
10
11
# File 'lib/kward/terminal/text.rb', line 9

def width(text)
  Unicode::DisplayWidth.of(text.to_s, ambiguous: 1, emoji: :all)
end

.wrap(text, width:, cursor: nil) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/kward/terminal/text.rb', line 30

def wrap(text, width:, cursor: nil)
  string = text.to_s
  line_width = [width.to_i, 1].max
  return wrap_ascii(string, line_width, cursor) if string.ascii_only?

  rows = [+""]
  row_widths = [0]
  cursor_row = nil
  cursor_col = nil
  offset = 0

  string.each_grapheme_cluster do |grapheme|
    if !cursor.nil? && cursor_row.nil? && cursor <= offset
      append_empty_row(rows, row_widths) if row_widths.last >= line_width
      cursor_row = rows.length - 1
      cursor_col = row_widths.last
    end

    grapheme_width = width(grapheme)
    append_empty_row(rows, row_widths) if row_widths.last.positive? && row_widths.last + grapheme_width > line_width
    rows.last << grapheme
    row_widths[-1] += grapheme_width
    offset += grapheme.length

    if !cursor.nil? && cursor_row.nil? && cursor <= offset
      cursor_row = rows.length - 1
      cursor_col = row_widths.last
    end
  end

  if !cursor.nil? && cursor_row.nil?
    append_empty_row(rows, row_widths) if row_widths.last >= line_width
    cursor_row = rows.length - 1
    cursor_col = row_widths.last
  elsif !cursor.nil? && cursor >= offset && row_widths.last >= line_width
    append_empty_row(rows, row_widths)
    cursor_row = rows.length - 1
    cursor_col = 0
  end

  { rows: rows, cursor_row: cursor_row, cursor_col: cursor_col }
end