Skip to content

feat(tree): hide children #454

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

Closed
wants to merge 15 commits into from
Closed

feat(tree): hide children #454

wants to merge 15 commits into from

Conversation

bashbunni
Copy link
Member

@bashbunni bashbunni commented Dec 10, 2024

I updated the godoc of tree to match capitalization used for leaf while I was at it

Signed-off-by: Carlos Alexandro Becker <[email protected]>
@bashbunni bashbunni self-assigned this Dec 10, 2024
@bashbunni bashbunni marked this pull request as ready for review December 10, 2024 17:03
@bashbunni
Copy link
Member Author

Another thing to improve this PR: have an example hiding both ways with comments on why you would use Hide vs SetHidden. It already says in their go doc, but could be handy to show how they differ in usage.

@bashbunni
Copy link
Member Author

bashbunni commented Dec 12, 2024

SetValue for tree does the same thing as Root. Should I mention any difference in the docs? Root can be used during or after creation, SetValue can be used after creation

@bashbunni bashbunni added bug Something isn't working enhancement New feature or request labels Dec 13, 2024
@bashbunni
Copy link
Member Author

After looking at this again and putting an example together, I think we may want a SetChildren method for Node. If I just set the value on a leaf, it overwrites the existing value with the new tree (which makes sense). So we're forced to get the value of the node we're modifying before modifying it. See below

	enumeratorStyle := lipgloss.NewStyle().Foreground(lipgloss.Color("63")).MarginRight(1)
	rootStyle := lipgloss.NewStyle().Foreground(lipgloss.Color("35"))
	itemStyle := lipgloss.NewStyle().Foreground(lipgloss.Color("212"))

	t := tree.
		Root("⁜ Makeup").
		Child(
			"Glossier",
			"Fenty Beauty",
			tree.New().Child(
				"Gloss Bomb Universal Lip Luminizer",
				"Hot Cheeks Velour Blushlighter",
			),
			"Nyx",
			"Mac",
			"Milk",
		).
		Enumerator(tree.RoundedEnumerator).
		EnumeratorStyle(enumeratorStyle).
		RootStyle(rootStyle).
		ItemStyle(itemStyle)
	glossier := t.Children().At(0)
	glossier.SetValue(tree.Root(glossier.Value()).Child(tree.Root("Apparel").Child("Pink Hoodie", "Baseball Cap")))
	fmt.Println(t.String())

image


Another thing I noticed here is that when you add a nested tree after the original tree was created, it doesn't inherit the styles. Check out the difference between Glossier and Fenty Beauty above. Will work on that in a separate PR using this as the base branch

@bashbunni bashbunni marked this pull request as draft January 9, 2025 16:34
@bashbunni
Copy link
Member Author

I simplified the usage and it feels much better now. You can update a Tree's children pretty easily. The changes include:

  • overwrite all existing children
  • insert a child at a specific index
  • replace a child at a specific index

and these work with Leaf types too by converting the Leaf to a Tree type.

Here's what it looks like:

        enumeratorStyle := lipgloss.NewStyle().Foreground(lipgloss.Color("63")).MarginRight(1)
	rootStyle := lipgloss.NewStyle().Foreground(lipgloss.Color("35"))
	itemStyle := lipgloss.NewStyle().Foreground(lipgloss.Color("212"))

	t := tree.
		Root("⁜ Makeup").
		Child(
			"Glossier",
			"Fenty Beauty",
			tree.New().Child(
				"Gloss Bomb Universal Lip Luminizer",
				"Hot Cheeks Velour Blushlighter",
			),
			"Nyx",
			"Mac",
			"Milk",
		).
		Enumerator(tree.RoundedEnumerator).
		EnumeratorStyle(enumeratorStyle).
		RootStyle(rootStyle).
		ItemStyle(itemStyle)
	// Add a Tree as a Child of "Glossier"
	t.Replace(0, t.Children().At(0).Child(
		tree.Root("Apparel").Child("Pink Hoodie", "Baseball Cap"),
	))

	// Add a Leaf as a Child of "Glossier"
	t.Children().At(0).Child("Makeup")
	fmt.Println(ansi.Strip(t.String()))

Note: you have to call t.Replace on the parent if you're trying to modify its child Leaf. Because it's changing type, we need to reassign the value for the parent (See "Add child to Glossier" above compared to following Child operation that's now working on a Tree) - Will add this note to the godoc for Leaf operations

@bashbunni bashbunni changed the title feat(tree): hide children feat(tree): modify children Jan 10, 2025
@bashbunni bashbunni changed the title feat(tree): modify children feat(tree): hide children Jan 10, 2025
@bashbunni
Copy link
Member Author

I'll split this PR into two... The other Leaf/Tree/NodeChildren modifications are probably out of scope here

@bashbunni
Copy link
Member Author

Simplified and split this into separate PRs #461 & #460

@bashbunni bashbunni closed this Jan 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants