summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAbinoam Praxedes Marques Jr <abinoam@gmail.com>2015-05-24 00:37:55 -0300
committerAbinoam Praxedes Marques Jr <abinoam@gmail.com>2015-05-24 00:37:58 -0300
commit63a90463bf3b5bcc12f1c4cfe57b7d69c1adb46e (patch)
treef62e297f5a54a2a9b0d2b7efdf6543db68debf24
parent34dc2c64f0ceadaa4bfe6dde8bae1bac18ca346e (diff)
downloadhighline-63a90463bf3b5bcc12f1c4cfe57b7d69c1adb46e.tar.gz
Change getbyte -> getc and easy all encoding issues
There was a lot of encoding handling because of compatibility with old Ruby versions. Using getc handles the multibyte chars correctly (with current Ruby versions) and make us be able to drop all the encoding handling.
-rwxr-xr-xlib/highline.rb36
-rw-r--r--lib/highline/statement.rb4
-rw-r--r--lib/highline/terminal/unix_stty.rb2
3 files changed, 15 insertions, 27 deletions
diff --git a/lib/highline.rb b/lib/highline.rb
index dad66dd..9adcdde 100755
--- a/lib/highline.rb
+++ b/lib/highline.rb
@@ -388,7 +388,7 @@ class HighLine
statement = render_statement(statement)
return if statement.empty?
- out = (indentation+statement).encode(Encoding.default_external, { :undef => :replace } )
+ out = (indentation+statement)
# Don't add a newline if statement ends with whitespace, OR
# if statement ends with whitespace before a color escape code.
@@ -689,26 +689,24 @@ class HighLine
if question.echo == true and question.limit.nil?
get_line(question)
else
- line = "".encode(Encoding::BINARY)
+ line = ""
backspace_limit = 0
terminal.raw_no_echo_mode_exec do
while character = terminal.get_character(@input)
# honor backspace and delete
- if character == 127 or character == 8
- line = line.force_encoding(Encoding.default_external)
+ if character == "\b"
line.slice!(-1, 1)
backspace_limit -= 1
- line = line.force_encoding(Encoding::BINARY)
else
- line << character.chr
- backspace_limit = line.dup.force_encoding(Encoding.default_external).size
+ line << character
+ backspace_limit = line.size
end
# looking for carriage return (decimal 13) or
# newline (decimal 10) in raw input
- break if character == 13 or character == 10
+ break if character == "\n" or character == "\r"
if question.echo != false
- if character == 127 or character == 8
+ if character == "\b"
# only backspace if we have characters on the line to
# eliminate, otherwise we'll tromp over the prompt
if backspace_limit >= 0 then
@@ -717,16 +715,10 @@ class HighLine
# do nothing
end
else
- line_with_next_char_encoded = line.dup.force_encoding(Encoding.default_external)
- # For multi-byte character, does this
- # last character completes the character?
- # Then print it.
- if line_with_next_char_encoded.valid_encoding?
- if question.echo == true
- @output.print(line_with_next_char_encoded[-1])
- else
- @output.print(question.echo)
- end
+ if question.echo == true
+ @output.print(line[-1])
+ else
+ @output.print(question.echo)
end
end
@output.flush
@@ -742,20 +734,20 @@ class HighLine
say("\n")
end
- question.format_answer(line.force_encoding(Encoding.default_external))
+ question.format_answer(line)
end
end
def get_response_getc_mode(question)
terminal.raw_no_echo_mode_exec do
- response = @input.getbyte.chr
+ response = @input.getc
question.format_answer(response)
end
end
def get_response_character_mode(question)
terminal.raw_no_echo_mode_exec do
- response = terminal.get_character(@input).chr
+ response = terminal.get_character(@input)
if question.overwrite
erase_current_line
else
diff --git a/lib/highline/statement.rb b/lib/highline/statement.rb
index 0c260be..4d386d3 100644
--- a/lib/highline/statement.rb
+++ b/lib/highline/statement.rb
@@ -37,10 +37,6 @@ class HighLine::Statement
statement = HighLine::Wrapper.wrap(statement, highline.wrap_at)
statement = HighLine::Paginator.new(highline).page_print(statement)
- # 'statement' is encoded in US-ASCII when using ruby 1.9.3(-p551)
- # 'indentation' is correctly encoded (same as default_external encoding)
- statement = statement.force_encoding(Encoding.default_external)
-
statement = statement.gsub(/\n(?!$)/,"\n#{highline.indentation}") if highline.multi_indent
statement
end
diff --git a/lib/highline/terminal/unix_stty.rb b/lib/highline/terminal/unix_stty.rb
index 00bb60f..1a3501d 100644
--- a/lib/highline/terminal/unix_stty.rb
+++ b/lib/highline/terminal/unix_stty.rb
@@ -34,7 +34,7 @@ class HighLine
end
def get_character( input = STDIN )
- input.getbyte
+ input.getc
end
def character_mode