()
| 563 | } |
| 564 | |
| 565 | function search() { |
| 566 | // Just print an empty line in case the user removed the search parameter. |
| 567 | if (input === '') { |
| 568 | print(repl.line, `${labels[dir]}_`); |
| 569 | return; |
| 570 | } |
| 571 | // Fix the bounds in case the direction has changed in the meanwhile. |
| 572 | if (dir === 'r') { |
| 573 | if (historyIndex < 0) { |
| 574 | historyIndex = 0; |
| 575 | } |
| 576 | } else if (historyIndex >= repl.history.length) { |
| 577 | historyIndex = repl.history.length - 1; |
| 578 | } |
| 579 | // Check the history entries until a match is found. |
| 580 | while (historyIndex >= 0 && historyIndex < repl.history.length) { |
| 581 | let entry = repl.history[historyIndex]; |
| 582 | // Visualize all potential matches only once. |
| 583 | if (alreadyMatched.has(entry)) { |
| 584 | historyIndex += dir === 'r' ? 1 : -1; |
| 585 | continue; |
| 586 | } |
| 587 | // Match the next entry either from the start or from the end, depending |
| 588 | // on the current direction. |
| 589 | if (dir === 'r') { |
| 590 | // Update the cursor in case it's necessary. |
| 591 | if (cursor === -1) { |
| 592 | cursor = entry.length; |
| 593 | } |
| 594 | cursor = StringPrototypeLastIndexOf(entry, input, cursor - 1); |
| 595 | } else { |
| 596 | cursor = StringPrototypeIndexOf(entry, input, cursor + 1); |
| 597 | } |
| 598 | // Match not found. |
| 599 | if (cursor === -1) { |
| 600 | goToNextHistoryIndex(); |
| 601 | // Match found. |
| 602 | } else { |
| 603 | if (repl.useColors) { |
| 604 | const start = StringPrototypeSlice(entry, 0, cursor); |
| 605 | const end = StringPrototypeSlice(entry, cursor + input.length); |
| 606 | entry = `${start}\x1B[4m${input}\x1B[24m${end}`; |
| 607 | } |
| 608 | print(entry, `${labels[dir]}${input}_`, cursor); |
| 609 | lastMatch = historyIndex; |
| 610 | lastCursor = cursor; |
| 611 | // Explicitly go to the next history item in case no further matches are |
| 612 | // possible with the current entry. |
| 613 | if ((dir === 'r' && cursor === 0) || |
| 614 | (dir === 's' && entry.length === cursor + input.length)) { |
| 615 | goToNextHistoryIndex(); |
| 616 | } |
| 617 | return; |
| 618 | } |
| 619 | } |
| 620 | print(repl.line, `failed-${labels[dir]}${input}_`); |
| 621 | } |
| 622 |
no test coverage detected
searching dependent graphs…