I need some advice about how to use wcswidth on Ruby 1.9 strings.
Wcswidth returns the column width of a string, i.e. how much of the
horizontal space on the screen a string takes up when printed to a
terminal. Being able to calculate column width is crucial for any
console-based program that deals with non-ASCII characters. For example,
Chinese characters typically take up two columns.
So, I’d like to write a gem that provides wcswidth to Ruby. I have a
simple version that works:
Unfortunately, this code requires that the string’s encoding match your
LC_CTYPE, because mbstowcs relies on LC_CTYPE to produce wchar_t’s. If
those don’t match, you’ll either get nothing, or, possibly, a wrong
So, is there a mapping between Ruby string encodings and LC_CTYPE
values? If so, I could at least check that the conversion is done
correctly (or possibly even skip the mbstowcs step, if I know what
wchar_t is and what encoding corresponds to it–of course what wchar_t
means is system-dependent.)
Or is there a better way to do this than using mbstowcs?
Or heck, is there already a way of getting string column width in Ruby?
It seems like there should be, but I don’t see one.