Set window title by control message

This commit is contained in:
Iwasaki Yudai 2015-08-23 20:40:18 +09:00
parent 67b54b7f20
commit a765d6c660
7 changed files with 106 additions and 55 deletions

View file

@ -6,6 +6,7 @@ import (
"net/http"
"os"
"os/exec"
"strings"
"syscall"
"unsafe"
@ -26,11 +27,23 @@ const (
ResizeTerminal = '1'
)
const (
Output = '0'
SetWindowTitle = '1'
)
type argResizeTerminal struct {
Columns float64
Rows float64
}
type ContextVars struct {
Command string
Pid int
Hostname string
RemoteAddr string
}
func (context *clientContext) goHandleClient() {
exit := make(chan bool, 2)
@ -56,6 +69,11 @@ func (context *clientContext) goHandleClient() {
}
func (context *clientContext) processSend() {
if err := context.sendInitialize(); err != nil {
log.Printf(err.Error())
return
}
buf := make([]byte, 1024)
utf8f := utf8reader.New(context.pty)
@ -71,11 +89,34 @@ func (context *clientContext) processSend() {
return
}
writer.Write([]byte{Output})
writer.Write(buf[:size])
writer.Close()
}
}
func (context *clientContext) sendInitialize() error {
hostname, _ := os.Hostname()
titleVars := ContextVars{
Command: strings.Join(context.app.options.Command, " "),
Pid: context.command.Process.Pid,
Hostname: hostname,
RemoteAddr: context.request.RemoteAddr,
}
writer, err := context.connection.NextWriter(websocket.TextMessage)
if err != nil {
return err
}
writer.Write([]byte{SetWindowTitle})
if err = context.app.titleTemplate.Execute(writer, titleVars); err != nil {
return err
}
writer.Close()
return nil
}
func (context *clientContext) processReceive() {
for {
_, data, err := context.connection.ReadMessage()