MCPcopy Index your code
hub / github.com/nodejs/node / [kAddNewLineOnTTY]

Method [kAddNewLineOnTTY]

lib/internal/readline/interface.js:966–1055  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

964 // No matter where the new line was entered.
965 // This function should only be used when the output is a TTY
966 [kAddNewLineOnTTY]() {
967 // Restore terminal state and store current line
968 this[kRestorePreviousState]();
969 const originalLine = this.line;
970
971 // Split the line at the current cursor position
972 const beforeCursor = StringPrototypeSlice(this.line, 0, this.cursor);
973 let afterCursor = StringPrototypeSlice(this.line, this.cursor, this.line.length);
974
975 // Add the new line where the cursor is at
976 this[kSetLine](`${beforeCursor}\n${afterCursor}`);
977
978 // To account for the new line
979 this.cursor += 1;
980
981 const hasContentAfterCursor = afterCursor.length > 0;
982 const cursorIsNotOnFirstLine = this.prevRows > 0;
983 let needsRewriteFirstLine = false;
984
985 // Handle cursor positioning based on different scenarios
986 if (hasContentAfterCursor) {
987 const splitBeg = StringPrototypeSplit(beforeCursor, '\n');
988 // Determine if we need to rewrite the first line
989 needsRewriteFirstLine = splitBeg.length < 2;
990
991 // If the cursor is not on the first line
992 if (cursorIsNotOnFirstLine) {
993 const splitEnd = StringPrototypeSplit(afterCursor, '\n');
994
995 // If the cursor when I pressed enter was at least on the second line
996 // I need to completely erase the line where the cursor was pressed because it is possible
997 // That it was pressed in the middle of the line, hence I need to write the whole line.
998 // To achieve that, I need to reach the line above the current line coming from the end
999 const dy = splitEnd.length + 1;
1000
1001 // Calculate how many Xs we need to move on the right to get to the end of the line
1002 const dxEndOfLineAbove = (splitBeg[splitBeg.length - 2] || '').length + kMultilinePrompt.description.length;
1003 moveCursor(this.output, dxEndOfLineAbove, -dy);
1004
1005 // This is the line that was split in the middle
1006 // Just add it to the rest of the line that will be printed later
1007 afterCursor = `${splitBeg[splitBeg.length - 1]}\n${afterCursor}`;
1008 } else {
1009 // Otherwise, go to the very beginning of the first line and erase everything
1010 const dy = StringPrototypeSplit(originalLine, '\n').length;
1011 moveCursor(this.output, 0, -dy);
1012 }
1013
1014 // Erase from the cursor to the end of the line
1015 clearScreenDown(this.output);
1016
1017 if (cursorIsNotOnFirstLine) {
1018 this[kWriteToOutput]('\n');
1019 }
1020 }
1021
1022 if (needsRewriteFirstLine) {
1023 this[kWriteToOutput](`${this[kPrompt]}${beforeCursor}\n${kMultilinePrompt.description}`);

Callers

nothing calls this directly

Calls 3

getCursorPosMethod · 0.95
moveCursorFunction · 0.85
clearScreenDownFunction · 0.85

Tested by

no test coverage detected