Module: RuVim::TextMetrics

Defined in:
lib/ruvim/text_metrics.rb

Defined Under Namespace

Classes: Cell

Constant Summary collapse

UNSAFE_CONTROL_CHAR_RE =
/[\u0000-\u0008\u000a-\u001f\u007f\u0080-\u009f]/

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.next_grapheme_char_index(line, char_index) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/ruvim/text_metrics.rb', line 28

def next_grapheme_char_index(line, char_index)
  s = line.to_s
  idx = [[char_index, 0].max, s.length].min
  return s.length if idx >= s.length

  rest = s[idx..].to_s
  m = /\A\X/.match(rest)
  return idx + 1 unless m

  idx + m[0].length
end

.pad_plain_to_screen_width(text, width, tabstop: 2) ⇒ Object



129
130
131
132
133
134
# File 'lib/ruvim/text_metrics.rb', line 129

def pad_plain_to_screen_width(text, width, tabstop: 2)
  cells, used = clip_cells_for_width(text, width, tabstop:)
  out = cells.map(&:glyph).join
  out << (" " * [width - used, 0].max)
  out
end

.previous_grapheme_char_index(line, char_index) ⇒ Object



17
18
19
20
21
22
23
24
25
26
# File 'lib/ruvim/text_metrics.rb', line 17

def previous_grapheme_char_index(line, char_index)
  idx = [char_index, 0].max
  return 0 if idx <= 0

  left = line[0...idx].to_s
  clusters = left.scan(/\X/)
  return 0 if clusters.empty?

  idx - clusters.last.length
end

.screen_col_for_char_index(line, char_index, tabstop: 2) ⇒ Object



40
41
42
43
44
# File 'lib/ruvim/text_metrics.rb', line 40

def screen_col_for_char_index(line, char_index, tabstop: 2)
  idx = [char_index, 0].max
  prefix = line[0...idx].to_s
  RuVim::DisplayWidth.display_width(prefix, tabstop:)
end

.terminal_safe_text(text) ⇒ Object



138
139
140
# File 'lib/ruvim/text_metrics.rb', line 138

def terminal_safe_text(text)
  text.to_s.gsub(UNSAFE_CONTROL_CHAR_RE, "?")
end

Instance Method Details

#char_index_for_screen_col(line, target_screen_col, tabstop: 2, align: :floor) ⇒ Object

---- Pure Ruby fallback ----



59
60
61
# File 'lib/ruvim/text_metrics.rb', line 59

def char_index_for_screen_col(line, target_screen_col, tabstop: 2, align: :floor)
  TextMetricsExt.char_index_for_screen_col(line, target_screen_col, tabstop:, align:)
end

#clip_cells_for_width(text, width, source_col_start: 0, tabstop: 2) ⇒ Object

---- C extension paths ----



49
50
51
# File 'lib/ruvim/text_metrics.rb', line 49

def clip_cells_for_width(text, width, source_col_start: 0, tabstop: 2)
  TextMetricsExt.clip_cells_for_width(text, width, source_col_start:, tabstop:)
end