Skip to content

Instantly share code, notes, and snippets.

@fd0
Created November 15, 2020 10:38
Show Gist options
  • Save fd0/ee6d351c9444632ea1133fd4acdc7d69 to your computer and use it in GitHub Desktop.
Save fd0/ee6d351c9444632ea1133fd4acdc7d69 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"os"
"time"
"golang.org/x/sys/unix"
)
const (
tcgetattr = unix.TCGETS
tcsetattr = unix.TCSETS
)
func read(escape, cmd string) string {
t, err := unix.IoctlGetTermios(unix.Stdout, tcgetattr)
if err != nil {
panic(err)
}
defer unix.IoctlSetTermios(unix.Stdout, tcsetattr, t)
noecho := *t
noecho.Lflag = noecho.Lflag &^ unix.ECHO
noecho.Lflag = noecho.Lflag &^ unix.ICANON
if err := unix.IoctlSetTermios(unix.Stdout, tcsetattr, &noecho); err != nil {
panic(err)
}
start := time.Now()
fmt.Printf(escape, cmd)
buf := make([]byte, 100)
n, err := os.Stdin.Read(buf)
if err != nil {
panic(err)
}
buf = buf[:n]
fmt.Printf("read after %.6fs: %q\n", time.Since(start).Seconds(), buf)
return string(buf)
}
const (
cmdCursorPosition = "\033[6n"
cmdBackgroundColor = "\033]11;?\a"
escapeRaw = "%s"
escapeScreen = "\033P\033%s\033\\"
escapeTmux = "\033Ptmux;\033%s\033\\"
)
func main() {
read(escapeRaw, cmdCursorPosition)
read(escapeScreen, cmdCursorPosition)
read(escapeTmux, cmdCursorPosition)
read(escapeRaw, cmdBackgroundColor)
read(escapeScreen, cmdBackgroundColor)
read(escapeTmux, cmdBackgroundColor)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment