-
Notifications
You must be signed in to change notification settings - Fork 308
feat(tree): modify children of existing Tree
#461
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
Open
bashbunni
wants to merge
9
commits into
master
Choose a base branch
from
modify-children
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 5 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
0173ef5
feat(tree): set Children on Leafs (converts their Node type)
bashbunni 96619fb
test(tree): add NodeChildren Replace testable example
bashbunni 5be2966
feat(tree): add SetChildren, Insert, and Replace to Node interface
bashbunni b0718a4
docs(tree): tidy examples
bashbunni 54aafad
test(tree): add TestInheritedStyles for items added to existing Tree
bashbunni 2ee0ac2
chore: (tree): remove TODOs
bashbunni 56890ce
Merge branch 'master' into modify-children
bashbunni ddb419f
merge master into modify-children
bashbunni c972ec0
Merge branch 'master' into modify-children
bashbunni File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,153 @@ | ||
| package tree_test | ||
|
|
||
| import ( | ||
| "fmt" | ||
|
|
||
| "github.com/charmbracelet/lipgloss/tree" | ||
| "github.com/charmbracelet/x/ansi" | ||
| ) | ||
|
|
||
| func ExampleLeaf_Insert() { | ||
| 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) | ||
| // Adds a new Tree Node to a Leaf (Mac). | ||
| t.Replace(3, t.Children().At(3).Insert(0, "Glow Play Cushion Blush")) | ||
| fmt.Println(ansi.Strip(t.String())) | ||
| // Output: | ||
| //⁜ Makeup | ||
| //├── Glossier | ||
| //├── Fenty Beauty | ||
| //│ ├── Gloss Bomb Universal Lip Luminizer | ||
| //│ ╰── Hot Cheeks Velour Blushlighter | ||
| //├── Nyx | ||
| //├── Mac | ||
| //│ ╰── Glow Play Cushion Blush | ||
| //╰── Milk | ||
| } | ||
|
|
||
| func ExampleLeaf_Replace() { | ||
| 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) | ||
| // Add Glow Play Cushion Blush to Mac Leaf. | ||
| t.Replace(3, t.Children().At(3).Replace(0, "Glow Play Cushion Blush")) | ||
| fmt.Println(ansi.Strip(t.String())) | ||
| // Output: | ||
| //⁜ Makeup | ||
| //├── Glossier | ||
| //├── Fenty Beauty | ||
| //│ ├── Gloss Bomb Universal Lip Luminizer | ||
| //│ ╰── Hot Cheeks Velour Blushlighter | ||
| //├── Nyx | ||
| //├── Mac | ||
| //│ ╰── Glow Play Cushion Blush | ||
| //╰── Milk | ||
| } | ||
|
|
||
| // Tree Examples | ||
|
|
||
| func ExampleTree_Replace() { | ||
| 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) | ||
| // Add a Tree as a Child of "Glossier". At this stage "Glossier" is a Leaf, | ||
| // so we re-assign the value of "Glossier" in the "Makeup" Tree to its new | ||
| // Tree value returned from Child(). | ||
| t.Replace(0, t.Children().At(0).Child( | ||
| tree.Root("Apparel").Child("Pink Hoodie", "Baseball Cap"), | ||
| )) | ||
|
|
||
| // Add a Leaf as a Child of "Glossier". At this stage "Glossier" is a Tree, | ||
| // so we don't need to use [Tree.Replace] on the parent tree. | ||
| t.Children().At(0).Child("Makeup") | ||
| fmt.Println(ansi.Strip(t.String())) | ||
| // Output: | ||
| // ⁜ Makeup | ||
| // ├── Glossier | ||
| // │ ├── Apparel | ||
| // │ │ ├── Pink Hoodie | ||
| // │ │ ╰── Baseball Cap | ||
| // │ ╰── Makeup | ||
| // ├── Fenty Beauty | ||
| // │ ├── Gloss Bomb Universal Lip Luminizer | ||
| // │ ╰── Hot Cheeks Velour Blushlighter | ||
| // ├── Nyx | ||
| // ├── Mac | ||
| // ╰── Milk | ||
| // | ||
| } | ||
|
|
||
| func ExampleTree_Insert() { | ||
| 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) | ||
| // Adds a new Tree Node after Fenty Beauty. | ||
| t.Insert(2, tree.Root("Lancôme").Child("Juicy Tubes Lip Gloss", "Lash Idôle", "Teint Idôle Highlighter")) | ||
|
|
||
| // Adds a new Tree Node in Fenty Beauty | ||
| t.Replace(1, t.Children().At(1).Insert(0, "Blurring Skin Tint")) | ||
|
|
||
| // Adds a new Tree Node to a Leaf (Mac) | ||
| t.Replace(4, t.Children().At(4).Insert(0, "Glow Play Cushion Blush")) | ||
| fmt.Println(ansi.Strip(t.String())) | ||
| // Output: | ||
| //⁜ Makeup | ||
| //├── Glossier | ||
| //├── Fenty Beauty | ||
| //│ ├── Blurring Skin Tint | ||
| //│ ├── Gloss Bomb Universal Lip Luminizer | ||
| //│ ╰── Hot Cheeks Velour Blushlighter | ||
| //├── Lancôme | ||
| //│ ├── Juicy Tubes Lip Gloss | ||
| //│ ├── Lash Idôle | ||
| //│ ╰── Teint Idôle Highlighter | ||
| //├── Nyx | ||
| //├── Mac | ||
| //│ ╰── Glow Play Cushion Blush | ||
| //╰── Milk | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| [38;5;35m⁜ Makeup[0m | ||
| [38;5;63m├──[0m [38;5;212mGlossier[0m | ||
| [38;5;63m│ [0m [38;5;63m├──[0m [38;5;212mApparel[0m | ||
| [38;5;63m│ [0m [38;5;63m│ [0m [38;5;63m├──[0m [38;5;212mPink Hoodie[0m | ||
| [38;5;63m│ [0m [38;5;63m│ [0m [38;5;63m╰──[0m [38;5;212mBaseball Cap[0m | ||
| [38;5;63m│ [0m [38;5;63m╰──[0m [38;5;212mMakeup[0m | ||
| [38;5;63m├──[0m [38;5;212mFenty Beauty[0m | ||
| [38;5;63m│ [0m [38;5;63m├──[0m [38;5;212mGloss Bomb Universal Lip Luminizer[0m | ||
| [38;5;63m│ [0m [38;5;63m╰──[0m [38;5;212mHot Cheeks Velour Blushlighter[0m | ||
| [38;5;63m├──[0m [38;5;212mNyx[0m | ||
| [38;5;63m├──[0m [38;5;212mMac[0m | ||
| [38;5;63m╰──[0m [38;5;212mMilk[0m |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Might be useful to support
encoding.TextMarshaleras wellThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't have this in other type checks in
tree, maybe something we can add down the line