-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdis-cli.js
More file actions
executable file
·195 lines (172 loc) · 6.12 KB
/
Copy pathdis-cli.js
File metadata and controls
executable file
·195 lines (172 loc) · 6.12 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
#!/usr/bin/env node
const path = require('path');
const { execFileSync } = require('child_process');
const {
HOTKEYS,
MACRO_KEYS,
getModKey
} = require('./src/macros');
const MacroRunner = require('./src/macro-runner');
const { getShowIndicator, loadSettings } = require('./src/settings');
const INVOCATION_CWD = process.cwd();
const MOD_KEY = getModKey(process.platform, true);
const ALT_SCREEN_ENTER = '\x1b[?1049h';
const ALT_SCREEN_EXIT = '\x1b[?1049l';
const DISABLE_WIN32_INPUT = '\x1b[?9001l';
const ENABLE_WIN32_INPUT = '\x1b[?9001h';
if (process.env.NECO_RUNNING) {
console.error('neco: already running inside neco');
process.exit(1);
}
const rawCommand = process.argv[2];
const args = process.argv.slice(3);
const DEFAULT_SHELL = process.platform === 'win32'
? (process.env.ComSpec || 'cmd.exe')
: (process.env.SHELL || '/bin/sh');
const shellMode = !rawCommand;
if (rawCommand === '--help' || rawCommand === '-h') {
console.log([
`Usage: neco [command] [args...]`,
``,
`Wraps any CLI with a file explorer and hotkey macros.`,
`With no command, wraps a terminal (${DEFAULT_SHELL}).`,
``,
` neco claude`,
` neco claude --resume`,
` neco npm run dev`,
``,
`Hotkeys:`,
` ${MOD_KEY}+1 Open Commander (file explorer + macro editor)`,
` ${MOD_KEY}+2-9 Run macro`,
``,
`Press F1 inside neco for full help.`,
].join('\n'));
process.exit(0);
}
const INDICATOR_PREFIX = '🫡 ';
function injectTitlePrefix(data, prefix) {
return data.replace(/(\x1b\][012];)(.*?)(\x07|\x1b\\)/gs, (_, osc, title, term) => {
return title.startsWith(prefix) ? _ : `${osc}${prefix}${title}${term}`;
});
}
function showSplash() {
process.stdout.write(ALT_SCREEN_ENTER);
const shellLine = shellMode ? ` \x1b[33m[NO COMMAND] Wrapping terminal (${DEFAULT_SHELL})\x1b[0m\n` : '';
console.log(`\x1b[2J\x1b[H\n \x1b[1m\x1b[36mNEURAL COMMANDER\x1b[0m\n\n \x1b[36m░█▄░█░█▀▀░█▀▀░█▀█\x1b[0m\n \x1b[36m░█░▀█░█▀▀░█░░░█░█\x1b[0m\n \x1b[36m░▀░░▀░▀▀▀░▀▀▀░▀▀▀\x1b[0m\n\n \x1b[33m( ̄^ ̄)ゞ\x1b[0m\n \x1b[33mSTANDING BY, SIR.\x1b[0m\n\n \x1b[32m[SYSTEM READY]\x1b[0m\n \x1b[33m[HOTKEYS]: \x1b[1m${MOD_KEY}+1\x1b[0m (Explorer) | \x1b[1m${MOD_KEY}+2..9\x1b[0m (Macros)\n${shellLine}`);
}
let command = rawCommand || DEFAULT_SHELL;
let spawnArgs = args;
if (process.platform === 'win32') {
if (shellMode) {
command = DEFAULT_SHELL;
spawnArgs = [];
} else {
command = 'cmd.exe';
spawnArgs = ['/c', rawCommand, ...args];
}
} else if (!shellMode && !path.isAbsolute(rawCommand)) {
try { command = execFileSync('which', [rawCommand], { encoding: 'utf8' }).trim(); } catch (e) {}
}
let showIndicator = getShowIndicator();
showSplash();
if (showIndicator) process.stdout.write(`\x1b]0;${INDICATOR_PREFIX}neco\x07`);
setTimeout(async () => {
process.stdout.write(ALT_SCREEN_EXIT);
let pty;
try {
pty = require('node-pty');
} catch (e) {
console.error(`neco: node-pty failed to load: ${e.message}`);
process.exit(1);
}
const ptyProcess = pty.spawn(command, spawnArgs, {
name: 'xterm-256color',
cols: process.stdout.columns,
rows: process.stdout.rows,
cwd: INVOCATION_CWD,
env: { ...process.env, NECO_RUNNING: '1' }
});
let isMenuOpen = false;
let onInput;
const macroRunner = new MacroRunner({
getMacro: (id) => {
const settings = loadSettings();
return settings.macros && settings.macros[id];
},
onWrite: (value) => ptyProcess.write(value),
onOpenMenu: () => openMenu(),
onError: (error, id) => {
process.stderr.write(`neco: failed to run macro ${id}: ${error.message}\n`);
}
});
const restoreTerminal = () => {
process.stdin.removeListener('data', onInput);
if (process.stdin.isTTY) {
try { process.stdin.setRawMode(false); } catch (e) {}
}
process.stdout.write(ENABLE_WIN32_INPUT);
process.stdout.write(ALT_SCREEN_EXIT);
};
const onResize = () => ptyProcess.resize(process.stdout.columns, process.stdout.rows);
process.once('SIGINT', () => {
restoreTerminal();
ptyProcess.kill();
});
process.once('SIGTERM', () => {
restoreTerminal();
ptyProcess.kill();
});
ptyProcess.onData(data => {
if (!isMenuOpen) {
process.stdout.write(showIndicator ? injectTitlePrefix(data, INDICATOR_PREFIX) : data);
}
});
process.stdout.on('resize', onResize);
onInput = (key) => {
if (macroRunner.shouldSuppressInput()) return;
if (HOTKEYS.includes(key)) { openMenu(); return; }
if (MACRO_KEYS[key]) { macroRunner.run(MACRO_KEYS[key]); return; }
if (!isMenuOpen) ptyProcess.write(key);
};
const setupTerminal = () => {
process.stdin.removeListener('data', onInput);
if (process.stdin.isTTY) process.stdin.setRawMode(true);
process.stdin.resume();
process.stdin.setEncoding('utf8');
process.stdin.on('data', onInput);
};
setupTerminal();
function openMenu() {
if (isMenuOpen) return;
isMenuOpen = true;
process.stdin.removeListener('data', onInput);
if (process.stdin.isTTY) process.stdin.setRawMode(false);
process.stdin.pause();
process.stdout.write(DISABLE_WIN32_INPUT);
process.stdout.write(ALT_SCREEN_ENTER);
const CommanderUI = require('./src/commander-ui');
new CommanderUI(
(selectedPath) => closeMenu(() => ptyProcess.write(selectedPath)),
() => closeMenu(),
INVOCATION_CWD
);
}
function closeMenu(callback) {
process.stdout.write(ALT_SCREEN_EXIT);
process.stdout.write(ENABLE_WIN32_INPUT);
setTimeout(() => {
isMenuOpen = false;
showIndicator = getShowIndicator();
if (showIndicator) process.stdout.write(`\x1b]0;${INDICATOR_PREFIX}neco\x07`);
setupTerminal();
ptyProcess.resize(process.stdout.columns, process.stdout.rows);
if (callback) callback();
}, 100);
}
ptyProcess.onExit(({ exitCode }) => {
process.stdout.removeListener('resize', onResize);
restoreTerminal();
if (showIndicator) process.stdout.write('\x1b]0;\x07');
process.exit(exitCode);
});
}, shellMode ? 3500 : 2000);