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 insert, pop and remove_items methods to ListView #4384

Merged
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions src/textual/widgets/_list_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,56 @@ def clear(self) -> AwaitRemove:
self.index = None
return await_remove

def insert(self, index: int, items: Iterable[ListItem]) -> AwaitMount:
"""Insert new ListItem(s) to specified index.

Args:
index: index to insert new ListItem.
items: The ListItems to insert.

Returns:
An awaitable that yields control to the event loop
until the DOM has been updated with the new child item.
"""
await_mount = self.mount(*items, before=index)
return await_mount

def pop(self, index: Optional[int]=None) -> AwaitRemove:
"""Remove last ListItem from ListView or
Remove ListItem from ListView by index

Args:
index: index of ListItem to remove from ListView

Returns:
An awaitable that yields control to the event loop until
the DOM has been updated to reflect item being removed.
"""
if index is None:
await_remove = self.query("ListItem").last().remove()
else:
await_remove = self.query("ListItem")[index].remove()
return await_remove

def remove_items(self, indices: Iterable[int]) -> AwaitRemove:
"""Remove ListItems from ListView by indices

Args:
indices: index(s) of ListItems to remove from ListView

Returns:
An awaitable object that waits for the direct children to be removed.
"""
if len(self._nodes) == len(indices):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If there are two items in the ListView and the developer passed [0, 0] as the indices this will have unintended consequences.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank for pointing it out! I have removed that block as it's not necessary.

return self.clear()

items_to_remove = []
for index in indices:
items_to_remove.append(self.query("ListItem")[index])
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could cache that query. Otherwise it would be performed once per loop.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please have a review


await_remove = self.app._remove_nodes(items_to_remove, self)
return await_remove

def action_select_cursor(self) -> None:
"""Select the current item in the list."""
selected_child = self.highlighted_child
Expand Down