utils

package
v0.0.0-...-2a19be3 Latest Latest
Warning

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

Go to latest
Published: Aug 25, 2025 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package utils includes miscellaneous utilities to reduce boilerplate code.

Index

Constants

View Source
const UnmarshalFileExtensions = ".toml,.json,.json5"

Comma-separated list of lowercased file extensions that can be Marshaled or Unmarshaled in this package.

Variables

View Source
var DefaultTerminalWidth = 80

Functions

func Dur

func Dur(t time.Time) string

Dur returns the string representation of the duration since t.

func DurLog

func DurLog(t time.Time) []any

DurLog returns a tuple that can be used as an attribute in slog.

NB: this is only useful if you're logging *just* the duration and no other attributes. More complex logging wrappers are TODO; for now we shall stick with having durations be their own log entries.

func GetTerminalWidth

func GetTerminalWidth() int

GetTerminalWidth attempts to obtain the current terminal width for formatting output, trying Stdout first and falling back to /dev/tty, finally defaulting to DefaultTerminalWidth.

func IsExecutable

func IsExecutable(path string) (bool, error)

IsExecutable checks whether a file is (i.e., appears to be) executable.

func JsonFile

func JsonFile(v any, file string) error

JsonFile marshals v to JSON and writes the data to file.

func JsonFilePretty

func JsonFilePretty(v any, file string) error

JsonFilePretty is as JsonFile but with indent.

func MustJson

func MustJson(v any) []byte

MustJson marshals v to a byte array or panics trying.

func MustJsonFile

func MustJsonFile(v any, file string)

MustJsonFile calls JsonFile and panics on error.

func MustJsonFilePretty

func MustJsonFilePretty(v any, file string)

MustJsonFilePretty calls JsonFilePretty and panics on error.

func MustJsonPretty

func MustJsonPretty(v any) []byte

MustJsonPretty marshals v to a byte array with indent or panics trying.

func MustJsonString

func MustJsonString(v any) string

MustJsonString marshals v to a string or panics trying.

func MustJsonStringPretty

func MustJsonStringPretty(v any) string

MustJsonStringPretty marshals v to a string with indent or panics trying.

func MustToml

func MustToml(v any) []byte

MustToml marshals v to a byte array or panics trying.

func MustTomlFile

func MustTomlFile(v any, file string)

MustTomlFile calls TomlFile and panics on error.

func MustTomlString

func MustTomlString(v any) string

MustTomlString marshals v to a string or panics trying.

func MustUnJson

func MustUnJson(b []byte, v any)

MustUnJson unmarshals b to v or panics trying.

func MustUnJsonString

func MustUnJsonString(s string, v any)

MustUnJsonString unmarshals s to v or panics trying.

func MustUnToml

func MustUnToml(b []byte, v any)

MustUnToml unmarshals b to v or panics trying.

func MustUnTomlString

func MustUnTomlString(s string, v any)

MustUnTomlString unmarshals s to v or panics trying.

func TomlFile

func TomlFile(v any, file string) error

TomlFile marshals v to TOML and writes the data to file.

func UnmarshalFile

func UnmarshalFile(file string, v any) error

UnmarshalFile reads file and unmarshals it into v if possible.

Intended for reading config files, the default format is TOML. Any other types in UnmarshalFileExtensions will be converted to TOML first.

Types

This section is empty.

Jump to

Keyboard shortcuts

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