Skip to content

[Console] Support a set of control keys and key combinations in QuestionHelper #48287

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

Open
wants to merge 1 commit into
base: 7.4
Choose a base branch
from

Conversation

maxbeckers
Copy link
Contributor

@maxbeckers maxbeckers commented Nov 22, 2022

Q A
Branch? 6.4
Bug fix? no
New feature? yes
Deprecations? no
Tickets Fix #36657
License MIT
Doc PR -

Add support for using arrow keys in the QuestionHelper.

There was already a try #36700 to fix the issue.

The approach here does not depend on readline at all (removed in #17669)

This key combinations are added:

Key combination Comment
Backward one character
CTRL + B Backward one character
Forward one character
CTRL + F Forward one character
CTRL + Backward one word
ALT + B Backward one word
CTRL + SHIFT + Backward one word
CTRL + Forward one word
ALT + F Forward one word
CTRL + SHIFT + Forward one word
CTRL + H Delete previous character
Backspace Delete previous character
Delete Delete next character
CTRL + A Beginning of input
Home Beginning of input
CTRL + E End of input
End End of input

Feel free to test it out and leave comments about the experience.

UPDATE For better testing i now created a repo https://github.com/maxbeckers/symfony-cli-input-handling

@maxbeckers maxbeckers requested a review from chalasr as a code owner November 22, 2022 15:54
@carsonbot carsonbot added Status: Needs Review Status: Needs Work Console DX DX = Developer eXperience (anything that improves the experience of using Symfony) Feature RFC RFC = Request For Comments (proposals about features that you want to be discussed) labels Nov 22, 2022
@carsonbot carsonbot added this to the 6.2 milestone Nov 22, 2022
@carsonbot
Copy link

Hey!

Thanks for your PR. You are targeting branch "6.2" but it seems your PR description refers to branch "6.3".
Could you update the PR description or change target branch? This helps core maintainers a lot.

Cheers!

Carsonbot

@carsonbot carsonbot changed the title [WIP][RFC][DX][Console] Support left and right arrow for QuestionHelper [Console] [WIP] Support left and right arrow for QuestionHelper Nov 22, 2022
@fabpot fabpot modified the milestones: 6.2, 6.3 Nov 23, 2022
@maxbeckers maxbeckers force-pushed the patch-36657 branch 3 times, most recently from 6d3e73c to 51b434d Compare November 24, 2022 13:17
@maxbeckers maxbeckers changed the title [Console] [WIP] Support left and right arrow for QuestionHelper [Console] Support left and right arrow for QuestionHelper Nov 24, 2022
@maxbeckers
Copy link
Contributor Author

maxbeckers commented Nov 24, 2022

Now I have a stand that needs to be reviewed.

I add a zip for easier testing. Feel free to test and try it and find all the bugs.
test-48287.zip

The solution might not be perfect, but for me it seems to work fine. Only with ESC the solution has it's problems. Maybe someone has for that an idea. ESC is "\033" and ctrl buttons as well start with this char.

Current Test failures seem not to be related to the PR.

Copy link
Member

@GromNaN GromNaN left a comment

Choose a reason for hiding this comment

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

This is a great improvement for user experience in interactive commands.

I understand that is a proof-of-concept. There is other key and key combination to get the same experience than the shell.

  • Key HOME and END (same as Ctrl+A and Ctrl+E)
  • Key Alt+Right Arrow and Alt+Left Arrow move from word to word (I just discovered that the behavior is not the same on bash or zsh, the - is a word-breaker on bash, not on zsh)

https://stackoverflow.com/questions/5029118/bash-ctrl-to-move-cursor-between-words-strings

@maxbeckers
Copy link
Contributor Author

@GromNaN thanks for your input. Yes, you're right, it's a proof of concept. Because of that i ask for comments on this approach. Now there is a solid base and i need feedback to optimize it.

I'll have a look to the other key combinations you mentioned.

@maxbeckers maxbeckers changed the title [Console] Support left and right arrow for QuestionHelper [Console] Support a set of control keys and key combinations in QuestionHelper Nov 25, 2022
@maxbeckers
Copy link
Contributor Author

Added the mentioned:

  • Key HOME and END
  • move from word to word

Test zip: test-48287-2.zip

@maxbeckers
Copy link
Contributor Author

maxbeckers commented Nov 26, 2022

One problem for now is with a question in a output section with SymfonyStyle ... I'll try to find a solution for that.

UPDATE found a solution for the section input. not very nice, maybe someone has a good idea here. I create a new ConsoleOutput because the input for the question in section in handled after the section with the stdin/stdout.
test-48287.zip

@maxbeckers maxbeckers force-pushed the patch-36657 branch 3 times, most recently from 0d1cbd1 to f06911a Compare November 29, 2022 12:04
@Chi-teck
Copy link
Contributor

I think the keys should match default readline configuration.
https://github.com/chzyer/readline/blob/v1.5.1/doc/shortcut.md

Though we might not need to cover all readline features.

@maxbeckers
Copy link
Contributor Author

hi @Chi-teck and thanks for the link. I'll have a look to that the next days in detail. But i guess some of the keys are already implemented the way readline has implemented it as well. But of course we'll only have a set of this keys.

@maxbeckers
Copy link
Contributor Author

maxbeckers commented Jan 23, 2023

Hi @Chi-teck,

I updated the description of this PR including all of the currently supported key combinations. It matches the keys of readline and has just some bash additions added.

I of course only added key combinations making sense to the question helper input.

@nicolas-grekas nicolas-grekas modified the milestones: 6.3, 6.4 May 23, 2023
@nicolas-grekas nicolas-grekas modified the milestones: 6.4, 7.1 Nov 15, 2023
@Chi-teck
Copy link
Contributor

Chi-teck commented Nov 29, 2023

The question helper used in Symfony console looks so outdated. Especially when compared with Laravel prompts and some JS implementations. I wonder if it possible to make it swappable. So a third party packages could provide a replacement for it.

@xabbuh xabbuh modified the milestones: 7.1, 7.2 May 15, 2024
@Seldaek
Copy link
Member

Seldaek commented Sep 16, 2024

@maxbeckers could you add support for CTRL-C as well here or is that another problem? See composer/composer#12106 for example.

@maxbeckers
Copy link
Contributor Author

Hi @Seldaek,

good idea. I'm currently on vacation. Afterwards I'll have a look on that.

@maxbeckers
Copy link
Contributor Author

Hi @Seldaek,

I tried to reproduce that. When I use composer, then I can reproduce your problem:
Screenshot 2024-10-02 151343
and i can only exit, when i press enter for no.
But with a simple php script that runs a test command, I can't reproduce that:
Screenshot 2024-10-02 151431
There the Ctrl + C exits the application. (Tested with symfony 5.4 and 7.2)

So it seems to be a bug in relation to run the composer script. => No need to change anything for Ctrl + C in the symfony console componenet from my POV.

@OskarStark OskarStark changed the title [Console] Support a set of control keys and key combinations in QuestionHelper [Console] Support a set of control keys and key combinations in QuestionHelper Oct 2, 2024
@Seldaek
Copy link
Member

Seldaek commented Oct 28, 2024

@maxbeckers ah you're right sorry about that. Figured out some more details here composer/composer#12106 (comment)

@fabpot fabpot modified the milestones: 7.2, 7.3 Nov 20, 2024
@fabpot fabpot modified the milestones: 7.3, 7.4 May 26, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Console DX DX = Developer eXperience (anything that improves the experience of using Symfony) Feature RFC RFC = Request For Comments (proposals about features that you want to be discussed) Status: Needs Review Status: Needs Work
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Console: Support Left/Right Arrow Keys
8 participants