Exemplo n.º 1
1
func (v *Vault) walktree(path string, options TreeOptions) (tree.Node, int, error) {
	t := tree.New(path)
	l, err := v.List(path)
	if err != nil {
		return t, 0, err
	}

	for _, p := range l {
		if p[len(p)-1:len(p)] == "/" {
			kid, n, err := v.walktree(path+"/"+p[0:len(p)-1], options)
			if err != nil {
				return t, 0, err
			}
			if n == 0 {
				continue
			}
			if options.UseANSI {
				kid.Name = ansi.Sprintf("@B{%s}", p)
			} else {
				kid.Name = p[0 : len(p)-1]
			}
			t.Append(kid)

		} else if options.HideLeaves {
			continue

		} else {
			var name string
			if options.UseANSI {
				name = ansi.Sprintf("@G{%s}", p)
			} else {
				name = p
			}
			t.Append(tree.New(name))
		}
	}
	return t, len(l), nil
}
Exemplo n.º 2
0
func main() {
	t := tree.New("a",
		tree.New("b"),
		tree.New("c"),
	)

	fmt.Printf("%s\n", t.Draw())
}
Exemplo n.º 3
0
func TestAppend(t *testing.T) {
	tr := tree.New("a", tree.New("b"))
	drawsOk(t, "{a -> b} before append", tr, `
		.
		└── a
		    └── b`)

	tr.Append(tree.New("c"))
	drawsOk(t, "{a -> [b c]} before append", tr, `
		.
		└── a
		    ├── b
		    └── c`)
}
Exemplo n.º 4
0
func TestPaths(t *testing.T) {
	pathsOk(t, "{a}",
		tree.New("a"),
		"a")

	pathsOk(t, "{a -> b}",
		tree.New("a", tree.New("b")),
		"a/b")

	pathsOk(t, "{a -> [b c]}",
		tree.New("a", tree.New("b"), tree.New("c")),
		"a/b",
		"a/c")

	pathsOk(t, "{a -> [{b -> c} d]",
		tree.New("a", tree.New("b", tree.New("c")), tree.New("d")),
		"a/b/c",
		"a/d")
}
Exemplo n.º 5
0
func TestDrawing(t *testing.T) {
	drawsOk(t, "{a}",
		tree.New("a"), `
		.
		└── a`)

	drawsOk(t, "{a -> b}",
		tree.New("a", tree.New("b")), `
		.
		└── a
		    └── b`)

	drawsOk(t, "{a -> [b c]}",
		tree.New("a", tree.New("b"), tree.New("c")), `
		.
		└── a
		    ├── b
		    └── c`)

	drawsOk(t, "{a -> b -> c}",
		tree.New("a", tree.New("b", tree.New("c"))), `
		.
		└── a
		    └── b
		        └── c`)

	drawsOk(t, "{a -> [{b -> c} d]}",
		tree.New("a", tree.New("b", tree.New("c")), tree.New("d")), `
		.
		└── a
		    ├── b
		    │   └── c
		    └── d`)

	drawsOk(t, "{a -> [{b -> c -> e} d]}",
		tree.New("a", tree.New("b", tree.New("c", tree.New("e"))), tree.New("d")), `
		.
		└── a
		    ├── b
		    │   └── c
		    │       └── e
		    └── d`)

	drawsOk(t, "multiline node strings",
		tree.New("Alpha\n(first)\n",
			tree.New("Beta\n(second)\n",
				tree.New("Gamma\n(third)\n"),
			),
			tree.New("Delta\n(fourth)\n"),
		), `
		.
		└── Alpha
		    (first)
		    ├── Beta
		    │   (second)
		    │   └── Gamma
		    │       (third)
		    └── Delta
		        (fourth)`)
}