dirsort

package
v0.5.39 Latest Latest
Warning

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

Go to latest
Published: Nov 15, 2026 License: BSD-3-Clause Imports: 21 Imported by: 3 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 dirsort provides the sort order of the files in each directory, shared by all of the apps (and all of their processes) that show directories: For returns the sort order of a directory, which is the one last Set for it in any app, or else the one that the file manager of the operating system uses for it (the Finder on macOS, from its .DS_Store files, and Dolphin or Nautilus on Linux; see Native), or else Default. The sort orders that are set are saved in a file in DataDir, which is locked for the changes of each process, and read again when another process has changed it.

Index

Constants

View Source
const FileName = "dir-sort.json"

FileName is the name of the file of the sort orders in DataDir.

Variables

View Source
var DataDir string

DataDir is the directory of the file of the sort orders that are Set, which is shared by all apps: core sets it to the Cogent Core data directory. If it is empty, the sort orders are only kept in memory.

View Source
var Default = Sort{}

Default is the sort order of a directory that has none (see For): by name, up.

View Source
var MaxDirs = 5000

MaxDirs is the maximum number of directories whose sort orders are saved: those set least recently are dropped beyond it.

View Source
var Native = func(dir string) (Sort, bool) {
	dir = cleanDir(dir)
	switch runtime.GOOS {
	case "darwin":
		return finderSort(dir)
	case "linux", "freebsd", "openbsd", "netbsd":
		return linuxSort(dir)
	}
	return Sort{}, false
}

Native returns the sort order that the file manager of the operating system uses for the given directory, and whether there is one: that of the directory, or else the default of the file manager.

  • macOS: the Finder, from the view settings in the .DS_Store file of the directory, or else its standard view settings (see [finderSort]).
  • Linux: Dolphin (KDE), from the .directory file of the directory, and Nautilus (GNOME), from the metadata of the directory, or else their defaults (see [linuxSort]).
  • Windows: none, as Explorer keeps the views of folders in the registry, in binary records ("shell bags") that are not documented.

It can be set to nil for none, as core does in tests.

Functions

func Set

func Set(dir string, s Sort) error

Set sets the sort order of the given directory, for all apps, saving it in the file shared by them (see DataDir).

Types

type Fields

type Fields int32 //enums:enum

Fields are the fields of the files in a directory that they can be sorted by, which are named as the fields of fileinfo.FileInfo.

const (
	// Name sorts the files by their names.
	Name Fields = iota

	// Size sorts the files by their sizes.
	Size

	// Kind sorts the files by their kinds (as Folder, or Code: Go).
	Kind

	// ModTime sorts the files by the time that they were last modified.
	ModTime
)
const FieldsN Fields = 4

FieldsN is the highest valid value for type Fields, plus one.

func FieldsValues

func FieldsValues() []Fields

FieldsValues returns all possible values for the type Fields.

func (Fields) Desc

func (i Fields) Desc() string

Desc returns the description of the Fields value.

func (Fields) Int64

func (i Fields) Int64() int64

Int64 returns the Fields value as an int64.

func (Fields) MarshalText

func (i Fields) MarshalText() ([]byte, error)

MarshalText implements the encoding.TextMarshaler interface.

func (*Fields) SetInt64

func (i *Fields) SetInt64(in int64)

SetInt64 sets the Fields value from an int64.

func (*Fields) SetString

func (i *Fields) SetString(s string) error

SetString sets the Fields value from its string representation, and returns an error if the string is invalid.

func (Fields) String

func (i Fields) String() string

String returns the string representation of this Fields value.

func (*Fields) UnmarshalText

func (i *Fields) UnmarshalText(text []byte) error

UnmarshalText implements the encoding.TextUnmarshaler interface.

func (Fields) Values

func (i Fields) Values() []enums.Enum

Values returns all possible values for the type Fields.

type Sort

type Sort struct {

	// By is the field that the files are sorted by.
	By Fields

	// Down is whether the files are sorted down (descending: Z to A,
	// largest or newest first), instead of up (ascending).
	Down bool
}

Sort is a sort order of the files in a directory: by the given field, up (ascending: A to Z, smallest or oldest first) or down (descending).

func For

func For(dir string) Sort

For returns the sort order of the given directory: the one last Set for it, by any app, or else the one of the file manager of the operating system (see Native), or else Default.

func Parse

func Parse(str string) (Sort, bool)

Parse returns the sort order of the given string, as returned by Sort.String (where :up is the default), and whether it is one.

func Saved

func Saved(dir string) (Sort, bool)

Saved returns the sort order last Set for the given directory, by any app, and whether there is one.

func (Sort) Compare

func (s Sort) Compare(a, b *fileinfo.FileInfo) int

Compare compares the given files in the sort order, returning -1 if a comes before b, and 1 if after, for slices.SortFunc. Files that are equal in the field of the sort order are in the order of their names (in the direction of the sort order).

func (Sort) SortFiles

func (s Sort) SortFiles(files []*fileinfo.FileInfo)

SortFiles sorts the given files in the sort order.

func (Sort) SortInfos

func (s Sort) SortInfos(infos []fs.FileInfo)

SortInfos sorts the given standard file infos in the sort order, as those of a directory listing. Their kinds are those of their names (see fileinfo.FileInfo.MimeFromFilename), without reading the files.

func (Sort) String

func (s Sort) String() string

String returns the sort order as the name of its field and :up or :down, as [core.Table.SortFieldName] does.

Jump to

Keyboard shortcuts

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