projectpath

package
v0.27.0 Latest Latest
Warning

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

Go to latest
Published: Nov 27, 2026 License: AGPL-3.0 Imports: 20 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

Index

Constants

View Source
const (
	DefaultMaxEntries = 200_000
	DefaultTimeout    = 3 * time.Second
	DefaultMaxMatches = 50
)

Default limits of Search, used for every limit of SearchOptions that is zero.

View Source
const MaxSuggestions = 100

MaxSuggestions bounds the suggestions returned for one typed text.

Variables

This section is empty.

Functions

func Canonical

func Canonical(dir string) string

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

func Expand(entry string) (string, error)

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

func IsHomeRelative(entry string) bool

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

func Roots(dir string) []string

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

func SplitSearch(typed string) (start, text string, ok bool)

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

func Within(dir, base string) bool

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
)

func (End) String added in v0.26.0

func (e End) String() string

String returns the name of e.

type Kind added in v0.26.0

type Kind int

Kind says which entries Search reports.

const (
	// Both reports directories and files. It is the zero value.
	Both Kind = iota
	// OnlyDirectories reports directories only.
	OnlyDirectories
	// OnlyFiles reports everything that is not a directory, links included.
	OnlyFiles
)

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(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

func NewSuggester(mode Mode, start, workDir string) *Suggester

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.

func (*Suggester) Suggest added in v0.16.0

func (s *Suggester) Suggest(typed string) []string

Suggest returns at most MaxSuggestions completions of typed by the rules of the package documentation.

Jump to

Keyboard shortcuts

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