list

package module
v0.0.1 Latest Latest
Warning

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

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

README

list: Filesystem listing program

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.
-F, --fromdepth: List files from a certain depth.

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: image, video, audio. Can be used multiple times.
-e, --exclude=: File type exclusion: image, video, audio. Can be used multiple times.
-I, --ignore: Ignores all paths which include any given strings. Can be used multiple times.
--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. Can be used multiple times.
-a, --ascending Results will be ordered in ascending order. Files are ordered into descending order by default.
--sort= Sort the result by word. Choices: [name|date|mod|size|none]. Date and mod are the same. (default: none)
--select=: Select a single element or a range of elements. Usage: [{index}] or [{from}:{to}] Supports negative indexing. Can be used without a flag as the last argument.

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.

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"
	Archive = "archive"
	ZipLike = "zip"

	MaskImage uint32
	MaskVideo
	MaskAudio
	MaskArchive
	MaskZipLike = 1<<iota + MaskArchive
)
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"},
}

Functions

func AsMask

func AsMask(sar []string) uint32

func BuildWalkDirFn

func BuildWalkDirFn(fns []Filter, res *Result) func(string, fs.DirEntry, error) error

func Clamp

func Clamp[T constraints.Ordered](val, lower, upper T) (res T)

func GenNgrams

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

func GetScoringFunction

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

func ImplicitSlice

func ImplicitSlice(opts *Options)

func Initialize

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

func PrintWithBuf

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

func ProcessList

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

func RegisterMasks

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

func Reverse

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

func Slice

func Slice[T any](pat string, input []T) (_ []T, err error)

func SortByScore

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

func StrToMask

func StrToMask(str string) uint32

func Traverse

func Traverse(wfn fs.WalkDirFunc, opts *Options)

Traverse traverses directories non-recursively and breadth first.

func TraverseZip

func TraverseZip(path string, depth int, wfn fs.WalkDirFunc, opts *Options)

Types

type Filter

type Filter func(*Finfo, fs.DirEntry) 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: image, video, audio. Can be used multiple times."`
	Exclude   []string `short:"e" long:"exclude" description:"File type exclusion: image, video, audio. 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
}

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"`
}

type Options

type Options struct {
	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."`

	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."`
	Clipboard bool `short:"c" long:"clipboard" description:"Copy the result to the clipboard."`
	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(pattern 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 Run

func Run(opts *Options) *Result

func (Result) Sar

func (r Result) Sar() []string

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

type ZipEntry

type ZipEntry struct{ *zip.File }

func (ZipEntry) Info

func (z ZipEntry) Info() (fs.FileInfo, error)

func (ZipEntry) IsDir

func (z ZipEntry) IsDir() bool

func (ZipEntry) Name

func (z ZipEntry) Name() string

func (ZipEntry) Type

func (z ZipEntry) Type() fs.FileMode

Directories

Path Synopsis
cmd
sm

Jump to

Keyboard shortcuts

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