-
-
Notifications
You must be signed in to change notification settings - Fork 31.9k
gh-60436: fix curses textbox backspace/del #103783
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
Conversation
Most changes to Python require a NEWS entry. Please add it using the blurb_it web app or the blurb command-line tool. |
I tested locally by running |
@aidanmelen Are there any tests you can add? I know this is a difficult question/request. I am discussing with the team here at pycon. Any ideas you may have would be helpful. |
I didn't see any existing tests for Enjoy PyCon! |
Okay, I didn't look very hard. There are two curses related files:
where |
Yep, I found those files as well, but you will need to add to one of them. |
I was able to modify the code from testpad.py into a proof of concept showing we can test that the actions performed on the textbox produce the desired output. Take a look at this and see if it might be useful.
Just throw it into a file and execute. In the commands above, I "accidentally" add an extra J, then an backspace is submitted. The final output shows the extra J has been removed. I know the test isn't perfect since it isn't emulating an exact keypress on a mac keyboard, but it is something. |
Just realized I should have used curses.ascii.DEL in the example above. Actually, probably should have used all that can remove a character. Anyways, you get the idea.... |
@mblahay I wrote a basic unittest that uses dependency injection to mock the I don't like having the import statements in the class but I there seemed to be some logic that skipped test when |
I have looked at the code that uses the mock, and it does seem to verify the functionality of Textbox, while not actually needing to deal with an ncurses window. @ambv, @ned-deily what do you think? |
As we are at it, why is the Azure Pipelines PR test failing? I have dug into it to a point where it is saying the issue is whitespace in the test file, but nothing appears to be wrong with the whitespace. |
the |
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.
Changes look good. Good use of mocking for a difficult test situation.
@ambv This is ready to move forward. |
I'm unable to confirm this fixes the issue as demonstrated in the StackOverflow post: import curses
import curses.textpad
def get_input(window):
curses.curs_set(1)
curses.set_escdelay(1)
window.addstr(0, 0, "Enter some text:")
input_box = curses.textpad.Textbox(curses.newwin(1, 40, 1, 0), insert_mode=True)
def validate(ch):
# exit input with the escape key
escape = 27
if ch == escape:
ch = curses.ascii.BEL # Control-G
# delete the character to the left of the cursor
elif ch in (curses.KEY_BACKSPACE, curses.ascii.DEL):
ch = curses.KEY_BACKSPACE
# exit input to resize windows
elif ch == curses.KEY_RESIZE:
ch = curses.ascii.BEL # Control-G
return ch
input_box.edit(validate)
curses.curs_set(0)
curses.set_escdelay(1000)
return input_box.gather().strip()
def main(window):
curses.noecho()
curses.cbreak()
window.keypad(True)
curses.curs_set(0)
input_text = get_input(window)
window.addstr(2, 0, f"You entered: {input_text}")
window.refresh()
window.getch()
if __name__ == "__main__":
curses.wrapper(main) This is still not handling the Delete key (Fn+Backspace) on an M1 Mac, macOS 12.6.5. I tested with iTerm2 and the built-in Terminal.app, with Fish and Bash. No combination works. |
Running |
I retract my previous comment about the change not fixing the issue. There's some confusion about what "DEL" and "delete" means. To me it means "remove character on the right of the cursor". In this PR we are using |
@ambv yes, that is also my understanding! Thanks for providing your context. |
@aidanmelen Please make the changes @amb is suggesting. Once those are done, this can proceed to merge. |
This has been fixed in the Should it also be backported to 3.11? If so, let's also include the test fix from #106196. |
fixes issue: #60436
update curses textbox to additionally handle backspace using the
curses.ascii.DEL
key press.My current workaround is to pass a custom validate func that handles the additional key press for backspace.