Skip to content

Commit

Permalink
Fix messed up display when empty prompt.
Browse files Browse the repository at this point in the history
  • Loading branch information
I3oris committed Aug 31, 2024
1 parent db423da commit 5da267e
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
33 changes: 33 additions & 0 deletions spec/expression_editor_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,39 @@ module Reply
"****>5"
end

it "Don't mess up the terminal when the prompt is empty" do
editor = ExpressionEditor.new { "" }
editor.output = IO::Memory.new
editor.color = false
editor.height = 5
editor.width = 15

editor.update { editor << "Hello,\nWorld" }
editor.verify_output "\e[1G\e[J" \
"Hello,\n" \
"World"

editor.output = IO::Memory.new
editor.update { editor << '\n' }
editor.verify_output "\e[1A\e[1G\e[J" \
"Hello,\n" \
"World\n"

editor.output = IO::Memory.new
editor.update { editor << "1+1" }
editor.verify_output "\e[2A\e[1G\e[J" \
"Hello,\n" \
"World\n" \
"1+1"

editor.output = IO::Memory.new
editor.update { editor << '\n' }
editor.verify_output "\e[2A\e[1G\e[J" \
"Hello,\n" \
"World\n" \
"1+1\n"
end

# TODO:
# header
end
Expand Down
3 changes: 3 additions & 0 deletions src/expression_editor.cr
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ module Reply
# Creates a new `ExpressionEditor` with the given *prompt*.
def initialize(&@prompt : Int32, Bool -> String)
@prompt_size = @prompt.call(0, false).size # uncolorized size
@prompt_size = 1 if @prompt_size == 0
end

# Sets a `Proc` allowing to display a header above the prompt. (used by auto-completion)
Expand Down Expand Up @@ -753,6 +754,8 @@ module Reply

private def print_prompt(io, line_index)
line_prompt_size = @prompt.call(line_index, false).size # uncolorized size
line_prompt_size = 1 if line_prompt_size == 0

@prompt_size = {line_prompt_size, @prompt_size}.max
io.print @prompt.call(line_index, color?)

Expand Down

0 comments on commit 5da267e

Please sign in to comment.