-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathterminal_windows.go
68 lines (61 loc) · 1.61 KB
/
terminal_windows.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// +build windows
package main
import (
"encoding/json"
"fmt"
"os"
"sync"
"time"
"github.com/gorilla/websocket"
terminal "golang.org/x/term"
)
type terminalSize struct {
Height int `json:"height"`
Width int `json:"width"`
}
func getTerminalSize() (terminalSize, error) {
width, height, err := terminal.GetSize(int(os.Stdout.Fd()))
if err != nil {
return terminalSize{}, fmt.Errorf("Could not get terminal size %s\n", err)
}
return terminalSize{height, width}, nil
}
func updateTerminalSize(c *websocket.Conn, writeMutex *sync.Mutex, writeWait time.Duration) error {
resizeMessage, err := getTerminalSize()
if err != nil {
return fmt.Errorf("Could not get terminal size: %s\n", err)
}
resizeMessageBinary, err := json.Marshal(&resizeMessage)
if err != nil {
return fmt.Errorf("Could not marshal resizeMessage %s\n", err)
}
writeMutex.Lock()
c.SetWriteDeadline(time.Now().Add(writeWait))
err = c.WriteMessage(websocket.BinaryMessage, append([]byte{1}, resizeMessageBinary...))
writeMutex.Unlock()
if err != nil {
return fmt.Errorf("write: %s", err)
}
return nil
}
func handleTerminalResize(c *websocket.Conn, done *chan bool, writeMutex *sync.Mutex, writeWait time.Duration) {
defer func() { *done <- true }()
oldTerminalSize := terminalSize{}
ticker := time.NewTicker(1000 * time.Millisecond)
for {
<-ticker.C
newTerminalSize, err := getTerminalSize()
if err != nil {
fmt.Println(err)
return
}
if newTerminalSize != oldTerminalSize {
err := updateTerminalSize(c, writeMutex, writeWait)
if err != nil {
fmt.Println(err)
return
}
}
oldTerminalSize = newTerminalSize
}
}