tree

package module
v0.0.0-...-2c8764a Latest Latest
Warning

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

Go to latest
Published: Jan 4, 2024 License: MIT Imports: 12 Imported by: 19

README

tree Build status License

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

tree command
Installation:
$ go get github.com/a8m/tree/cmd/tree
How to use tree programmatically ?

You can take a look on cmd/tree, and s3tree or see the example below.

import (
    "github.com/a8m/tree"
)

func main() {
    opts := &tree.Options{
        // Fs, and OutFile are required fields.
        // fs should implement the tree file-system interface(see: tree.Fs),
        // and OutFile should be type io.Writer
        Fs: fs,
        OutFile: os.Stdout,
        // ...
    }
    inf.New("root-dir")
    // Visit all nodes recursively
    inf.Visit(opts)
    // Print nodes 
    inf.Print(opts)
}

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 int64 = 1 << (10 * iota)
	MB
	GB
	TB
	PB
	EB
)
View Source
const Escape = "\x1b"

Variables

This section is empty.

Functions

func ANSIColor

func ANSIColor(node *Node, s string) string

ANSIColor

func ANSIColorFormat

func ANSIColorFormat(style string, s string) string

ANSIColorFormat

func CTimeSort

func CTimeSort(f1, f2 os.FileInfo) bool

func DirSort

func DirSort(f1, f2 os.FileInfo) bool

func ModSort

func ModSort(f1, f2 os.FileInfo) bool

func NameSort

func NameSort(f1, f2 os.FileInfo) 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 SizeSort

func SizeSort(f1, f2 os.FileInfo) bool

func VerSort

func VerSort(f1, f2 os.FileInfo) 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) Path

func (node *Node) Path() string

Path returns the Node's absolute path

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
	MatchDirs  bool
	Prune      bool
	// 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
	// Color defaults to ANSIColor()
	Color func(*Node, string) string
	Now   time.Time
}

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 os.FileInfo) bool

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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