predict

package
v0.1.5 Latest Latest
Warning

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

Go to latest
Published: Nov 4, 2024 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// CachedDir is the parent directory under the user cache dir that cached suggestions
	// will be stored
	CachedDir = "tab_complete"
)

Variables

View Source
var Anything = Func(func(args.Args) []string { return nil })

Anything expects something, but nothing particular, such as a number or arbitrary name.

View Source
var UserCacheDir = func() (string, error) {
	return os.UserCacheDir()
}

Allow overriding for tests, but defaults to the operating system's preferred location.

Functions

This section is empty.

Types

type Predictor

type Predictor interface {
	Predict(args.Args) []string
}

Predictor implements a predict method, in which given command line arguments returns a list of options it predicts.

It's given context about what the user has typed for the predictor to make the decision.

var Nothing Predictor

Nothing does not expect anything after.

func Cached

func Cached(name string, ttl time.Duration, load func() []string) Predictor

Cached returns a predictor that can re-use previous values for some time until needing to regenerate.

This is useful when the source of truth for items is expensive to calculate, like requiring a network request.

Suggestions are written to a file in the preferred cache directory for the user's OS. The contents of the file a line-delimeted, with every entry being a suggestion. The file's modification time is used to determine whether to refresh or not.

An example filepath might look like: ~/.cache/tab_complete/cmd/name

Where 'cmd' is os.Args[0] and 'name' is provided to this function. CachedDir can be written to customize the containing directory.

func Dirs

func Dirs(pattern string) Predictor

Dirs will search for directories in the given started to be typed path, if no path was started to be typed, it will complete to directories in the current working directory.

func FileSet added in v0.1.1

func FileSet(files []string) Predictor

FileSet predict according to file rules to a given set of file names

func Files

func Files(pattern string) Predictor

Files will search for files matching the given pattern in the started to be typed path, if no path was started to be typed, it will complete to files that match the pattern in the current working directory. To match any file, use "*" as pattern. To match go files use "*.go", and so on.

func Func

func Func(inner func(args.Args) []string) Predictor

Func determines what terms can follow a command or a flag It is used for auto completion, given last - the last word in the already in the command line, what words can complete it.

func Or

func Or(predictors ...Predictor) Predictor

Or unions two predicate functions, so that the result predicate returns the union of their predication

func ScopedCache

func ScopedCache(scope string, name string, ttl time.Duration, load func() []string) Predictor

ScopedCache returns a cached predictor that is scoped manually instead of by the command.

This is useful if you have multiple command-line programs that want to share tab-complete suggestions without duplicating the results.

func Set

func Set(options ...string) Predictor

Set expects specific set of terms, given in the options argument.

Jump to

Keyboard shortcuts

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