mgutil

package
v19.10.22+incompatible Latest Latest
Warning

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

Go to latest
Published: Oct 22, 2019 License: MIT, MIT Imports: 11 Imported by: 0

Documentation

Overview

Package mgutil is a collections of utility types and functions with no dependency on margo.sh/mg

Index

Constants

This section is empty.

Variables

View Source
var (
	PrimaryDigits   = DigitDisplay{'🄌', '➊', '➋', '➌', '➍', '➎', '➏', '➐', '➑', '➒'}
	SecondaryDigits = DigitDisplay{'🄋', '➀', '➁', '➂', '➃', '➄', '➅', '➆', '➇', '➈'}
)
View Source
var (
	// ShortFnEnv is the list of envvars that are used in ShortFn.
	ShortFnEnv = []string{
		"GOPATH",
		"GOROOT",
	}
)

Functions

func Clamp

func Clamp(lo, hi int, n int) int

Clamp limits n to the interval [ lo, hi ]

func ClampPos

func ClampPos(s []byte, pos int) int

ClampPos limits pos to the interval [ 0, len(s) ]

func FilePathParent

func FilePathParent(fn string) string

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

func IsParentDir(parentDir, childPath string) bool

IsParentDir returns true if parentDir is a parent of childPath

func Max

func Max(p int, q ...int) int

Max returns the largest of p or q.

func Min

func Min(p int, q ...int) int

Min returns the smallest of p or q.

func MinNumCPU

func MinNumCPU(q ...int) int

MinNumCPU calls Min(runtime.NumCPU(), q...).

func PathList

func PathList(s string) []string

PathList splits s by filepath.ListSeparator and returns the list with empty and duplicate components removed

func PathParent

func PathParent(fn string) string

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

func QuoteCmd(name string, args ...string) string

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

func QuoteCmdArg(s string) string

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

func RepositionLeft(src []byte, pos int, cond func(rune) bool) int

RepositionLeft moves pos left-wards through src until cond returns false

func RepositionRight

func RepositionRight(src []byte, pos int, cond func(rune) bool) int

RepositionRight moves pos right-wards through src until cond returns false

func ShortFilename

func ShortFilename(fn string) string

ShortFilename calls ShortFn(fn, nil)

func ShortFn

func ShortFn(fn string, env EnvMap) string

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

func NewChanQ(cap int) *ChanQ

NewChanQ creates a new ChanQ if cap is less than 1, it panics

func (*ChanQ) C

func (q *ChanQ) C() <-chan interface{}

C returns a channel on which values are sent

func (*ChanQ) Close

func (q *ChanQ) Close()

Close closes the queue and the channel returned by C(). closing a closed queue has no effect.

func (*ChanQ) Put

func (q *ChanQ) Put(v interface{})

Put puts v into the queue. It removes the oldest value if no space is available.

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

type EnvMap map[string]string

EnvMap is a map of environment variables

func (EnvMap) Add

func (e EnvMap) Add(k, v string) EnvMap

Add is an alias of Set

func (EnvMap) Environ

func (e EnvMap) Environ() []string

Environ returns a copy of os.Environ merged with the values in the map

func (EnvMap) Get

func (e EnvMap) Get(k, def string) string

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

func (e EnvMap) Getenv(k, def string) string

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) List

func (e EnvMap) List(k string) []string

List is an alias of EventMap.PathList

func (EnvMap) Merge

func (e EnvMap) Merge(p map[string]string) EnvMap

Merge merges p into the map and returns a the new map

func (EnvMap) PathList

func (e EnvMap) PathList(k string) []string

PathList is the equivalent of PathList(e[k])

func (EnvMap) Set

func (e EnvMap) Set(k, v string) EnvMap

Set sets the key k in the map to the value v and a the new map

func (EnvMap) Unset

func (e EnvMap) Unset(keys ...string) EnvMap

Unset removes the list of keys from the map and returns 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

func (iow *IOWrapper) Close() error

Close calls Closer.Close() if Closer is not nil otherwise it returns `nil`

func (*IOWrapper) Flush

func (iow *IOWrapper) Flush() error

Flush calls Flushr.Flush() if Flusher is not nil otherwise it returns `nil`

func (*IOWrapper) Read

func (iow *IOWrapper) Read(p []byte) (int, error)

Read calls Reader.Read() if Reader is not nil otherwise it returns `0, io.EOF`

func (*IOWrapper) Write

func (iow *IOWrapper) Write(p []byte) (int, error)

Write calls Writer.Write() if Writer is not nil otherwise it returns `len(p), nil`

type Memo

type Memo struct {
	// contains filtered or unexported fields
}

func (*Memo) Clear

func (m *Memo) Clear()

func (*Memo) Del

func (m *Memo) Del(k interface{})

func (*Memo) Read

func (m *Memo) Read(k interface{}, new func() interface{}) interface{}

func (*Memo) Values

func (m *Memo) Values() map[interface{}]interface{}

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

func (*NoCopy) Lock

func (*NoCopy) Lock()

Lock is a no-op used by the `go vet -copylocks` checker.

type ReaderFunc

type ReaderFunc func([]byte) (int, error)

ReaderFunc implements io.Reader using a function.

func (ReaderFunc) Read

func (f ReaderFunc) Read(p []byte) (int, error)

Read calls f(p)

type RuneWriter

type RuneWriter interface {
	WriteRune(rune) (int, error)
}

type StrSet

type StrSet []string

StrSet holds a set of strings

func NewStrSet

func NewStrSet(l ...string) StrSet

NewStrSet returns a new StrSet initialised with the strings in l

func (StrSet) Add

func (s StrSet) Add(l ...string) StrSet

Add add the list of strings l to the set and returns the new set

func (StrSet) Has

func (s StrSet) Has(p string) bool

Has returns true if p is in the set

type WriterFunc

type WriterFunc func([]byte) (int, error)

WriterFunc implements io.Writer using a function.

func (WriterFunc) Write

func (f WriterFunc) Write(p []byte) (int, error)

Write calls f(p)

Jump to

Keyboard shortcuts

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