mgutil

package
v0.0.0-...-52b1b20 Latest Latest
Warning

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

Go to latest
Published: Jul 21, 2020 License: MIT Imports: 16 Imported by: 118

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 ErrSplitWriterClosed = errors.New("SplitWriter: closed")
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)-1 ]

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.

func SplitLine

func SplitLine(s []byte) (line, rest []byte, ok bool)

func SplitLineOrCR

func SplitLineOrCR(s []byte) (line, rest []byte, ok bool)

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 AtomicInt

type AtomicInt int64

func (*AtomicInt) Add

func (i *AtomicInt) Add(n int64) int64

func (*AtomicInt) Dec

func (i *AtomicInt) Dec() int64

func (*AtomicInt) Inc

func (i *AtomicInt) Inc() int64

func (*AtomicInt) N

func (i *AtomicInt) N() int64

func (*AtomicInt) Set

func (i *AtomicInt) Set(n int64)

func (*AtomicInt) Sub

func (i *AtomicInt) Sub(n int64) int64

func (*AtomicInt) Swap

func (i *AtomicInt) Swap(old, new int64)

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 NewChanQLoop

func NewChanQLoop(cap int, f func(v interface{})) *ChanQ

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

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 = memo.M

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 OutputStream

type OutputStream interface {
	io.Writer
	io.Closer
	Flush() error
}

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 SplitFunc

type SplitFunc func(buf []byte) (next, rest []byte, ok bool)

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)

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