Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Force encode ncurses input to $encoding #125

Merged
merged 1 commit into from
Aug 15, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/sup/buffer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions lib/sup/textfield.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 1 addition & 9 deletions lib/sup/util.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down