tree

package module
v1.0.4 Latest Latest
Warning

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

Go to latest
Published: Jul 18, 2020 License: MIT Imports: 16 Imported by: 0

README

tree License

An implementation of the tree command written in Go, that can be used programmatically.

Based off of github.com/a8m/tree, but with a bunch of UI changes.

tree command output
Installation:
$ go get github.com/james-antill/tree/cmd/tree

Next

The next big thing is to get dircolors support included.

License

MIT

Documentation

Index

Constants

View Source
const (
	Reset int = 0
	// Not used, remove.
	Bold  int = 1
	Black int = iota + 28
	Red
	Green
	Yellow
	Blue
	Magenta
	Cyan
	White
)
View Source
const (
	KB = 1 * 1000
	MB = KB * 1000
	GB = MB * 1000
	TB = GB * 1000
	PB = TB * 1000
	EB = PB * 1000
)

KB = 1000 bytes

View Source
const (
	KiB int64 = 1 << (10 * iota)
	MiB
	GiB
	TiB
	PiB
	EiB
)

Do it using KiB format, so KB = 1024 ...

View Source
const Escape = "\x1b"

Variables

This section is empty.

Functions

func ANSIColor

func ANSIColor(node *Node, s string) string

ANSIColor

func CTimeSort

func CTimeSort(nf1, nf2 *Node) bool

func DirRecursiveSize added in v1.0.3

func DirRecursiveSize(node *Node) (size int64, err error)

DirRecursiveSize returns the size of the directory, as the total of all child nodes.

func DirSort

func DirSort(nf1, nf2 *Node, nxt SortFunc) bool

This is a secondary sort function...

func FormatSize added in v1.0.3

func FormatSize(opts *Options, size int64) string

FormatSize as a string

func ModSort

func ModSort(nf1, nf2 *Node) bool

func NameSort

func NameSort(f1, f2 *Node) bool

func NaturalLess

func NaturalLess(str1, str2 string) bool

NaturalLess compares two strings using natural ordering. This means that e.g. "abc2" < "abc12".

Non-digit sequences and numbers are compared separately. The former are compared bytewise, while the latter are compared numerically (except that the number of leading zeros is used as a tie-breaker, so e.g. "2" < "02")

Limitation: only ASCII digits (0-9) are considered. Code taken from: https://github.com/fvbommel/util/blob/master/sortorder/natsort.go

func NodeSize added in v1.0.3

func NodeSize(node *Node) int64

NodeSize returns the size of the directory/file, errors are ignored.

func SizeSort

func SizeSort(f1, f2 *Node) bool

func VerSort

func VerSort(f1, f2 *Node) bool

Types

type ByFunc

type ByFunc struct {
	Nodes
	Fn SortFunc
}

func (ByFunc) Less

func (b ByFunc) Less(i, j int) bool

type Fs

type Fs interface {
	Stat(path string) (os.FileInfo, error)
	ReadDir(path string) ([]string, error)
}

To use this package programmatically, you must implement this interface. For example: PTAL on 'cmd/tree/tree.go'

type Node

type Node struct {
	os.FileInfo
	// contains filtered or unexported fields
}

Node represent some node in the tree contains FileInfo, and its childs

func New

func New(path string) *Node

New get path and create new node(root).

func (*Node) Print

func (node *Node) Print(opts *Options)

Print nodes based on the given configuration.

func (*Node) Visit

func (node *Node) Visit(opts *Options) (dirs, files int)

Visit all files under the given node.

type Nodes

type Nodes []*Node

List of nodes

func (Nodes) Len

func (n Nodes) Len() int

func (Nodes) Swap

func (n Nodes) Swap(i, j int)

type Options

type Options struct {
	Fs      Fs
	OutFile io.Writer
	// List
	All        bool
	DirsOnly   bool
	FullPath   bool
	IgnoreCase bool
	FollowLink bool
	DeepLevel  int
	Pattern    string
	IPattern   string
	// File
	ByteSize bool
	UnitSize bool
	FileMode bool
	ShowUid  bool
	ShowGid  bool
	LastMod  bool
	Quotes   bool
	Inodes   bool
	Device   bool
	// Sort
	NoSort    bool
	VerSort   bool
	ModSort   bool
	DirSort   bool
	NameSort  bool
	SizeSort  bool
	CTimeSort bool
	ReverSort bool
	// Graphics
	NoIndent   bool
	Colorize   bool
	JoinSingle bool
	// contains filtered or unexported fields
}

Options store the configuration for specific tree. Note, that 'Fs', and 'OutFile' are required (OutFile can be os.Stdout).

type SortFunc

type SortFunc func(f1, f2 *Node) bool

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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