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

Cmdline history: deque, restore input command, ability to use up/down arrow keys #600

Merged
merged 7 commits into from
Sep 18, 2023

Conversation

raphCode
Copy link
Contributor

This PR aims to change three things related to the internal command line history:

  • using collections.deque for the history storage: This handles the maximum size internally, automatically deleting old entries on append()
    I just found a deque a nice fit for the use case while working on the other features, but there is no absolute need for this change.
  • Restore command line input when browsing history: See commit messages and in-code comments for detailed explanation
    This fixes one of my pain points in pudb: When accidentally hitting Ctrl-N in the command line, it deletes my potentially long command input with no possibility to get it back.
  • Using up/down arrow keys for browsing history: This is not implemented yet as I want to ask how I should implement this. Is adding a boolean config option fine that enables this?
    This is my other pain point in the pudb internal shell: After not being aware for a year that browsing the history is even possible, I still find Ctrl-N/P highly unfamiliar despite using pudb for multiple years now. I still press the arrow keys now and then.

@inducer
Copy link
Owner

inducer commented Apr 26, 2023

This is not implemented yet

Converting to draft to avoid mistakes.

Is adding a boolean config option fine that enables this?

I agree that arrow keys are more discoverable. Let's just switch it over for everyone. Maybe add shift-page up/down (or so) for history scrollback. (It should still also be available as Cursor right (go to 'clear' button) and then up/down.)

@inducer inducer marked this pull request as draft April 26, 2023 17:16
@raphCode
Copy link
Contributor Author

raphCode commented Apr 26, 2023

I agree that arrow keys are more discoverable. Let's just switch it over for everyone.

In case you want a setting option, I started to hack something together:
image
It's not finished yet, so I am fine with discarding the setting and just changing it for everyone.
But I fear it might break someone's workflow when changing the keybinding after so long.

Maybe add shift-page up/down (or so) for history scrollback.

Sure.
Btw, I am in favor of calling it the "result scrollback" or something to avoid confusion with the command history.

It should still also be available as Cursor right (go to 'clear' button) and then up/down.

It is. I also intended to write it next to the proposed setting, like in the screenshot.

@raphCode raphCode force-pushed the history-improvements branch from 6c4eeeb to 536db4a Compare April 26, 2023 18:56
@inducer
Copy link
Owner

inducer commented May 3, 2023

I think the instant-apply logic for that setting will be cumbersome/hard to maintain. Let's just switch it over.

raphCode added a commit to raphCode/pudb that referenced this pull request May 12, 2023
This changes the arow keys from accessing the command line results to
browsing the command history, as requested by the main developer:
inducer#600 (comment)
@raphCode raphCode force-pushed the history-improvements branch from 536db4a to 8833e50 Compare May 12, 2023 08:43
raphCode added a commit to raphCode/pudb that referenced this pull request May 12, 2023
This changes the arow keys from accessing the command line results to
browsing the command history, as requested by the main developer:
inducer#600 (comment)
@raphCode raphCode force-pushed the history-improvements branch from 8833e50 to 43605e5 Compare May 12, 2023 08:48
@raphCode
Copy link
Contributor Author

  • I added the arrow keys to the existing Ctrl-n/p keys for history browsing.
  • I also rephrased the help a bit to avoid confusion and reflect the new behavior.
  • I was using my modifications the last weeks and I really like them.
    I added one minor fix / improvement: "Always recall the just executed command on arrow up".

One thing still remains: I am not sure what the behavior should be when:

  • browsing to a historic command
  • editing the prompt
  • browsing up/down the history
    Currently, the next history element is recalled, overwriting all modifications to the prompt, loosing the text.
    This is probably a rare edge case.

Maybe add shift-page up/down (or so) for history scrollback.

How would I do that? Add a keybinding that calls set_focus() on the result scrollback list? How can I then move the focused element there?

@inducer
Copy link
Owner

inducer commented May 17, 2023

I am not sure what the behavior should be when:

I think that's fine.

How would I do that?

How about just send a "Page-Up" keypress to the widget? (without changing focus) Not sure if that'll work, but it may.

@raphCode
Copy link
Contributor Author

So the idea is to scroll / browse the results scrollback, but leaving the focus in the prompt? Interesting, I will try that.

@raphCode
Copy link
Contributor Author

How about just send a "Page-Up" keypress to the widget?

I am having trouble sending a keypress to an unfocused widget.
Calling keypress() seems to need a tuple defining the widget's size. From the urwid manual I read that sizes are calculated on the go internally and not really exposed to the user code.

Alternatively, the docs recommend to scroll ListBoxes with set_focus() that can be called with a position only (no size), but that jumps result-wise. Meaning if a result spans more than the visible scrollback height, scrolling is not satisfactory and skips parts of the result.

A related question: https://stackoverflow.com/questions/49416332/focus-widget-in-list-but-keep-cursor-in-another/49520580, but that seems to jump result-wise again.

I think the instant-apply logic for that setting will be cumbersome/hard to maintain.

By researching the keypress stuff, I stumbled across this:
http://urwid.org/reference/command_map.html#urwid.CommandMap
I reads like that could help, but I didn't try.

@inducer
Copy link
Owner

inducer commented May 21, 2023

Thanks for exploring. Given the apparent difficulty, I think it might be best to leave making the history scrollback more efficient out of scope for now.

raphCode added a commit to raphCode/pudb that referenced this pull request May 31, 2023
This changes the arow keys from accessing the command line results to
browsing the command history, as requested by the main developer:
inducer#600 (comment)
@raphCode raphCode force-pushed the history-improvements branch from 43605e5 to 6c0c11f Compare May 31, 2023 14:41
@raphCode raphCode marked this pull request as ready for review May 31, 2023 14:42
@raphCode
Copy link
Contributor Author

raphCode commented May 31, 2023

  • ✔️ rebased on main
  • ✔️ figured out how to make the shift page up/down scrolling work.
    I think I needed some time to understand the urwid way of doing thing :D

Let me know what you think!

@raphCode raphCode force-pushed the history-improvements branch from 6c0c11f to 16d2c97 Compare May 31, 2023 14:47
@raphCode
Copy link
Contributor Author

raphCode commented Aug 30, 2023

Hi! Just wondering if there's still anything to be done on my end do get this merged?
(except maybe merge / rebase on main)

Copy link
Owner

@inducer inducer left a comment

Choose a reason for hiding this comment

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

This LGTM. Could you rebase and add docs for Shift+Pg{Up,Dn}?

Comment on lines +2010 to +2012
for key in ("page up", "page down"):
self.cmdline_sigwrap.listen("shift " + key, cmdline_results_scroll)
Copy link
Owner

Choose a reason for hiding this comment

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

Could you document these keystrokes?

A deque handles the size limit internally, eliminating some logic in the
debugger itself.
Text input to the internal command line is now restored and not lost
after browsing the history. It is designed to behave just like a
dedicated history item at the end of the history. This matches the
behavior of readline in most shells.
For example, writing a command without executing it, then moving up to
the most recent history item and down again restores the typed command.

The pudb special behavior of clearing the command when moving down from
the end of the history is also preserved. In that case the text can be
restored too by moving up in the history again before typing anything
new.
This is akin to using Ctrl-C in common shells to clear the current
command, but with the ability to restore the input again.

Using the clear button which empties the displayed shell history now
also clears the command input, since it can be easily restored.

This fixes one of my pain points with the internal pudb command line:
When writing a lengthy command, accidentally hitting Ctrl-N or Ctrl-P
overwrites the input text field with no possibility to get the content
back.
This is particularly bad after using vim's word completion with Ctrl-N
for an extensive amount of time, and then accidentally hitting the same
shortcut in pudb instead of tab completion.
This changes the arow keys from accessing the command line results to
browsing the command history, as requested by the main developer:
inducer#600 (comment)
There is the
- command history, which stores previously executed commands
- result scrollback, which refers to list of shell prompts and their
  results

The latter one was previously called 'command line history'.
To avoid confusion, this commit introduces a major new term.
After executing a command, immediatly hitting the arrow up key should
recall the just executed command again, like in all shells.
This commit ensures this behavior by clearing out the saved edit text on
successful command execution.

Previously, it recalled the last saved edit text if set, which is an
annoying workflow break compared to other shells.
@raphCode raphCode force-pushed the history-improvements branch from 16d2c97 to 6a1e11d Compare September 15, 2023 09:12
@raphCode
Copy link
Contributor Author

raphCode commented Sep 15, 2023

Sorry for the delay. I added docs and rebased it, it should merge now.


While testing the new results browse function, I experienced some UI glitches sometimes.
The seems to be a strange combination of urwid, my terminal multiplexer zellij (UI must be unlocked) and the keystrokes shift+up/down.
This is not an pudb bug and it happened before this PR as well. Nothing too serious to worry about, but maybe something to keep in mind when someone complains.

@inducer inducer merged commit 55a8d75 into inducer:main Sep 18, 2023
inducer pushed a commit that referenced this pull request Sep 18, 2023
This changes the arow keys from accessing the command line results to
browsing the command history, as requested by the main developer:
#600 (comment)
@inducer
Copy link
Owner

inducer commented Sep 18, 2023

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants