vendor/highline/highline.rb in keybox-1.1.0 vs vendor/highline/highline.rb in keybox-1.1.1
- old
+ new
@@ -27,11 +27,11 @@
# even if HighLine had to ask many times, validate results, perform range
# checking, convert types, etc.
#
class HighLine
# The version of the installed library.
- VERSION = "1.2.6".freeze
+ VERSION = "1.2.9".freeze
# An internal HighLine error. User code does not need to trap this.
class QuestionError < StandardError
# do nothing, just creating a unique error type
end
@@ -46,10 +46,23 @@
# Returns true if HighLine is currently using color escapes.
def self.use_color?
@@use_color
end
+
+ # The setting used to disable EOF tracking.
+ @@track_eof = true
+
+ # Pass +false+ to _setting_ to turn off HighLine's EOF tracking.
+ def self.track_eof=( setting )
+ @@track_eof = setting
+ end
+
+ # Returns true if HighLine is currently tracking EOF for input.
+ def self.track_eof?
+ @@track_eof
+ end
# The setting used to control color schemes.
@@color_scheme = nil
# Pass ColorScheme to _setting_ to turn set a HighLine color scheme.
@@ -73,11 +86,13 @@
#
CLEAR = "\e[0m"
# An alias for CLEAR.
RESET = CLEAR
# Erase the current line of terminal output.
- ERASE_LINE = "\e[K"
+ ERASE_LINE = "\e[K"
+ # Erase the character under the cursor.
+ ERASE_CHAR = "\e[P"
# The start of an ANSI bold sequence.
BOLD = "\e[1m"
# The start of an ANSI dark sequence. (Terminal support uncommon.)
DARK = "\e[2m"
# The start of an ANSI underline sequence.
@@ -187,13 +202,15 @@
#
def ask( question, answer_type = String, &details ) # :yields: question
@question ||= Question.new(question, answer_type, &details)
return gather if @question.gather
-
- # readline() needs to handle it's own output
- say(@question) unless @question.readline
+
+ # readline() needs to handle it's own output, but readline only supports
+ # full line reading. Therefore if @question.echo is anything but true,
+ # the prompt will not be issued. And we have to account for that now.
+ say(@question) unless (@question.readline and @question.echo == true)
begin
@answer = @question.answer_or_default(get_response)
unless @question.valid_answer?(@answer)
explain_error(:not_valid)
raise QuestionError
@@ -571,11 +588,12 @@
Readline.readline(question, true) ) )
$VERBOSE = old_verbose
answer
else
- raise EOFError, "The input stream is exhausted." if @input.eof?
+ raise EOFError, "The input stream is exhausted." if @@track_eof and
+ @input.eof?
@question.change_case(@question.remove_whitespace(@input.gets))
end
end
@@ -596,26 +614,48 @@
get_line
else
raw_no_echo_mode if stty = CHARACTER_MODE == "stty"
line = ""
+ backspace_limit = 0
begin
+
while character = (stty ? @input.getc : get_character(@input))
- line << character.chr
+ # honor backspace and delete
+ if character == 127 or character == 8
+ line.slice!(-1, 1)
+ backspace_limit -= 1
+ else
+ line << character.chr
+ 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 or
(@question.limit and line.size == @question.limit)
- @output.print(@question.echo) if @question.echo != false
+ if @question.echo != false
+ if character == 127 or character == 8
+ # only backspace if we have characters on the line to
+ # eliminate, otherwise we'll tromp over the prompt
+ if backspace_limit >= 0 then
+ @output.print("\b#{ERASE_CHAR}")
+ else
+ # do nothing
+ end
+ else
+ @output.print(@question.echo)
+ end
+ @output.flush
+ end
end
- if @question.overwrite
- @output.print("\r#{ERASE_LINE}")
- @output.flush
- else
- say("\n")
- end
ensure
restore_mode if stty
+ end
+ if @question.overwrite
+ @output.print("\r#{ERASE_LINE}")
+ @output.flush
+ else
+ say("\n")
end
@question.change_case(@question.remove_whitespace(line))
end
elsif @question.character == :getc