MCPcopy Index your code
hub / github.com/nodejs/node / setupReverseSearch

Function setupReverseSearch

lib/internal/repl/utils.js:524–754  ·  view source on GitHub ↗
(repl)

Source from the content-addressed store, hash-verified

522}
523
524function setupReverseSearch(repl) {
525 // Simple terminals can't use reverse search.
526 if (process.env.TERM === 'dumb') {
527 return { reverseSearch() { return false; } };
528 }
529
530 const alreadyMatched = new SafeSet();
531 const labels = {
532 r: 'bck-i-search: ',
533 s: 'fwd-i-search: ',
534 };
535 let isInReverseSearch = false;
536 let historyIndex = -1;
537 let input = '';
538 let cursor = -1;
539 let dir = 'r';
540 let lastMatch = -1;
541 let lastCursor = -1;
542 let promptPos;
543
544 function checkAndSetDirectionKey(keyName) {
545 if (!labels[keyName]) {
546 return false;
547 }
548 if (dir !== keyName) {
549 // Reset the already matched set in case the direction is changed. That
550 // way it's possible to find those entries again.
551 alreadyMatched.clear();
552 dir = keyName;
553 }
554 return true;
555 }
556
557 function goToNextHistoryIndex() {
558 // Ignore this entry for further searches and continue to the next
559 // history entry.
560 alreadyMatched.add(repl.history[historyIndex]);
561 historyIndex += dir === 'r' ? 1 : -1;
562 cursor = -1;
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];

Callers 1

constructorMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…