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

Open
opened 2026-01-30 21:50:03 +00:00 by claunia · 0 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:03 +00:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/terminal#363