2021-03-19 16:36:02 -04:00
|
|
|
package terminal
|
|
|
|
|
|
|
|
import (
|
2021-03-24 13:24:40 -04:00
|
|
|
"bufio"
|
2021-03-19 16:36:02 -04:00
|
|
|
"fmt"
|
|
|
|
"os"
|
2021-03-19 17:08:19 -04:00
|
|
|
|
|
|
|
"golang.org/x/term"
|
2021-03-19 16:36:02 -04:00
|
|
|
)
|
|
|
|
|
2021-03-19 17:39:15 -04:00
|
|
|
const ClearScreen = "2J"
|
|
|
|
const ResetCursor = "H"
|
|
|
|
|
2021-03-24 13:24:40 -04:00
|
|
|
var reader = bufio.NewReader(os.Stdin)
|
|
|
|
|
|
|
|
func ReadKey() (rune, int) {
|
|
|
|
ch, size, err := reader.ReadRune()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return ch, size
|
|
|
|
}
|
|
|
|
|
2021-03-19 17:08:19 -04:00
|
|
|
// 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!")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-19 16:36:02 -04:00
|
|
|
// Put the terminal in raw mode
|
|
|
|
func RawOn() *term.State {
|
2021-03-19 17:08:19 -04:00
|
|
|
check()
|
|
|
|
|
2021-03-19 16:36:02 -04:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-19 17:39:15 -04:00
|
|
|
func ANSICode (code string) {
|
|
|
|
fmt.Printf("\x1b[%s", code)
|
|
|
|
}
|
|
|
|
|
2021-03-24 13:24:40 -04:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-03-23 15:51:59 -04:00
|
|
|
func Size () (width int, height int) {
|
2021-03-24 13:24:40 -04:00
|
|
|
width = 80
|
|
|
|
height = 24
|
|
|
|
|
|
|
|
// Try the syscall first
|
2021-03-23 15:51:59 -04:00
|
|
|
width, height, err := term.GetSize(int(os.Stdin.Fd()))
|
2021-03-24 13:24:40 -04:00
|
|
|
if err == nil {
|
|
|
|
return width, height
|
2021-03-23 15:51:59 -04:00
|
|
|
}
|
|
|
|
|
2021-03-24 13:24:40 -04:00
|
|
|
// Figure out the size the hard way
|
|
|
|
height, width = sizeTrick()
|
|
|
|
|
|
|
|
OutLn("%d Rows, %d Cols", height, width)
|
|
|
|
|
2021-03-23 15:51:59 -04:00
|
|
|
return width, height
|
|
|
|
}
|
|
|
|
|
2021-03-19 16:36:02 -04:00
|
|
|
// Print a formatted string to stdout, with CRLF line endings for proper terminal formatting
|
|
|
|
func OutLn(format string, a ...interface{}) {
|
|
|
|
formatted := fmt.Sprintf(format, a...)
|
|
|
|
|
|
|
|
fmt.Printf("%s\r\n", formatted)
|
2021-03-19 17:08:19 -04:00
|
|
|
}
|