miscutils

package module
v0.0.0-...-6a2fa9a Latest Latest
Warning

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

Go to latest
Published: Jan 26, 2025 License: MIT Imports: 12 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. As with many things in Go, there is more than one way to skin a cat.

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

IsNil checks whether an interface value is nil, and to do this correctly, it checks BOTH values.

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

Must enforces error-free func calls: a panic is triggered by a non-nil error returned by any func of the form: MyFunc(..) (someValue, error)

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 issues a message to the user and then 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: - 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

Tri implements the ternary operator.

func Uuidv7

func Uuidv7() ([16]byte, error)

Uuidv7 is a cut & paste from somewhere or 'nother.

Types

This section is empty.

Jump to

Keyboard shortcuts

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