Documentation
¶
Overview ¶
Package projectpath expands home-relative paths, resolves canonical paths, finds Git working tree roots, checks whether one path lies within another, suggests completions for a typed file or directory path, and searches below a start path for names that hold a text. It reads the file system directly and does not start a process.
A Suggester turns typed text into an ordered list of suggestions so that a user interface can offer path completion without reading the file system itself. It reads only the directory the typed text names, once per instance. Directories come first, each with a trailing separator of the running platform; files follow in Files mode. Hidden entries appear once the typed last segment starts with a dot, names match with their case, a suggestion keeps the typed spelling of the directory part, and a link to a directory counts as a directory.
Search finds, from a start path, the entries that are directories, files or both and whose name holds a text, ignoring case, over any number of levels. SplitSearch splits a typed "<start>*<text>" into its parts; without a "*" the text stays a plain completion for a Suggester. Four directories are read at the same time. Matches are ordered by name quality (the same name, then a name that starts with the text, then any other), then by depth, then by path. A link can match but is never followed, so it is reported as a file. The result says why the search ended; a limit or a canceled context ends it at once and is not an error, and then the matches are those found so far.
Always enforced ¶
A name that is not valid UTF-8 or holds a control or format character, such as a right-to-left override, is never suggested, because file names are untrusted data. A missing or unreadable directory yields no suggestions and no error. At most MaxSuggestions suggestions are returned for one typed text. A Suggester is not safe for concurrent use.
A search reads at most 200,000 entries, runs for at most three seconds and returns at most 50 matches unless the options set lower or higher limits. It stays on the device of the start path on Unix, does not enter /proc, /sys, /dev and /run on Linux, and skips a directory it cannot read, counting it. Names that are not valid UTF-8 or hold a control or format character never match. A start that is not a readable directory, an empty text and an unknown Kind are errors.
Options ¶
- Mode: Directories completes only directories and only absolute and "~/" paths; Files also offers files and completes paths relative to the working directory. Required by NewSuggester, which has no default.
- The workDir argument of NewSuggester: what a relative path is resolved against in Files mode. Default: the process working directory.
- SearchOptions.Kind: Both, OnlyDirectories or OnlyFiles. Default: Both.
- SearchOptions.MaxEntries, Timeout and MaxMatches: the limits of one search. Default: DefaultMaxEntries, DefaultTimeout and DefaultMaxMatches.
Index ¶
- Constants
- func Canonical(dir string) string
- func Expand(entry string) (string, error)
- func IsHomeRelative(entry string) bool
- func Roots(dir string) []string
- func SplitSearch(typed string) (start, text string, ok bool)
- func Within(dir, base string) bool
- type End
- type Kind
- type Match
- type Mode
- type SearchOptions
- type SearchResult
- type Suggester
Constants ¶
const ( DefaultMaxEntries = 200_000 DefaultTimeout = 3 * time.Second DefaultMaxMatches = 50 )
Default limits of Search, used for every limit of SearchOptions that is zero.
const MaxSuggestions = 100
MaxSuggestions bounds the suggestions returned for one typed text.
Variables ¶
This section is empty.
Functions ¶
func Canonical ¶
Canonical returns the absolute, cleaned form of dir with every symbolic link resolved. A directory that does not exist, or whose links cannot be resolved, keeps its cleaned absolute form, so a configured path that is created later still compares by its written form.
func Expand ¶
Expand returns entry with a leading "~" replaced by the user's home directory. Only "~" itself and "~" followed by a separator are expanded; any other entry is returned as it is.
func IsHomeRelative ¶
IsHomeRelative reports whether entry is "~" or starts with "~" and a path separator of the running platform, the only home-relative form Expand accepts. "~user" is not one.
func Roots ¶
Roots returns the directories that stand for the project dir lies in: the root of the Git working tree that contains dir, or dir itself outside any, and for a linked worktree also the main working tree of the same repository, so a worktree checked out elsewhere belongs to the project of its repository. Every returned directory is Canonical.
func SplitSearch ¶ added in v0.26.0
SplitSearch splits typed of the form "<start>*<text>" at its first "*". ok is false, and the typed text stays a plain completion, when there is no "*" or one side is empty.
func Within ¶
Within reports whether dir equals base or lies below it. Both are compared whole path segment by whole path segment, so "/repos/kunde-a" does not contain "/repos/kunde-ab", and under the rules of the running platform: on Windows without regard to case, separator style, or the case of the drive letter.
Types ¶
type End ¶ added in v0.26.0
type End int
End says why a search ended.
const ( // Complete means the whole tree below the start was read. Complete End = iota // EntryLimit means MaxEntries entries were read. EntryLimit // TimeLimit means Timeout elapsed. TimeLimit // MatchLimit means MaxMatches matches were found. MatchLimit // Canceled means the context ended. Canceled )
type Match ¶ added in v0.26.0
type Match struct {
Path string
Dir bool
Link bool // a symbolic link, which is never followed; Dir is false for it
}
Match is one entry whose name holds the search text.
type Mode ¶ added in v0.16.0
type Mode int
Mode says which entries a Suggester offers.
const ( // Directories offers directories only, and completes only absolute and home-relative paths. Directories Mode = iota + 1 // Files offers directories and files, and also completes paths relative to the working directory. The // file itself may not exist yet, so only its directory part has to name a directory. Files )
type SearchOptions ¶ added in v0.26.0
type SearchOptions struct {
Kind Kind
MaxEntries int // entries read at most; default [DefaultMaxEntries]
Timeout time.Duration // time at most; default [DefaultTimeout]
MaxMatches int // matches at most; default [DefaultMaxMatches]
}
SearchOptions holds the options of Search. The zero value searches for both kinds with the default limits.
type SearchResult ¶ added in v0.26.0
type SearchResult struct {
Matches []Match
End End
Entries int // entries read
Skipped int // directories not entered: unreadable, on another device or excluded
}
SearchResult is the outcome of Search. Matches are ordered by Search. When End is not Complete, the matches are those found before the end, not necessarily the best ones.
func Search ¶ added in v0.26.0
func Search(ctx context.Context, start, text string, opt SearchOptions) (SearchResult, error)
Search finds the entries below start whose name holds text, by the rules of the package documentation. A start that is not a readable directory is an error; a search that ends early, by a limit or by ctx, is not: SearchResult.End says why it ended.
type Suggester ¶ added in v0.16.0
type Suggester struct {
// contains filtered or unexported fields
}
Suggester completes a typed file or directory path from the file system. Its cache belongs to the instance, so a new Suggester sees the file system anew. It is not safe for concurrent use.
func NewSuggester ¶ added in v0.16.0
NewSuggester returns a Suggester for mode. start is the single suggestion for an empty text, usually the directory a dialog opens in; workDir is what a relative path is resolved against in Files mode, and the process working directory when empty.