prelude

package
v0.0.0-...-e0215d9 Latest Latest
Warning

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

Go to latest
Published: Dec 9, 2021 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package prelude provides convenient functionality for Go one-liners.

Most of its source code is embedded directly in the generated source as a Prelude. It is however also used for godoc, and can be used by the golf command.

Index

Constants

This section is empty.

Variables

View Source
var (

	// Filename is the current filename.
	Filename string
	// LineNum is the current line number, 1-based.
	LineNum int
	// Line is the current line. It may be edited by the script.
	// Its contents are automatically printed in -p mode.
	Line string

	// Fields is the Split field slice. See the convenience Field accessor.
	// Updated automatically in -a mode.
	Fields []string

	// IFS is the input field separator used in -a mode. Overridden by -F.
	IFS = " "
	// OFS is the output field separator used by Field(0).
	OFS = " "
	// Warnings controls whether to print warnings. Overridden by -w.
	Warnings = false
	// GolfFlgL controls whether to strip/add newlines on I/O. Overridden by -l.
	GolfFlgL = false

	// GolfInPlace reports whether we are in-place edit mode.
	// It is set when -i or -I were given on the command-line.
	GolfInPlace = false
	// GolfInPlaceBak is the file pattern for in-place edit backups.
	GolfInPlaceBak string

	// CurOut is the default writer for Print and Printf.
	// Overridden to each Filename in -i.
	CurOut io.WriteCloser = os.Stdout
)
View Source
var (
	// Join is an alias for strings.Join.
	Join = strings.Join

	// RE is an alias for regexp.MustCompile.
	RE = regexp.MustCompile
)

Functions

func BackupName

func BackupName(orig, ext string) string

BackupName returns the filename used as a backup in in-place edit mode.

Replacement rules follow Perl -i:

  • if ext contains no '*' characters, it is appended to orig as a suffix.
  • otherwise, each * is replaced with orig.

func Die

func Die(xs ...interface{})

Die prints an error to stderr and exits the program with a failure status.

Arguments follow the semantics of Warn.

func Field

func Field(n int) string

Field retrieves a split field. Index 0 returns the entire line re-joined using the OFS. Positive values are taken to be a 1-based index to Fields. Negative values index from the end (so -1 is the last Fields element). Indexes out of range silently return the empty string.

func GAtoi

func GAtoi(s string) int

GAtoi calls strconv.Atoi on s, and issues an optional warning if that returned an error.

func GSplit

func GSplit(sep, input string) []string

GSplit splits an input string, with some golf affordances. It is a bit more similar to Perl's split than it is to strings.Split.

When sep is a single space, strings.Fields is used. This is the default behavior with -a and no -F override, and operates how awk/perl do by default: The input is trimmed of leading and trailing whitespace, then any amount of (any) whitespace is taken as a field separator.

When sep has the form /pat/, pat is compiled into a regexp and regexp.Split is used.

Otherwise, sep is taken as a literal for strings.Split.

func Print

func Print(xs ...interface{})

Print prints a string to CurOut.

With no arguments, the string to be printed defaults to Line.

In -l mode, a newline is appended to the string.

In -i mode, the "current output" is the replacement for the current Filename. Otherwise, it is os.Stdout.

func Printf

func Printf(format string, xs ...interface{})

Printf prints a string to CurOut.

In -i mode, the "current output" is the replacement for the current Filename. Otherwise, it is os.Stdout.

Newline is not added automatically, even in -l mode, nor is Line used as a default string.

func Source

func Source() []byte

Source returns the source code of the prelude.

func Warn

func Warn(xs ...interface{})

Warn prints an error to stderr.

If no arguments are supplied, a generic message is printed. If multiple arguments are provided and the first one is a string, it is taken as a format string. Otherwise everything is printed using default fmt formatting.

Types

This section is empty.

Jump to

Keyboard shortcuts

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