go-cursorposition
Request()
Query and display the cursor position with ESC[6n
package main
import (
"os"
"golang.org/x/term"
"github.com/hymkor/go-windows1x-virtualterminal"
"github.com/hymkor/go-cursorposition"
)
func main() {
// On Windows, enable ANSI ESCAPE SEQUENCE.
// On other OSes, do nothing.
if closer, err := virtualterminal.EnableStderr(); err != nil {
panic(err.Error())
} else {
defer closer()
}
// Switch terminal to raw-mode.
if oldState, err := term.MakeRaw(int(os.Stdin.Fd())); err != nil {
panic(err.Error())
} else {
defer term.Restore(int(os.Stdin.Fd()), oldState)
}
// Query and display the cursor position with ESC[6n
row, col, err := cursorposition.Request(os.Stderr)
if err != nil {
println(err.Error())
} else {
println(row, col)
}
}
AmbiguousWidth()
Measure how far the cursor moves while the ▽ is printed
package main
import (
"os"
"golang.org/x/term"
"github.com/hymkor/go-windows1x-virtualterminal"
"github.com/hymkor/go-cursorposition"
)
func main() {
// On Windows, enable ANSI ESCAPE SEQUENCE.
// On other OSes, do nothing.
if closer, err := virtualterminal.EnableStderr(); err != nil {
panic(err.Error())
} else {
defer closer()
}
// Switch terminal to raw-mode.
if oldState, err := term.MakeRaw(int(os.Stdin.Fd())); err != nil {
panic(err.Error())
} else {
defer term.Restore(int(os.Stdin.Fd()), oldState)
}
// Measure how far the cursor moves while the `▽` is printed
w, err := cursorposition.AmbiguousWidth(os.Stderr)
if err != nil {
println(err.Error())
} else {
println(w)
}
}
AmbiguousWidthGoTty
AmbiguousWidthGoTty works same as AmbiguousWidth, but it switches the terminal raw-mode with the instance of "github.com/mattn/go-tty".TTY
package main
import (
"os"
"github.com/mattn/go-tty"
"github.com/hymkor/go-windows1x-virtualterminal"
"github.com/hymkor/go-cursorposition"
)
func main() {
// On Windows, enable ANSI ESCAPE SEQUENCE.
// On other OSes, do nothing.
if closer, err := virtualterminal.EnableStderr(); err != nil {
panic(err.Error())
} else {
defer closer()
}
tty1, err := tty.Open()
if err != nil {
panic(err.Error())
}
tty1.Close()
// Measure how far the cursor moves while the `▽` is printed
w, err := cursorposition.AmbiguousWidthGoTty(tty1, os.Stderr)
if err != nil {
println(err.Error())
} else {
println(w)
}
}
Referneces