miscutils

package module
v0.0.0-...-459b3fe Latest Latest
Warning

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

Go to latest
Published: Apr 23, 2024 License: MIT Imports: 11 Imported by: 3

README

Miscellaneous utilities for Golang. Nothing major. No rocket science.

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func DupeByteSlice

func DupeByteSlice(in []byte) (out []byte)

DupeByteSlice allocates and returns a duplicate of the input byte slice.

func GetKeypress

func GetKeypress() byte

GetKeypress reads and returns (as a `byte`) a keypress.

func InteractiveInput

func InteractiveInput(prompt string) string

InteractiveInput displays the argument string as a prompt and then reads and returns a string entered by the user (ended by `Enter`).

func Into

func Into(s string) time.Time

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

func IsBitSet(flagset, index byte) bool

IsBitSet returns the numbered bit. Bit 0 is rightmost. It may or may not work for more than 16 bits in flagset. .

func IsNil

func IsNil(x interface{}) bool

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 Must

func Must[T any](val T, err error) T

func Outa

func Outa(s string, t time.Time)

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

func RunCommand(cmdname string, args ...string) (stdout string, stderr string, err error)

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

func SanitizeInput(input string) string

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

func Tri

func Tri[T any](cond bool, a, b T) T

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

func (*Errer) Error

func (p *Errer) Error() string

Error is an NPE-proof improvement on the standard error.Error() .

func (*Errer) GetError

func (p *Errer) GetError() error

GetError is a convenience func because getting Error.Err is ugly. .

func (*Errer) HasError

func (p *Errer) HasError() bool

HasError is a convenience function. Since Err is publicly visible, HasError is not really needed, but it seems appropriate given that we also have func Error() .

func (*Errer) SetError

func (p *Errer) SetError(e error)

SerError is a convenience func because setting Error.Err is ugly. .

Jump to

Keyboard shortcuts

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