textedit

package
v0.16.0 Latest Latest
Warning

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

Go to latest
Published: Sep 9, 2026 License: Apache-2.0 Imports: 14 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,
) (bool, error)

func EditFileAsRoot added in v0.7.0

func EditFileAsRoot(
	ctx context.Context,
	fileName string,
	editor Editor,
) (bool, error)

func EditFileUnsafe added in v0.15.1

func EditFileUnsafe(
	fileName string,
	editor Editor,
) (bool, error)

EditFileUnsafe is like EditFile, except that it writes directly to the output file instead of to a temp file and renaming.

It does still compute the new output and avoid rewrites if no changes are necessary.

Only use this for scenarios where permissions prevent doing the temp file dance.

func ReplaceLine added in v0.7.0

func ReplaceLine(oldLine, newLine string) *replaceLineEditor

ReplaceLine makes an editor that will replace oldLine with newLine, with optional prior lines that must immediately precede old/new in order.

This editor is stateful and can only be used once.

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