textual icon indicating copy to clipboard operation
textual copied to clipboard

Calling `remove_items` or `pop` on a ListView does not update index or highlighting

Open chriskingio opened this issue 1 year ago • 2 comments

It seems that calling pop (or remove_items, though that's not what I'm using for my use case) does not correctly update highlighting (presumably because calling pop does not update the index).

A small reproduction is as follows:

from textual.app import App, ComposeResult
from textual.widgets import ListItem, ListView, Footer


class TestApp(App):
    BINDINGS = [
        ('a', 'add', 'Adds a blank item'),
        ('d', 'delete_highlighted', 'Deletes the highlighted cell'),
    ]

    _listview: ListView

    def compose(self) -> ComposeResult:
        self._listview = ListView(ListItem())

        yield Footer()
        yield self._listview

    def action_add(self):
        self._listview.append(ListItem())

    def action_delete_highlighted(self):
        index = self._listview.index
        self._listview.pop(index)


if __name__ == '__main__':
    TestApp().run()

Spawning a few items and trying to delete them does not set any highlighting, only manually updating the index will do that.

Textual Diagnostics

Versions

Name Value
Textual 0.83.0
Rich 13.9.2

Python

Name Value
Version 3.12.1
Implementation CPython
Compiler Clang 17.0.6
Executable /home/king/Projects/private/.venv/bin/python

Operating System

Name Value
System Linux
Release 6.10.11-amd64
Version #1 SMP PREEMPT_DYNAMIC Debian 6.10.11-1 (2024-09-22)

Terminal

Name Value
Terminal Application Unknown
TERM xterm-256color
COLORTERM truecolor
FORCE_COLOR Not set
NO_COLOR Not set

Rich Console options

Name Value
size width=223, height=76
legacy_windows False
min_width 1
max_width 223
is_terminal True
encoding utf-8
max_height 76
justify None
overflow None
no_wrap False
highlight None
markup None
height None

chriskingio avatar Oct 13 '24 04:10 chriskingio

We found the following entries in the FAQ which you may find helpful:

Feel free to close this issue if you found an answer in the FAQ. Otherwise, please give us a little time to review.

This is an automated reply, generated by FAQtory

github-actions[bot] avatar Oct 13 '24 04:10 github-actions[bot]

Good catch - it looks like the index (and therefore the highlight) isn't properly accounted for when items are removed.

I'm also wondering if these ListView methods should return an AwaitComplete rather than AwaitRemove, to ensure that any re-validation of the index is done only after the items are removed?

TomJGooding avatar Oct 15 '24 21:10 TomJGooding

Don't forget to star the repository!

Follow @textualizeio for Textual updates.

github-actions[bot] avatar Nov 28 '24 16:11 github-actions[bot]