-
Notifications
You must be signed in to change notification settings - Fork 3
/
terminal.go
55 lines (49 loc) · 1.26 KB
/
terminal.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
// +build !windows
package main
import (
"encoding/json"
"fmt"
"os"
"os/signal"
"sync"
"syscall"
"time"
"github.com/gorilla/websocket"
terminal "golang.org/x/term"
)
type terminalSize struct {
Height int `json:"height"`
Width int `json:"width"`
}
func updateTerminalSize(c *websocket.Conn, writeMutex *sync.Mutex, writeWait time.Duration) error {
width, height, err := terminal.GetSize(int(os.Stdin.Fd()))
if err != nil {
return fmt.Errorf("Could not get terminal size %s\n", err)
}
resizeMessage := terminalSize{height, width}
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 }()
sigc := make(chan os.Signal, 1)
signal.Notify(sigc, syscall.SIGWINCH)
for {
<-sigc
err := updateTerminalSize(c, writeMutex, writeWait)
if err != nil {
fmt.Println(err)
return
}
}
}