utils

package
v1.10.4 Latest Latest
Warning

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

Go to latest
Published: Feb 11, 2024 License: MIT Imports: 17 Imported by: 0

Documentation

Overview

Package utils provides common utility functions that are not reminder specific

Index

Constants

This section is empty.

Variables

View Source
var CurrentTime = func() time.Time {
	return time.Now()
}

CurrentTime function gets current time. Note: It is deliberately defined as a variable to make it easer to patch in tests.

View Source
var HomeDir func() string = func() string {
	usr, _ := user.Current()
	dir := usr.HomeDir
	return dir
}

HomeDir return the home directory path for current user. Note: It is deliberately defined as a variable to make it easer to patch in tests.

View Source
var Location *time.Location

Location variable provides location info for `time`. It can be set to update behavior of UnixTimestampToTime.

View Source
var Symbols = map[string]string{
	"add":          "➕",
	"backup":       "💾",
	"calendar":     "📅",
	"checkerdFlag": "🏁",
	"clip":         "🔗",
	"clock":        "⏰",
	"comment":      "💬",
	"done":         "✅",
	"downArrow":    "⬇️",
	"downVote":     "👎",
	"error":        "❌",
	"glossary":     "📖",
	"hat":          "🎩",
	"home":         "⛺",
	"noAction":     "❎",
	"pad":          "📋",
	"redFlag":      "🚩",
	"refresh":      "🔄",
	"search":       "🔎",
	"spark":        "⚡",
	"tag":          "🏷t",
	"telescope":    "🔭",
	"text":         "📝",
	"think":        "🤔",
	"upArrow":      "⬆️",
	"upVote":       "👍",
	"warning":      "⚠️ ",
	"zzz":          "💤",
}
View Source
var TerminalSize = func() (int, int, error) {
	out, err := PerformShellOperation("stty", "size")
	if err != nil {
		return 0, 0, err
	}
	output := strings.TrimSpace(string(out))
	dims := strings.Split(output, " ")
	height, _ := strconv.Atoi(dims[0])
	width, _ := strconv.Atoi(dims[1])
	return height, width, nil
}

TerminalSize function gets terminal size. Note: It is deliberately defined as a variable to make it easer to patch in tests.

Functions

func AskBoolean added in v1.9.27

func AskBoolean(msg string) (bool, error)

AskBoolean asks a boolean question to the user.

func AskOption

func AskOption(options []string, label string) (int, string, error)

AskOption function asks option to the user. It print error, if encountered any (so that they don't have to printed by calling function). It return a tuple (chosen index, chosen string, err if any).

func AssertEqual

func AssertEqual(t *testing.T, got interface{}, want interface{})

AssertEqual function makes assertion that `go` and `want` are nearly equal.

func ChopStrings

func ChopStrings(texts []string, length int) []string

ChopStrings function returns a chopped strings (to a desired length).

func CurrentUnixTimestamp

func CurrentUnixTimestamp() int64

CurrentUnixTimestamp function gets current unix timestamp.

func GenerateNoteSearchSelect

func GenerateNoteSearchSelect(items []string, searchFunc func(filter string, value string, index int) bool) (int, error)

GenerateNoteSearchSelect function generates survey.Select and return index of selected option.

func GeneratePrompt

func GeneratePrompt(promptName string, defaultText string) (string, error)

GeneratePrompt function generates survey.Input.

func GetCommonMembersOfSlices added in v1.9.28

func GetCommonMembersOfSlices[V comparable](list1 []V, list2 []V) []V

GetCommonMembersOfSlices function gets common elements of slices.

func GetLocalZone added in v1.9.20

func GetLocalZone() (string, time.Duration)

GetLocalZone returns the local timezone abbreviation and offset (in time.Duration).

func GetZoneFromLocation added in v1.9.20

func GetZoneFromLocation(loc string) (time.Duration, error)

GetZoneFromLocation returns zone offset (in time.Duration) for given location string like "Melbourne/Australia".

func IsMemberOfSlice added in v1.9.28

func IsMemberOfSlice[V comparable](a V, list []V) bool

IsMemberOf function performs membership test.

func LogError added in v1.9.13

func LogError(err error)

LogError function ignores but prints the error (if present).

func MatchedTimestamp added in v1.9.28

func MatchedTimestamp(noteTimestampCurrent, noteTimestampPrevious, noteTimestampNext, daysBefore, daysAfter int64) (bool, int64)

MatchedTimestamp function determines if currentTime matches with (within allowed daysBefore and daysAfter) given 3 timestamps (current, previous, and next). That is, it checks to see if any of the current timestamp falls in between [TIMESTAMP - DaysBefore, TIMESTAMP + DaysAfter]

func PerformCat

func PerformCat(filePath string) error

PerformCat function cats a file.

func PerformCwdiff

func PerformCwdiff(oldFilePath string, newFilePath string) error

PerformCwdiff function gets colored wdiff between two files.

func PerformFilePresence

func PerformFilePresence(filePath string) error

PerformFilePresence function checks presence of a file.

func PerformShellOperation

func PerformShellOperation(exe string, args ...string) (string, error)

PerformShellOperation function performs shell operation and return its output. Note: it is better to avoid such functions.

func PerformWhich

func PerformWhich(shellCmd string) error

PerformWhich function checks if a shell command is available.

func SkipCI added in v1.9.28

func SkipCI(t *testing.T)

SkipCI skips a unit test in CI environment.

func Spinner

func Spinner(delay time.Duration)

Spinner function displays spinner.

func StrToTime added in v1.9.16

func StrToTime(tString string, timezone string) (time.Time, error)

StrToTime converts RFC3339 time sting to time.Time, and sets location to given timezone. If location is blank, then it returns the time as it is.

func TemplateResult

func TemplateResult(reportTemplate string, funcMap template.FuncMap, data interface{}) (string, error)

TemplateResult function runs given go template with given data and function map, and return the result as string. It is interesting to note that even though data is recieved as `interface{}`, the template is able to access those attributes without even having to perform type assertion to get the underneath concrete value; this is contrary to masking behavior of interfaces.

func TerminalWidth

func TerminalWidth() (int, error)

TerminalWidth function gets terminal width.

func TimeToStr added in v1.9.16

func TimeToStr(t time.Time) string

TimeToStr converts time.Time to RFC3339 time string.

func TryConvertTildaBasedPath added in v1.9.14

func TryConvertTildaBasedPath(path string) string

TryConvertTildaBasedPath converts tilda (~) based path to complete path. For a non-tilda based path, return as it is.

func UTCLocation

func UTCLocation() *time.Location

UTCLocation function returns UTC location.

func UnixTimestampForCorrespondingCurrentYear

func UnixTimestampForCorrespondingCurrentYear(month int, day int) int64

UnixTimestampForCorrespondingCurrentYear function gets unix timestamp for date corresponding to current year.

func UnixTimestampForCorrespondingCurrentYearMonth

func UnixTimestampForCorrespondingCurrentYearMonth(day int) int64

UnixTimestampForCorrespondingCurrentYearMonth function gets unix timestamp for date corresponding to current year and current month.

func UnixTimestampToLongTimeStr

func UnixTimestampToLongTimeStr(unixTimestamp int64) string

UnixTimestampToLongTimeStr function converts unix timestamp to long time string.

func UnixTimestampToMediumTimeStr

func UnixTimestampToMediumTimeStr(unixTimestamp int64) string

UnixTimestampToMediumTimeStr function converts unix timestamp to medium time string.

func UnixTimestampToShortTimeStr

func UnixTimestampToShortTimeStr(unixTimestamp int64) string

UnixTimestampToShortTimeStr function converts unix timestamp to short time string.

func UnixTimestampToTime

func UnixTimestampToTime(unixTimestamp int64) time.Time

UnixTimestampToTime function converts unix timestamp to time. It serves as central place to switch between UTC and local time. by default use local time, but behavior can be changed via `Location`. In either case, the value of the time (in seconds) remains same, the use of the Location just changes how time is displayed. For example, 5:30 PM in SGT is equivalent to 12 Noon in India.

func UnixTimestampToTimeStr

func UnixTimestampToTimeStr(unixTimestamp int64, timeFormat string) string

UnixTimestampToTimeStr function converts unix timestamp to time string.

func ValidateDateString

func ValidateDateString() survey.Validator

ValidateDateString function validates date string (DD-MM-YYYY) or (DD-MM). nil is also valid input

func YearForDueDateDDMM

func YearForDueDateDDMM(dateMonth string) (int, error)

YearForDueDateDDMM return the current year if DD-MM is falling after current date, otherwise returns next year

Types

This section is empty.

Jump to

Keyboard shortcuts

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