Module: WideCharsJR

Defined in:
lib/ffi-ncurses/widechars.rb

Overview

This won’t work in 1.8.7 (chars == bytes)

Instance Method Summary collapse

Instance Method Details

#display_width2Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/ffi-ncurses/widechars.rb', line 103

def display_width2
  width = 0
  wchar_t_size = FFI::WideChars.find_type(:wchar_t).size
  # we rely on Ruby to convert a char into a mbs
  each_char do |ch|
    buffer_size = (ch.size + 1) * wchar_t_size
    # p [:size, size]
    buffer = FFI::Buffer.new(FFI::WideChars.find_type(:wchar_t), buffer_size)
    rv = FFI::WideChars.mbstowcs(buffer, ch, ch.size)
    # p [:mbstowcs, rv]
    if rv == -1
      raise ArgumentError, "Invalid multibyte sequence"
    else
      rv = FFI::WideChars.wcswidth(buffer, rv)
      # p [:wcswidth, rv]
      if rv == -1
        raise ArgumentError, "Wide character string contains non-printable characters"
      else
        width += rv
      end
    end
  end
  width
end