Documentation ¶
Overview ¶
Package util contains utility functions.
Index ¶
- Constants
- Variables
- func CamelToDashed(camel string) string
- func Catch(perr *error)
- func CeilDiv(a, b int) int
- func ClaimFile(dir, pattern string) (*os.File, error)
- func DeepPrint(x interface{}) string
- func DoesntThrow(f func()) bool
- func DontSearch(exe string) bool
- func Errors(errs ...error) error
- func FindContext(text string, pos int) (lineno, colno int, line string)
- func FindFirstEOL(s string) int
- func FindLastSOL(s string) int
- func ForceWcwidth(s string, width int) string
- func FullNames(dir string) []string
- func GetHome(uname string) (string, error)
- func GetLogger(prefix string) *log.Logger
- func Getwd() string
- func HasSubseq(s, t string) bool
- func InTempDir(f func(string))
- func IsExecutable(path string) bool
- func MatchSubseq(s, pattern string) bool
- func NthRune(s string, n int) (rune, error)
- func OverrideWcwidth(r rune, w int)
- func PCall(f func()) (e error)
- func PprintError(err error)
- func SetOutput(newout io.Writer)
- func SetOutputFile(fname string) error
- func SubstringByRune(s string, low, high int) (string, error)
- func Throw(err error)
- func Throws(f func(), e error) bool
- func ThrowsAny(f func()) bool
- func TildeAbbr(path string) string
- func TrimEachLineWcwidth(s string, width int) string
- func TrimWcwidth(s string, wmax int) string
- func UnoverrideWcwidth(r rune)
- func Wcswidth(s string) (w int)
- func Wcwidth(r rune) int
- func WithTempDir(f func(string))
- func WithTempDirs(n int, f func([]string))
- type MultiError
- type Pprinter
- type SourceRange
- type Thrown
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 ( CulpritLineBegin = "\033[1;4m" CulpritLineEnd = "\033[m" CulpritPlaceHolder = "^" )
Variables controlling the style of the culprit.
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 CamelToDashed ¶
CamelToDashed converts a CamelCaseIdentifier to a dash-separated-identifier, or a camelCaseIdentifier to a -dash-separated-identifier.
func Catch ¶
func Catch(perr *error)
Catch tries to catch an error thrown by Throw and stop the panic. If the panic is not caused by Throw, the panic is not stopped. It should be called directly from defer.
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 DoesntThrow ¶
func DoesntThrow(f func()) bool
DoesntThrow returns whether calling f does not throw anything. It is useful for testing.
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 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 ForceWcwidth ¶
ForceWcwidth forces the string s to the given display width by trimming and padding.
func FullNames ¶
FullNames returns the full names of non-hidden files under a directory. The directory name should end in a slash. If the directory cannot be listed, it returns nil.
The output should be the same as globbing dir + "*". It is used for testing globbing.
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 InTempDir ¶
func InTempDir(f func(string))
InTempDir is like WithTempDir, but also cd into the directory before running the function, and cd backs after running the function if possible.
It panics if it could not get the working directory or change directory.
It is useful in tests.
func IsExecutable ¶
IsExecutable determines whether path refers to an executable file.
func MatchSubseq ¶
MatchSubseq returns whether pattern is a subsequence of s.
func OverrideWcwidth ¶
OverrideWcwidth overrides the wcwidth of a rune to be a specific non-negative value. OverrideWcwidth panics if w < 0.
func PCall ¶
func PCall(f func()) (e error)
PCall calls a function and catches anything Thrown'n and returns it. It does not protect against panics not using Throw, nor can it distinguish between nothing thrown and Throw(nil).
func PprintError ¶
func PprintError(err error)
PprintError pretty-prints an error if it implements Pprinter, and prints it in bold and red otherwise.
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 Throw ¶
func Throw(err error)
Throw panics with err wrapped properly so that it can be catched by Catch.
func Throws ¶
Throws returns whether calling f throws out a certain error (using Throw). It is useful for testing.
func ThrowsAny ¶
func ThrowsAny(f func()) bool
ThrowsAny returns whether calling f throws out anything that is not nil. It is useful for testing.
func TrimEachLineWcwidth ¶
TrimEachLineWcwidth trims each line of s so that it is no wider than the specified width.
func TrimWcwidth ¶
TrimWcwidth trims the string s so that it has a width of at most wmax.
func UnoverrideWcwidth ¶
func UnoverrideWcwidth(r rune)
UnoverrideWcwidth removes the override of a rune.
func Wcswidth ¶
Wcswidth returns the width of a string when displayed on the terminal, assuming no soft line breaks.
func WithTempDir ¶
func WithTempDir(f func(string))
WithTempDir is like with WithTempDirs, except that it always creates one temporary directory and pass the function a string instead of []string.
func WithTempDirs ¶
WithTempDirs creates a requested number of temporary directories and runs a function, passing the paths of the temporary directories; the passed paths all have their symlinks resolved using filepath.EvalSymlinks. After the function returns, it removes the temporary directories. It panics if it cannot make a temporary directory, and prints an error message to stderr if it cannot remove the temporary directories.
It is useful in tests.
Types ¶
type MultiError ¶
type MultiError struct {
Errors []error
}
MultiError pack multiple errors into one error.
func (MultiError) Error ¶
func (es MultiError) Error() string
type Pprinter ¶
type Pprinter interface { // Pprint takes an indentation string and pretty-prints. Pprint(indent string) string }
Pprinter wraps the Pprint function.
type SourceRange ¶
type SourceRange struct { Name string Source string Begin int End int // contains filtered or unexported fields }
SourceRange is a range of text in a source code. It can point to another SourceRange, thus forming a linked list. It is used for tracebacks.
func NewSourceRange ¶
func NewSourceRange(name, source string, begin, end int) *SourceRange
NewSourceRange creates a new SourceRange.
func (*SourceRange) Pprint ¶
func (sr *SourceRange) Pprint(sourceIndent string) string
Pprint pretty-prints a SourceContext.
func (*SourceRange) PprintCompact ¶
func (sr *SourceRange) PprintCompact(sourceIndent string) string
PprintCompact pretty-prints a SourceContext, with no line break between the source position range description and relevant source excerpt.