Skip to content
Kward Search API index

Module: Kward::TerminalText

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

Overview

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

Constant Summary collapse

LOCALE_WCWIDTH =

Standalone native shells use a scoped libc locale rather than the TUI's Unicode gem. Call only inside NativeShell::Locale.with; never setlocale.

Fiddle::Function.new(Fiddle::Handle::DEFAULT["wcwidth"], [Fiddle::TYPE_INT], Fiddle::TYPE_INT)

Class Method Summary collapse

Class Method Details

.drop_cells(text, columns) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/kward/terminal/text.rb', line 49

def drop_cells(text, columns)
  return text if columns <= 0
  return text[columns..].to_s if text.ascii_only?

  remaining = columns
  offset = 0
  text.each_grapheme_cluster do |grapheme|
    cell_width = width(grapheme)
    offset += grapheme.length
    remaining -= cell_width
    return (" " * -remaining) + text[offset..] if remaining <= 0
  end
  ""
end

.locale_cells(text) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/kward/terminal/locale_text.rb', line 9

def self.locale_cells(text)
  text.each_char.map do |character|
    begin
      codepoint = character.encode(Encoding::UTF_8).ord
      width = LOCALE_WCWIDTH.call(codepoint)
      if codepoint < 32 || codepoint == 127
        ["^#{(codepoint ^ 64).chr}", 2]
      elsif width >= 0
        [character, width]
      else
        escaped = "\\u{#{codepoint.to_s(16)}}"
        [escaped, escaped.length]
      end
    rescue EncodingError
      escaped = character.bytes.map { |byte| format('\\x%02x', byte) }.join
      [escaped, escaped.length]
    end
  end
end

.next_grapheme_boundary(text, index) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/kward/terminal/text.rb', line 123

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



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/kward/terminal/text.rb', line 107

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

.truncate_middle(text, max_width) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/kward/terminal/text.rb', line 30

def truncate_middle(text, max_width)
  limit = max_width.to_i
  return "" unless limit.positive?
  return text if width(text) <= limit

  tail_budget = limit / 2
  tail_space = tail_budget
  tail = []
  text.grapheme_clusters.reverse_each do |grapheme|
    cells = width(grapheme)
    break if cells > tail_space

    tail << grapheme
    tail_space -= cells
  end
  head_space = limit - 1 - (tail_budget - tail_space)
  "#{truncate(text, head_space)}…#{tail.reverse.join}"
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



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/kward/terminal/text.rb', line 64

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