util

package
v0.3.7 Latest Latest
Warning

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

Go to latest
Published: Jan 11, 2023 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// DateFormat for date formatting
	DateFormat = "2006-01-02"
	// TimeFormat for time formatting
	TimeFormat = "15:04"
	// FileTimeFormat for file name time formatting
	FileTimeFormat = "15-04"
	// FileDateTimeFormat for date and time from paths
	FileDateTimeFormat = "2006-01-02 15-04"
	// DateTimeFormat for date and time formatting
	DateTimeFormat = "2006-01-02 15:04"
	// NoTimeString string representation for zero end time
	NoTimeString = " now "
)
View Source
const (
	// PrevDayPrefix is a prefix for a time on the previous day
	PrevDayPrefix = "<"
	// NextDaySuffix is a suffix for a time on the next day
	NextDaySuffix = ">"
)

Variables

View Source
var BlockRunes = [9]rune{'·', 9601, 9602, 9603, 9604, 9605, 9606, 9607, 9608}

BlockRunes are utf8 8th blocks from empty to full

View Source
var (
	// ErrNoFiles is an error for no files found at all
	ErrNoFiles = errors.New("no files")
)
View Source
var NoTime time.Time = time.Time{}

NoTime is a zero time

View Source
var SkipEditingForTests = false

SkipEditingForTests makes editing just fall through, for unit testing

Functions

func Aggregate added in v0.3.6

func Aggregate[T Named, V any](t *MapTree[T], values map[string]V, zero V, fn func(a, b V) V)

Aggregate aggregates values over the tree

func CreateDir added in v0.3.6

func CreateDir(path string) error

CreateDir creates directories recursively

func Date

func Date(year int, month time.Month, day int) time.Time

Date creates a date from a year, a month and a day. Assumes the local time zone.

func DateAndTime added in v0.3.0

func DateAndTime(d, t time.Time) time.Time

DateAndTime combines a date with a time

func DateTime

func DateTime(year int, month time.Month, day, hours, minutes, seconds int) time.Time

DateTime creates a datetime

func DirExists added in v0.3.6

func DirExists(path string) bool

DirExists checks if a directory exists

func DirIsEmpty added in v0.3.6

func DirIsEmpty(path string) (bool, error)

DirIsEmpty checks if a directory is empty

func DurationClip added in v0.3.0

func DurationClip(start, end, min, max time.Time) time.Duration

DurationClip calculated the duration a time span, clipped by another time span

func EditFile added in v0.3.6

func EditFile(path string, editor string) error

EditFile opens a file in the default editor and waits for the process to finish

func FileExists added in v0.3.6

func FileExists(path string) bool

FileExists checks if a file exists

func FindLatests added in v0.3.6

func FindLatests(path string, isDir bool) (string, string, error)

FindLatests finds the "latest" file or directory in a file, by name

func FloatToBlock added in v0.2.0

func FloatToBlock(value float64, space *rune) rune

FloatToBlock returns utf8 8th blocks for values between 0 and 1

func Format

func Format(str string, repl map[string]string) string

Format formats a string with named placeholders.

Example: s := Format("foo {name} bar", map[string]string{"name": "baz"})

func FormatCmdTree

func FormatCmdTree(command *cobra.Command) (string, error)

FormatCmdTree creates a tree-like representation of a command and its sub-commands

func FormatDuration

func FormatDuration(d time.Duration, zeroPadHours ...bool) string

FormatDuration formats a duration

func FormatTimeWithOffset added in v0.3.0

func FormatTimeWithOffset(t time.Time, reference time.Time) string

FormatTimeWithOffset formats a time with day offset indicators

func Monday added in v0.3.1

func Monday(date time.Time) time.Time

Monday returns the monday of the week of the given date

func ParseDate

func ParseDate(text string) (time.Time, error)

ParseDate parses a date string

func ParseDateTime

func ParseDateTime(text string) (time.Time, error)

ParseDateTime parses a datetime string. Assumes the local time zone.

func ParseTimeRange added in v0.3.0

func ParseTimeRange(text string, date time.Time) (start, end time.Time, err error)

ParseTimeRange parses a time range string. Assumes the local time zone.

func ParseTimeWithOffset added in v0.3.0

func ParseTimeWithOffset(text string, date time.Time) (time.Time, error)

ParseTimeWithOffset parses a time with offset markers

func Reverse added in v0.3.0

func Reverse[S ~[]E, E any](s S)

Reverse reverts a slice in-place

func Sanitize added in v0.3.6

func Sanitize(file string) string

Sanitize makes stings filename compatible

func TerminalSize added in v0.2.0

func TerminalSize() (width int, height int, err error)

TerminalSize returns the size of the terminal

func ToDate added in v0.2.0

func ToDate(t time.Time) time.Time

ToDate creates a date from a time by setting to 00:00

func Unique added in v0.3.5

func Unique[T comparable](slice []T) []T

Unique returns a slice with only unique elements of the original slice

func WrappedArgs

func WrappedArgs(fn cobra.PositionalArgs) cobra.PositionalArgs

WrappedArgs are PositionalArgs that print usage on error

Types

type CmdNode

type CmdNode = MapNode[CmdWrapper]

CmdNode is a tree of cobra commands

type CmdTree

type CmdTree = MapTree[CmdWrapper]

CmdTree is a tree of cobra commands

type CmdWrapper added in v0.3.3

type CmdWrapper struct {
	*cobra.Command
}

CmdWrapper wraps *cobra.Command to implement the Named interface

func (CmdWrapper) GetName added in v0.3.3

func (cmd CmdWrapper) GetName() string

GetName implements the Named interface required for the MapTree

type MapNode added in v0.3.6

type MapNode[T Named] struct {
	Parent   *MapNode[T]
	Children map[string]*MapNode[T]
	Value    T
}

MapNode is a node in the tree data structure

func NewNode added in v0.3.6

func NewNode[T Named](value T) *MapNode[T]

NewNode creates a new tree node

type MapTree added in v0.3.6

type MapTree[T Named] struct {
	Root  *MapNode[T]
	Nodes map[string]*MapNode[T]
}

MapTree is a tree data structure

func NewTree added in v0.3.6

func NewTree[T Named](value T) *MapTree[T]

NewTree creates a new tree node

func (*MapTree[T]) Add added in v0.3.6

func (t *MapTree[T]) Add(parent *MapNode[T], child T) (*MapNode[T], error)

AddTree adds a sub-tree without children

func (*MapTree[T]) AddNode added in v0.3.6

func (t *MapTree[T]) AddNode(parent *MapNode[T], child *MapNode[T]) error

AddNode adds a sub-tree

func (*MapTree[T]) Ancestors added in v0.3.6

func (t *MapTree[T]) Ancestors(name string) ([]*MapNode[T], bool)

Ancestors returns a slice of all ancestors (i.e. recursive parents), and an ok bool whether the requested node was found.

The first ancestor is the direct parent, while the last ancestor is the root node.

func (*MapTree[T]) Descendants added in v0.3.6

func (t *MapTree[T]) Descendants(name string) ([]*MapNode[T], bool)

Descendants returns a slice of all descendants (i.e. recursive children), and an ok bool whether the requested node was found.

Descendants in the returned slice have undefined order.

type Named added in v0.3.6

type Named interface {
	GetName() string
}

Named is an interface for stuff that has a name

type Pair added in v0.3.5

type Pair[K any, V any] struct {
	Key   K
	Value V
}

Pair is a (key/value) pair

func NewPair added in v0.3.5

func NewPair[K any, V any](key K, value V) Pair[K, V]

NewPair creates a new Pair

type TreeFormatter

type TreeFormatter[T Named] struct {
	NameFunc func(t *MapNode[T], indent int) string
	Indent   int
	// contains filtered or unexported fields
}

TreeFormatter formats trees

func NewTreeFormatter

func NewTreeFormatter[T Named](
	nameFunc func(t *MapNode[T], indent int) string,
	indent int,
) TreeFormatter[T]

NewTreeFormatter creates a new TreeFormatter

func (*TreeFormatter[T]) FormatTree

func (f *TreeFormatter[T]) FormatTree(t *MapTree[T]) string

FormatTree formats a tree

Jump to

Keyboard shortcuts

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