Created
November 15, 2020 10:38
-
-
Save fd0/ee6d351c9444632ea1133fd4acdc7d69 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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