Skip to content

Commit f0363be

Browse files
committed
Use containerd/console to fix arrow keys on Windows
1 parent ecef75a commit f0363be

File tree

5 files changed

+18
-38
lines changed

5 files changed

+18
-38
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ go 1.15
55
require (
66
github.com/MakeNowJust/heredoc v1.0.0
77
github.com/Masterminds/semver/v3 v3.1.0
8+
github.com/containerd/console v1.0.1
89
github.com/dgrijalva/jwt-go/v4 v4.0.0-preview1
910
github.com/dustin/go-humanize v1.0.0
1011
github.com/githubnemo/CompileDaemon v1.2.1

go.sum

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ github.com/bgentry/speakeasy v0.1.0/go.mod h1:+zsyZBPWlz7T6j88CTgSN5bM796AkVf0kB
3333
github.com/bketelsen/crypt v0.0.3-0.20200106085610-5cbc8cc4026c/go.mod h1:MKsuJmJgSg28kpZDP6UIiPt0e0Oz0kqKNGyRaWEPv84=
3434
github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc=
3535
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
36+
github.com/containerd/console v1.0.1 h1:u7SFAJyRqWcG6ogaMAx3KjSTy1e3hT9QxqX7Jco7dRc=
37+
github.com/containerd/console v1.0.1/go.mod h1:XUsP6YE/mKtz6bxc+I8UiKKTP04qjQL4qcS3XoQ5xkw=
3638
github.com/coreos/bbolt v1.3.2/go.mod h1:iRUV2dpdMOn7Bo10OQBFzIJO9kkE559Wcmn+qkEiiKk=
3739
github.com/coreos/etcd v3.3.10+incompatible/go.mod h1:uF7uidLiAD3TWHmW31ZFd/JWoc32PjwdhPthX9715RE=
3840
github.com/coreos/etcd v3.3.13+incompatible/go.mod h1:uF7uidLiAD3TWHmW31ZFd/JWoc32PjwdhPthX9715RE=
@@ -179,6 +181,8 @@ github.com/pelletier/go-toml v1.2.0 h1:T5zMGML61Wp+FlcbWjRDT7yAxhJNAiPPLOFECq181
179181
github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic=
180182
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
181183
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
184+
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
185+
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
182186
github.com/pkg/profile v1.2.1/go.mod h1:hJw3o1OdXxsrSjjVksARp5W95eeEaEfptyVZyv6JUPA=
183187
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
184188
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
@@ -310,6 +314,8 @@ golang.org/x/sys v0.0.0-20190624142023-c5567b49c5d0 h1:HyfiK1WMnHj5FXFXatD+Qs1A/
310314
golang.org/x/sys v0.0.0-20190624142023-c5567b49c5d0/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
311315
golang.org/x/sys v0.0.0-20200116001909-b77594299b42 h1:vEOn+mP2zCOVzKckCZy6YsCtDblrpj/w7B9nxGNELpg=
312316
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
317+
golang.org/x/sys v0.0.0-20200916030750-2334cc1a136f h1:6Sc1XOXTulBN6imkqo6XoAXDEzoQ4/ro6xy7Vn8+rOM=
318+
golang.org/x/sys v0.0.0-20200916030750-2334cc1a136f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
313319
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
314320
golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
315321
golang.org/x/text v0.3.2 h1:tW2bmiBqwgJj/UpqtC8EpXEZVYOwU0yG4iWbprSVAcs=

pkg/cmd/webspace/console.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ import (
77
"log"
88
"os"
99

10+
"github.com/containerd/console"
1011
"github.com/spf13/cobra"
11-
"golang.org/x/crypto/ssh/terminal"
1212

1313
"github.com/netsoc/cli/pkg/config"
1414
"github.com/netsoc/cli/pkg/util"
@@ -58,23 +58,22 @@ func consoleRun(opts consoleOptions) error {
5858
return fmt.Errorf("failed to open websocket connection: %w", err)
5959
}
6060

61-
stdin := int(os.Stdin.Fd())
61+
tty := console.Current()
6262

63-
w, h, err := util.GetTerminalSize(stdin)
63+
s, err := tty.Size()
6464
if err != nil {
6565
return fmt.Errorf("failed to get terminal size: %w", err)
6666
}
67-
if err := conn.WriteJSON(util.ConsoleSize{Width: w, Height: h}); err != nil {
67+
if err := conn.WriteJSON(util.ConsoleSize{Width: int(s.Width), Height: int(s.Height)}); err != nil {
6868
conn.Close()
6969
return fmt.Errorf("failed to send initial terminal size: %w", err)
7070
}
7171

72-
ttyState, err := terminal.MakeRaw(stdin)
73-
if err != nil {
72+
if err := tty.SetRaw(); err != nil {
7473
conn.Close()
75-
return fmt.Errorf("failed to configure terminal: %w", err)
74+
return fmt.Errorf("failed to put terminal in raw mode: %w", err)
7675
}
77-
defer terminal.Restore(stdin, ttyState)
76+
defer tty.Reset()
7877

7978
rw := util.NewWebsocketIO(conn, func(s string, _ *util.WebsocketIO) {
8079
util.Debugf("Received websocket text message: %v", s)

pkg/util/console_unix.go

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,32 +8,27 @@ import (
88
"os/signal"
99
"syscall"
1010

11-
"golang.org/x/crypto/ssh/terminal"
11+
"github.com/containerd/console"
1212
)
1313

1414
// ResizeListener listens for changes in console size
1515
func ResizeListener(sizeChan chan ConsoleSize, stop chan struct{}) {
1616
winchChan := make(chan os.Signal, 1)
1717
signal.Notify(winchChan, syscall.SIGWINCH)
1818

19-
stdin := int(os.Stdin.Fd())
19+
tty := console.Current()
2020
for {
2121
select {
2222
case <-winchChan:
23-
w, h, err := terminal.GetSize(stdin)
23+
s, err := tty.Size()
2424
if err != nil {
2525
log.Printf("Failed to get terminal size: %v", err)
2626
return
2727
}
2828

29-
sizeChan <- ConsoleSize{w, h}
29+
sizeChan <- ConsoleSize{int(s.Width), int(s.Height)}
3030
case <-stop:
3131
return
3232
}
3333
}
3434
}
35-
36-
// GetTerminalSize returns the dimensions of a TTY (lines x cols)
37-
func GetTerminalSize(fd int) (int, int, error) {
38-
return terminal.GetSize(fd)
39-
}

pkg/util/console_windows.go

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,5 @@
22

33
package util
44

5-
//import (
6-
// "os"
7-
//
8-
// "github.com/TheTitanrain/w32"
9-
//)
10-
115
// ResizeListener on Windows does nothing
126
func ResizeListener(sizeChan chan ConsoleSize, stop chan struct{}) {}
13-
14-
// TODO: do this properly
15-
// GetTerminalSize on Windows does nothing (Windows bad)
16-
func GetTerminalSize(fd int) (int, int, error) {
17-
//stdout := w32.HANDLE(os.Stdout.Fd())
18-
//info := w32.GetConsoleScreenBufferInfo(stdout)
19-
20-
//lines := info.SrWindow.Bottom - info.SrWindow.Top + 1
21-
//cols := info.SrWindow.Right - info.SrWindow.Left + 1
22-
//cols := info.DwSize.X
23-
//lines := info.DwSize.Y
24-
25-
//return int(cols), int(lines), nil
26-
return 80, 24, nil
27-
}

0 commit comments

Comments
 (0)