Skip to content

Commit fcf01f7

Browse files
committed
repl: use unicode chars for new lines
1 parent 8f52797 commit fcf01f7

2 files changed

Lines changed: 34 additions & 4 deletions

File tree

lib/internal/repl/history.js

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
'use strict';
22

33
const {
4+
ArrayPrototypeFilter,
45
ArrayPrototypeJoin,
6+
ArrayPrototypeMap,
7+
ArrayPrototypeSlice,
58
Boolean,
69
FunctionPrototype,
10+
RegExpPrototypeSymbolReplace,
711
RegExpPrototypeSymbolSplit,
12+
StringPrototypeReplace,
13+
StringPrototypeSplit,
814
StringPrototypeTrim,
915
} = primordials;
1016

@@ -23,6 +29,9 @@ const noop = FunctionPrototype;
2329
// XXX(chrisdickinson): The 15ms debounce value is somewhat arbitrary.
2430
// The debounce is to guard against code pasted into the REPL.
2531
const kDebounceHistoryMS = 15;
32+
const kCommandSeparator = '\0';
33+
const kLineSeparator = '\u2028'; // Unicode LINE SEPARATOR
34+
const kLineFeed = '\n';
2635

2736
module.exports = setupHistory;
2837

@@ -97,9 +106,27 @@ function setupHistory(repl, historyPath, ready) {
97106
}
98107

99108
if (data) {
100-
repl.history = RegExpPrototypeSymbolSplit(/\n+/, data, repl.historySize);
101-
} else {
102-
repl.history = [];
109+
const splitOnCommandSeparator = StringPrototypeSplit(data, kCommandSeparator);
110+
if (splitOnCommandSeparator.length > 1) {
111+
repl.history = ArrayPrototypeFilter(
112+
ArrayPrototypeMap(splitOnCommandSeparator,
113+
(cmd) => RegExpPrototypeSymbolReplace(
114+
/\r?\n+/g,
115+
StringPrototypeReplace(cmd, kLineSeparator, kLineFeed),
116+
kLineFeed,
117+
),
118+
),
119+
Boolean,
120+
);
121+
} else {
122+
repl.history = RegExpPrototypeSymbolSplit(/\r?\n+/, data);
123+
}
124+
125+
repl.history = ArrayPrototypeSlice(
126+
repl.history,
127+
0,
128+
repl.historySize,
129+
);
103130
}
104131

105132
fs.open(historyPath, 'r+', onhandle);
@@ -141,7 +168,10 @@ function setupHistory(repl, historyPath, ready) {
141168
return;
142169
}
143170
writing = true;
144-
const historyData = ArrayPrototypeJoin(repl.history, '\n');
171+
const historyData = ArrayPrototypeJoin(
172+
ArrayPrototypeMap(repl.history, (cmd) => StringPrototypeReplace(`${cmd}`, kLineFeed, kLineSeparator)),
173+
kCommandSeparator,
174+
);
145175
fs.write(repl._historyHandle, historyData, 0, 'utf8', onwritten);
146176
}
147177

0 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)