Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added a small method to return the last node #3

Merged
merged 1 commit into from
Mar 16, 2018

Conversation

eiso
Copy link
Contributor

@eiso eiso commented Dec 7, 2017

First of all thank you for this library, it saved me a lot of time.

I wanted to use treeprint to take a list of file paths and echo it in the same way as the tree command does on Linux.

To make this work, I added a small method to return the last mode of a tree, named, FindLastNode().

	tree := treeprint.New()
	b = r.FilePaths()

	for _, path := range b {

		parts := strings.Split(path, string(os.PathSeparator))

		t := tree.FindByValue(parts[0])

		if t == nil {
			t = tree.AddBranch(parts[0])
		}

		for i := 1; i < len(parts); i++ {
			if i == 1 {
				t = t.AddNode(parts[1])
				continue
			}
			t = t.FindLastNode()
			t = t.AddNode(parts[i])
			t = t.FindLastNode()
		}

	}

	fmt.Println(tree.String())

It is possible the above behaviour could be done before with existing methods in the library but I wasn't able to discover how.

@xlab xlab merged commit f3a15cf into xlab:master Mar 16, 2018
@xlab
Copy link
Owner

xlab commented Mar 16, 2018

/me slowpoke

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants