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

[features-] fix cursor index in hide-uniform-cols #2578

Merged
merged 1 commit into from
Nov 12, 2024

Conversation

midichef
Copy link
Contributor

Closes #2577.

Repro case:
echo "a,a\n1,1\n1,1" |vd -f csv then move the cursor to the right column, then hide-uniform-cols:

File "/home/midichef/.venv/lib/python3.12/site-packages/visidata/sheets.py", line 780, in allAggregators
    col = self.availCols[vcolidx]
          ~~~~~~~~~~~~~~^^^^^^^^^
IndexError: list index out of range

In this PR, I do the cursor move in hide_uniform_cols(), not inside Column.hide(). That's because I think of hide() as a low level width adjuster that does not conceptually involve the cursor, and we don't want to be calling calcColLayout() in it unnecessarily. Or does that leave the door open for a repeat of this error with other callers of Column.hide()?

@saulpw
Copy link
Owner

saulpw commented Oct 21, 2024

Does it work to just call checkCursor() after the columns are hidden? That would be my preferred method, whether in the execstr or hide_uniform_cols or in .hide() itself.

@midichef
Copy link
Contributor Author

midichef commented Oct 25, 2024

It almost works, but the problem is in one of the checkCursor() calls. The full stack trace shows the problem is in the new allAggregators function from 551b06c.

  File "/home/midichef/.venv/lib/python3.12/site-packages/visidata/mainloop.py", line 252, in mainloop
    vd.callNoExceptions(sheet.checkCursor)
...
  File "/home/midichef/.venv/lib/python3.12/site-packages/visidata/sheets.py", line 643, in checkCursor
    elif self.bottomRowIndex < self.cursorRowIndex:
...                                            ^^^^^^^^^^^^^^^^
  File "/home/midichef/.venv/lib/python3.12/site-packages/visidata/sheets.py", line 427, in nFooterRows
    return len(self.allAggregators) + 1
               ^^^^^^^^^^^^^^^^^^^
...
  File "/home/midichef/.venv/lib/python3.12/site-packages/visidata/sheets.py", line 780, in allAggregators
    col = self.availCols[vcolidx]
          ~~~~~~~~~~~~~~^^^^^^^^^
IndexError: list index out of range

It seems like availCols is using a cached value that is wrong due to the col.hide() calls. The IndexError goes away if I clear the cache with Sheet.visibleCols.fget.cache_clear() in hide_uniform_cols(), right after col.hide(). But the error still happens if I instead clear the cache on availCols with Sheet.availCols.fget.cache_clear().

What's the right solution here?

@saulpw
Copy link
Owner

saulpw commented Nov 7, 2024

Hmmm...does Sheet.clear_all_caches() work? That's the recommended way to clear these caches; instead of trying to find the exact right one to clear, just clear them all. (Cache invalidation being one of the hardest problems in computer science.)

visidata/features/layout.py Outdated Show resolved Hide resolved
@midichef
Copy link
Contributor Author

midichef commented Nov 12, 2024

The allAggregators IndexError error is being triggered in other situations:

    col = self.availCols[vcolidx]
          ~~~~~~~~~~~~~~^^^^^^^^^
IndexError: list index out of range

For example, it can be triggered on a standard TsvSheet by pressing i, then U. So there is more work to do relating to allAggregators. I'll open a separate Issue for that.

Copy link
Owner

@saulpw saulpw left a comment

Choose a reason for hiding this comment

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

Thanks for fixing this!

@anjakefala anjakefala merged commit f9f435c into saulpw:develop Nov 12, 2024
14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[hide-uniform-cols] Couple issues.
3 participants