asciinema-editor/viewer.go

71 lines
1.1 KiB
Go

package main
import (
"log"
"net"
"os"
"os/exec"
"strings"
"golang.org/x/sys/unix"
)
var commandListener net.Listener
func resetScreen() {
cmd := exec.Command("tput", "reset")
cmd.Stdout = os.Stdout
cmd.Run()
}
func disableEcho() {
fd := int(os.Stdin.Fd())
termios, err := unix.IoctlGetTermios(fd, unix.TCGETS)
if err != nil {
log.Fatal(err)
}
termios.Lflag &^= unix.ECHO
if err := unix.IoctlSetTermios(fd, unix.TCSETS, termios); err != nil {
log.Fatal(err)
}
}
func hostViewer(address string) error {
var initialized bool
var err error
if strings.HasPrefix(address, "/") {
commandListener, err = net.Listen("unix", address)
} else {
commandListener, err = net.Listen("tcp", address)
}
if err != nil {
return err
}
for {
commandConn, err = commandListener.Accept()
if err != nil {
return err
}
if !initialized {
go handleViewer()
initialized = true
}
}
}
func handleViewer() {
go handleRead()
go handleWrite()
for c := range commandsIn {
castCommand <- c
}
}
func runViewer(controlAddress string) {
err := hostViewer(controlAddress)
if err != nil {
log.Fatalf("failed to host viwer: %s", err)
}
}