gilo/internal/terminal/terminal.go
2021-03-24 15:09:28 -04:00

101 lines
1.7 KiB
Go

package terminal
import (
"bufio"
"fmt"
"os"
"golang.org/x/term"
)
var reader = bufio.NewReader(os.Stdin)
func ReadKey() (rune, int) {
ch, size, err := reader.ReadRune()
if err != nil {
panic(err)
}
return ch, size
}
// Is this a valid interactive terminal?
func check() {
if !term.IsTerminal(int(os.Stdin.Fd())) {
panic("An interactive terminal is required to use a text editor!")
}
}
// Put the terminal in raw mode
func RawOn() *term.State {
check()
oldState, err := term.MakeRaw(int(os.Stdin.Fd()))
if err != nil {
panic(err)
}
return oldState
}
// Restore the terminal to canonical mode
func RawOff(oldState *term.State) {
err := term.Restore(int(os.Stdin.Fd()), oldState)
if err != nil {
panic(err)
}
}
func sizeTrick () (rows int, cols int) {
// Move cursor to location further than likely screen size
// The cursor will move to maximum available position
fmt.Print("\x1b[999C\x1b[99B")
// Ask the terminal where the cursor is
fmt.Print("\x1b[6n")
// Read stdin looking for the reported location
buffer := ""
for char, _ := ReadKey(); char != 'R'; char, _ = ReadKey() {
if char == '\x1b' || char == '[' {
continue
}
if char == 'R' || char == '\x00'{
break
}
buffer += string(char)
}
_, err := fmt.Sscanf(buffer, "%d;%d", &rows, &cols)
if err != nil {
panic(err)
}
return rows, cols
}
// Get the size of the terminal in rows and columns
func Size () (rows int, cols int) {
cols = 80
rows = 24
// Try the syscall first
cols, rows, err := term.GetSize(int(os.Stdin.Fd()))
if err == nil {
return rows, cols
}
// Figure out the size the hard way
rows, cols = sizeTrick()
return rows, cols
}
// Print string to stdout
func Write(s string) {
fmt.Print(s)
}