Skip to content

Commit db73933

Browse files
committed
Fix Linux PTY render
1 parent 9a6a53d commit db73933

File tree

6 files changed

+40
-31
lines changed

6 files changed

+40
-31
lines changed

cli/root.go

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212
"github.com/manifoldco/promptui"
1313
"github.com/mattn/go-isatty"
1414
"github.com/spf13/cobra"
15-
"golang.org/x/xerrors"
1615

1716
"github.com/coder/coder/cli/config"
1817
"github.com/coder/coder/coderd"
@@ -138,21 +137,9 @@ func isTTY(cmd *cobra.Command) bool {
138137
}
139138

140139
func prompt(cmd *cobra.Command, prompt *promptui.Prompt) (string, error) {
141-
var ok bool
142-
reader, ok := cmd.InOrStdin().(io.Reader)
143-
if !ok {
144-
return "", xerrors.New("stdin must be a readcloser")
145-
}
146-
prompt.Stdin = readWriteCloser{
147-
Reader: reader,
148-
}
149-
150-
writer, ok := cmd.OutOrStdout().(io.Writer)
151-
if !ok {
152-
return "", xerrors.New("stdout must be a readcloser")
153-
}
140+
prompt.Stdin = io.NopCloser(cmd.InOrStdin())
154141
prompt.Stdout = readWriteCloser{
155-
Writer: writer,
142+
Writer: cmd.OutOrStdout(),
156143
}
157144

158145
// The prompt library displays defaults in a jarring way for the user

pty/pty.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,23 @@ import (
77
// PTY is a minimal interface for interacting with a TTY.
88
type PTY interface {
99
io.Closer
10-
// Output handles PTY output.
10+
11+
// Output handles TTY output.
1112
//
1213
// cmd.SetOutput(pty.Output()) would be used to specify a command
1314
// uses the output stream for writing.
1415
//
1516
// The same stream could be read to validate output.
1617
Output() io.ReadWriter
17-
// Input handles PTY input.
18+
19+
// Input handles TTY input.
1820
//
1921
// cmd.SetInput(pty.Input()) would be used to specify a command
2022
// uses the PTY input for reading.
2123
//
2224
// The same stream would be used to provide user input: pty.Input().Write(...)
2325
Input() io.ReadWriter
26+
2427
// Resize sets the size of the PTY.
2528
Resize(cols uint16, rows uint16) error
2629
}
@@ -29,3 +32,8 @@ type PTY interface {
2932
func New() (PTY, error) {
3033
return newPty()
3134
}
35+
36+
type readWriter struct {
37+
io.Reader
38+
io.Writer
39+
}

pty/pty_other.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,17 @@ type otherPty struct {
2727
}
2828

2929
func (p *otherPty) Input() io.ReadWriter {
30-
return p.pty
30+
return readWriter{
31+
Reader: p.tty,
32+
Writer: p.pty,
33+
}
3134
}
3235

3336
func (p *otherPty) Output() io.ReadWriter {
34-
return p.pty
35-
}
36-
37-
func (p *otherPty) WriteString(str string) (int, error) {
38-
return p.pty.WriteString(str)
37+
return readWriter{
38+
Reader: p.pty,
39+
Writer: p.tty,
40+
}
3941
}
4042

4143
func (p *otherPty) Resize(cols uint16, rows uint16) error {

pty/pty_windows.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,6 @@ type ptyWindows struct {
6767
closed bool
6868
}
6969

70-
type readWriter struct {
71-
io.Reader
72-
io.Writer
73-
}
74-
7570
func (p *ptyWindows) Output() io.ReadWriter {
7671
return readWriter{
7772
Reader: p.outputRead,

pty/ptytest/ptytest.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package ptytest
33
import (
44
"bufio"
55
"bytes"
6+
"fmt"
67
"io"
78
"os/exec"
89
"regexp"
@@ -88,6 +89,6 @@ func (p *PTY) ExpectMatch(str string) string {
8889
}
8990

9091
func (p *PTY) WriteLine(str string) {
91-
_, err := p.PTY.Input().Write([]byte(str + "\n"))
92+
_, err := fmt.Fprintf(p.PTY.Input(), "%s\n", str)
9293
require.NoError(p.t, err)
9394
}

pty/start_other.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,30 @@ package pty
55

66
import (
77
"os/exec"
8+
"syscall"
89

910
"github.com/creack/pty"
1011
)
1112

1213
func startPty(cmd *exec.Cmd) (PTY, error) {
13-
pty, err := pty.Start(cmd)
14+
pty, tty, err := pty.Open()
1415
if err != nil {
1516
return nil, err
1617
}
17-
return &otherPty{pty, pty}, nil
18+
cmd.SysProcAttr = &syscall.SysProcAttr{
19+
Setsid: true,
20+
Setctty: true,
21+
}
22+
cmd.Stdout = tty
23+
cmd.Stderr = tty
24+
cmd.Stdin = tty
25+
err = cmd.Start()
26+
if err != nil {
27+
_ = pty.Close()
28+
return nil, err
29+
}
30+
return &otherPty{
31+
pty: pty,
32+
tty: tty,
33+
}, nil
1834
}

0 commit comments

Comments
 (0)