diff --git a/lib/sup/buffer.rb b/lib/sup/buffer.rb index 8e0b04890..d6f618c9d 100644 --- a/lib/sup/buffer.rb +++ b/lib/sup/buffer.rb @@ -622,7 +622,7 @@ def ask domain, question, default=nil, &block tf.deactivate draw_screen :sync => false, :status => status, :title => title end - tf.value.tap { |x| x.fix_encoding if x } + tf.value.tap { |x| x } end def ask_getch question, accept=nil diff --git a/lib/sup/textfield.rb b/lib/sup/textfield.rb index 343e450f2..c69d3e373 100644 --- a/lib/sup/textfield.rb +++ b/lib/sup/textfield.rb @@ -168,6 +168,16 @@ def get_cursed_value else # trailing spaces v + (" " * (x - @question.length - v.length)) end + + # ncurses returns a ASCII-8BIT (binary) string, which + # bytes presumably are of current charset encoding. we force_encoding + # so that the char representation / string is tagged will be the + # system locale and also hopefully the terminal/input encoding. an + # incorrectly configured terminal encoding (not matching the system + # encoding) will produce erronous results, but will also do that for + # a log of other programs since it is impossible to detect which is + # which and what encoding the inputted byte chars are supposed to have. + v.force_encoding($encoding).fix_encoding end def remove_extra_space diff --git a/lib/sup/util.rb b/lib/sup/util.rb index bf33dcff2..5cff6fa8c 100644 --- a/lib/sup/util.rb +++ b/lib/sup/util.rb @@ -367,15 +367,7 @@ def wrap len # # Not Ruby 1.8 compatible def fix_encoding - # first try to set the string to utf-8 and check if it is valid - # in case ruby just thinks it is something else - orig_encoding = encoding - force_encoding 'UTF-8' - return self if valid_encoding? - - # that didn't work, go back to original and try to convert - force_encoding orig_encoding - + # first try to encode to utf-8 from whatever current encoding encode!('UTF-8', :invalid => :replace, :undef => :replace) # do this anyway in case string is set to be UTF-8, encoding to