-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathapp.js
More file actions
302 lines (270 loc) · 6.84 KB
/
Copy pathapp.js
File metadata and controls
302 lines (270 loc) · 6.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
"use strict";
import fs from 'fs';
import readline from 'readline';
import properLockFile from 'proper-lockfile';
import ansi from 'ansi-escapes';
import boring from 'boring';
import { inspect } from 'util';
import clipboardFactory from './clipboard.js';
import Editor from './editor.js';
import selectorsByName from './selectors-by-name.js';
import loadHandlerFactories from './load-handler-factories.js';
import loadLanguages from './load-languages.js';
import Screen from './screen.js';
const stdin = process.stdin;
const stdout = process.stdout;
stdin.setRawMode(true);
readline.emitKeypressEvents(stdin);
const screen = new Screen({
stdout,
log
});
const tabSpaces = 2;
const argv = boring();
const filename = argv._[0];
if (!filename && !argv['debug-keycodes']) {
usage();
}
// TODO consider Windows
const localFolder = `${process.env.HOME}/.local`;
const stateFolder = `${localFolder}/state/tome`;
fs.mkdirSync(stateFolder, { recursive: true });
const logFile = fs.createWriteStream(`${stateFolder}/log.txt`, 'utf8');
const clipboard = clipboardFactory({
stateFolder,
lock
});
const hintStack = [
[
'^S: Save',
'^Q: Quit',
'ESC: Select',
'^X: Cut',
'^C: Copy',
'^P: Paste',
'^Z: Undo',
'^Y: Redo',
'^F: Find',
'^B: Block',
'^L: Language'
]
];
let deliverKey;
let editor;
let originalText;
let keyQueue = [];
const handlerFactories = await loadHandlerFactories();
const languages = await loadLanguages();
editor = new Editor({
getKey,
save: saveFile,
close: closeEditor,
status,
selectorsByName,
clipboard,
tabSpaces,
chars: loadFile() || newFile(),
languages,
language: guessLanguage(filename),
hintStack,
handlerFactories,
screen,
log
});
resize();
stdin.on('keypress', (c, k) => {
let key;
if ((c == null) || (c.charCodeAt(0) < 32) || (c.charCodeAt(0) === 127)) {
if (k.name == null) {
// ignore occasional undefined keys on mac
return;
}
if (k.shift) {
k.name = `shift-${k.name}`;
}
if (k.ctrl) {
k.name = `control-${k.name}`;
}
if ((k.sequence.charCodeAt(0) === 27) && (k.sequence.charCodeAt(1) === 27)) {
// readline isn't quite smart enough on its own to do the right thing if
// ESC is followed quickly by an arrow key, but gives us enough information
// to figure it out ourselves
output('escape');
}
key = k.name;
} else {
key = c;
}
if (key == null) {
// Don't crash on weird input
return;
}
output(key);
function output(key) {
if (deliverKey) {
const fn = deliverKey;
deliverKey = null;
fn(key);
} else {
keyQueue.push(key);
}
}
});
process.on('SIGWINCH', () => {
resize();
});
process.on('SIGCONT', () => {
// In case the user remaps the keyboard to free up control-z for this purpose.
//
// Returning from control-Z we have to go back into raw mode in two steps
// https://stackoverflow.com/questions/48483796/stdin-setrawmode-not-working-after-resuming-from-background
stdin.setRawMode(false);
stdin.setRawMode(true);
stdin.resume();
stdin.setEncoding('utf8');
});
screen.draw();
while (true) {
const key = await getKey();
await editor.acceptKey(key);
}
function shortFilename(prompt) {
return filename.split('/').pop().substring(0, stdout.columns - (prompt || '').length - 5);
}
function printKey(key) {
console.log(name || key.split('').map(ch => ch.charCodeAt(0)).join(',') + `:${key}`);
}
function logCodes(s) {
try {
logFile.write(s.split('').map(ch => ch.charCodeAt(0)).join(' ') + '\n');
} catch (e) {
log('logCodes failed on non string argument:');
log(s);
}
}
function log(...args) {
for (let arg of args) {
if ((typeof arg) === 'object') {
arg = inspect(arg, { depth: 10 });
}
logFile.write(arg + '\n');
}
}
function lock(filename) {
return properLockFile(filename, {
retries: {
retries: 30,
factor: 1,
minTimeout: 1000,
maxTimeout: 1000
},
// Avoid chicken and egg problem when the file does not exist yet
realpath: false
});
}
function loadFile() {
if (!fs.existsSync(filename)) {
return false;
}
// Emoji-safe split by character (split('') is not safe)
const content = fs.readFileSync(filename, 'utf8').split('\n').map(line => [...line]);
if (!content.length) {
content.push([]);
}
originalText = getText(content);
return content;
}
function newFile() {
return [ [] ];
}
function saveFile() {
fs.writeFileSync(filename, getText(editor.chars));
}
function getText(chars) {
return chars.map(line => line.join('')).join('\n');
}
async function closeEditor() {
const text = getText(editor.chars);
if (text !== originalText) {
if (await confirm('Save before exiting? [Y/n]', true)) {
saveFile();
}
}
stdout.write(ansi.clearScreen);
process.exit(0);
}
async function confirm(msg, def) {
status(msg);
screen.cursor(screen.width - 1, screen.height - 3);
screen.draw();
const response = await getKey();
if (def === true) {
return ((response !== 'n') && (response !== 'N'));
} else {
return ((response === 'y') || (response === 'Y'));
}
}
// Returns a promise for the next key pressed
function getKey() {
if (keyQueue.length) {
return keyQueue.shift();
}
return new Promise(resolve => {
deliverKey = resolve;
});
}
function usage() {
process.stderr.write('Usage: tome filename\n');
process.exit(1);
}
function resize() {
screen.resize();
editor.resize(process.stdout.columns, process.stdout.rows - 3);
}
function guessLanguage(filename) {
const matches = filename.match(/\.([^\.]+)$/);
if (matches) {
const found = Object.values(languages).find(language => language.extensions.includes(matches[1]));
return found || languages.default;
}
return languages.default;
}
function status(prompt) {
const hints = hintStack[hintStack.length - 1];
const width = Math.max(...hints.map(s => s.length)) + 2;
let col = 0;
let row = process.stdout.rows - 2;
for (const hint of hints) {
if (col + width >= process.stdout.columns) {
fillRest();
row++;
if (row >= process.stdout.rows) {
break;
}
}
for (let sx = 0; (sx < width); sx++) {
screen.set(col + sx, row, (sx < hint.length) ? hint.charAt(sx) : ' ');
}
col += width;
}
while (row < process.stdout.rows) {
for (let sx = col; (sx < screen.width); sx++) {
screen.set(sx, row, ' ');
}
col = 0;
row++;
}
const left = `${editor.row + 1} ${editor.col + 1} ${shortFilename()}`;
const right = (prompt !== false) ? prompt : '';
const s = left + ' '.repeat(process.stdout.columns - 1 - right.length - left.length) + right;
for (let i = 0; (i < s.length); i++) {
screen.set(i, process.stdout.rows - 3, s.charAt(i));
}
function fillRest() {
while (col < screen.width) {
screen.set(col, row, ' ');
col++;
}
col = 0;
}
}