textedit

package
v0.3.0 Latest Latest
Warning

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

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

Documentation

Overview

Package textedit provides helpers for making changes to line-oriented text files. This is especially targeted at "shell rc" files like `~/.bashrc` or `~/.zshrc`.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Edit

func Edit(
	in io.Reader,
	out io.Writer,
	editor Editor,
) error

func EditFile

func EditFile(
	fileName string,
	editor Editor,
) error

Types

type Editor

type Editor interface {
	// Next is called after each input line is read. If err is non-nil, the edit
	// operation will fail. Otherwise any lines in output will be emitted. This
	// must include the input line if it should be copied to the output.
	Next(line string) (output iter.Seq[string], err error)
	// EOF is called after all input lines have been processed through Next. Its
	// return will be processed the same way as Next.
	EOF() (output iter.Seq[string], err error)
}

func AppendLine

func AppendLine(line string, oldLines ...string) Editor

AppendLine adds the given line to the end of the file if it is not already present. It will also insert it in place of any of the given oldLines (optional).

It will panic if the line or any of the oldLines are empty after trimming leading & trailing whitespace.

func SpliceRange

func SpliceRange(lines ...string) Editor

Splice a range of lines marked by the start and end lines. Panics if len(lines) < 3, i.e. must have start & end markers and at least one "content" line, or if the start or end markers are empty after trimming leading & trailing whitespace.

Matching the start and end markers is done after trimming leading & trailing whitespace from both the sought & observed lines.

Jump to

Keyboard shortcuts

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