utils

package
v0.43.0 Latest Latest
Warning

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

Go to latest
Published: Feb 24, 2023 License: MPL-2.0 Imports: 23 Imported by: 69

Documentation

Index

Constants

View Source
const (
	SimpleDateFormat    = "Jan 2"
	SimpleTimeFormat    = "15:04 MST"
	MinimumTimeFormat12 = "3:04 PM"
	MinimumTimeFormat24 = "15:04"

	FullDateFormat         = "Monday, Jan 2"
	FriendlyDateFormat     = "Mon, Jan 2"
	FriendlyDateTimeFormat = "Mon, Jan 2, 15:04"

	TimestampFormat = "2006-01-02T15:04:05-0700"
)

Variables

View Source
var OpenFileUtil = "open"

OpenFileUtil defines the system utility to use to open files

View Source
var OpenUrlUtil = []string{}

Functions

func CalculateDimensions added in v0.23.0

func CalculateDimensions(moduleConfig, globalConfig *config.Config) (int, int, error)

CalculateDimensions reads the module dimensions from the module and global config. The border is already subtracted.

func CenterText added in v0.20.0

func CenterText(str string, width int) string

CenterText takes a string and a width and pads the left and right of the string with empty spaces to ensure that the string is in the middle of the returned value

Example:

x := CenterText("cat", 11)
> "    cat    "

func Clamp added in v0.24.0

func Clamp(x, a, b int) int

Clamp restricts values to a minimum and maximum value

Examples:

clamp(6, 3, 8) => 6
clamp(1, 3, 8) => 3
clamp(9, 3, 8) => 8

func ColorizePercent added in v0.40.0

func ColorizePercent(percent float64) string

ColorizePercent provides a standard way to colorize percentages for which large numbers are good (green) and small numbers are bad (red).

func DoesNotInclude added in v0.20.0

func DoesNotInclude(strs []string, val string) bool

DoesNotInclude takes a slice of strings and a target string and returns TRUE if the slice does not include the target, FALSE if it does

Example:

x := DoesNotInclude([]string{"cat", "dog", "rat"}, "dog")
> false

x := DoesNotInclude([]string{"cat", "dog", "rat"}, "pig")
> true

func ExecuteCommand added in v0.20.0

func ExecuteCommand(cmd *exec.Cmd) string

ExecuteCommand executes an external command on the local machine as the current user

func ExpandHomeDir

func ExpandHomeDir(path string) (string, error)

ExpandHomeDir expands the path to include the home directory if the path is prefixed with `~`. If it isn't prefixed with `~`, the path is returned as-is.

func FindBetween added in v0.40.0

func FindBetween(input string, left string, right string) []string

FindBetween finds and returns the text between two strings

Example:

a := "{ cat } { dog }"
b := FindBetween(a, "{", "}")
> [" cat ", " dog "]

func FindMatch added in v0.20.0

func FindMatch(pattern string, data string) [][]string

FindMatch takes a regex pattern and a string of data and returns back all the matches in that string

func HelpFromInterface added in v0.11.0

func HelpFromInterface(item interface{}) string

func HighlightableHelper added in v0.20.0

func HighlightableHelper(view *tview.TextView, input string, idx, offset int) string

HighlightableHelper pads the given text with blank spaces to the width of the view containing it. This is helpful for extending row highlighting across the entire width of the view

func Includes added in v0.21.0

func Includes(strs []string, val string) bool

Includes takes a slice of strings and a target string and returns TRUE if the slice includes the target, FALSE if it does not

Example:

x := Includes([]string{"cat", "dog", "rat"}, "dog")
> true

x := Includes([]string{"cat", "dog", "rat"}, "pig")
> false

func Init added in v0.20.0

func Init(openFileUtil string, openUrlUtil []string)

Init initializes global settings in the wtf package

func IntsToUints added in v0.26.0

func IntsToUints(slice []int) []uint

IntsToUints takes a slice of ints and returns a slice of uints

func MapToStrs added in v0.20.0

func MapToStrs(aMap map[string]interface{}) map[string]string

MapToStrs takes a map of interfaces and returns a map of strings

func MaxInt added in v0.24.0

func MaxInt(x, y int) int

MaxInt returns the larger of x or y

Examples:

MaxInt(3, 2) => 3
MaxInt(2, 3) => 3

func NameFromEmail added in v0.20.0

func NameFromEmail(email string) string

NameFromEmail takes an email address and returns the part that comes before the @ symbol

Example:

NameFromEmail("test_user@example.com")
> "Test_user"

func NamesFromEmails added in v0.20.0

func NamesFromEmails(emails []string) []string

NamesFromEmails takes a slice of email addresses and returns a slice of the parts that come before the @ symbol

Example:

NamesFromEmail("test_user@example.com", "other_user@example.com")
> []string{"Test_user", "Other_user"}

func OpenFile added in v0.20.0

func OpenFile(path string)

OpenFile opens the file defined in `path` via the operating system

func ParseJSON added in v0.24.0

func ParseJSON(obj interface{}, text io.Reader) error

ParseJSON is a standard JSON reader from text

func PrettyNumber added in v0.26.0

func PrettyNumber(prtr *message.Printer, number float64) string

PrettyNumber formats number as string with 1000 delimiters and, if necessary, rounds it to 2 decimals

func ReadFileBytes added in v0.20.0

func ReadFileBytes(filePath string) ([]byte, error)

ReadFileBytes reads the contents of a file and returns those contents as a slice of bytes

func RowPadding added in v0.20.0

func RowPadding(offset int, max int) string

RowPadding returns a padding for a row to make it the full width of the containing widget. Useful for ensuring row highlighting spans the full width (I suspect tcell has a better way to do this, but I haven't yet found it)

func StringValueForProperty added in v0.40.0

func StringValueForProperty(ref interface{}, propName string) (string, error)

StringValueForProperty returns a string value for the given property If the property doesn't exist, it returns an error

func StripColorTags added in v0.11.0

func StripColorTags(input string) string

StripColorTags removes tcell color tags from a given string

func SumInts added in v0.26.0

func SumInts(vals []int) int

SumInts takes a slice of ints and returns the sum of them

func ToInts added in v0.20.0

func ToInts(slice []interface{}) []int

ToInts takes a slice of interfaces and returns a slice of ints

func ToStrs added in v0.20.0

func ToStrs(slice []interface{}) []string

ToStrs takes a slice of interfaces and returns a slice of strings

func ToUints added in v0.26.0

func ToUints(slice []interface{}) []uint

ToUints takes a slice of interfaces and returns a slice of ints

func Truncate added in v0.25.0

func Truncate(src string, maxLen int, withEllipse bool) string

Truncate chops a given string at len length. Appends an ellipse character if warranted

Types

This section is empty.

Jump to

Keyboard shortcuts

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