edi

package module
v1.1.1 Latest Latest
Warning

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

Go to latest
Published: Apr 21, 2024 License: Apache-2.0 Imports: 9 Imported by: 0

README

Edi

Tool to edit files.

Documentation online

Author

Jonás Melián (https://bitbucket.org/ares)

License

The source files are distributed under the Apache License, version 2.0.

Documentation

Overview

Package edi allows edit files.

There are a great number of functions related. It avoids to have to use an external command to get the same result, with the advantage of that it can create automatically a backup before of editing.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Append

func Append(filename string, b []byte) error

Append writes len(b) bytes at the end of the named file. It returns an error, if any.

func AppendString

func AppendString(filename string, s string) error

AppendString is like Append, but writes the contents of string s rather than an array of bytes.

func Backup

func Backup(filename string) error

Backup creates a backup of the named file.

The schema used for the new name is: {name}\+[1-9]~

name: The original file name.
+ : Character used to separate the file name from rest.
number: A number from 1 to 9, using rotation.
~ : To indicate that it is a backup, just like it is used in Unix systems.

func Comment

func Comment(filename string, reLine string) error

Comment inserts the comment character in lines that mach the regular expression in reLine, at the named file.

func CommentM

func CommentM(filename string, reLine []string) error

CommentM inserts the comment character in lines that mach any regular expression in reLine, at the named file.

func CommentOut

func CommentOut(filename string, reLine string) error

CommentOut removes the comment character of lines that mach the regular expression in reLine, at the named file.

func CommentOutM

func CommentOutM(filename string, reLine []string) error

CommentOutM removes the comment character of lines that mach any regular expression in reLine, at the named file.

func Delete

func Delete(filename string, begin, end int64) error

Delete removes the text given at position 'begin:end'.

func Replace

func Replace(filename string, r []Replacer) error

Replace replaces all regular expressions mathed in r for the named file.

func ReplaceAtLine

func ReplaceAtLine(filename string, r []ReplacerAtLine) error

ReplaceAtLine replaces all regular expressions mathed in r for the named file, if the line is matched at the first.

func ReplaceAtLineN

func ReplaceAtLineN(filename string, r []ReplacerAtLine, n int) error

ReplaceAtLineN replaces a number of regular expressions mathed in r for the named file, if the line is matched at the first.

func ReplaceN

func ReplaceN(filename string, r []Replacer, n int) error

ReplaceN replaces a number of regular expressions mathed in r for the named file.

func SetCommonOpts

func SetCommonOpts(opts *Options)

SetCommonOpts sets the options to use by the package-level functions.

Types

type Editor

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

Editor represents the file to edit.

func NewEditor

func NewEditor(filename string, opts *Options) (*Editor, error)

NewEdit prepares a file to edit. There is to use 'Close()' at finish.

Whether `opts` is null, then uses options by default.

func (*Editor) Append

func (ed *Editor) Append(b []byte) error

Append writes len(b) bytes at the end of the File. It returns an error, if any.

func (*Editor) AppendString

func (ed *Editor) AppendString(s string) error

AppendString is like Append, but writes the contents of string s rather than an array of bytes.

func (*Editor) Close

func (ed *Editor) Close() error

Close closes the file.

func (*Editor) Comment

func (ed *Editor) Comment(reLine []string) error

Comment inserts the comment character in lines that mach any regular expression in reLine.

func (*Editor) CommentOut

func (ed *Editor) CommentOut(reLine []string) error

CommentOut removes the comment character of lines that mach any regular expression in reLine.

func (*Editor) Delete

func (ed *Editor) Delete(begin, end int64) error

Delete removes the text given at position 'begin:end'.

func (*Editor) Replace

func (ed *Editor) Replace(r []Replacer) error

Replace replaces all regular expressions mathed in r.

func (*Editor) ReplaceAtLine

func (ed *Editor) ReplaceAtLine(r []ReplacerAtLine) error

ReplaceAtLine replaces all regular expressions mathed in r, if the line is matched at the first.

func (*Editor) ReplaceAtLineN

func (ed *Editor) ReplaceAtLineN(r []ReplacerAtLine, n int) error

ReplaceAtLineN replaces regular expressions mathed in r, if the line is matched at the first. The count determines the number to match:

n > 0: at most n matches
n == 0: the result is none
n < 0: all matches

func (*Editor) ReplaceN

func (ed *Editor) ReplaceN(r []Replacer, n int) error

ReplaceN replaces regular expressions mathed in r. The count determines the number to match:

n > 0: at most n matches
n == 0: the result is none
n < 0: all matches

type Finder

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

Finder represents the file where find a string.

func NewFinder

func NewFinder(src []byte, comment string, mode ModeFind) (
	*Finder, error,
)

NewFinder prepares the 'Finder' via the `src` parameter.

func NewFinderFromFile added in v1.1.1

func NewFinderFromFile(filename, comment string, mode ModeFind) (
	*Finder, error,
)

NewFinder prepares the 'Finder' via the `filename`.

func (*Finder) Contains

func (fi *Finder) Contains(b []byte) (found bool, err error)

Contains reports whether the file contains `b`.

func (*Finder) HasPrefix

func (fi *Finder) HasPrefix(b []byte) (found bool, err error)

HasPrefix reports whether the file has a line that begins with `b`.

func (*Finder) HasSuffix

func (fi *Finder) HasSuffix(b []byte) (found bool, err error)

HasSuffix reports whether the file has a line that ends with `b`.

func (*Finder) Line added in v1.1.0

func (fi *Finder) Line() string

Line returns the line that was found.

func (*Finder) Pos added in v1.1.0

func (fi *Finder) Pos() (start, end int64)

Pos returns the position where the string was found.

type ModeFind

type ModeFind uint

A ModeFind value is a set of flags (or 0) to control behavior at find into a file.

const (
	ModTrimSpace   ModeFind = 1 << iota // Removes all leading and trailing white spaces.
	ModSkipComment                      // Skip skip lines that start with the comment string.
	ModGetLine                          // Enable to get the line using `(Finder).Line()`.
)

Modes used at find into a file.

type Options

type Options struct {
	// Character for comment.
	Comment []byte

	// Do backup before of edit.
	Backup bool

	Log *log.Logger
}

Options are options to edit a text file.

func Default

func Default() *Options

Default returns the options used by the package-level functions.

type Replacer

type Replacer struct {
	Search, Replace string
}

Replacer represents the text to be replaced.

type ReplacerAtLine

type ReplacerAtLine struct {
	Line, Search, Replace string
}

ReplacerAtLine represents the text to be replaced into a line.

Jump to

Keyboard shortcuts

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