ansistrip

package module
v0.0.6 Latest Latest
Warning

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

Go to latest
Published: Jul 8, 2025 License: Apache-2.0 Imports: 9 Imported by: 0

README

ansistrip

ansistrip is a buffering writer for Go that strips ANSI escape sequences (cursor movement, text styling, erase commands) while maintaining a virtual cursor position and screen buffer.

Output is flushed when:

  • Cursor doesn't move (inactivity timeout)
  • A force flush timeout is reached
  • The writer is closed

Use ansistrip if you want to run a Docker container in TTY mode and stream logs for both TTY and non-TTY modes.

Usage

package main

import (
    "os"
    "time"
    "github.com/dstackai/ansistrip"
)

func main() {
    // Create a new ANSI-aware writer
    writer := ansistrip.NewWriter(
        os.Stdout,                // downstream writer
        500*time.Millisecond,     // inactivity timeout
        3*time.Second,            // force flush timeout
    )
    defer writer.Close()

    // Write data with ANSI sequences
    writer.Write([]byte("\x1b[31mRed text\x1b[0m\n"))
    writer.Write([]byte("\x1b[1;32mBold green text\x1b[0m\n"))
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func SetLogLevel added in v0.0.4

func SetLogLevel(level logrus.Level)

SetLogLevel allows setting the log level for the ansistrip package.

Types

type Writer

type Writer struct {
	// contains filtered or unexported fields
}

Writer is a thread-safe, ANSI-aware, buffering io.Writer.

func NewWriter

func NewWriter(downstream io.Writer, inactivityTimeout, forceFlushTimeout time.Duration, maxBufferSize int) *Writer

NewWriter creates and starts a new ANSI-aware buffering Writer. maxBufferSize sets the maximum size of the internal buffer in bytes. If the buffer exceeds this size, a flush is triggered. It also sets the maximum size of any single write to the downstream writer. A value of 0 or less disables this feature.

func (*Writer) Close

func (w *Writer) Close() error

Close gracefully shuts down the writer.

func (*Writer) Write

func (w *Writer) Write(p []byte) (n int, err error)

Write sends data to the manager for processing.

Jump to

Keyboard shortcuts

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