Skip to content

Commit a7ead6d

Browse files
Derek Cheagl
Derek Che
authored andcommitted
ssh/terminal: fix SetSize when nothing on current line
SetSize has a problem may cause the following ReadPassword setting temporary prompt not working, when changing width the current SetSize will call clearAndRepaintLinePlusNPrevious which would print an old prompt whatever the current line has, causing a following ReadPassword with temporary prompt not printing the different prompt. When running code like this, the nt.SetSize prints a "> " as prompt then the temporary "Password: " prompt would never show up. ```go oldState, err := terminal.MakeRaw(int(os.Stdin.Fd())) width, height, _ = terminal.GetSize(int(os.Stdin.Fd())) nt := terminal.NewTerminal(os.Stdin, "> ") nt.SetSize(width, height) password, err = nt.ReadPassword("Password: ") ``` the new test cases is to test SetSize with different terminal sizes, either shrinking or expanding, a following ReadPassword should get the correct temporary prompt. Change-Id: I33d13b2c732997c0c88670d53545b8c0048b94b6 Reviewed-on: https://go-review.googlesource.com/1861 Reviewed-by: Adam Langley <[email protected]>
1 parent 1fbbd62 commit a7ead6d

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

ssh/terminal/terminal.go

+4
Original file line numberDiff line numberDiff line change
@@ -789,6 +789,10 @@ func (t *Terminal) SetSize(width, height int) error {
789789
// If the width didn't change then nothing else needs to be
790790
// done.
791791
return nil
792+
case len(t.line) == 0 && t.cursorX == 0 && t.cursorY == 0:
793+
// If there is nothing on current line and no prompt printed,
794+
// just do nothing
795+
return nil
792796
case width < oldWidth:
793797
// Some terminals (e.g. xterm) will truncate lines that were
794798
// too long when shinking. Others, (e.g. gnome-terminal) will

ssh/terminal/terminal_test.go

+26
Original file line numberDiff line numberDiff line change
@@ -241,3 +241,29 @@ func TestPasswordNotSaved(t *testing.T) {
241241
t.Fatalf("password was saved in history")
242242
}
243243
}
244+
245+
var setSizeTests = []struct {
246+
width, height int
247+
}{
248+
{40, 13},
249+
{80, 24},
250+
{132, 43},
251+
}
252+
253+
func TestTerminalSetSize(t *testing.T) {
254+
for _, setSize := range setSizeTests {
255+
c := &MockTerminal{
256+
toSend: []byte("password\r\x1b[A\r"),
257+
bytesPerRead: 1,
258+
}
259+
ss := NewTerminal(c, "> ")
260+
ss.SetSize(setSize.width, setSize.height)
261+
pw, _ := ss.ReadPassword("Password: ")
262+
if pw != "password" {
263+
t.Fatalf("failed to read password, got %s", pw)
264+
}
265+
if string(c.received) != "Password: \r\n" {
266+
t.Errorf("failed to set the temporary prompt expected %q, got %q", "Password: ", c.received)
267+
}
268+
}
269+
}

0 commit comments

Comments
 (0)