This repository has been archived by the owner on Apr 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 358
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix re-rendering when prompt is exactly the width of the terminal (#321)
* Fix re-rendering when prompt is exactly the width of the terminal The code that measures width of rendered lines of content and whether any have overflown in the terminal did not account for the possibility that the content could be _exactly_ the width of the terminal. In that case, it shouldn't be counted as overflow. * Reduce test dependencies by going straight to `pty` This skips the go-expect & vt10x dependencies when stubbing only terminal size and goes straight to the `creack/pty` library, which is already used by go-expect under its old name, `kr/pty`. * Use old name of `creack/pty` since it was already vendored
- Loading branch information
Showing
3 changed files
with
101 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
// +build !windows | ||
|
||
package survey | ||
|
||
import ( | ||
"bytes" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/AlecAivazis/survey/v2/terminal" | ||
pseudotty "github.com/kr/pty" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestRenderer_countLines(t *testing.T) { | ||
t.Parallel() | ||
|
||
termWidth := 72 | ||
pty, tty, err := pseudotty.Open() | ||
require.Nil(t, err) | ||
defer pty.Close() | ||
defer tty.Close() | ||
|
||
err = pseudotty.Setsize(tty, &pseudotty.Winsize{ | ||
Rows: 30, | ||
Cols: uint16(termWidth), | ||
}) | ||
require.Nil(t, err) | ||
|
||
r := Renderer{ | ||
stdio: terminal.Stdio{ | ||
In: tty, | ||
Out: tty, | ||
Err: tty, | ||
}, | ||
} | ||
|
||
tests := []struct { | ||
name string | ||
buf *bytes.Buffer | ||
wants int | ||
}{ | ||
{ | ||
name: "empty", | ||
buf: new(bytes.Buffer), | ||
wants: 0, | ||
}, | ||
{ | ||
name: "no newline", | ||
buf: bytes.NewBufferString("hello"), | ||
wants: 0, | ||
}, | ||
{ | ||
name: "short line", | ||
buf: bytes.NewBufferString("hello\n"), | ||
wants: 1, | ||
}, | ||
{ | ||
name: "three short lines", | ||
buf: bytes.NewBufferString("hello\nbeautiful\nworld\n"), | ||
wants: 3, | ||
}, | ||
{ | ||
name: "full line", | ||
buf: bytes.NewBufferString(strings.Repeat("A", termWidth) + "\n"), | ||
wants: 1, | ||
}, | ||
{ | ||
name: "overflow", | ||
buf: bytes.NewBufferString(strings.Repeat("A", termWidth+1) + "\n"), | ||
wants: 2, | ||
}, | ||
{ | ||
name: "overflow fills 2nd line", | ||
buf: bytes.NewBufferString(strings.Repeat("A", termWidth*2) + "\n"), | ||
wants: 2, | ||
}, | ||
{ | ||
name: "overflow spills to 3rd line", | ||
buf: bytes.NewBufferString(strings.Repeat("A", termWidth*2+1) + "\n"), | ||
wants: 3, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
n := r.countLines(*tt.buf) | ||
assert.Equal(t, tt.wants, n) | ||
}) | ||
} | ||
} |