Skip to content
Open
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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,8 @@
/spec/reports/
/tmp/

# gem file
*.gem

# rspec failure tracking
.rspec_status
6 changes: 3 additions & 3 deletions lib/tty/reader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ def read_line(prompt = "", value: "", echo: true, raw: true,
key_name = console.keys[char]

if exit_keys && exit_keys.include?(key_name)
trigger_key_event(char, line: line.to_s)
trigger_key_event(char, line: line)
break
end

Expand All @@ -323,7 +323,7 @@ def read_line(prompt = "", value: "", echo: true, raw: true,
direction = key_name == :shift_tab ? :previous : :next
if completion = @completer.complete(line, initial: initial,
direction: direction)
trigger_completion_event(completion, line.to_s)
trigger_completion_event(completion, line)
end
elsif key_name == :escape && completion_handler &&
(previous_key_name == :tab || previous_key_name == :shift_tab)
Expand Down Expand Up @@ -379,7 +379,7 @@ def read_line(prompt = "", value: "", echo: true, raw: true,
previous_key_name = key_name

# trigger before line is printed to allow for line changes
trigger_key_event(char, line: line.to_s)
trigger_key_event(char, line: line)

if raw && echo
output.print(line.to_s)
Expand Down
30 changes: 26 additions & 4 deletions lib/tty/reader/line.rb
Original file line number Diff line number Diff line change
Expand Up @@ -303,9 +303,9 @@ def insert(chars)
# Add char and move cursor
#
# @api public
def <<(char)
@text << char
@cursor += 1
def <<(str)
@text << str
@cursor += str.length
end

# Remove char from the line at current position
Expand All @@ -332,7 +332,29 @@ def remove(n = 1)
def to_s
"#{@prompt}#{@text}"
end
alias inspect to_s

# to_str enables type coercion and thus `line` in
# `trigger_char_event` does not need to be flattened to a string.
alias to_str to_s

# Overload inspect
#
# @api public
def inspect
to_s.inspect
end

# Overload equality comparison
#
# @api public
def ==(other)
if other.is_a? self.class
super other
else
other = other.to_s if other.respond_to? :to_s
to_s == other
end
end

# Text size
#
Expand Down
5 changes: 4 additions & 1 deletion spec/unit/complete_word_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -302,8 +302,11 @@
it "triggers completion event after completing the first suggestion" do
@completions = %w[aa ab ac]
completion_event = nil
test_line = nil # state of the line at the time the event is fired

reader.on(:complete) do |event|
completion_event = event
test_line = event.line.dup # we duplicate to cut the strings
formatted_completions = event.completions.map do |compl|
compl == event.completion ? "(#{compl})" : compl
end
Expand All @@ -316,7 +319,7 @@
expect(answer).to eq("x aa\n")
expect(completion_event.completion).to eq("aa")
expect(completion_event.completions).to eq(@completions)
expect(completion_event.line).to eq("x aa")
expect(test_line).to eq("x aa") # implicit test of to_s and ==
expect(completion_event.word).to eq("a")

output.rewind
Expand Down
10 changes: 8 additions & 2 deletions spec/unit/history_disabled_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@
input.rewind
chars = []
lines = []
reader.on(:keypress) { |event| chars << event.value; lines << event.line }
reader.on(:keypress) do |event|
chars << event.value
lines << event.line.dup # sever line object
end
answer = reader.read_line

expect(chars).to eq(%W(a b c \e[A d e f \n))
Expand All @@ -28,7 +31,10 @@
input.rewind
chars = []
lines = []
reader.on(:keypress) { |event| chars << event.value; lines << event.line }
reader.on(:keypress) do |event|
chars << event.value
lines << event.line.dup
end
answer = reader.read_line

expect(chars).to eq(%W(a b c \e[B d e f \n))
Expand Down
6 changes: 5 additions & 1 deletion spec/unit/publish_keypress_event_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@
input.rewind
chars = []
lines = []
reader.on(:keypress) { |event| chars << event.value; lines << event.line }
reader.on(:keypress) do |event|
chars << event.value
lines << event.line.to_s
end

answer = reader.read_line

expect(chars).to eq(%W(a b c \n))
Expand Down
12 changes: 6 additions & 6 deletions spec/unit/read_line_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@

reader.on(:keypress) do |event|
chars << event.value
lines << event.line
lines << event.line.to_s
end

3.times do
Expand All @@ -171,7 +171,7 @@

reader.on(:keypress) do |event|
chars << event.value
lines << event.line
lines << event.line.to_s
end

reader.read_line
Expand Down Expand Up @@ -206,7 +206,7 @@
answer = nil
lines = []

reader.on(:keypress) { |event| lines << event.line }
reader.on(:keypress) { |event| lines << event.line.to_s }

4.times do
answer = reader.read_line
Expand Down Expand Up @@ -250,7 +250,7 @@
answer = nil
lines = []

reader.on(:keypress) { |event| lines << event.line }
reader.on(:keypress) { |event| lines << event.line.to_s }

3.times do
answer = reader.read_line
Expand All @@ -271,7 +271,7 @@
answer = nil
lines = []

reader.on(:keypress) { |event| lines << event.line }
reader.on(:keypress) { |event| lines << event.line.to_s }

3.times do
answer = reader.read_line
Expand All @@ -289,7 +289,7 @@
answer = nil
lines = []

reader.on(:keypress) { |event| lines << event.line }
reader.on(:keypress) { |event| lines << event.line.to_s }

3.times do
answer = reader.read_line
Expand Down