1
0
Fork 0
mirror of https://github.com/pry/pry.git synced 2022-11-09 12:35:05 -05:00

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.
This commit is contained in:
Kyrylo Silin 2018-10-22 04:29:28 +08:00
parent 453607353a
commit 565d540c79

View file

@ -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