Feature request: API function for adding text lines to console history #369

Closed
opened 2026-01-30 21:50:11 +00:00 by claunia · 6 comments
Owner

Originally created by @HBelusca on GitHub (Aug 31, 2018).

While there are existing console-helper API functions to set and retrieve command aliases,
and functions to retrieve or reset the command history (aka. strings read through the ReadConsole() function): GetConsoleCommandHistoryLength() and GetConsoleCommandHistory(), etc...,
there is NO function for adding a string to the history.

I have attempted to program such a functionality using only the existing APIs for that, see for example:
https://github.com/HBelusca/reactos/blob/cmd_improvements/sdk/lib/conutils/history.c#L369
and associated helpers,
which basically simulates console line input (using WriteConsoleInput()) by injecting key events, ending with ENTER keypress, then reading this input immediately via ReadConsole() while the console is in ENABLE_LINE_INPUT | ENABLE_ECHO_INPUT mode, in order to add a new string into the history.
But this method has its drawbacks, amongst which its slowness.

A proposition for the feature, based on the existing alias APIs and the history getter functions, would be:

BOOL
AddToConsoleHistory(
    _In_ LPCTSTR lpExeName,
    _In_ LPCTSTR lpString
);

which appends the string pointed by lpString to the current console history (selected via the lpExeName), and would return TRUE on success, FALSE on failure and sets a win32 last error.
If the history grows larger than its max size (specified by existing API) it would then trim the oldest strings in it.

This feature, together with the already-existing GetConsoleCommandHistoryLength() and GetConsoleCommandHistory() functions, would allow third-party console programs to load and save their own history files (similar to e.g. bash with its .bash_history file), while using ReadConsole() for command input (and TAB-completion).

(NOTE: It could also be used by doskey.exe if an additional option to load history files -- much similar to the one that loads "macros" (aka. aliases) files -- is implemented.)

Originally created by @HBelusca on GitHub (Aug 31, 2018). While there are existing console-helper API functions to set and retrieve command aliases, and functions to retrieve or reset the command history (aka. strings read through the ReadConsole() function): GetConsoleCommandHistoryLength() and GetConsoleCommandHistory(), etc..., there is NO function for adding a string to the history. I have attempted to program such a functionality using only the existing APIs for that, see for example: https://github.com/HBelusca/reactos/blob/cmd_improvements/sdk/lib/conutils/history.c#L369 and associated helpers, which basically simulates console line input (using WriteConsoleInput()) by injecting key events, ending with ENTER keypress, then reading this input immediately via ReadConsole() while the console is in ENABLE_LINE_INPUT | ENABLE_ECHO_INPUT mode, in order to add a new string into the history. But this method has its drawbacks, amongst which its slowness. A proposition for the feature, based on the existing alias APIs and the history getter functions, would be: ```c BOOL AddToConsoleHistory( _In_ LPCTSTR lpExeName, _In_ LPCTSTR lpString ); ``` which appends the string pointed by `lpString` to the current console history (selected via the `lpExeName`), and would return TRUE on success, FALSE on failure and sets a win32 last error. If the history grows larger than its max size (specified by existing API) it would then trim the oldest strings in it. This feature, together with the already-existing GetConsoleCommandHistoryLength() and GetConsoleCommandHistory() functions, would allow third-party console programs to load and save their own history files (similar to e.g. bash with its .bash_history file), while using ReadConsole() for command input (and TAB-completion). (NOTE: It could also be used by doskey.exe if an additional option to load history files -- much similar to the one that loads "macros" (aka. aliases) files -- is implemented.)
claunia added the Issue-FeatureProduct-ConhostNeeds-Tag-FixArea-Server labels 2026-01-30 21:50:11 +00:00
Author
Owner

@ysc3839 commented on GitHub (Sep 1, 2018):

I think you should use third-party line editing library like readline.

@ysc3839 commented on GitHub (Sep 1, 2018): I think you should use third-party line editing library like readline.
Author
Owner

@HBelusca commented on GitHub (Sep 1, 2018):

I'm talking here about regular win32 console applications, not *nix ones (I just mentioned bash as an illustrative example) nor .Net or whatever else. And if you point to me any "readline" library for win32, these kind of libraries just reimplement the whole readline stuff (not only the history feature) by doing as many similar convoluted maneuvers as the ones I described above (i.e. dealing with key events to implement the whole thing).
I clearly don't intend to use such a library for simple console applications, where similar results could be obtained by using the regular ReadConsole() plus the existing history functions AND a single new one history function to add user-specified lines to the history.

@HBelusca commented on GitHub (Sep 1, 2018): I'm talking here about regular win32 console applications, not *nix ones (I just mentioned bash as an illustrative example) nor .Net or whatever else. And if you point to me any "readline" library for win32, these kind of libraries just reimplement the **whole** readline stuff (not only the history feature) by doing as many similar convoluted maneuvers as the ones I described above (i.e. dealing with key events to implement the whole thing). I clearly don't intend to use such a library for simple console applications, where similar results could be obtained by using the regular ReadConsole() plus the existing history functions *AND* a single new one history function to add user-specified lines to the history.
Author
Owner

@ysc3839 commented on GitHub (Sep 2, 2018):

I suggest this because it takes long time waiting for Microsoft add this feature and old systems still can't use such feature.
A readline library solves this problem while compatible with old systems.

@ysc3839 commented on GitHub (Sep 2, 2018): I suggest this because it takes long time waiting for Microsoft add this feature and old systems still can't use such feature. A readline library solves this problem while compatible with old systems.
Author
Owner

@zadjii-msft commented on GitHub (Sep 4, 2018):

Great suggestion, however, we have no plans to extend the current console client application API at the time.

I do really like the idea of a readline library on windows that people can use without needing to rely on the console history APIs, though implementing it ourselves would be pretty far down the backlog unfortunately :/

@zadjii-msft commented on GitHub (Sep 4, 2018): Great suggestion, however, we have no plans to extend the current console client application API at the time. I do really like the idea of a readline library on windows that people can use without needing to rely on the console history APIs, though implementing it ourselves would be pretty far down the backlog unfortunately :/
Author
Owner

@HBelusca commented on GitHub (Sep 4, 2018):

Hi, thanks for the feedback!

I do really like the idea of a readline library on windows that people can use without needing to rely on the console history APIs [...]

There is either the port of the GNU readline, or clink ; for the latter it is actually more targeting cmd.exe (via hooking ReadConsole() and WriteConsole()), but a fork could be developed to transform it into a separate library. And there are certainly all the separate readline ports used by win32 ports of originally *nix apps...

@HBelusca commented on GitHub (Sep 4, 2018): Hi, thanks for the feedback! > I do really like the idea of a readline library on windows that people can use without needing to rely on the console history APIs [...] There is either the port of the [GNU readline](http://gnuwin32.sourceforge.net/packages/readline.htm), or [clink](https://mridgers.github.io/clink/) ; for the latter it is actually more targeting cmd.exe (via hooking ReadConsole() and WriteConsole()), but a fork could be developed to transform it into a separate library. And there are certainly all the separate readline ports used by win32 ports of originally *nix apps...
Author
Owner

@zadjii-msft commented on GitHub (Nov 22, 2021):

This has been sitting open for a while, but honestly, we're not planning on making any changes to the Console API surface any time soon. I've added this to our internal note of "commandline/readline API ideas", but that's more of a vague idea than something that's anywhere near shipping. Thanks for the suggestion though!

@zadjii-msft commented on GitHub (Nov 22, 2021): This has been sitting open for a while, but honestly, we're not planning on making any changes to the Console API surface any time soon. I've added this to our internal note of "commandline/readline API ideas", but that's more of a vague idea than something that's anywhere near shipping. Thanks for the suggestion though!
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/terminal#369