list

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Apr 17, 2024 License: MIT Imports: 17 Imported by: 0

README

list

Overview

list is a directory traversal program with built in searching, filtering, and sorting functionalities.

Usage:

list [OPTIONS]

Options

Traversal and filtering options are called at the same time. Then Processing options, then printing options. The order in which options are listed in this document mirrors the order in which they are evaluated or utilized.

Traversal options

Determines how the traversal is done.:
-r, --recurse Recursively list files in subdirectories. Directory traversal is done iteratively and breadth first.
-z Treat zip archives as directories.
-T, --todepth= List files to a certain depth. (default: 0)
-F, --fromdepth= List files from a certain depth. (default: -1)

Filtering options

Applied while traversing, called on every entry found.:
-s, --search= Only include items which have search terms as substrings. Can be used multiple times. Multiple values are inclusive by default. (OR)
--AND Including this flag makes search with multiple values conjuctive, i.e., all search terms must be matched. (AND)
-i, --include= File type inclusion. Can be used multiple times.
-e, --exclude= File type exclusion. Can be used multiple times.
[image|video|audio|archive|ziplike]
-I, --ignore= Ignores all paths which include any given strings.
--dirs Only include directories in the result.
--files Only include files in the result.

Processing options

Applied after traversal, called on the final list of files.:
-q, --query= Fuzzy search query. Results will be ordered by their score.
-a, --ascending Results will be ordered in ascending order.
-S, --sort= Sort the result by word. (default: none)
[none|name|n|mod|time|t|size|s|creation|c]
--select= Select a single element or a range of elements. Usage: [{index}] [{from}:{to}] [{from}:{to}={page}] Supports negative indexing, and relative + indexing. Can be used without a flag as the last argument.
--shuffle Randomly shuffle the result.
--seed= Seed for the random shuffle. (default: -1)

Printing options

Determines how the results are printed.:
-A, --absolute Format paths to be absolute. Relative by default.
-D, --debug Debug flag enables debug logging.
-Q, --quiet Quiet flag disables printing results.
-c, --clipboard Copy the result to the clipboard.
--tree Prints as tree.

Examples

All of the examples implicitly traverse from the current working directory ./. Traverse recursively and list last 10 results:
list -r [-10:]

Traverse recursively, listing only the files in the immediate subdirectories, but not their subdirectories:
list -T 2 -F 1

Traverse recursively, ignoring all directories with the substrings ".git" and ".thumbs", only including image files, only including files with the substring "_p01", traversing archives as directories, fuzzy searching with queries "picasso" and "museum" and sorting by score, and printing only the top 100 files with absolute paths:
list -rAz -I ".git" -I ".thumbs" -s "_p01" -q "picasso" -q "museum" -i image [:100]

Documentation

Index

Constants

View Source
const (
	Other    = "other"
	Image    = "image"
	Video    = "video"
	Audio    = "audio"
	Media    = "media"
	Archive  = "archive"
	ZipLike  = "zip"
	Code     = "code"
	Conf     = "conf"
	Docs     = "docs"
	OtherDev = "odev"

	MaskImage uint32
	MaskVideo
	MaskAudio
	MaskArchive
	MaskZipLike = 1<<iota + MaskArchive
	MaskCode    = 1 << iota
	MaskConf
	MaskDocs
	MaskOtherDev
)
View Source
const N = 3

Variables

View Source
var CntMap = map[string]uint32{}
View Source
var CntMasks = map[uint32][]string{
	MaskImage:    {".jpg", ".jpeg", ".png", ".apng", ".gif", ".bmp", ".webp", ".avif", ".jxl", ".tiff"},
	MaskVideo:    {".mp4", ".m4v", ".webm", ".mkv", ".avi", ".mov", ".mpg", ".mpeg"},
	MaskAudio:    {".m4a", ".opus", ".ogg", ".mp3", ".flac", ".wav", ".aac"},
	MaskArchive:  {".zip", ".rar", ".7z", ".tar", ".gz", ".bz2", ".xz", ".lz4", ".zst", ".lzma", ".lzip", ".lz", ".cbz"},
	MaskZipLike:  {".zip", ".cbz", ".cbr"},
	MaskCode:     {".go", ".c", ".h", ".cpp", ".hpp", ".rs", ".py", ".js", ".ts", ".html", ".css", ".scss", ".java", ".php"},
	MaskConf:     {".json", ".toml", ".yaml", ".yml", ".xml", ".ini", ".cfg", ".conf", ".properties", ".env"},
	MaskDocs:     {".pdf", ".epub", ".mobi", ".doc", ".docx", ".xls", ".xlsx", ".ppt", ".pptx", ".odt", ".ods", ".odp", ".txt", ".rtf", ".csv", ".tsv", ".md"},
	MaskOtherDev: {".sql", ".sh", ".bat", ".cmd", ".ps1", ".psm1", ".psd1", ".ps1xml", ".pssc", ".psc1", ".pssc", ".psh"},
}
View Source
var Hide = map[string]bool{
	"Thumbs.db":                 true,
	"desktop.ini":               true,
	"Icon\r":                    true,
	"System Volume Information": true,
	"$RECYCLE.BIN":              true,
	"lost+found":                true,
	"node_modules":              true,
}

Hide contains commonly unwanted files and directories. Any beginning with a dot hidden by default.

Functions

func AsMask

func AsMask(sar []string) uint32

func Exec added in v0.0.2

func Exec(res *Result, opts *Options)

func FileTraverser added in v0.0.2

func FileTraverser(opts *Options, rfn ResultFilters)

func GenNgrams

func GenNgrams(sar []string, n int) (map[string]int, int)

func GetScoringFunction

func GetScoringFunction(queries []string) func(string) float32

func Implicit added in v0.0.2

func Implicit(opts *Options)

func Initialize

func Initialize(opts *Options) (*Result, []Filter, []Process)

func PrintWithBuf

func PrintWithBuf(els []*Finfo, opts *Options)

func ProcessList

func ProcessList(res *Result, fns []Process)

func QuickCommand added in v0.0.2

func QuickCommand(arg string, opts *Options)

func Recurse added in v0.0.2

func Recurse(opts *Options)

func RegisterMasks

func RegisterMasks(mask uint32, keys ...string)

func Reverse

func Reverse[T any](filenames []T) []T

func SortByScore

func SortByScore[T any](files ScoredFiles[T])

func StrToMask

func StrToMask(str string) uint32

func TraverseArgs added in v0.0.2

func TraverseArgs(opts *Options, rfn ResultFilters)

func TraverseDir added in v0.0.2

func TraverseDir(path string, depth int, opts *Options) (files []fs.FileInfo)

func TraverseFS added in v0.0.2

func TraverseFS(opts *Options, rfn ResultFilters)

TraverseFS traverses directories non-recursively and breadth first.

func TraverseZip

func TraverseZip(path string, depth int, opts *Options) (files []fs.FileInfo)

Types

type Filter

type Filter func(*Finfo) bool

func CollectFilters

func CollectFilters(opts *Options) []Filter

func FilterList

func FilterList(opts *Options) Filter

type FilterOpts

type FilterOpts struct {
	Search    []string `` /* 170-byte string literal not displayed */
	SearchAnd bool     `` /* 136-byte string literal not displayed */
	Include   []string `short:"i" long:"include" description:"File type inclusion. Can be used multiple times."`
	Exclude   []string `short:"e" long:"exclude" description:"File type exclusion. Can be used multiple times."`
	Ignore    []string `short:"I" long:"ignore" description:"Ignores all paths which include any given strings."`

	DirOnly  bool `long:"dirs" description:"Only include directories in the result."`
	FileOnly bool `long:"files" description:"Only include files in the result."`
}

type Finfo

type Finfo struct {
	Name      string
	Path      string // includes name, relative path to cwd
	Vany      int64  // any numeric value, used for sorting
	Mask      uint32 // file kind, bitmask, see Mask* constants
	IsDir     bool
	IsArchive bool // is a readable archive; ziplike
}

func StringParser added in v0.0.2

func StringParser(s string) *Finfo

type FinfoParser added in v0.0.2

type FinfoParser func(string, fs.FileInfo) *Finfo

func InitFileParser added in v0.0.2

func InitFileParser(opts *Options) FinfoParser

type ListingOpts

type ListingOpts struct {
	Recurse   bool     `` /* 139-byte string literal not displayed */
	Archive   bool     `short:"z" description:"Treat zip archives as directories."`
	ToDepth   int      `short:"T" long:"todepth" description:"List files to a certain depth." default:"0"`
	FromDepth int      `short:"F" long:"fromdepth" description:"List files from a certain depth." default:"-1"`
	DirSearch []string `` /* 210-byte string literal not displayed */
	NoHide    bool     `short:"h" long:"hide" description:"Toggle of hiding of commonly unwanted files."`
	MaxLimit  int      `short:"m" long:"max" description:"Maximum number of elements traversed in a single directory. Unlimited by default."`
}

type ModeOpts added in v0.0.2

type ModeOpts struct {
	ArgMode  bool   `short:"l" long:"arg" description:"Skips listing, uses the input arguments as elements."`
	FileMode string `` /* 156-byte string literal not displayed */
}

type Options

type Options struct {
	ModeOpts    `group:"Mode options - Determines which mode list executes in, fs, string, file, etc."`
	ListingOpts `group:"Traversal options - Determines how the traversal is done."`
	FilterOpts  `group:"Filtering options - Applied while traversing, called on every entry found."`
	ProcessOpts `group:"Processing options - Applied after traversal, called on the final list of files."`
	Printing    `group:"Printing options - Determines how the results are printed."`

	ExecArgs []string
	Args     []string
}

func Parse

func Parse(args []string) *Options

type Printing

type Printing struct {
	Absolute bool `short:"A" long:"absolute" description:"Format paths to be absolute. Relative by default."`
	Debug    bool `short:"D" long:"debug" description:"Debug flag enables debug logging."`
	Quiet    bool `short:"Q" long:"quiet" description:"Quiet flag disables printing results."`
	Count    bool `short:"C" long:"count" description:"Print the number of results."`
	Tree     bool `long:"tree" description:"Prints as tree."`
}

type Process

type Process func(filenames []*Finfo) []*Finfo

func CollectProcess

func CollectProcess(opts *Options) []Process

func QueryProcess

func QueryProcess(opts *Options) Process

func ShuffleProcess

func ShuffleProcess(src rand.Source) Process

func SliceProcess

func SliceProcess(patterns []string) Process

func SortProcess

func SortProcess(sorting SortBy) Process

type ProcessOpts

type ProcessOpts struct {
	Query     []string `short:"q" long:"query" description:"Fuzzy search query. Results will be ordered by their score."`
	Ascending bool     `` /* 136-byte string literal not displayed */

	Sort string `` /* 206-byte string literal not displayed */

	Select []string `` /* 183-byte string literal not displayed */

	Shuffle bool  `long:"shuffle" description:"Randomly shuffle the result."`
	Seed    int64 `long:"seed" description:"Seed for the random shuffle." default:"-1"`
}

type Result

type Result struct{ Files []*Finfo }

func Do added in v0.0.2

func Do(args ...string) *Result

func Run

func Run(opts *Options) *Result

func (Result) Sar

func (r Result) Sar() []string

type ResultFilters added in v0.0.2

type ResultFilters func(*Finfo)

func InitFilters added in v0.0.2

func InitFilters(fns []Filter, res *Result) ResultFilters

type ScoredFiles

type ScoredFiles[T any] []scored[T]

func (ScoredFiles[T]) Items

func (s ScoredFiles[T]) Items() []T

Items returns all T with a score above 0.

type SortBy

type SortBy uint8
const (
	ByNone SortBy = iota
	ByMod
	BySize
	ByCreation
	ByName
)

func StrToSortBy

func StrToSortBy(s string) SortBy

type Traverser added in v0.0.2

type Traverser func(*Options, ResultFilters)

func GetTraverser added in v0.0.2

func GetTraverser(opts *Options) Traverser

type TreeNode

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

func AddFilesToTree

func AddFilesToTree(files []*Finfo) *TreeNode

func NewTreeNode

func NewTreeNode(name string) *TreeNode

func (*TreeNode) AddPath

func (t *TreeNode) AddPath(path string)

func (*TreeNode) PrintTree

func (t *TreeNode) PrintTree(prefix string)

Directories

Path Synopsis
cmd
list command
sm command
test command

Jump to

Keyboard shortcuts

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