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.)
I think you should use third-party line editing library like readline.
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.
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.
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 :/
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...
Most helpful comment
Hi, thanks for the feedback!
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...