diff --git a/.gitignore b/.gitignore index 8eb3b06..87d7039 100644 --- a/.gitignore +++ b/.gitignore @@ -8,5 +8,8 @@ /spec/reports/ /tmp/ +# gem file +*.gem + # rspec failure tracking .rspec_status diff --git a/lib/tty/reader.rb b/lib/tty/reader.rb index c30062b..8ff48fd 100644 --- a/lib/tty/reader.rb +++ b/lib/tty/reader.rb @@ -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 @@ -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) @@ -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) diff --git a/lib/tty/reader/line.rb b/lib/tty/reader/line.rb index fb2bc57..962a3df 100644 --- a/lib/tty/reader/line.rb +++ b/lib/tty/reader/line.rb @@ -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 @@ -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 # diff --git a/spec/unit/complete_word_spec.rb b/spec/unit/complete_word_spec.rb index 842a654..b0e12ad 100644 --- a/spec/unit/complete_word_spec.rb +++ b/spec/unit/complete_word_spec.rb @@ -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 @@ -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 diff --git a/spec/unit/history_disabled_spec.rb b/spec/unit/history_disabled_spec.rb index 2643d4b..5a33b6c 100644 --- a/spec/unit/history_disabled_spec.rb +++ b/spec/unit/history_disabled_spec.rb @@ -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)) @@ -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)) diff --git a/spec/unit/publish_keypress_event_spec.rb b/spec/unit/publish_keypress_event_spec.rb index 6638150..3180b35 100644 --- a/spec/unit/publish_keypress_event_spec.rb +++ b/spec/unit/publish_keypress_event_spec.rb @@ -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)) diff --git a/spec/unit/read_line_spec.rb b/spec/unit/read_line_spec.rb index aab27c5..bd32a2e 100644 --- a/spec/unit/read_line_spec.rb +++ b/spec/unit/read_line_spec.rb @@ -151,7 +151,7 @@ reader.on(:keypress) do |event| chars << event.value - lines << event.line + lines << event.line.to_s end 3.times do @@ -171,7 +171,7 @@ reader.on(:keypress) do |event| chars << event.value - lines << event.line + lines << event.line.to_s end reader.read_line @@ -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 @@ -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 @@ -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 @@ -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