util

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Feb 7, 2024 License: AGPL-3.0 Imports: 12 Imported by: 0

Documentation

Index

Constants

View Source
const (
	KB = 1000
	MB = 1000 * KB
	GB = 1000 * MB
	TB = 1000 * GB
	PB = 1000 * TB

	KiB = 1024
	MiB = 1024 * KiB
	GiB = 1024 * MiB
	TiB = 1024 * GiB
	PiB = 1024 * TiB
)

See: http://en.wikipedia.org/wiki/Binary_prefix

Variables

This section is empty.

Functions

func AppendUniqueCapSlice

func AppendUniqueCapSlice[T comparable](s []T, cap int, els ...T) (ns []T)

Append els to s, then return the last cap unique elements of result

func BytesSize

func BytesSize(size float64) string

BytesSize returns a human-readable size in bytes, kibibytes, mebibytes, gibibytes, or tebibytes (e.g.: "44kiB", "17MiB").

func BytesSizeAround

func BytesSizeAround(size float64) string

return at most 4 bytes, e.g.: "123G"

func Cd

func Cd(dir string) (cwd string, err error)

Change cwd to dir and return the new cwd. Try to parse real path of dir using system shell, so env expression can be used in dir. If dir is empty, use user home dir instead.

func Chunks

func Chunks(s string, chunkSize int) []string

Split s to slice of string. Eath string in result slice has at most chunkSize UTF-8 characters. From https://stackoverflow.com/questions/25686109/split-string-by-length-in-golang

func ContextWithCancelSign

func ContextWithCancelSign(ctx context.Context, sign <-chan struct{}) (context.Context, context.CancelFunc)

return a ctx that is automatcally cancelled if sign received a data

func CustomSize

func CustomSize(format string, size float64, base float64, _map []string) string

CustomSize returns a human-readable approximation of a size using custom format.

func DownloadTgFileToLocal

func DownloadTgFileToLocal(ctx context.Context, tgtoken string, tgFileId string, filepath string) error

downloading file from url, then send it to telegram bot

func Filter

func Filter[T any](ss []T, test func(T) bool) (ret []T)

func FindLineDataByFirstField

func FindLineDataByFirstField(lines []string, index string) string

Each line in "no data" format, find the first line which no equals index and return it's data

func FromHumanSize

func FromHumanSize(size string) (int64, error)

FromHumanSize returns an integer from a human-readable specification of a size using SI standard (e.g.: "44kB", "17MB").

func HumanSize

func HumanSize(size float64) string

HumanSize returns a human-readable approximation of a size capped at 4 valid numbers (e.g.: "2.746 MB", "796 KB").

func HumanSizeWithPrecision

func HumanSizeWithPrecision(size float64, precision int) string

HumanSizeWithPrecision allows the size to be in any precision, instead of 4 digit precision used in units.HumanSize.

func RAMInBytes

func RAMInBytes(size string) (int64, error)

RAMInBytes parses a human-readable string representing an amount of RAM in bytes, kibibytes, mebibytes, gibibytes, or tebibytes and returns the number of bytes, or -1 if the string is unparseable. Units are case-insensitive, and the 'b' suffix is optional.

func RunCommand

func RunCommand(cmd string, args ...string) (result string, err error)

Run a cmd and return combined output. If args is not provided, treat cmd as a cmdline and parse it to extract cmd args

func SliceLastIndex

func SliceLastIndex[T comparable](s []T, v T) int

func SliceLastIndexFunc

func SliceLastIndexFunc[T comparable](s []T, test func(T) bool) int

func SplitFirstAndOthers

func SplitFirstAndOthers(str string) (first string, others string)

split str at first space character, return first and others part, both TrimSpaced

func UniqueSlice

func UniqueSlice[T comparable](s []T, except *T) (list []T)

func UniqueSliceInLastOrder

func UniqueSliceInLastOrder[T comparable](s []T, except *T) (list []T)

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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