-
Notifications
You must be signed in to change notification settings - Fork 822
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
Add Tree.reset
, which clears a Tree
and then then resets the Tree.root
's label
and data
to given values
#1709
Conversation
See Textualize#1437 for background. While it would be ideal to allow for the complete emptying of a Tree, the root node is required (and it's part of the construction of a Tree). So, here, when clearing the Tree we optionally allow for a new label to be given. Ideally we'll also allow for fresh data to be provided too; but there's a wrinkle there in knowing the difference between the data being None, and no data being provided (so the current root's data being carried over). Following the method of defaulting used in __init__ would cause problems. As such, rather than roll all of this into one commit, this goes with the basic requirement and the solution for data will follow. Note this also starts some unit tests for the clearing of a Tree.
Just before doing the commit I decided to rename the test tree in the new unit test for clearing down a tree. Of course I managed to name it in such a way that it becomes special to pytest. This fixes that.
I think the behaviour we are implementing makes sense but belongs in a new method, perhaps called If you pay me to wash your car, you don't expect to come back and see your car painted fresh with a different colour! |
src/textual/widgets/_tree.py
Outdated
@@ -560,13 +560,20 @@ def get_label_width(self, node: TreeNode[TreeDataType]) -> int: | |||
label = self.render_label(node, NULL_STYLE, NULL_STYLE) | |||
return label.cell_len | |||
|
|||
def clear(self) -> None: | |||
"""Clear all nodes under root.""" | |||
def clear(self, label: TextType | None = None, data: TreeDataType = ...) -> None: |
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.
I get the limitation of not being able to clear the root and I think this is so "exceptional" that it should be made obvious from the name of the method, e.g., by renaming it to clear_children
or clear_under_root
.
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.
I like @rodrigogiraoserrao 's idea to have a clear
method and a reset
. Where clear did the bare minimum, but reset
adds label and data. Feels cleaner.
It means that reset
wouldn't need the ellipsis sentinel.
Sold. |
After some internal discussion we've decided to keep `Tree.clear` as it was, and add a `Tree.reset`, which does a `Tree.clear` but resets the label and data of `Tree.root` to the values given, while mirroring how `Tree.__init__` takes those parameters.
Tree.reset
, which clears a Tree
and then then resets the Tree.root
's label
and data
to given values
See #1437 for background. While it would be ideal to allow for the complete emptying of a Tree, the root node is required (and it's part of the construction of a Tree). So, here, when clearing the Tree we optionally allow for a new label to be given.
Ideally we'll also allow for fresh data to be provided too; but there's a wrinkle there in knowing the difference between the data being None, and no data being provided (so the current root's data being carried over). Following the method of defaulting used in init would cause problems. As such, rather than roll all of this into one commit, this goes with the basic requirement and the solution for data will follow.
Note this also starts some unit tests for the clearing of a Tree.
Edit to add: following a bit of internal chat (see comments for the conclusions), we've decided to keep
Tree.clear
as ways and move this intoTree.reset
, which makes things a wee bit cleaner on thedata
front too.