textedit

package module
v0.0.0-...-5a3e22b Latest Latest
Warning

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

Go to latest
Published: Dec 9, 2025 License: MIT Imports: 2 Imported by: 0

README

textedit

Textedit provides a non-regexp, streaming editor to apply basic text manipulation.

Docs:

Made in Berlin, DE

Documentation

Overview

Package textedit provides a non-regexp, streaming editor to apply basic text manipulation.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Editor

type Editor interface {

	// Edit takes an input rune and the current state of the editor, and returns zero or more resulting runes
	// and the updated state.
	Edit(r rune, state any) ([]rune, any)

	// On flushing the editing writer, the writer will call ReleaseState on each provided editor in the
	// configured sequence with their latest associated state, and if there is any pending text to be written
	// out, the editors should return it at that point.
	ReleaseState(state any) []rune
}

Editor instances can be used to edit a text stream. It is expected from the implementations to be reusable with fresh state after the Flush was called on the enclosing writer.

func Func

func Func[S any](edit func(r rune, state S) ([]rune, S), releaseState func(S) []rune) Editor

Func can be used to define an Editor providing only the edit and releaseState functions.

func FuncInit

func FuncInit[S any](edit func(r rune, state S) ([]rune, S), releaseState func(S) []rune, init S) Editor

FuncInit can be used to define an Editor providing only the edit and releaseState functions, and the initial state.

func Indent

func Indent(first, rest string) Editor

Indent applies indentation to multi-line text. Whitespaces are preserved.

func Replace

func Replace(a ...string) Editor

Replace is a built-in editor for simple replace operations. The arguments are mapped such that the ones at odd positions will be the sequences to match in the input, and the immediately following ones will be the sequences replacing the matched sequences. Empty replacement sequences can be used to deleted the matched ones. In case of odd number of arguments, an empty arg is automatically appended.

func SingleLine

func SingleLine() Editor

SingleLine is like Wrap, but with infinite text width.

func Wrap

func Wrap(firstWidth, restWidth int) Editor

Wrap wrap wraps multiline text to define maximual text width. Duplicate, leading, trailing and non-space whitespaces are collapsed into a single space.

func WrapIndent

func WrapIndent(firstIndent, restIndent string, firstWidth, restWidth int) Editor

WrapIndent is like Wrap, but applies indentation.

type Writer

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

Writer implements the io.Writer interface, passing the input to every editor in the configured sequence, and writing out the edited text to the underlying io.Writer.

func New

func New(out io.Writer, e ...Editor) *Writer

New initializes an editing writer. The editor instances will be called in the order they are passed in to New.

func (*Writer) Flush

func (w *Writer) Flush() error

Flush makes the underlying editors to release their associated state, and writes out the resulting text to the underlying io.Writer, but first passes it to the subsequent editors for editing. Is there were no errors, and the used editor instances comply with the expectations of the Editor interface, the writer will have a fresh state and can be reused for further editing. The Flush method of underlying writers is not called.

func (*Writer) Write

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

Write calls the configured editors with the input and forwards their output to the underlying io.Writer. It always returns len(p) as the number bytes written. It only returns an error when the underlying writer returns and error.

func (*Writer) WriteRune

func (w *Writer) WriteRune(r rune) (int, error)

WriteRune calls the configured editors with the input and forwards their output to the underlying io.Writer. It always returns byte length of the input rune as the number bytes written. It only returns an error when the underlying writer returns and error.

Jump to

Keyboard shortcuts

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