secrez 2.2.0-beta.2 → 2.2.0-beta.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/editor/MemoryEditor.js +40 -16
package/package.json
CHANGED
|
@@ -15,10 +15,34 @@ const insert = (s, x, c) => s.slice(0, x) + c + s.slice(x);
|
|
|
15
15
|
const EDITOR_STATUS =
|
|
16
16
|
"Ctrl-d save · Ctrl-c abort · Ctrl-k del line · Ctrl-a home · Ctrl-e end · Ctrl-g reset";
|
|
17
17
|
|
|
18
|
+
// ansi-escapes `clearScreen` is RIS (\u001bc). Terminal.app answers that
|
|
19
|
+
// reset with a blue background and a foreground that does not contrast, so
|
|
20
|
+
// the file text is drawn and then invisible. Erase the viewport, home the
|
|
21
|
+
// cursor, and restore the default colors before painting the buffer.
|
|
22
|
+
function clearViewport() {
|
|
23
|
+
return "\u001b[0m" + esc.eraseScreen + esc.cursorTo(0, 0);
|
|
24
|
+
}
|
|
25
|
+
|
|
18
26
|
function terminalRows() {
|
|
19
27
|
return process.stdout.rows || 24;
|
|
20
28
|
}
|
|
21
29
|
|
|
30
|
+
function terminalColumns() {
|
|
31
|
+
return Math.max(2, process.stdout.columns || 80);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// The footer is drawn on its own rows, one column short of the edge.
|
|
35
|
+
// A character in the last column wraps, and a wrap on the last row scrolls
|
|
36
|
+
// the viewport, which moves the text above the footer.
|
|
37
|
+
function statusChunks(status, columns) {
|
|
38
|
+
const width = Math.max(1, columns - 1);
|
|
39
|
+
const chunks = [];
|
|
40
|
+
for (let i = 0; i < status.length; i += width) {
|
|
41
|
+
chunks.push(status.slice(i, i + width));
|
|
42
|
+
}
|
|
43
|
+
return chunks.length ? chunks : [""];
|
|
44
|
+
}
|
|
45
|
+
|
|
22
46
|
function keyAction(key) {
|
|
23
47
|
if (key.ctrl) {
|
|
24
48
|
if (key.name === "a") return "first";
|
|
@@ -55,7 +79,7 @@ const Editor = {
|
|
|
55
79
|
abort(c) {
|
|
56
80
|
this.done = true;
|
|
57
81
|
this.aborted = !(c.ctrl && c.name === "d");
|
|
58
|
-
this.out.write(
|
|
82
|
+
this.out.write(clearViewport());
|
|
59
83
|
this.close();
|
|
60
84
|
},
|
|
61
85
|
|
|
@@ -176,23 +200,24 @@ const Editor = {
|
|
|
176
200
|
},
|
|
177
201
|
|
|
178
202
|
render() {
|
|
179
|
-
if (this.firstRender) {
|
|
180
|
-
this.firstRender = false;
|
|
181
|
-
this.out.write("\n".repeat(terminalRows()));
|
|
182
|
-
}
|
|
183
|
-
|
|
184
203
|
const rows = terminalRows();
|
|
185
|
-
const
|
|
204
|
+
const columns = terminalColumns();
|
|
205
|
+
const chunks = statusChunks(EDITOR_STATUS, columns);
|
|
206
|
+
const statusTop = Math.max(0, rows - chunks.length);
|
|
186
207
|
const { lines, cursorX, cursorY } = this;
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
esc.cursorTo(0,
|
|
208
|
+
let out = clearViewport() + lines.join("\n");
|
|
209
|
+
chunks.forEach((chunk, i) => {
|
|
210
|
+
out +=
|
|
211
|
+
esc.cursorTo(0, statusTop + i) +
|
|
212
|
+
esc.eraseLine +
|
|
191
213
|
"\x1b[2m" +
|
|
192
|
-
|
|
193
|
-
"\x1b[0m"
|
|
194
|
-
|
|
195
|
-
);
|
|
214
|
+
chunk +
|
|
215
|
+
"\x1b[0m";
|
|
216
|
+
});
|
|
217
|
+
const cursorRow = Math.min(cursorY, Math.max(0, statusTop - 1));
|
|
218
|
+
const cursorCol = Math.min(cursorX, columns - 1);
|
|
219
|
+
out += esc.cursorTo(cursorCol, cursorRow);
|
|
220
|
+
this.out.write(out);
|
|
196
221
|
},
|
|
197
222
|
};
|
|
198
223
|
|
|
@@ -207,7 +232,6 @@ function createEditor(text) {
|
|
|
207
232
|
initialLines: [],
|
|
208
233
|
cursorX: 0,
|
|
209
234
|
cursorY: 0,
|
|
210
|
-
firstRender: true,
|
|
211
235
|
done: false,
|
|
212
236
|
aborted: false,
|
|
213
237
|
value: "",
|