Documentation
¶
Overview ¶
Package textedit provides a non-regexp, streaming editor to apply basic text manipulation.
Index ¶
- type Editor
- func Func[S any](edit func(r rune, state S) ([]rune, S), releaseState func(S) []rune) Editor
- func FuncInit[S any](edit func(r rune, state S) ([]rune, S), releaseState func(S) []rune, init S) Editor
- func Indent(first, rest string) Editor
- func Replace(a ...string) Editor
- func SingleLine() Editor
- func Wrap(firstWidth, restWidth int) Editor
- func WrapIndent(firstIndent, restIndent string, firstWidth, restWidth int) Editor
- type Writer
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 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 Replace ¶
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 Wrap ¶
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 ¶
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 ¶
New initializes an editing writer. The editor instances will be called in the order they are passed in to New.
func (*Writer) Flush ¶
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 ¶
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.