utils

package
v0.0.0-...-00ab8b8 Latest Latest
Warning

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

Go to latest
Published: Mar 15, 2024 License: MIT Imports: 28 Imported by: 0

Documentation

Index

Constants

View Source
const (
	SECONDS_IN_SECOND = 1
	SECONDS_IN_MINUTE = 60
	SECONDS_IN_HOUR   = 3600
	SECONDS_IN_DAY    = 86400
	SECONDS_IN_WEEK   = 604800
	SECONDS_IN_YEAR   = 31536000
	SECONDS_IN_MONTH  = SECONDS_IN_YEAR / 12
)
View Source
const COMMIT_HASH_SHORT_SIZE = 8
View Source
const LoaderAnimationInterval = 50

The duration between two frames of the loader animation in milliseconds

Variables

This section is empty.

Functions

func AsJson

func AsJson(i interface{}) string

func CaseAwareContains

func CaseAwareContains(haystack, needle string) bool

func CaseInsensitiveContains

func CaseInsensitiveContains(haystack, needle string) bool

func Clamp

func Clamp(x int, min int, max int) int

func ContainsUppercase

func ContainsUppercase(s string) bool

func Decolorise

func Decolorise(str string) string

Decolorise strips a string of color

func EditRebaseTodo

func EditRebaseTodo(filePath string, changes []TodoChange, commentChar byte) error

Read a git-rebase-todo file, change the actions for the given commits, and write it back

func EscapeSpecialChars

func EscapeSpecialChars(str string) string

EscapeSpecialChars - Replaces all special chars like \n with \\n

func FilePath

func FilePath(skip int) string

returns the path of the file that calls the function. 'skip' is the number of stack frames to skip.

func FindNamedMatches

func FindNamedMatches(regex *regexp.Regexp, str string) map[string]string

func FindStringSubmatch

func FindStringSubmatch(str string, regexpStr string) (bool, []string)

func ForEachLineInFile

func ForEachLineInFile(path string, f func(string, int)) error

func FormatPaths

func FormatPaths(paths []string) string

Returns comma-separated list of paths, with ellipsis if there are more than 3 e.g. "foo, bar, baz, [...3 more]"

func FuzzySearch

func FuzzySearch(needle string, haystack []string) []string

func GetProjectRoot

func GetProjectRoot() string

GetProjectRoot returns the path to the root of the project. Only to be used in testing contexts, as with binaries it's unlikely this path will exist on the machine

func IsValidHexValue

func IsValidHexValue(v string) bool

func Limit

func Limit(values []string, limit int) []string

func LimitStr

func LimitStr(value string, limit int) string

func Loader

func Loader(now time.Time) string

Loader dumps a string to be displayed as a loader

func Max

func Max(x, y int) int

func MaxFn

func MaxFn[T any](items []T, fn func(T) int) int

func Min

func Min(x, y int) int

Min returns the minimum of two integers

func ModuloWithWrap

func ModuloWithWrap(n, max int) int

used to keep a number n between 0 and max, allowing for wraparounds

func Move

func Move[T any](slice []T, fromIndex int, toIndex int) []T

Removes the element at the 'fromIndex' and then inserts it at 'toIndex'. Operates on the input slice. Expected use is to reassign the result to the input slice.

func MoveElement

func MoveElement[T any](slice []T, from int, to int) []T

Returns a new slice with the element at index 'from' moved to index 'to'. Does not mutate original slice.

func MoveFixupCommitDown

func MoveFixupCommitDown(fileName string, originalSha string, fixupSha string, commentChar byte) error

func MoveTodosDown

func MoveTodosDown(fileName string, todosToMove []Todo, commentChar byte) error

func MoveTodosUp

func MoveTodosUp(fileName string, todosToMove []Todo, commentChar byte) error

func MuiltiGroupBy

func MuiltiGroupBy[T any, K comparable](slice []T, f func(T) []K) map[K][]T

Similar to a regular GroupBy, except that each item can be grouped under multiple keys, so the callback returns a slice of keys instead of just one key.

func MustConvertToInt

func MustConvertToInt(s string) int

func NewDummyCommon

func NewDummyCommon() *common.Common

func NewDummyCommonWithUserConfigAndAppState

func NewDummyCommonWithUserConfigAndAppState(userConfig *config.UserConfig, appState *config.AppState) *common.Common

func NewDummyLog

func NewDummyLog() *logrus.Entry

NewDummyLog creates a new dummy Log for testing

func NextIndex

func NextIndex(numbers []int, currentNumber int) int

NextIndex returns the index of the element that comes after the given number

func NextIntInCycle

func NextIntInCycle(sl []int, current int) int

NextIntInCycle returns the next int in a slice, returning to the first index if we've reached the end

func NormalizeLinefeeds

func NormalizeLinefeeds(str string) string

NormalizeLinefeeds - Removes all Windows and Mac style line feeds

func Partition

func Partition[T any](slice []T, test func(T) bool) ([]T, []T)

returns two slices: the first is for elements that pass the test, the second for those that don't.

func Pop

func Pop[T any](slice []T) (T, []T)

Pops item from the end of the slice and returns it, along with the updated slice Mutates original slice. Intended usage is to reassign the slice result to the input slice.

func Prepend

func Prepend[T any](slice []T, values ...T) []T

Prepends items to the beginning of a slice. E.g. Prepend([]int{1,2}, 3, 4) = []int{3,4,1,2} Mutates original slice. Intended usage is to reassign the slice result to the input slice.

func PrependStrToTodoFile

func PrependStrToTodoFile(filePath string, linesToPrepend []byte) error

func PrevIndex

func PrevIndex(numbers []int, currentNumber int) int

PrevIndex returns the index that comes before the given number, cycling if we reach the end

func PrevIntInCycle

func PrevIntInCycle(sl []int, current int) int

PrevIntInCycle returns the prev int in a slice, returning to the first index if we've reached the end

func ReadRebaseTodoFile

func ReadRebaseTodoFile(fileName string, commentChar byte) ([]todo.Todo, error)

func Remove

func Remove[T any](slice []T, index int) []T

Removes the element at the given index. Intended usage is to reassign the result to the input slice.

func RenderDisplayStrings

func RenderDisplayStrings(displayStringsArr [][]string, columnAlignments []Alignment) ([]string, []int)

defaults to left-aligning each column. If you want to set the alignment of each column, pass in a slice of Alignment values. returns a list of strings that should be joined with "\n", and an array of the column positions

func ResolvePlaceholderString

func ResolvePlaceholderString(str string, arguments map[string]string) string

ResolvePlaceholderString populates a template with values

func ResolveTemplate

func ResolveTemplate(templateStr string, object interface{}, funcs template.FuncMap) (string, error)

func Safe

func Safe(f func())

Safe will close tcell if a panic occurs so that we don't end up in a malformed terminal state

func SafeTruncate

func SafeTruncate(str string, limit int) string

func SafeWithError

func SafeWithError(f func() error) error

func SetCustomColors

func SetCustomColors(customColors map[string]string) map[string]style.TextStyle

func Shift

func Shift[T any](slice []T) (T, []T)

Shifts item from the beginning of the slice and returns it, along with the updated slice. Mutates original slice. Intended usage is to reassign the slice result to the input slice.

func ShortSha

func ShortSha(sha string) string

func SortRange

func SortRange(x int, y int) (int, int)

func SplitLines

func SplitLines(multilineString string) []string

SplitLines takes a multiline string and splits it on newlines currently we are also stripping \r's which may have adverse effects for windows users (but no issues have been raised yet)

func SplitNul

func SplitNul(str string) []string

func StackTrace

func StackTrace() string

func StringArraysOverlap

func StringArraysOverlap(strArrA []string, strArrB []string) bool

func TruncateWithEllipsis

func TruncateWithEllipsis(str string, limit int) string

TruncateWithEllipsis returns a string, truncated to a certain length, with an ellipsis

func UnixToDateSmart

func UnixToDateSmart(now time.Time, timestamp int64, longTimeFormat string, shortTimeFormat string) string

formats the date in a smart way, if the date is today, it will show the time, otherwise it will show the date

func UnixToTimeAgo

func UnixToTimeAgo(timestamp int64) string

func ValuesAtIndices

func ValuesAtIndices[T any](slice []T, indices []int) []T

func WithPadding

func WithPadding(str string, padding int, alignment Alignment) string

WithPadding pads a string as much as you want

func WrapError

func WrapError(err error) error

WrapError wraps an error for the sake of showing a stack trace at the top level the go-errors package, for some reason, does not return nil when you try to wrap a non-error, so we're just doing it here

func WriteRebaseTodoFile

func WriteRebaseTodoFile(fileName string, todos []todo.Todo, commentChar byte) error

Types

type Alignment

type Alignment int
const (
	AlignLeft Alignment = iota
	AlignRight
)

type ColumnConfig

type ColumnConfig struct {
	Width     int
	Alignment Alignment
}

type HistoryBuffer

type HistoryBuffer[T any] struct {
	// contains filtered or unexported fields
}

func NewHistoryBuffer

func NewHistoryBuffer[T any](maxSize int) *HistoryBuffer[T]

func (*HistoryBuffer[T]) PeekAt

func (self *HistoryBuffer[T]) PeekAt(index int) (T, error)

func (*HistoryBuffer[T]) Push

func (self *HistoryBuffer[T]) Push(item T)

type OnceWriter

type OnceWriter struct {
	// contains filtered or unexported fields
}

func NewOnceWriter

func NewOnceWriter(writer io.Writer, f func()) *OnceWriter

func (*OnceWriter) Write

func (self *OnceWriter) Write(p []byte) (n int, err error)

type StringStack

type StringStack struct {
	// contains filtered or unexported fields
}

func (*StringStack) Clear

func (self *StringStack) Clear()

func (*StringStack) IsEmpty

func (self *StringStack) IsEmpty() bool

func (*StringStack) Pop

func (self *StringStack) Pop() string

func (*StringStack) Push

func (self *StringStack) Push(s string)

type ThreadSafeMap

type ThreadSafeMap[K comparable, V any] struct {
	// contains filtered or unexported fields
}

func NewThreadSafeMap

func NewThreadSafeMap[K comparable, V any]() *ThreadSafeMap[K, V]

func (*ThreadSafeMap[K, V]) Clear

func (m *ThreadSafeMap[K, V]) Clear()

func (*ThreadSafeMap[K, V]) Delete

func (m *ThreadSafeMap[K, V]) Delete(key K)

func (*ThreadSafeMap[K, V]) Get

func (m *ThreadSafeMap[K, V]) Get(key K) (V, bool)

func (*ThreadSafeMap[K, V]) Has

func (m *ThreadSafeMap[K, V]) Has(key K) bool

func (*ThreadSafeMap[K, V]) IsEmpty

func (m *ThreadSafeMap[K, V]) IsEmpty() bool

func (*ThreadSafeMap[K, V]) Keys

func (m *ThreadSafeMap[K, V]) Keys() []K

func (*ThreadSafeMap[K, V]) Len

func (m *ThreadSafeMap[K, V]) Len() int

func (*ThreadSafeMap[K, V]) Set

func (m *ThreadSafeMap[K, V]) Set(key K, value V)

func (*ThreadSafeMap[K, V]) Values

func (m *ThreadSafeMap[K, V]) Values() []V

type Todo

type Todo struct {
	Sha    string
	Action todo.TodoCommand
}

type TodoChange

type TodoChange struct {
	Sha       string
	OldAction todo.TodoCommand
	NewAction todo.TodoCommand
}

In order to change a TODO in git-rebase-todo, we need to specify the old action, because sometimes the same sha appears multiple times in the file (e.g. in a pick and later in a merge)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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