cursorposition

package module
v0.0.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 9, 2025 License: MIT Imports: 4 Imported by: 2

README

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

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrAnsiEscapeSequenceNotSupported = errors.New("ANSI Escape sequence not supported")

Functions

func AmbiguousWidth

func AmbiguousWidth(w io.Writer) (int, error)

AmbiguousWidth returns the width of Ambiguous Unicode characters. Before it called, the terminal has to be raw-mode.

func AmbiguousWidthGoTty

func AmbiguousWidthGoTty(tty interface{ Raw() (func() error, error) }, w io.Writer) (int, error)

AmbiguousWidthGoTty returns the width of Ambiguous Unicode characters. It makes the terminal raw-mode with `"github.com/mattn/go-tty".TTY`

func Request

func Request(w io.Writer) (int, int, error)

Request sends the terminal `ESC[6n` and recieves the position of the cursor from the terminal. Before it called, the terminal has to be raw-mode.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL