Mayil-AI-Sandbox / kuzudb_jan15

MIT License
0 stars 0 forks source link

Backward Search in CLI (hashtag2725) #47

Open vikramsubramanian opened 7 months ago

vikramsubramanian commented 7 months ago

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. )

mayil-ai[bot] commented 7 months ago

We're not sure how to solve this one.

but here are some ideas that might help you get started -

Summary: Implement backward search functionality in CLI for all platforms.

Implementation ideas

To implement backward search in the CLI similar to the control+r functionality in Unix-like systems, you can follow these steps:

Here's a high-level overview of the implementation:

  1. Define the CTRL_R key action in the enum KEY_ACTION:
enum KEY_ACTION {
    // ... existing key actions ...
    CTRL_R = 18,    /* Ctrl+r */
};
  1. Add a case for CTRL_R in the switch statement:
case CTRL_R: /* ctrl+r, initiate backward search */
    linenoiseEditInitiateBackwardSearch(&l);
    break;
  1. Implement the 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;
        }
    }
}
  1. 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.

  2. 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.

Code snippets to check