Code Monkey home page Code Monkey logo

treefs's Introduction

Package treefs provides functionality to print a simple graph of an fs.FS using the template of the tree command.

The version of tree whose graph is mimicked is tree v2.0.2 (c) 1996 - 2022 by Steve Baker, Thomas Moore, Francesc Rocher, Florian Sesser, Kyosuke Tokoro.

To get the graph representation and metadata of an fs.FS, construct a TreeFS and use its String method.

Consider the following directory (whose graph was generated by tree v2.0.2)

testdata
└── a
    ├── a1.test
    ├── a2.test
    ├── a3.test
    ├── b
    │   ├── b1.test
    │   ├── b2.test
    │   ├── b3.test
    │   └── d
    │       └── d1.test
    └── c
        ├── c1.test
        └── c2.test

4 directories, 9 files

Its graph could be constructed using treefs in the following way

var testdataFS fs.FS // assume this is embedded or read with os.DirFS
tfs, err := New(testdataFS, "testdata")
if err != nil {
    log.Fatal(err)
}
fmt.Println(tfs)

which would generate the following output

testdata
└── a
    ├── a1.test
    ├── a2.test
    ├── a3.test
    ├── b
    │   ├── b1.test
    │   ├── b2.test
    │   ├── b3.test
    │   └── d
    │       └── d1.test
    └── c
        ├── c1.test
        └── c2.test

4 directories, 9 files

Aggregated trees are allowed as well:

var args []Arg // see internal/examples/multi
multitfs, err := NewMutli(args...)
if err != nil {
    log.Fatal(err)
}
fmt.Println(multitfs)

could result in the following aggregate

.
└── main.go
../../../../treefs
├── LICENSE
├── go.mod
├── internal
│   └── examples
│       ├── multi
│       │   └── main.go
│       └── single
│           └── main.go
├── testdata
│   └── a
│       ├── a1.test
│       ├── a2.test
│       ├── a3.test
│       ├── b
│       │   ├── b1.test
│       │   ├── b2.test
│       │   ├── b3.test
│       │   └── d
│       │       └── d1.test
│       └── c
│           ├── c1.test
│           └── c2.test
├── treefs.go
└── treefs_test.go

9 directories, 16 files

Options can be provided with Opts.

For example, to display hidden directories and files (which are excluded by default), use the Hidden option:

tree, err := Tree(fsys, ".", Hidden)
if err != nil {
    log.Fatal(err)
}
fmt.Println(tree)
.
├── .hidden1
├── .hidden2
├── .hidden3
├── a1.test
├── a2.test
├── a3.test
├── b
│   ├── .hidden1
│   ├── b1.test
│   ├── b2.test
│   ├── b3.test
│   └── d
│       ├── .hidden1
│       └── d1.test
└── c
    ├── .hidden1
    ├── c1.test
    └── c2.test

3 directories, 15 files

The DirOnly option only displays directories:

tree, err := Tree(fsys, ".", DirOnly)
if err != nil {
    log.Fatal(err)
}
fmt.Println(tree)
testdata
└── a
    ├── b
    │   └── d
    └── c

4 directories

FullPathPrefix includes the full path prefix for each file:

tree, err := Tree(fsys, ".", FullPathPrefix)
if err != nil {
    log.Fatal(err)
}
fmt.Println(tree)
testdata
└── testdata/a
    ├── testdata/a/a1.test
    ├── testdata/a/a2.test
    ├── testdata/a/a3.test
    ├── testdata/a/b
    │   ├── testdata/a/b/b1.test
    │   ├── testdata/a/b/b2.test
    │   ├── testdata/a/b/b3.test
    │   └── testdata/a/b/d
    │       └── testdata/a/b/d/d1.test
    └── testdata/a/c
        ├── testdata/a/c/c1.test
        └── testdata/a/c/c2.test

4 directories, 9 files

Level sets the max display depth of the directory tree:

tree, err := Tree(fsys, ".", Level(2))
if err != nil {
    log.Fatal(err)
}
fmt.Println(tree)
testdata
└── a
    ├── a1.test
    ├── a2.test
    ├── a3.test
    ├── b
    └── c

3 directories, 3 files

See examples for example usage.

treefs's People

Contributors

algebra8 avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.