Documentation ¶
Overview ¶
Package util contains utility functions.
Index ¶
- Constants
- Variables
- func ApplyDir(dir Dir)
- func CamelToDashed(camel string) string
- func CeilDiv(a, b int) int
- func ClaimFile(dir, pattern string) (*os.File, error)
- func DeepPrint(x interface{}) string
- func DontSearch(exe string) bool
- func Errors(errs ...error) error
- func Feed(f func(interface{}) bool, values ...interface{})
- func FindContext(text string, pos int) (lineno, colno int, line string)
- func FindFirstEOL(s string) int
- func FindLastSOL(s string) int
- func GetHome(uname string) (string, error)
- func GetLogger(prefix string) *log.Logger
- func Getwd() string
- func HasSubseq(s, t string) bool
- func InTestDir() (string, func())
- func InTestDirWithSetup(dir Dir) func()
- func IsExecutable(path string) bool
- func MatchSubseq(s, pattern string) bool
- func NthRune(s string, n int) (rune, error)
- func SetOutput(newout io.Writer)
- func SetOutputFile(fname string) error
- func SubstringByRune(s string, low, high int) (string, error)
- func TestDir() (string, func())
- func TildeAbbr(path string) string
- func WithTempEnv(name, value string) func()
- type Dir
- type File
- type MultiError
Constants ¶
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 ¶
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.
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 ¶
CamelToDashed converts a CamelCaseIdentifier to a dash-separated-identifier, or a camelCaseIdentifier to a -dash-separated-identifier.
func ClaimFile ¶
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 ¶
DontSearch determines whether the path to an external command should be taken literally and not searched.
func Errors ¶
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 ¶
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 ¶
FindFirstEOL returns the index of the first '\n'. When there is no '\n', the length of s is returned.
func FindLastSOL ¶
FindLastSOL returns an index just after the last '\n'.
func GetHome ¶
GetHome finds the home directory of a specified user. When given an empty string, it finds the home directory of the current user.
func Getwd ¶
func Getwd() string
Getwd returns path of the working directory in a format suitable as the prompt.
func HasSubseq ¶
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 ¶
IsExecutable determines whether path refers to an executable file.
func MatchSubseq ¶
MatchSubseq returns whether pattern is a subsequence of s.
func SetOutput ¶
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 ¶
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 ¶
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 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 MultiError ¶
type MultiError struct {
Errors []error
}
MultiError pack multiple errors into one error.
func (MultiError) Error ¶
func (es MultiError) Error() string