ansiwalker

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: May 7, 2025 License: MIT Imports: 1 Imported by: 2

README

ansiwalker logo

ansiwalker is an extremely lightweight Go package for iterating through strings or byte slices while skipping all ANSI/VT100 escape sequences, ensuring that only printable runes are processed.

Features

  • ANSI‑aware iteration: skips CSI, OSC, DCS, SOS, PM, APC, and C1 control codes.
  • UTF‑8 safe: decodes and returns full runes for proper Unicode support.
  • Simple API: single function to advance to the next visible character.
  • Lightweight and dependency‑free.

🚀 Getting Started

go get github.com/galactixx/ansiwalker@latest

📚 Usage

import (
    "fmt"
    "github.com/galactixx/ansiwalker"
)

func main() {
    input := "\x1b[31mError:\x1b[0m Something went wrong"
    buf := strings.Builder{}
    i := 0
    for i < len(input) {
        r, size, next, ok := ansiwalker.ANSIWalk(input, i)
        if ok {
          buf.WriteRune(r)
        }
        i = next
    }
    fmt.Println(buf.String())
}

Output:

Error: Something went wrong

🔍 API

ANSIWalk(s string, i int) (r rune, size int, next int, ok bool) Skips any ANSI escape sequence starting at index i and returns:

  • r: the next visible rune (or 0 at EOF).
  • size: the byte length of r in UTF‑8.
  • next: the index to resume parsing from.
  • ok: false if end of string is reached.

🤝 License

This project is licensed under the MIT License. See the LICENSE file for details.

📞 Contact

For questions or support, please open an issue on the GitHub repository.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ANSIWalk

func ANSIWalk(s string, i int) (r rune, size int, next int, ok bool)

ANSIWalk scans the input string s from index i, skipping any ANSI/VT100 escape sequences, and returns the next visible rune, its byte size, the index to resume from, and a boolean indicating if the end of the string has been reached.

Types

This section is empty.

Jump to

Keyboard shortcuts

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