Documentation ¶
Overview ¶
Package mgutil is a collections of utility types and functions with no dependency on margo.sh/mg
Index ¶
- Variables
- func Clamp(lo, hi int, n int) int
- func ClampPos(s []byte, pos int) int
- func FilePathParent(fn string) string
- func IsParentDir(parentDir, childPath string) bool
- func Max(p int, q ...int) int
- func Min(p int, q ...int) int
- func MinNumCPU(q ...int) int
- func PathList(s string) []string
- func PathParent(fn string) string
- func QuoteCmd(name string, args ...string) string
- func QuoteCmdArg(s string) string
- func RepositionLeft(src []byte, pos int, cond func(rune) bool) int
- func RepositionRight(src []byte, pos int, cond func(rune) bool) int
- func ShortFilename(fn string) string
- func ShortFn(fn string, env EnvMap) string
- func SplitLine(s []byte) (line, rest []byte, ok bool)
- func SplitLineOrCR(s []byte) (line, rest []byte, ok bool)
- type AtomicBool
- type AtomicInt
- type ChanQ
- type DigitDisplay
- type EnvMap
- func (e EnvMap) Add(k, v string) EnvMap
- func (e EnvMap) Environ() []string
- func (e EnvMap) Get(k, def string) string
- func (e EnvMap) Getenv(k, def string) string
- func (e EnvMap) List(k string) []string
- func (e EnvMap) Merge(p map[string]string) EnvMap
- func (e EnvMap) PathList(k string) []string
- func (e EnvMap) Set(k, v string) EnvMap
- func (e EnvMap) Unset(keys ...string) EnvMap
- type IOWrapper
- type Memo
- type NoCopy
- type OutputStream
- type ReaderFunc
- type RuneWriter
- type SplitFunc
- type SplitWriter
- type StrSet
- type WriterFunc
Constants ¶
This section is empty.
Variables ¶
var ( PrimaryDigits = DigitDisplay{'🄌', '➊', '➋', '➌', '➍', '➎', '➏', '➐', '➑', '➒'} SecondaryDigits = DigitDisplay{'🄋', '➀', '➁', '➂', '➃', '➄', '➅', '➆', '➇', '➈'} )
var ( // ShortFnEnv is the list of envvars that are used in ShortFn. ShortFnEnv = []string{ "GOPATH", "GOROOT", } )
Functions ¶
func ClampPos ¶
ClampPos limits pos to the interval [ 0, len(s)-1 ]
func FilePathParent ¶
FilePathParent returns the parent(filepath.Dir) of fn if it has a parent. If fn has no parent, an empty string is returned instead of ".", "/" or fn itself.
func IsParentDir ¶
IsParentDir returns true if parentDir is a parent of childPath
func PathList ¶
PathList splits s by filepath.ListSeparator and returns the list with empty and duplicate components removed
func PathParent ¶
PathParent returns the parent(path.Dir) of fn if it has a parent. If fn has no parent, an empty string is returned instead of "." or fn itself.
func QuoteCmd ¶
QuoteCmd joins `name [args]` with name and each arg quoted with QuoteCmdArg NOTE: the result is for display only, and should not be used for shell security.
func QuoteCmdArg ¶
QuoteCmdArg uses strconv.Quote to quote the command arg s. NOTE: the result is for display only, and should not be used for shell security. e.g. `a b c` -> `"a b c"` `abc` -> `abc` `-abc=123` -> `-abc=123` `-abc=1 2 3` -> `-abc="1 2 3"`
func RepositionLeft ¶
RepositionLeft moves pos left-wards through src until cond returns false
func RepositionRight ¶
RepositionRight moves pos right-wards through src until cond returns false
func ShortFn ¶
ShortFn returns a shortened form of filename fn for display in UIs.
If env is set, it's used to override os.Getenv.
The following prefix/ replacements are made (in the listed order): * Envvar names listed in ShortFnEnv are replaced with `$name/`. * `HOME` or `USERPROFILE` envvars are replaced with `~/`.
All other (non-prefix) path components except the last 2 are replaced with their first letter and preceding dots.
This mimics the similar path display feature in shells like Fish.
e.g. Given a long path like `/home/user/.config/sublime-text-3/Packages/User/GoSublime/pkg/mod/github.com/DisposaBoy/pkg@v1.23/go.mod`:
- Given `$GOPATH=/home/user/.config/sublime-text-3/Packages/User/GoSublime`, `$GOPATH/p/m/g/D/pkg@v1.2.3/go.mod`
- Otherwise, `~/.c/s/P/U/G/p/m/g/D/pkg@v1.2.3/go.mod` is returned.
Types ¶
type AtomicBool ¶
type AtomicBool struct {
// contains filtered or unexported fields
}
func (*AtomicBool) IsSet ¶
func (a *AtomicBool) IsSet() bool
func (*AtomicBool) Set ¶
func (a *AtomicBool) Set(v bool)
type ChanQ ¶
type ChanQ struct {
// contains filtered or unexported fields
}
ChanQ is a bounded queue
func NewChanQ ¶
NewChanQ creates a new ChanQ if cap is less than 1, it panics
func NewChanQLoop ¶
NewChanQLoop creates a new ChanQ and launches a gorotuine to handle objects received on the channel. If cap is less than 1, it panics If f panics, the panic is recovered and a stack traced is printed to os.Stderr.
func (*ChanQ) C ¶
func (q *ChanQ) C() <-chan interface{}
C returns a channel on which values are sent
type DigitDisplay ¶
type DigitDisplay []rune
func (DigitDisplay) Draw ¶
func (p DigitDisplay) Draw(n int, f func(rune))
func (DigitDisplay) DrawInto ¶
func (p DigitDisplay) DrawInto(n int, w RuneWriter)
type EnvMap ¶
EnvMap is a map of environment variables
func (EnvMap) Environ ¶
Environ returns a copy of os.Environ merged with the values in the map
func (EnvMap) Get ¶
Get returns the value for k if it exists in the map. If it doesn't exists or is an empty string, def is returned.
func (EnvMap) Getenv ¶
Getenv returns the value for k if it exists in the map or via os.Getenv. If it doesn't exists or is an empty string, def is returned.
func (EnvMap) Merge ¶
Merge merges p into the map and returns a the new map
func (EnvMap) PathList ¶
PathList is the equivalent of PathList(e[k])
func (EnvMap) Set ¶
Set sets the key k in the map to the value v and a the new map
type IOWrapper ¶
type IOWrapper struct { // If Locker is not nil, all methods are called while holding the lock Locker sync.Locker // If Reader is not nil, it will be called to handle reads Reader io.Reader // If Writer is not nil, it will be called to handle writes Writer io.Writer // If Closer is not nil, it will be called to handle closes Closer io.Closer // If Flusher is not nil, it will be called to handle flushes Flusher interface{ Flush() error } }
IOWrapper implements various optional io interfaces. It delegates to the interface fields that are not nil
func (*IOWrapper) Close ¶
Close calls Closer.Close() if Closer is not nil otherwise it returns `nil`
func (*IOWrapper) Flush ¶
Flush calls Flushr.Flush() if Flusher is not nil otherwise it returns `nil`
func (*IOWrapper) Read ¶
Read calls Reader.Read() if Reader is not nil otherwise it returns `0, io.EOF`
type NoCopy ¶
type NoCopy struct{}
NoCopy is used to prevent struct copying through `go vet`s -copylocks checker. It's an export of the type `sync.noCopy` See https://golang.org/issues/8005#issuecomment-190753527
To prevent struct copying, add a field `noCopy NoCopy` to the struct
type ReaderFunc ¶
ReaderFunc implements io.Reader using a function.
type SplitWriter ¶
type SplitWriter struct {
// contains filtered or unexported fields
}
func NewSplitStream ¶
func NewSplitStream(split SplitFunc, w OutputStream) *SplitWriter
func NewSplitWriter ¶
func NewSplitWriter(split SplitFunc, w io.WriteCloser) *SplitWriter
func (*SplitWriter) Close ¶
func (w *SplitWriter) Close() error
func (*SplitWriter) Flush ¶
func (w *SplitWriter) Flush() error
func (*SplitWriter) Write ¶
func (w *SplitWriter) Write(p []byte) (int, error)