util

package
v0.14.1 Latest Latest
Warning

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

Go to latest
Published: Aug 16, 2020 License: BSD-2-Clause Imports: 14 Imported by: 0

Documentation

Overview

Package util contains utility functions.

Index

Constants

View Source
const (
	MaxUint = ^uint(0)
	MinUint = 0
	MaxInt  = int(MaxUint >> 1)
	MinInt  = -MaxInt - 1
)

Limit values for uint and int.

NOTE: The math package contains similar constants for explicitly sized integer types, but lack those for uint and int.

Variables

View Source
var ErrClaimFileBadPattern = errors.New("ClaimFile: pattern must contain exactly one asterisk")

ErrClaimFileBadPattern is thrown when the pattern argument passed to ClaimFile does not contain exactly one asterisk.

View Source
var ErrIndexOutOfRange = errors.New("substring out of range")

ErrIndexOutOfRange is returned when out-of-range errors occur.

Functions

func ApplyDir

func ApplyDir(dir Dir)

ApplyDir creates the given files specified by ta directory layout to the current directory.

func CamelToDashed

func CamelToDashed(camel string) string

CamelToDashed converts a CamelCaseIdentifier to a dash-separated-identifier, or a camelCaseIdentifier to a -dash-separated-identifier.

func CeilDiv

func CeilDiv(a, b int) int

CeilDiv computes ceil(float(a)/b) without using float arithmetics.

func ClaimFile

func ClaimFile(dir, pattern string) (*os.File, error)

ClaimFile takes a directory and a pattern string containing exactly one asterisk (e.g. "a*.log"). It opens a file in that directory, with a filename matching the template, with "*" replaced by a number. That number is one plus the largest of all existing files matching the template. If no such file exists, "*" is replaced by 1. The file is opened for read and write, with permission 0666 (before umask).

For example, if the directory /tmp/elvish contains a1.log, a2.log and a9.log, calling ClaimFile("/tmp/elvish", "a*.log") will open a10.log. If the directory has no files matching the pattern, this same call will open a1.log.

This function is useful for automatically determining unique names for log files. Unique filenames can also be derived by embedding the PID, but using this function preserves the chronical order of the files.

This function is concurrency-safe: it always opens a new, unclaimed file and is not subject to race condition.

func DeepPrint

func DeepPrint(x interface{}) string

DeepPrint is like printing with the %#v formatter of fmt, but it prints pointer fields recursively.

func DontSearch

func DontSearch(exe string) bool

DontSearch determines whether the path to an external command should be taken literally and not searched.

func Errors

func Errors(errs ...error) error

Errors concatenate multiple errors into one. If all errors are nil, it returns nil. If there is one non-nil error, it is returned. Otherwise the return value is a MultiError containing all the non-nil arguments. Arguments of the type MultiError are flattened.

func Feed

func Feed(f func(interface{}) bool, values ...interface{})

Feed calls the function with given values, breaking earlier if the function returns false.

func FindContext

func FindContext(text string, pos int) (lineno, colno int, line string)

FindContext takes a position in a text and finds its line number, corresponding line and column numbers. Line and column numbers are counted from 0. Used in diagnostic messages.

func FindFirstEOL

func FindFirstEOL(s string) int

FindFirstEOL returns the index of the first '\n'. When there is no '\n', the length of s is returned.

func FindLastSOL

func FindLastSOL(s string) int

FindLastSOL returns an index just after the last '\n'.

func GetHome

func GetHome(uname string) (string, error)

GetHome finds the home directory of a specified user. When given an empty string, it finds the home directory of the current user.

func GetLogger

func GetLogger(prefix string) *log.Logger

GetLogger gets a logger with a prefix.

func Getwd

func Getwd() string

Getwd returns path of the working directory in a format suitable as the prompt.

func HasSubseq

func HasSubseq(s, t string) bool

HasSubseq determines whether s has t as its subsequence. A string t is a subsequence of a string s if and only if there is a possible sequence of steps of deleting characters from s that result in t.

func InTestDir

func InTestDir() (string, func())

InTestDir is like TestDir, but also changes into the test directory, and the cleanup function also changes back to the original working directory.

It panics if it could not get the working directory or change directory. It is only suitable for use in tests.

func InTestDirWithSetup

func InTestDirWithSetup(dir Dir) func()

InTestDirWithSetup sets up a temporary directory using the given cli. If wd is not empty, it also changes into the given subdirectory. It returns a cleanup function to remove the temporary directory and restore the working directory.

It panics if there are any errors.

func IsExecutable

func IsExecutable(path string) bool

IsExecutable determines whether path refers to an executable file.

func MatchSubseq

func MatchSubseq(s, pattern string) bool

MatchSubseq returns whether pattern is a subsequence of s.

func NthRune

func NthRune(s string, n int) (rune, error)

NthRune returns the n-th rune of s.

func SetOutput

func SetOutput(newout io.Writer)

SetOutput redirects the output of all loggers obtained with GetLogger to the new io.Writer. If the old output was a file opened by SetOutputFile, it is closed.

func SetOutputFile

func SetOutputFile(fname string) error

SetOutputFile redirects the output of all loggers obtained with GetLogger to the named file. If the old output was a file opened by SetOutputFile, it is closed. The new file is truncated. SetOutFile("") is equivalent to SetOutput(ioutil.Discard).

func SubstringByRune

func SubstringByRune(s string, low, high int) (string, error)

SubstringByRune returns the range of the i-th rune (inclusive) through the j-th rune (exclusive) in s.

func TestDir

func TestDir() (string, func())

TestDir creates a temporary directory for testing. It returns the path of the temporary directory and a cleanup function to remove the temporary directory. The path has symlinks resolved with filepath.EvalSymlinks.

It panics if the test directory cannot be created or symlinks cannot be resolved. It is only suitable for use in tests.

func TildeAbbr

func TildeAbbr(path string) string

TildeAbbr abbreviates the user's home directory to ~.

func WithTempEnv

func WithTempEnv(name, value string) func()

WithTempEnv sets an environment variable to a temporary value, and returns a function for restoring the old value.

Types

type Dir

type Dir map[string]interface{}

Dir describes the layout of a directory. The keys of the map represent filenames. Each value is either a string (for the content of a regular file with permission 0644), a File, or a Dir.

type File

type File struct {
	Perm    os.FileMode
	Content string
}

File describes a file to create.

type MultiError

type MultiError struct {
	Errors []error
}

MultiError pack multiple errors into one error.

func (MultiError) Error

func (es MultiError) Error() string

Jump to

Keyboard shortcuts

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