elvish: github.com/elves/elvish/pkg/util Index | Files

package util

import "github.com/elves/elvish/pkg/util"

Package util contains utility functions.


Package Files

camel_to_dashed.go ceildiv.go claim.go deepprint.go feed.go gethome.go getwd.go limits.go log.go multierror.go pprinter.go search.go strings.go subseq.go temp_env.go test_utils.go testdir.go util.go wcwidth.go


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.


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.

func ApplyDir Uses

func ApplyDir(dir Dir)

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

func CamelToDashed Uses

func CamelToDashed(camel string) string

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

func CeilDiv Uses

func CeilDiv(a, b int) int

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

func ClaimFile Uses

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 Uses

func DeepPrint(x interface{}) string

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

func DontSearch Uses

func DontSearch(exe string) bool

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

func Errors Uses

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 Uses

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

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

func FindContext Uses

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 Uses

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 Uses

func FindLastSOL(s string) int

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

func ForceWcwidth Uses

func ForceWcwidth(s string, width int) string

ForceWcwidth forces the string s to the given display width by trimming and padding.

func GetHome Uses

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 Uses

func GetLogger(prefix string) *log.Logger

GetLogger gets a logger with a prefix.

func Getwd Uses

func Getwd() string

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

func HasSubseq Uses

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 Uses

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 Uses

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 Uses

func IsExecutable(path string) bool

IsExecutable determines whether path refers to an executable file.

func MatchSubseq Uses

func MatchSubseq(s, pattern string) bool

MatchSubseq returns whether pattern is a subsequence of s.

func NthRune Uses

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

NthRune returns the n-th rune of s.

func OverrideWcwidth Uses

func OverrideWcwidth(r rune, w int)

OverrideWcwidth overrides the wcwidth of a rune to be a specific non-negative value. OverrideWcwidth panics if w < 0.

func SetOutput Uses

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 Uses

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 Uses

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 Uses

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 Uses

func TildeAbbr(path string) string

TildeAbbr abbreviates the user's home directory to ~.

func TrimEachLineWcwidth Uses

func TrimEachLineWcwidth(s string, width int) string

TrimEachLineWcwidth trims each line of s so that it is no wider than the specified width.

func TrimWcwidth Uses

func TrimWcwidth(s string, wmax int) string

TrimWcwidth trims the string s so that it has a width of at most wmax.

func UnoverrideWcwidth Uses

func UnoverrideWcwidth(r rune)

UnoverrideWcwidth removes the override of a rune.

func Wcswidth Uses

func Wcswidth(s string) (w int)

Wcswidth returns the width of a string when displayed on the terminal, assuming no soft line breaks.

func Wcwidth Uses

func Wcwidth(r rune) int

Wcwidth returns the width of a rune when displayed on the terminal.

func WithTempEnv Uses

func WithTempEnv(name, value string) func()

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

type Dir Uses

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 Uses

type File struct {
    Perm    os.FileMode
    Content string

File describes a file to create.

type MultiError Uses

type MultiError struct {
    Errors []error

MultiError pack multiple errors into one error.

func (MultiError) Error Uses

func (es MultiError) Error() string

type PPrinter Uses

type PPrinter interface {
    // PPrint takes an indentation string and pretty-prints.
    PPrint(indent string) string

PPrinter wraps the PPrint function.

Package util imports 15 packages (graph) and is imported by 14 packages. Updated 2020-02-22. Refresh now. Tools for package owners.