Handling tty size

This commit is contained in:
Manfred Touron 2015-08-25 13:30:04 +02:00
parent d3a30fd41c
commit 298a302026
No known key found for this signature in database
GPG Key ID: 0DCB9CE0CABAE1B5
1 changed files with 37 additions and 0 deletions

View File

@ -2,12 +2,16 @@ package gottyclient
import ( import (
"bufio" "bufio"
"encoding/json"
"fmt" "fmt"
"net/http" "net/http"
"net/url" "net/url"
"os" "os"
"os/signal"
"strings" "strings"
"syscall"
"unicode/utf8" "unicode/utf8"
"unsafe"
"github.com/Sirupsen/logrus" "github.com/Sirupsen/logrus"
"github.com/gorilla/websocket" "github.com/gorilla/websocket"
@ -67,10 +71,43 @@ func (c *Client) Loop() error {
done := make(chan bool) done := make(chan bool)
go c.readLoop(done) go c.readLoop(done)
go c.writeLoop(done) go c.writeLoop(done)
go c.termsizeLoop(done)
<-done <-done
return nil return nil
} }
type winsize struct {
Rows uint16 `json:"rows"`
Columns uint16 `json:"columns"`
// unused
x uint16
y uint16
}
func (c *Client) termsizeLoop(done chan bool) {
ch := make(chan os.Signal, 1)
signal.Notify(ch, syscall.SIGWINCH)
ws := winsize{}
for {
syscall.Syscall(syscall.SYS_IOCTL,
uintptr(0), uintptr(syscall.TIOCGWINSZ),
uintptr(unsafe.Pointer(&ws)))
b, err := json.Marshal(ws)
if err != nil {
logrus.Warnf("json.Marshal error: %v", err)
}
err = c.Conn.WriteMessage(websocket.TextMessage, append([]byte("1"), b...))
if err != nil {
logrus.Warnf("ws.WriteMessage failed: %v", err)
}
<-ch
}
}
func (c *Client) writeLoop(done chan bool) { func (c *Client) writeLoop(done chan bool) {
oldState, err := terminal.MakeRaw(0) oldState, err := terminal.MakeRaw(0)
if err == nil { if err == nil {