https://github.com/hymkor/go-cursorposition
Query and display the cursor position with ESC[6n
https://github.com/hymkor/go-cursorposition
go terminal
Last synced: 23 days ago
JSON representation
Query and display the cursor position with ESC[6n
- Host: GitHub
- URL: https://github.com/hymkor/go-cursorposition
- Owner: hymkor
- Created: 2024-02-04T01:56:41.000Z (about 1 year ago)
- Default Branch: master
- Last Pushed: 2024-02-28T12:57:56.000Z (about 1 year ago)
- Last Synced: 2025-02-10T15:50:54.965Z (3 months ago)
- Topics: go, terminal
- Language: Go
- Homepage:
- Size: 12.7 KB
- Stars: 2
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
go-cursorposition
=================Request()
---------Query and display the cursor position with ESC[6n
```example1.go
package mainimport (
"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
```example2.go
package mainimport (
"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][go-tty]".TTY
[go-tty]: https://github.com/mattn/go-tty
```example3.go
package mainimport (
"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
----------
+ [端末の文字幅問題の傾向と対策 | IIJ Engineers Blog](https://eng-blog.iij.ad.jp/archives/12576)
+ [ANSI Escape Codes](https://gist.github.com/fnky/458719343aabd01cfb17a3a4f7296797)