History for TextPrompt to emulate shell history-style functionality #1777
Replies: 4 comments 3 replies
-
Ok, I used your branch and could easily replace my clumsy KeyRead() loop. It took me some time to figure out how to 'enable' the feature by providing the History List from the outside of the Prompt Object and also that I am responsible on my side to archive a line. In hindsight I think this is not a bad idea, because I can decide on my own (e.g. not to store empty lines even when they are treated as valid in my command loop): The Up/Down does work but the down arrow has a wrong border check 😄! You do not get down to the latest entry here. I think you have to change line 104 of the AnsiConsoleExtensions.Input.cs: That said, I do think this is a valuable feature to add. Maybe you could consider to make it someway configurable. Either by adding the History as optional Constructor parameter with null as default, or you could instantiate it as static if not provided. Other Option would be to add another Method to the TextPromptExtensions (like the AllowEmpty()). Could be somewhat like So thx. for your work here and looking forward for a enhanced version :-).... |
Beta Was this translation helpful? Give feedback.
-
yaw. |
Beta Was this translation helpful? Give feedback.
-
some 2nd thoughts:
|
Beta Was this translation helpful? Give feedback.
-
@seanmcelroy We try to limit the number of extensions in the main Spectre.Console library, especially around interactivity, but I encourage you to release something like this as an extension in the Spectre.Console community. If you want inspiration, the Spectre.Console team has created something similar that you might find helpful as an inspiration: https://github.com/spectreconsole/radline/ |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Greetings,
I'd like some input as to what others think about a new feature that would allow
TextPrompt
to support up/down arrow key scrolling of a history of items. The purpose of this feature is to support an experience in the console similar to how a terminal shell history works, where previously-entered commands can be enumerated by hitting 'up arrow' to see progressively older entries, or 'down arrow' to scroll back towards newer entries, eventually erasing that history if down was hit the same number of times as up. I have a use case that gathers input from aTextPrompt
in a long-running do/while loop to provide an interactive CLI experience, and a scrollable history of previously entered commands would be useful. I'm unable to emulate this outside of a modification to TextPrompt, as internally,TextPrompt
does aReadKeyAsync()
on anIAnsiConsole
, which prevents me from separately capturing an up or down arrow key press consistently.The design I would propose is to add a new nullable string enumerable property on
TextPrompt
called History that can contain any enumerable of previous text inputs the programmer wants to make available for scrolling. If this enumerable is null or empty, there is no change in behavior. If this is supplied, it is passed on down intoAnsiConsoleExtensions.ReadLine
, where it is used when anConsoleKey.UpArrow
or aConsoleKey.DownArrow
key is pressed.Because users might print something to the console, such as a styled prompt (which I do with a "
[green]>[/]
" parameter toTextPrompt
's constructor), replacing text works by moving the cursor left the number of spaces of the current length of text recorded by theTextPrompt
, overwriting it with spaces, then moving the cursor left again by the number of spaces of the current length of text, and then writing the appropriate history entry. I'm not certain this accommodates every unique styling situation, and I'm particularly interested in test cases that might challenge this approach.I have a working implementation of this idea along with passing unit tests (but no documentation updates yet) available in my forked feature branch here: https://github.com/seanmcelroy/spectre.console/tree/textprompt-history
I would welcome feedback about the idea, design, and implementation. If this is something a maintainer would consider accepting as a feature, I would proceed by opening an issue, incorporating feedback, and submitting this as a pull request.
Thank you for your consideration!
-Sean
Beta Was this translation helpful? Give feedback.
All reactions