filesview

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Nov 14, 2026 License: MIT Imports: 23 Imported by: ? 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 filesview owns the Files tool of the /files page (0.9 P4, N20): lazy direct-directory listing, directories-first stable sort, symlink no-recursive-follow, ignore semantics (fixed names + git-owned paths), bounded local file operations, the 1 MiB preview cap with text/binary classification, and copy-path/reveal helpers. Opening the panel performs zero repository-wide recursive scans; only a search the user starts reads the whole tree (search.go: file index + ripgrep/git grep content search).

Index

Constants

View Source
const (
	// SearchDeadline bounds one search or file listing.
	SearchDeadline = 15 * time.Second
	// MaxIndexedFiles caps the listed files of one root.
	MaxIndexedFiles = 100000
	// MaxContentMatches caps the matches one content search returns.
	MaxContentMatches = 500
	// MaxMatchesPerFile caps the matches kept from one file.
	MaxMatchesPerFile = 20
	// MaxLineChars caps the characters of a matching line that are shown.
	MaxLineChars = 300
	// MinContentQuery is the shortest pattern a content search runs for.
	MinContentQuery = 2
)
View Source
const IgnoreDeadline = 5 * time.Second

IgnoreDeadline bounds the git ignore lookup; gitintel uses the same discipline. A non-repository root fails fast with an empty set.

View Source
const ImagePreviewCap = 24 << 20

ImagePreviewCap is the largest image the WebView tier takes as a data URL (24 MiB; text keeps the 1 MiB PreviewCap). Beyond it the Native UI tier renders.

View Source
const MaxNativeImagePixels = 32 << 20

MaxNativeImagePixels bounds the Native tier's decoded bitmap: 32M pixels (a 128 MiB RGBA buffer). DecodeConfig rejects insanity before the full decode allocates.

View Source
const PreviewCap = 1024 * 1024

PreviewCap is the 1 MiB maximum size for a file preview (WIX-055): what the WebView page takes of a Markdown file to render.

View Source
const TextPreviewCap = 8 << 20

TextPreviewCap bounds what the native code view takes of a file: the framework's editor virtualizes its lines, so text is shown to 8 MiB, past which the head shows with Truncated set.

Variables

View Source
var ErrBadName = errors.New("invalid name")

ErrBadName reports a name that cannot be one child entry.

View Source
var ErrDimensionsTooLarge = errors.New("image dimensions exceed the preview budget")

ErrDimensionsTooLarge reports an image whose decoded size exceeds the Native tier's pixel budget.

View Source
var ErrNoContentEngine = errors.New("Content search needs ripgrep installed, or a Git repository")

ErrNoContentEngine reports that neither ripgrep nor a Git repository is available to search the root's content.

View Source
var ErrNotADirectory = errors.New("path is not a directory")

ErrNotADirectory reports attempting to list a non-directory.

View Source
var ErrTooLarge = errors.New("file is too large for the web preview")

ErrTooLarge reports a file beyond its flavor's WebView cap; the caller falls back to the Native UI tier.

Functions

func CreateDir

func CreateDir(root, dirRel, name string) (string, error)

CreateDir creates one directory inside dirRel (single level, so a typo cannot build an unexpected hierarchy). The name must not exist.

func CreateFile

func CreateFile(root, dirRel, name string) (string, error)

CreateFile creates one empty file inside dirRel. The name must not exist. Returns the created absolute path.

func DecodeImageBounded

func DecodeImageBounded(path string) (image.Image, error)

DecodeImageBounded decodes an image for the Native UI tier (the WebView tier already refused it as too large): the pixel budget is checked via DecodeConfig before the full decode allocates. Runs off the UI lane; the caller wraps the result in ui.NewBitmap on the UI lane.

func DecodeImageBytes

func DecodeImageBytes(raw []byte) (image.Image, error)

DecodeImageBytes is DecodeImageBounded for bytes already in hand (a Git blob): the same pixel budget, checked before the full decode allocates.

func Delete

func Delete(root, rel string) (string, error)

Delete removes the entry at rel. Directories are removed recursively (the UI confirms first); a symlink is unlinked, never followed.

func FormatFileSize

func FormatFileSize(bytes int64) string

FormatFileSize returns a compact, human-readable size string.

func GitIgnorePaths

func GitIgnorePaths(ctx context.Context, root string) ([]string, error)

GitIgnorePaths returns the root's ignored paths as slash-separated relative paths, directories without a trailing slash (--directory folds node_modules into one line instead of enumerating its contents). -z keeps paths verbatim: no C-style quoting for non-ASCII (core.quotePath), no whitespace mangling. Any git failure — not a repository, no git binary — yields an empty list and an error; the caller keeps the fixed-name set.

func ImageMIME

func ImageMIME(name string) string

ImageMIME returns the data-URL media type for an image filename.

func IsBinary

func IsBinary(buf []byte) bool

IsBinary detects binary content (WIX-054): contains NUL bytes in the first 8 KiB or is not valid UTF-8.

func MatchFiles

func MatchFiles(paths []string, query string, limit int) (hits []string, total int)

MatchFiles ranks the paths that match every word of the query, best first. A word matches as a substring of the path or, failing that, as a subsequence of it; a hit in the file's own name beats one in a folder, and a shorter path beats a longer one. total is the count before limit.

func ReadImageDataURL

func ReadImageDataURL(path string) (string, error)

ReadImageDataURL reads an image up to ImagePreviewCap and returns it as a data URL for the WebView tier's <img>.

func Rename

func Rename(root, rel, newName string) (string, error)

Rename renames the entry at rel within its own directory. A distinct existing target is refused rather than silently replaced (os.Rename would overwrite it); a case-only variant of the same name is allowed through — on case-insensitive filesystems (default APFS) it collides in Lstat but is the ordinary "fix the casing" rename.

func ResolveRipgrep

func ResolveRipgrep() string

ResolveRipgrep finds ripgrep: PATH first, then the places a Finder-launched app (which does not inherit the shell's PATH) will not look on its own. Empty when it is not installed.

func SortEntries

func SortEntries(entries []Entry)

SortEntries applies the stable WIX-051 order: directories first, then alphabetical case-insensitive.

func ValidateName

func ValidateName(name string) error

ValidateName refuses names that are blank, carry path separators (on any platform, so a cross-platform tree cannot create names Windows breaks on) or NUL, or are relative path segments themselves.

Types

type ContentFile

type ContentFile struct {
	Path    string // slash-separated, relative to the root
	Matches []ContentMatch
	More    bool // more lines matched than MaxMatchesPerFile
}

ContentFile is the matches of one file.

type ContentMatch

type ContentMatch struct {
	Line       int // 1-based
	Text       string
	Start, End int
	Col        int
}

ContentMatch is one matching line. Text is the line without its leading whitespace, cut to MaxLineChars; Start and End are the first match's byte range in Text (-1 when it cannot be located); Col is that match's byte offset in the whole line, for opening the file on it.

type ContentQuery

type ContentQuery struct {
	Pattern        string
	CaseSensitive  bool
	WholeWord      bool
	Regex          bool
	IncludeIgnored bool
}

ContentQuery is one full-text search over a root.

type ContentResult

type ContentResult struct {
	Files     []ContentFile
	Matches   int
	Truncated bool   // MaxContentMatches reached
	Engine    string // "ripgrep" or "git grep"
}

ContentResult is a finished search.

func SearchContent

func SearchContent(ctx context.Context, root string, q ContentQuery) (ContentResult, error)

SearchContent runs a full-text search under root. A pattern shorter than MinContentQuery yields no result.

type Entry

type Entry struct {
	Name      string    `json:"name"`
	Path      string    `json:"path"` // absolute path
	RelPath   string    `json:"rel_path"`
	Kind      EntryKind `json:"kind"`
	SizeBytes int64     `json:"size_bytes"`
	IsDir     bool      `json:"is_dir"`
	IsSymlink bool      `json:"is_symlink"`
	// TargetDir marks a symlink pointing to a directory; the no-recursive-
	// follow rule prevents expanding it in the tree (WIX-052).
	TargetDir bool `json:"target_dir"`
}

Entry is one direct child of a listed directory.

func ListDirect

func ListDirect(dir string, root string) ([]Entry, error)

ListDirect lists only the immediate children of dir (no recursion; WIX-050). Results sort directories first, then alphabetical case- insensitive (WIX-051). Hidden files/directories (.git, .DS_Store, ...) stay visible by default so developers see project realities.

type EntryKind

type EntryKind string

EntryKind distinguishes directory nodes from files and symlinks.

const (
	KindDir     EntryKind = "dir"
	KindFile    EntryKind = "file"
	KindSymlink EntryKind = "symlink"
)

type FileIndex

type FileIndex struct {
	Paths     []string
	Truncated bool // more than MaxIndexedFiles existed
}

FileIndex is a root's files as slash-separated relative paths.

func ListFiles

func ListFiles(ctx context.Context, root string, includeIgnored bool) (FileIndex, error)

ListFiles lists the files under root: the ones Git tracks plus the ones it does not ignore, minus the tree's fixed heavy names; includeIgnored adds what the rules hide. A root outside any repository is listed by ripgrep when it is installed.

type Flavor

type Flavor string

Flavor names the three render paths a preview can take.

const (
	FlavorText     Flavor = "text"
	FlavorMarkdown Flavor = "markdown"
	FlavorImage    Flavor = "image"
)

func PreviewFlavor

func PreviewFlavor(name string) Flavor

PreviewFlavor maps a filename to its preview flavor. Everything that is neither Markdown nor an image previews as text (the code view highlights by extension; unknown extensions render plain).

type IgnoreSet

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

IgnoreSet answers one question for the tree renderer: is this entry hidden? It merges the fixed names with git's ignored paths for the root.

func NewIgnoreSet

func NewIgnoreSet(gitPaths []string) *IgnoreSet

NewIgnoreSet builds the merged set from git's ignored relative paths.

func (*IgnoreSet) Ignored

func (s *IgnoreSet) Ignored(name, relPath string) bool

Ignored reports whether the entry hides: by its own name (any depth), by an exact git-ignored relative path, or inside a git-ignored directory.

type Preview

type Preview struct {
	Path      string `json:"path"`
	SizeBytes int64  `json:"size_bytes"`
	IsBinary  bool   `json:"is_binary"`
	Truncated bool   `json:"truncated"`
	Content   string `json:"content"`
}

Preview carries the read-only content of one inspected file.

func ReadPreview

func ReadPreview(path string, root ...string) (Preview, error)

ReadPreview reads a file up to PreviewCap (1 MiB; WIX-055), classifying text vs binary (WIX-054). Truncated is true when the file exceeds 1 MiB. If root is provided, path is verified to stay within root (symlink escape guard).

func ReadText

func ReadText(path string, root ...string) (Preview, error)

ReadText is ReadPreview for the native code view: the same classification and escape guard, up to TextPreviewCap.

Jump to

Keyboard shortcuts

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