asciitree

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Dec 27, 2021 License: MIT Imports: 3 Imported by: 2

README

Go Reference Codacy Quality Codacy Coverage

asciitree

Human-friendly Go module that builds and prints directory trees using ASCII art.

Installation

go get github.com/borodean/asciitree

Usage

// Build a directory tree representation:
tree := asciitree.NewDir("albums").Add(
  asciitree.NewFile("ONUKA.jpg"),
  asciitree.NewDir("VIDLIK").AddFiles(
    "Svitanok.mp3",
    "Vidlik.mp3",
  ),
  asciitree.NewDir("KOLIR").AddFiles(
    "CEAHC.mp3",
    "ZENIT.mp3",
    "UYAVY (feat. DakhaBrakha).mp3",
    "XASHI.mp3",
  ),
)

// Sort the tree's descendants alphanumerically while placing directories
// before files:
tree.Sort(asciitree.WithDirsFirst(true))

// Print an ASCII art representation of the directory tree:
fmt.Println(tree)

License

MIT.

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

func NewDir(name string) *Node

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

func NewFile(name string) *Node

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

func (n *Node) Add(nodes ...*Node) *Node

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

func (n *Node) AddDir(name string) *Node

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

func (n *Node) AddDirs(names ...string) *Node

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

func (n *Node) AddFile(name string) *Node

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

func (n *Node) AddFiles(names ...string) *Node

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

func (n *Node) String() 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.

Jump to

Keyboard shortcuts

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