Documentation ¶
Overview ¶
Package asciitree builds and prints directory trees using ASCII art.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Node ¶
type Node struct { // Name is the name of the directory or file described by the node. Name string // IsDir identifies whether the node describes a directory. IsDir bool // Children is a slice of all the immediate descendants of the node. Children []*Node }
Node describes a directory tree node. It can be either a directory or a file, depending on the IsDir value.
func NewDir ¶
NewDir creates a directory node.
Example ¶
package main import ( "fmt" "github.com/borodean/asciitree" ) func main() { dir := asciitree.NewDir("albums") fmt.Printf("%#v", dir) }
Output: &asciitree.Node{Name:"albums", IsDir:true, Children:[]*asciitree.Node(nil)}
func NewFile ¶
NewFile creates a file node.
Example ¶
package main import ( "fmt" "github.com/borodean/asciitree" ) func main() { file := asciitree.NewFile("ONUKA.jpg") fmt.Printf("%#v", file) }
Output: &asciitree.Node{Name:"ONUKA.jpg", IsDir:false, Children:[]*asciitree.Node(nil)}
func (*Node) Add ¶
Add places the given nodes under the current node and returns the current node.
Example ¶
package main import ( "fmt" "github.com/borodean/asciitree" ) func main() { tree := asciitree.NewDir("albums") tree.Add(asciitree.NewDir("VIDLIK"), asciitree.NewFile("ONUKA.jpg"), asciitree.NewFile(".DS_Store")) fmt.Println(tree) }
Output: albums ├── VIDLIK ├── ONUKA.jpg └── .DS_Store
func (*Node) AddDir ¶
AddDir creates a directory node under the current node and returns the newly created node.
Example ¶
package main import ( "fmt" "github.com/borodean/asciitree" ) func main() { tree := asciitree.NewDir("albums") dir := tree.AddDir("VIDLIK") fmt.Println(tree) fmt.Println("--") fmt.Println(dir) }
Output: albums └── VIDLIK -- VIDLIK
func (*Node) AddDirs ¶
AddDirs creates one or more directory nodes under the current node and returns the current node.
Example ¶
package main import ( "fmt" "github.com/borodean/asciitree" ) func main() { tree := asciitree.NewDir("albums") tree.AddDirs("VIDLIK", "KOLIR") fmt.Println(tree) }
Output: albums ├── VIDLIK └── KOLIR
func (*Node) AddFile ¶
AddFile creates a file node under the current node and returns the newly created node.
Example ¶
package main import ( "fmt" "github.com/borodean/asciitree" ) func main() { tree := asciitree.NewDir("albums") file := tree.AddDir("ONUKA.jpg") fmt.Println(tree) fmt.Println("--") fmt.Println(file) }
Output: albums └── ONUKA.jpg -- ONUKA.jpg
func (*Node) AddFiles ¶
AddFiles creates one or more file nodes under the current node and returns the current node.
Example ¶
package main import ( "fmt" "github.com/borodean/asciitree" ) func main() { tree := asciitree.NewDir("albums") tree.AddFiles(".DS_Store", "ONUKA.jpg") fmt.Println(tree) }
Output: albums ├── .DS_Store └── ONUKA.jpg
func (*Node) Sort ¶
func (n *Node) Sort(opts ...SortOption) *Node
Sort recursively sorts the node's descendants alphanumerically and returns the current node.
Example ¶
package main import ( "fmt" "github.com/borodean/asciitree" ) func main() { tree := asciitree.NewDir("albums").Add( asciitree.NewFile(".DS_Store"), asciitree.NewFile("ONUKA.jpg"), asciitree.NewDir("VIDLIK").AddFiles( "Svitanok.mp3", "Vidlik.mp3", "Other (Intro).mp3", "Other.mp3", "19 86.mp3", ), asciitree.NewDir("KOLIR").AddFiles( "VSTUP.mp3", "CEAHC.mp3", "ZENIT.mp3", "UYAVY (feat. DakhaBrakha).mp3", "TY.mp3", "GUMA.mp3", "XASHI.mp3", "NA SAMOTI.mp3", "SON.mp3", "23: 42.mp3", ), ) fmt.Println(tree.Sort()) }
Output: albums ├── .DS_Store ├── KOLIR │ ├── 23: 42.mp3 │ ├── CEAHC.mp3 │ ├── GUMA.mp3 │ ├── NA SAMOTI.mp3 │ ├── SON.mp3 │ ├── TY.mp3 │ ├── UYAVY (feat. DakhaBrakha).mp3 │ ├── VSTUP.mp3 │ ├── XASHI.mp3 │ └── ZENIT.mp3 ├── ONUKA.jpg └── VIDLIK ├── 19 86.mp3 ├── Other (Intro).mp3 ├── Other.mp3 ├── Svitanok.mp3 └── Vidlik.mp3
Example (WithDirsFirst) ¶
To place directories before files, enable the WithDirsFirst option.
package main import ( "fmt" "github.com/borodean/asciitree" ) func main() { tree := asciitree.NewDir("albums").Add( asciitree.NewFile(".DS_Store"), asciitree.NewFile("ONUKA.jpg"), asciitree.NewDir("VIDLIK").AddFiles( "Svitanok.mp3", "Vidlik.mp3", "Other (Intro).mp3", "Other.mp3", "19 86.mp3", ), asciitree.NewDir("KOLIR").AddFiles( "VSTUP.mp3", "CEAHC.mp3", "ZENIT.mp3", "UYAVY (feat. DakhaBrakha).mp3", "TY.mp3", "GUMA.mp3", "XASHI.mp3", "NA SAMOTI.mp3", "SON.mp3", "23: 42.mp3", ), ) fmt.Println(tree.Sort(asciitree.WithDirsFirst(true))) }
Output: albums ├── KOLIR │ ├── 23: 42.mp3 │ ├── CEAHC.mp3 │ ├── GUMA.mp3 │ ├── NA SAMOTI.mp3 │ ├── SON.mp3 │ ├── TY.mp3 │ ├── UYAVY (feat. DakhaBrakha).mp3 │ ├── VSTUP.mp3 │ ├── XASHI.mp3 │ └── ZENIT.mp3 ├── VIDLIK │ ├── 19 86.mp3 │ ├── Other (Intro).mp3 │ ├── Other.mp3 │ ├── Svitanok.mp3 │ └── Vidlik.mp3 ├── .DS_Store └── ONUKA.jpg
func (*Node) String ¶
String returns the ASCII art representation of the directory tree described by the current node and its descendants.
Example ¶
package main import ( "fmt" "github.com/borodean/asciitree" ) func main() { tree := asciitree.NewDir("albums").Add( asciitree.NewFile(".DS_Store"), asciitree.NewFile("ONUKA.jpg"), asciitree.NewDir("VIDLIK").AddFiles( "Svitanok.mp3", "Vidlik.mp3", "Other (Intro).mp3", "Other.mp3", "19 86.mp3", ), asciitree.NewDir("KOLIR").AddFiles( "VSTUP.mp3", "CEAHC.mp3", "ZENIT.mp3", "UYAVY (feat. DakhaBrakha).mp3", "TY.mp3", "GUMA.mp3", "XASHI.mp3", "NA SAMOTI.mp3", "SON.mp3", "23: 42.mp3", ), ) // asciitree.Node implements the fmt.Stringer interface; hence the String call // can be omitted here to yield the same result: // fmt.Println(tree) fmt.Println(tree.String()) }
Output: albums ├── .DS_Store ├── ONUKA.jpg ├── VIDLIK │ ├── Svitanok.mp3 │ ├── Vidlik.mp3 │ ├── Other (Intro).mp3 │ ├── Other.mp3 │ └── 19 86.mp3 └── KOLIR ├── VSTUP.mp3 ├── CEAHC.mp3 ├── ZENIT.mp3 ├── UYAVY (feat. DakhaBrakha).mp3 ├── TY.mp3 ├── GUMA.mp3 ├── XASHI.mp3 ├── NA SAMOTI.mp3 ├── SON.mp3 └── 23: 42.mp3
type SortOption ¶
type SortOption interface {
// contains filtered or unexported methods
}
SortOption describes a functional option that configures the Sort method.
func WithDirsFirst ¶
func WithDirsFirst(value bool) SortOption
WithDirsFirst configures the Sort method to place directories before files.