Skip to content

go-leap/std

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

30 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ustd

-- import "github.com/go-leap/std"

Usage

var Flags struct {
	// Flags that were registered via any of the `FlagOf*` funcs.
	Known []Flag
	// OnErr is called when parsing of `value` for flag `name` failed.
	OnErr func(name string, value string, err error)
	// AddShortNames adds support for name abbreviations, ie. for `--long-named-flag` we also check for `--lnf`.
	AddShortNames bool
}

func DoNowAndThenEvery

func DoNowAndThenEvery(interval time.Duration, should func() bool, do func()) (start func(), stop func())

DoNowAndThenEvery invokes do immediately, then if interval is given, returns a start func that invokes do every interval time.Duration together with a stop func to time.Ticker.Stop doing so.

func FlagOfBool

func FlagOfBool(name string, defaultVal bool, desc string) (val bool)

FlagOfBool obtains val from the command-line argument flag named name or the defaultVal.

func FlagOfDuration

func FlagOfDuration(name string, defaultVal time.Duration, desc string) (val time.Duration)

FlagOfDuration obtains val from the command-line argument flag named name or the defaultVal.

func FlagOfOther

func FlagOfOther(name string, defaultVal interface{}, desc string, fromString func(string) (interface{}, error), toString func(interface{}) string) (val interface{})

FlagOfOther obtains val from the command-line argument flag named name or the defaultVal.

func FlagOfString

func FlagOfString(name string, defaultVal string, desc string) (val string)

FlagOfString obtains val from the command-line argument flag named name or the defaultVal.

func FlagOfStrings

func FlagOfStrings(name string, defaultVal []string, sep string, desc string) (val []string)

FlagOfStrings obtains val from the command-line argument flag named name (items joined by sep) or the defaultVal.

func FlagOfUint

func FlagOfUint(name string, defaultVal uint64, desc string) (val uint64)

FlagOfUint obtains val from the command-line argument flag named name or the defaultVal.

func For

func For(numIter int, on func(int))

For is a sync.WaitGroup-based parallel for loop equivalent.

func HashTwo

func HashTwo(oldSum1, oldSum2 uint64, data []byte) (newSum1, newSum2 uint64, bothUnchanged bool)

HashTwo computes at least newSum1 and (if equal to non-0 oldSum1) also newSum2. The first uses wyhash, the second xxhash.

func IfNil

func IfNil(val interface{}, thenFallbackTo interface{}) interface{}

IfNil returns val if not nil, else thenFallbackTo.

func JsonDecodeFromFile

func JsonDecodeFromFile(fromFilePath string, intoDestination interface{}) (err error)

JsonDecodeFromFile opens the specified file and attempts to JSON-decode into the specified destination location.

func JsonEncodeToFile

func JsonEncodeToFile(from interface{}, toFilePath string) (err error)

JsonEncodeToFile creates the specified file and attempts to JSON-encode into it from the specified source.

func ReadAll

func ReadAll(r io.Reader, initialBufSize int64) (data []byte, err error)

ReadAll is a somewhat leaner alternative to ioutil.ReadAll, for io.Readers known to be of limited (not potentially-infinite or RAM-exceeding) size.

func Time

func Time() func() time.Duration

Time records a start time and returns a func to compute the time.Duration since then.

func WriteLines

func WriteLines(w io.Writer) func(...string)

WriteLines returns a func that writes the specified lines to w.

type BytesReader

type BytesReader struct {
	Data []byte
}

BytesReader implements io.Reader over a slice of bytes in a leaner manner than the reader-and-writer bytes.Buffer.

func (*BytesReader) Read

func (me *BytesReader) Read(p []byte) (n int, err error)

Read implements io.Reader.

type BytesWriter

type BytesWriter struct{ Data []byte }

BytesWriter implements io.Writer over a slice of bytes in a leaner manner than the reader-and-writer bytes.Buffer.

func (*BytesWriter) Bytes

func (me *BytesWriter) Bytes() []byte

Bytes returns me.Data and aids in compatibility with bytes.Buffer.

func (*BytesWriter) Reset

func (me *BytesWriter) Reset()

Reset sets the len of me.Data to 0.

func (*BytesWriter) String

func (me *BytesWriter) String() string

func (*BytesWriter) TrimSuffix

func (me *BytesWriter) TrimSuffix(suffix byte)

TrimSuffix ensures that me.Data is not suffixed by the specified suffix byte.

func (*BytesWriter) Write

func (me *BytesWriter) Write(b []byte) (int, error)

Write implements io.Writer and writes the specified bytes to me.Data, always returning len(b), nil.

func (*BytesWriter) WriteByte

func (me *BytesWriter) WriteByte(b byte)

WriteByte writes a single byte to me.Data.

func (*BytesWriter) WriteString

func (me *BytesWriter) WriteString(b string)

WriteString writes the specified string to me.Data.

func (*BytesWriter) WriteTo

func (me *BytesWriter) WriteTo(w io.Writer) (int64, error)

WriteTo implements the io.WriterTo interface.

type Flag

type Flag struct {
	// Name, not including any leading dashes
	Name string
	// Desc, the help text
	Desc string
	// Default, the fall-back value if none was specified by the user
	Default string
	// AllowForAccidentalSpaceAtTheExpenseOfEmptyStrVals allows accidental arg placements such as `--foo= bar` (instead of either `--foo bar` or `--foo=bar`) but hence drops support for "" (empty string) values
	AllowForAccidentalSpaceAtTheExpenseOfEmptyStrVals bool
}

Flag represents a named command-line args flag, in the fashion of either --flag-name value or --flag-name=value, as well as both --fn value and --fn=value.

func (*Flag) ShortName

func (me *Flag) ShortName() (shortForm string)

ShortName returns an abbreviation of Name, ie. long-flag-name will return lfn.

type Writer

type Writer struct {
	io.Writer
	On struct {
		AfterEveryNth                int
		Byte                         byte
		Do                           func(int) bool
		ButDontCountImmediateRepeats bool
	}
}

Writer wraps any given io.Writer with additional byte-based processing on Writes.

func (*Writer) RestartOnDo

func (me *Writer) RestartOnDo()

RestartOnDo resets the counter for On.Do calls during Writes.

func (*Writer) SuspendOnDo

func (me *Writer) SuspendOnDo()

SuspendOnDo causes On.Do not to be called during Writes until RestartOnDo.

func (*Writer) Write

func (me *Writer) Write(p []byte) (n int, err error)

Write calls me.Writer.Write and if me.On.AfterEveryNth and me.On.Do are set, traverses p to conditionally call me.On.Do with an internal counter.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages