Documentation ¶
Overview ¶
Package miscutils does miscellanithings; it imports no other packages beyond the standard library.
This package does some OS-type stuff and maybe parts of it do not work well on Windoze. Feel free to file a bug report, but please propose a fix too cos I have no way to test on Windoze. .
Index ¶
- func DupeByteSlice(in []byte) (out []byte)
- func GetKeypress() byte
- func InteractiveInput(prompt string) string
- func Into(s string) time.Time
- func IsBitSet(flagset, index byte) bool
- func IsNil(x interface{}) bool
- func It() *singleton
- func Must[T any](val T, err error) T
- func Outa(s string, t time.Time)
- func PressAnyKey()
- func RTEnv() string
- func RunCommand(cmdname string, args ...string) (stdout string, stderr string, err error)
- func SanitizeInput(input string) string
- func Tri[T any](cond bool, a, b T) T
- type Errer
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func DupeByteSlice ¶
DupeByteSlice allocates and returns a duplicate of the input byte slice.
func InteractiveInput ¶
InteractiveInput displays the argument string as a prompt and then reads and returns a string entered by the user (ended by `Enter`).
func Into ¶
Into starts a timer and (if the string argument is non-empty) writes to `os.Stderr`; the timer is ended by passing it to `Outa(..)`, which can be placed in a `defer` statement. Note that the caller must store the start time, but that this means we can have nested timers.
These functions add a dependency on package `time`, but it is also possible to rewrite them to use `interface{}`.
func IsBitSet ¶
IsBitSet returns the numbered bit. Bit 0 is rightmost. It may or may not work for more than 16 bits in flagset. .
func It ¶
func It() *singleton
It returns the single instance. Come to think of it tho, this looks like lazy instantiation; maybe it belongs in a `func init()` ?
func Outa ¶
Outa stops a clock that was started by `Into(..)`, and also writes the elapsed time (in a human-friendly format) to `os.Stderr`. The message is less informative if the string starts with `!` (which is not printed).
func PressAnyKey ¶
func PressAnyKey()
PressAnyKey waits until any key is pressed. The key is not echoed back to the user and is not returned to the caller.
func RTEnv ¶
func RTEnv() string
RTEnv tries to identify the runtime environment. It uses lots of stuff from package os:
- Probably useful:
- Environ() []string
- Getenv(key string) string // LookupEnv distinguishes btwn empty and unset
- Getwd() (dir string, err error)
- UserHomeDir() (string, error)
- UserConfigDir() (string, error)
- Hostname() (name string, err error)
- Probably not useful:
- Executable() (string, error)
- UserCacheDir() (string, error)
- Getegid() int
- Geteuid() int
- Getgid() int
- Getuid() int
func RunCommand ¶
RunCommand runs `cmdname` with the supplied 0-or-more arguments. It waits for the command to finish, and then it returns the command's `Stdout` and `Stderr`, plus any error that prevented execution.
Pass the command and its arguments as a list of individual tokens, rather than as a single string with blanks as separators. .
func SanitizeInput ¶
SanitizeInput applies the following rules iteratively until no further processing can be done: - trim all extra white space - trim all carriage return characters - trim leading / ending quotation marks (ex.: "my text") - trim leading / ending spaces
Types ¶
type Errer ¶
type Errer struct {
Err error
}
Errer is a struct that can be used to embed an error in another struct, when we want to execute (pointer) methods on a struct in the style of a data pipeline, i.e. chainable, and executed left-to-right.
We make the error public so that it is easily set, and so that we can wrap errors easily using the "%w" printf format spec.
Methods are on *Errer, not Errer, so that modification is possible. .
func (*Errer) ClearError ¶
func (p *Errer) ClearError()