Skip to content
This repository was archived by the owner on Aug 30, 2024. It is now read-only.

Commit 6a5fbc5

Browse files
committed
Adds support for windows
Change-Id: I2797929cfa72752932e334fc44614095eb1cdea6
1 parent 27020b7 commit 6a5fbc5

File tree

7 files changed

+112
-28
lines changed

7 files changed

+112
-28
lines changed

.github/workflows/test.yaml

Whitespace-only changes.

ci/build.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ build(){
3030
if [[ "$(uname)" == "Darwin" ]]; then
3131
GOOS=linux build
3232
CGO_ENABLED=1 GOOS=darwin build
33+
GOOS=windows GOARCH=386 build
3334
exit 0
3435
fi
3536

cmd/coder/resize_unix.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// +build !windows
2+
3+
package main
4+
5+
import (
6+
"context"
7+
"os"
8+
"os/signal"
9+
10+
"golang.org/x/crypto/ssh/terminal"
11+
"golang.org/x/sys/unix"
12+
13+
"go.coder.com/flog"
14+
)
15+
16+
func resizeEvents(ctx context.Context, termfd int) chan resizeEvent {
17+
sigs := make(chan os.Signal, 16)
18+
signal.Notify(sigs, unix.SIGWINCH)
19+
20+
events := make(chan resizeEvent)
21+
22+
go func() {
23+
for ctx.Err() == nil {
24+
width, height, err := terminal.GetSize(termfd)
25+
if err != nil {
26+
flog.Error("get term size: %v", err)
27+
break
28+
}
29+
events <- resizeEvent{
30+
height: uint16(height),
31+
width: uint16(width),
32+
}
33+
34+
<-sigs
35+
}
36+
}()
37+
38+
return events
39+
}

cmd/coder/resize_windows.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// +build windows
2+
3+
package main
4+
5+
import (
6+
"context"
7+
"time"
8+
9+
"golang.org/x/crypto/ssh/terminal"
10+
11+
"go.coder.com/flog"
12+
)
13+
14+
// windows does have a unix.SIGWINCH equivalent, so we poll the terminal size
15+
// and send resize events when needed
16+
func resizeEvents(ctx context.Context, termfd int) chan resizeEvent {
17+
events := make(chan resizeEvent)
18+
t := time.Tick(time.Millisecond * 100)
19+
20+
go func() {
21+
var lastEvent *resizeEvent
22+
23+
for {
24+
select {
25+
case <-ctx.Done():
26+
break
27+
case <-t:
28+
width, height, err := terminal.GetSize(termfd)
29+
if err != nil {
30+
flog.Error("get term size: %v", err)
31+
break
32+
}
33+
event := resizeEvent{
34+
height: uint16(height),
35+
width: uint16(width),
36+
}
37+
if !event.equal(lastEvent) {
38+
events <- event
39+
}
40+
lastEvent = &event
41+
}
42+
}
43+
}()
44+
45+
return events
46+
}

cmd/coder/shell.go

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,11 @@ import (
44
"context"
55
"io"
66
"os"
7-
"os/signal"
87
"strings"
98
"time"
109

1110
"github.com/spf13/pflag"
1211
"golang.org/x/crypto/ssh/terminal"
13-
"golang.org/x/sys/unix"
1412
"golang.org/x/time/rate"
1513
"golang.org/x/xerrors"
1614
"nhooyr.io/websocket"
@@ -46,31 +44,33 @@ func enableTerminal(fd int) (restore func(), err error) {
4644
}, nil
4745
}
4846

47+
type resizeEvent struct {
48+
height, width uint16
49+
}
50+
51+
func (s resizeEvent) equal(s2 *resizeEvent) bool {
52+
if s2 == nil {
53+
return false
54+
}
55+
return s.height == s2.height && s.width == s2.width
56+
}
57+
4958
func sendResizeEvents(ctx context.Context, termfd int, process wsep.Process) {
50-
sigs := make(chan os.Signal, 16)
51-
signal.Notify(sigs, unix.SIGWINCH)
59+
events := resizeEvents(ctx, termfd)
5260

5361
// Limit the frequency of resizes to prevent a stuttering effect.
5462
resizeLimiter := rate.NewLimiter(rate.Every(time.Millisecond*100), 1)
55-
56-
for ctx.Err() == nil {
57-
if ctx.Err() != nil {
58-
return
59-
}
60-
width, height, err := terminal.GetSize(termfd)
61-
if err != nil {
62-
flog.Error("get term size: %v", err)
63-
return
64-
}
65-
66-
err = process.Resize(ctx, uint16(height), uint16(width))
67-
if err != nil {
63+
for {
64+
select {
65+
case newsize := <-events:
66+
err := process.Resize(ctx, newsize.height, newsize.width)
67+
if err != nil {
68+
return
69+
}
70+
_ = resizeLimiter.Wait(ctx)
71+
case <-ctx.Done():
6872
return
6973
}
70-
71-
// Do this last so the first resize is sent.
72-
<-sigs
73-
resizeLimiter.Wait(ctx)
7474
}
7575
}
7676

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module cdr.dev/coder-cli
33
go 1.14
44

55
require (
6-
cdr.dev/wsep v0.0.0-20200616212001-0613cfe9a4ac
6+
cdr.dev/wsep v0.0.0-20200727194627-13ef1f8df965
77
github.com/fatih/color v1.9.0 // indirect
88
github.com/gorilla/websocket v1.4.1
99
github.com/kirsle/configdir v0.0.0-20170128060238-e45d2f54772f

go.sum

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
cdr.dev/slog v1.3.0 h1:MYN1BChIaVEGxdS7I5cpdyMC0+WfJfK8BETAfzfLUGQ=
22
cdr.dev/slog v1.3.0/go.mod h1:C5OL99WyuOK8YHZdYY57dAPN1jK2WJlCdq2VP6xeQns=
3-
cdr.dev/wsep v0.0.0-20200615020153-e2b1c576fc40 h1:f369880iSAZ3cXwvbdc9WIyy3FZ4yanusYZjaVHeis4=
4-
cdr.dev/wsep v0.0.0-20200615020153-e2b1c576fc40/go.mod h1:2VKClUml3gfmLez0gBxTJIjSKszpQotc2ZqPdApfK/Y=
5-
cdr.dev/wsep v0.0.0-20200616212001-0613cfe9a4ac h1:rl4O0qfxgNRWBUe5gQu4of2cdsclcpjGYmLQhSCHX7c=
6-
cdr.dev/wsep v0.0.0-20200616212001-0613cfe9a4ac/go.mod h1:2VKClUml3gfmLez0gBxTJIjSKszpQotc2ZqPdApfK/Y=
3+
cdr.dev/wsep v0.0.0-20200727194627-13ef1f8df965 h1:zFpraxgmcgX60oWFs8r1moWFMJ0x945t4moxNb6fjJ8=
4+
cdr.dev/wsep v0.0.0-20200727194627-13ef1f8df965/go.mod h1:N20HJdMn6q9NG7sjxL4uYdBQBGOf8/6psfdMyuJnYw8=
75
cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
86
cloud.google.com/go v0.34.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
97
cloud.google.com/go v0.38.0/go.mod h1:990N+gfupTy94rShfmMCWGDn0LpTmnzTp2qbd1dvSRU=
@@ -109,6 +107,8 @@ github.com/gorilla/websocket v1.4.1/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/ad
109107
github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
110108
github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
111109
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
110+
github.com/iamacarpet/go-winpty v1.0.2 h1:jwPVTYrjAHZx6Mcm6K5i9G4opMp5TblEHH5EQCl/Gzw=
111+
github.com/iamacarpet/go-winpty v1.0.2/go.mod h1:/GHKJicG/EVRQIK1IQikMYBakBkhj/3hTjLgdzYsmpI=
112112
github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI=
113113
github.com/json-iterator/go v1.1.9 h1:9yzud/Ht36ygwatGx56VwCZtlI/2AD15T1X2sjSuGns=
114114
github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
@@ -240,8 +240,6 @@ golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7w
240240
golang.org/x/sys v0.0.0-20191210023423-ac6580df4449/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
241241
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
242242
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
243-
golang.org/x/sys v0.0.0-20200610111108-226ff32320da h1:bGb80FudwxpeucJUjPYJXuJ8Hk91vNtfvrymzwiei38=
244-
golang.org/x/sys v0.0.0-20200610111108-226ff32320da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
245243
golang.org/x/sys v0.0.0-20200615200032-f1bc736245b1 h1:ogLJMz+qpzav7lGMh10LMvAkM/fAoGlaiiHYiFYdm80=
246244
golang.org/x/sys v0.0.0-20200615200032-f1bc736245b1/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
247245
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=

0 commit comments

Comments
 (0)