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

Function print

lib/internal/repl/utils.js:623–680  ·  view source on GitHub ↗
(outputLine, inputLine, cursor = repl.cursor)

Source from the content-addressed store, hash-verified

621 }
622
623 function print(outputLine, inputLine, cursor = repl.cursor) {
624 // TODO(BridgeAR): Resizing the terminal window hides the overlay. To fix
625 // that, readline must be aware of this information. It's probably best to
626 // add a couple of properties to readline that allow to do the following:
627 // 1. Add arbitrary data to the end of the current line while not counting
628 // towards the line. This would be useful for the completion previews.
629 // 2. Add arbitrary extra lines that do not count towards the regular line.
630 // This would be useful for both, the input preview and the reverse
631 // search. It might be combined with the first part?
632 // 3. Add arbitrary input that is "on top" of the current line. That is
633 // useful for the reverse search.
634 // 4. To trigger the line refresh, functions should be used to pass through
635 // the information. Alternatively, getters and setters could be used.
636 // That might even be more elegant.
637 // The data would then be accounted for when calling `_refreshLine()`.
638 // This function would then look similar to:
639 // repl.overlay(outputLine);
640 // repl.addTrailingLine(inputLine);
641 // repl.setCursor(cursor);
642 // More potential improvements: use something similar to stream.cork().
643 // Multiple cursor moves on the same tick could be prevented in case all
644 // writes from the same tick are combined and the cursor is moved at the
645 // tick end instead of after each operation.
646 let rows = 0;
647 if (lastMatch !== -1) {
648 const line = StringPrototypeSlice(repl.history[lastMatch], 0, lastCursor);
649 rows = repl._getDisplayPos(`${repl.getPrompt()}${line}`).rows;
650 cursorTo(repl.output, promptPos.cols);
651 } else if (isInReverseSearch && repl.line !== '') {
652 rows = repl.getCursorPos().rows;
653 cursorTo(repl.output, promptPos.cols);
654 }
655 if (rows !== 0)
656 moveCursor(repl.output, 0, -rows);
657
658 if (isInReverseSearch) {
659 clearScreenDown(repl.output);
660 repl.output.write(`${outputLine}\n${inputLine}`);
661 } else {
662 repl.output.write(`\n${inputLine}`);
663 }
664
665 lastMatch = -1;
666
667 // To know exactly how many rows we have to move the cursor back we need the
668 // cursor rows, the output rows and the input rows.
669 const prompt = repl.getPrompt();
670 const cursorLine = prompt + StringPrototypeSlice(outputLine, 0, cursor);
671 const cursorPos = repl._getDisplayPos(cursorLine);
672 const outputPos = repl._getDisplayPos(`${prompt}${outputLine}`);
673 const inputPos = repl._getDisplayPos(inputLine);
674 const inputRows = inputPos.rows - (inputPos.cols === 0 ? 1 : 0);
675
676 rows = -1 - inputRows - (outputPos.rows - cursorPos.rows);
677
678 moveCursor(repl.output, 0, rows);
679 cursorTo(repl.output, cursorPos.cols);
680 }

Callers 2

searchFunction · 0.70
reverseSearchFunction · 0.70

Calls 6

cursorToFunction · 0.85
moveCursorFunction · 0.85
clearScreenDownFunction · 0.85
getPromptMethod · 0.80
getCursorPosMethod · 0.80
writeMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…