summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKyrylo Silin <silin@kyrylo.org>2018-10-22 04:29:28 +0800
committerKyrylo Silin <silin@kyrylo.org>2018-10-22 04:29:28 +0800
commit565d540c791aa555af4fc8d805d9bb0f7b91252d (patch)
treed91362176ef6b45e056ced7131fdd37cb0620528
parent453607353a77db7d7864f4ee9e01297b1483ccb9 (diff)
downloadpry-565d540c791aa555af4fc8d805d9bb0f7b91252d.tar.gz
repl: correctly calculate overhang for empty lines
This improves on https://github.com/pry/pry/pull/1813. I spotted a problem with the way it works: sometimes, if you enter an empty line, the output would span across two lines: ``` [1] pry(main)> [2] pry(main)> [3] pry(main)> ``` This is because overhang is bigger than the current line. I also noticed a bug with method definition, where defining a method leaves an unwanted empty line: ``` [3] pry(main)> def foo | end => :foo ``` To fix this I changed the way we calculate overhang. First and the most important change is that I stopped calculating overhang for emacs mode. This is because it's too risky to introduce this change because it's the default mode and 99% of our users use this, so there's no need to change this behaviour. Another reason is that emacs mode users typically don't use any mode indicators (because emacs has no modes), so it strikes me as a more pragmatic solution. With Vi mode we calculate overhang and still support custom indicators.
-rw-r--r--lib/pry/repl.rb26
1 files changed, 13 insertions, 13 deletions
diff --git a/lib/pry/repl.rb b/lib/pry/repl.rb
index 4377c6e2..f3a7b7fa 100644
--- a/lib/pry/repl.rb
+++ b/lib/pry/repl.rb
@@ -108,7 +108,7 @@ class Pry
output.print @indent.correct_indentation(
current_prompt,
indented_val,
- calculate_overhang(original_val, indented_val)
+ calculate_overhang(current_prompt, original_val, indented_val)
)
output.flush
end
@@ -232,19 +232,19 @@ class Pry
end
end
- def calculate_overhang(original_val, indented_val)
- if readline_available?
- # Readline is able to display current mode indicator as prefix for the
- # current line. That causes some characters being left in the terminal
- # after we indent. So let's overwrite whole line to be sure.
- #
- # See: https://github.com/pry/pry/issues/1812
- _rows, cols = Terminal.screen_size
- cols - indented_val.length
- else
- [ original_val.length - indented_val.length, 0 ].max
+ # Calculates correct overhang for current line. Supports vi Readline
+ # mode and its indicators such as "(ins)" or "(cmd)".
+ #
+ # @return [Integer]
+ # @note This doesn't calculate overhang for Readline's emacs mode with an
+ # indicator because emacs is the default mode and it doesn't use
+ # indicators in 99% of cases.
+ def calculate_overhang(current_prompt, original_val, indented_val)
+ overhang = original_val.length - indented_val.length
+ if readline_available? && Readline.vi_editing_mode?
+ overhang += current_prompt.length - indented_val.length
end
+ [0, overhang].max
end
-
end
end