| 2680 | } |
| 2681 | |
| 2682 | static void refreshSearch(struct linenoiseState* l) { |
| 2683 | std::string search_prompt; |
| 2684 | std::string no_matches_text = std::string(l->buf); |
| 2685 | std::string truncatedSearchText = ""; |
| 2686 | bool no_matches = l->search_index >= l->search_matches.size(); |
| 2687 | if (l->search_buf.empty()) { |
| 2688 | l->prev_search_match = l->buf; |
| 2689 | l->prev_search_match_history_index = history_len - 1 - l->history_index; |
| 2690 | search_prompt = "bck-i-search: _"; |
| 2691 | } else { |
| 2692 | std::string search_text; |
| 2693 | std::string matches_text; |
| 2694 | search_text = l->search_buf; |
| 2695 | if (!no_matches) { |
| 2696 | search_prompt = "bck-i-search: "; |
| 2697 | } else { |
| 2698 | no_matches_text = std::string(l->prev_search_match); |
| 2699 | search_prompt = "failing-bck-i-search: "; |
| 2700 | } |
| 2701 | |
| 2702 | char* search_buf = (char*)search_text.c_str(); |
| 2703 | size_t search_len = search_text.size(); |
| 2704 | |
| 2705 | size_t cols = l->cols - search_prompt.length() - 1; |
| 2706 | |
| 2707 | size_t render_pos = 0; |
| 2708 | char emptyHighlightBuf[LINENOISE_MAX_LINE]; |
| 2709 | truncateText(search_buf, search_len, search_len, cols, false, 0, render_pos, |
| 2710 | emptyHighlightBuf); |
| 2711 | truncatedSearchText = std::string(search_buf, search_len); |
| 2712 | search_prompt += truncatedSearchText; |
| 2713 | search_prompt += "_"; |
| 2714 | } |
| 2715 | |
| 2716 | linenoiseState clone = *l; |
| 2717 | l->plen = search_prompt.size(); |
| 2718 | if (no_matches) { |
| 2719 | // if there are no matches render the no_matches_text |
| 2720 | l->buf = (char*)no_matches_text.c_str(); |
| 2721 | l->len = no_matches_text.size(); |
| 2722 | l->pos = 0; |
| 2723 | } else { |
| 2724 | // if there are matches render the current history item |
| 2725 | auto search_match = l->search_matches[l->search_index]; |
| 2726 | auto history_index = search_match.history_index; |
| 2727 | auto cursor_position = search_match.match_end; |
| 2728 | l->buf = history[history_index]; |
| 2729 | l->len = strlen(history[history_index]); |
| 2730 | l->pos = cursor_position; |
| 2731 | l->prev_search_match = l->buf; |
| 2732 | l->prev_search_match_history_index = history_index; |
| 2733 | } |
| 2734 | refreshSearchMultiLine(l, search_prompt.c_str(), truncatedSearchText.c_str()); |
| 2735 | |
| 2736 | l->buf = clone.buf; |
| 2737 | l->len = clone.len; |
| 2738 | l->pos = clone.pos; |
| 2739 | l->plen = clone.plen; |
no test coverage detected