Module: WideChars

Extended by:
WideChars
Included in:
WideChars
Defined in:
lib/ffi-ncurses/widechars.rb

Instance Method Summary collapse

Instance Method Details

#display_slice(txt, *args, &b) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/ffi-ncurses/widechars.rb', line 23

def display_slice(txt, start, length = 1)
  count = 0
  str = ""
  if start.kind_of?(Range)
    limit = start.last + (start.exclude_end? ? 0 : 1)
    start = start.first
  else
    limit = start + length
  end
  txt.each_char do |char|
    if count >= start && count < limit
      str << char
    end
    count += 1
  end
  str
end

#display_width(txt) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/ffi-ncurses/widechars.rb', line 4

def display_width(txt)
  #buffer_size = (FFI::WideChars.mbstowcs(nil, self, 0) + 1) * FFI::WideChars.find_type(:wchar_t).size
  wchar_t = FFI::WideChars.find_type(:wchar_t)
  buffer_size = (txt.size + 1) * wchar_t.size
  buffer = FFI::Buffer.new(wchar_t, buffer_size)
  rv = FFI::WideChars.mbstowcs(buffer, txt, txt.size)
  if rv == -1
    raise ArgumentError, "Invalid multibyte sequence"
  else
    rv = FFI::WideChars.wcswidth(buffer, rv)
    if rv == -1
      raise ArgumentError, "Wide character string contains non-printable characters"
    else
      rv
    end
  end
end