Open vikramsubramanian opened 7 months ago
but here are some ideas that might help you get started -
Summary: Implement backward search functionality in CLI for all platforms.
To implement backward search in the CLI similar to the control+r
functionality in Unix-like systems, you can follow these steps:
CTRL_R
.CTRL_R
in the switch statement that handles key actions.Here's a high-level overview of the implementation:
CTRL_R
key action in the enum KEY_ACTION
:enum KEY_ACTION {
// ... existing key actions ...
CTRL_R = 18, /* Ctrl+r */
};
CTRL_R
in the switch statement:case CTRL_R: /* ctrl+r, initiate backward search */
linenoiseEditInitiateBackwardSearch(&l);
break;
linenoiseEditInitiateBackwardSearch
function:static void linenoiseEditInitiateBackwardSearch(struct linenoiseState* l) {
std::string searchBuffer;
int historyIndex = history_len - 1; // Start from the end of the history
// Display search prompt
printf("(reverse-i-search)`': ");
fflush(stdout);
// Handle input characters for search
while (true) {
char c;
if (read(l->ifd, &c, 1) <= 0) {
// Handle read error or EOF
break;
}
switch (c) {
case CTRL_H: // Backspace, remove last character from searchBuffer
if (!searchBuffer.empty()) {
searchBuffer.pop_back();
printf("\b \b"); // Erase last character from terminal
fflush(stdout);
}
break;
case ENTER: // Finalize search
// Set the current line to the found history entry
strncpy(l->buf, history[historyIndex], l->buflen);
l->len = l->pos = strlen(l->buf);
refreshLine(l);
return;
default: // Add character to searchBuffer and update search
searchBuffer += c;
printf("%c", c); // Echo character to terminal
fflush(stdout);
// Perform search in history
for (; historyIndex >= 0; --historyIndex) {
if (strstr(history[historyIndex], searchBuffer.c_str())) {
// Found a match, display it
printf("\r(reverse-i-search)`%s': %s", searchBuffer.c_str(), history[historyIndex]);
fflush(stdout);
break;
}
}
break;
}
}
}
Make sure to handle the case where no match is found and allow the user to cancel the search with a key like CTRL_C
.
Update the refreshLine
function if necessary to handle the display of the search prompt and the current match.
This is a high-level overview and the actual implementation may require additional error handling, edge case checks, and integration with the existing codebase. The read
function and terminal output (printf
) are used for simplicity, but you may need to use more sophisticated I/O handling depending on the project's requirements.
This file contains the key bindings for the shell, which is where the backward search functionality would be implemented.
This snippet shows existing key bindings and is a logical place to add a new key binding for backward search.
This snippet defines the enum for key actions, which would need to be updated to include a new key binding for backward search.
In Mac, when you press control+r, you can do backward search. This is very handy. Not sure of the shortcut in mac and linux but let's implement this across all platforms. )