Refactor terminal input handling in StatVitTerm to improve signal management and process control. Update example.js to streamline output handling and modify command execution flow for better user experience. Enhance environment variable settings for terminal compatibility.
This commit is contained in:
@@ -4,9 +4,7 @@ import StatVitTerm from './stat-vit-term.js';
|
|||||||
const termSession = new StatVitTerm();
|
const termSession = new StatVitTerm();
|
||||||
|
|
||||||
termSession.on('std', (data) => {
|
termSession.on('std', (data) => {
|
||||||
// Entferne ANSI Escape Sequenzen für saubere Ausgabe
|
process.stdout.write(data);
|
||||||
const cleanData = data.replace(/\u001b\[[0-9;]*m/g, '').replace(/\u001b\][0-9;]*\u0007/g, '');
|
|
||||||
process.stdout.write(cleanData);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
termSession.on('err', (data) => {
|
termSession.on('err', (data) => {
|
||||||
@@ -20,22 +18,19 @@ termSession.on('close', (code) => {
|
|||||||
termSession.on('ready', () => {
|
termSession.on('ready', () => {
|
||||||
console.log('Terminal ready\n');
|
console.log('Terminal ready\n');
|
||||||
|
|
||||||
// Test mit einem Befehl, der wirklich lange läuft
|
// Top-Befehl starten
|
||||||
console.log('--- Starte langen Prozess ---');
|
console.log('--- Starte top ---');
|
||||||
termSession.input('sleep 10 &\n'); // Hintergrundprozess
|
termSession.input('top\n');
|
||||||
|
|
||||||
|
// Nach 3 Sekunden abbrechen
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
console.log('\n--- Jobs anzeigen ---');
|
console.log('\n--- Top abbrechen mit ^C ---');
|
||||||
termSession.input('jobs\n');
|
termSession.input('^C');
|
||||||
}, 1000);
|
}, 3000);
|
||||||
|
|
||||||
setTimeout(() => {
|
|
||||||
console.log('\n--- Alle Prozesse stoppen ---');
|
|
||||||
termSession.input('^C'); // Das sollte alle Prozesse stoppen
|
|
||||||
}, 2000);
|
|
||||||
|
|
||||||
|
// Nach 4 Sekunden beenden
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
console.log('\n--- Terminal beenden ---');
|
console.log('\n--- Terminal beenden ---');
|
||||||
termSession.kill();
|
termSession.kill();
|
||||||
}, 3000);
|
}, 4000);
|
||||||
});
|
});
|
||||||
@@ -8,7 +8,11 @@ class StatVitTerm extends EventEmitter {
|
|||||||
this.options = {
|
this.options = {
|
||||||
shell: process.platform === 'win32' ? 'cmd.exe' : '/bin/bash',
|
shell: process.platform === 'win32' ? 'cmd.exe' : '/bin/bash',
|
||||||
cwd: process.cwd(),
|
cwd: process.cwd(),
|
||||||
env: process.env,
|
env: {
|
||||||
|
...process.env,
|
||||||
|
PS1: '$ ',
|
||||||
|
TERM: 'xterm-256color'
|
||||||
|
},
|
||||||
...options
|
...options
|
||||||
};
|
};
|
||||||
this.process = null;
|
this.process = null;
|
||||||
@@ -18,10 +22,13 @@ class StatVitTerm extends EventEmitter {
|
|||||||
|
|
||||||
_init() {
|
_init() {
|
||||||
try {
|
try {
|
||||||
this.process = spawn(this.options.shell, [], {
|
// Wichtige Änderung: Shell im richtigen Modus starten
|
||||||
|
const args = process.platform === 'win32' ? [] : ['-i', '-l']; // login shell
|
||||||
|
this.process = spawn(this.options.shell, args, {
|
||||||
stdio: ['pipe', 'pipe', 'pipe'],
|
stdio: ['pipe', 'pipe', 'pipe'],
|
||||||
cwd: this.options.cwd,
|
cwd: this.options.cwd,
|
||||||
env: this.options.env
|
env: this.options.env,
|
||||||
|
detached: true // Wichtig für Signalbehandlung
|
||||||
});
|
});
|
||||||
|
|
||||||
this.process.stdout.on('data', (data) => {
|
this.process.stdout.on('data', (data) => {
|
||||||
@@ -29,7 +36,7 @@ class StatVitTerm extends EventEmitter {
|
|||||||
});
|
});
|
||||||
|
|
||||||
this.process.stderr.on('data', (data) => {
|
this.process.stderr.on('data', (data) => {
|
||||||
this.emit('std', data.toString()); // stderr als std behandeln
|
this.emit('std', data.toString());
|
||||||
});
|
});
|
||||||
|
|
||||||
this.process.on('close', (code) => {
|
this.process.on('close', (code) => {
|
||||||
@@ -42,9 +49,10 @@ class StatVitTerm extends EventEmitter {
|
|||||||
});
|
});
|
||||||
|
|
||||||
this.isRunning = true;
|
this.isRunning = true;
|
||||||
|
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
this.emit('ready');
|
this.emit('ready');
|
||||||
}, 100);
|
}, 500);
|
||||||
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
this.emit('err', err.message);
|
this.emit('err', err.message);
|
||||||
@@ -59,11 +67,28 @@ class StatVitTerm extends EventEmitter {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
if (data === '^C') {
|
if (data === '^C') {
|
||||||
// WICHTIG: Sende Ctrl+C direkt an stdin
|
// Methode 1: SIGINT an die Prozessgruppe senden
|
||||||
this.process.stdin.write('\x03');
|
if (process.platform !== 'win32' && this.process.pid) {
|
||||||
|
try {
|
||||||
|
process.kill(-this.process.pid, 'SIGINT');
|
||||||
|
} catch (e) {
|
||||||
|
// Fallback: Ctrl+C direkt senden
|
||||||
|
this.process.stdin.write('\x03');
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
this.process.stdin.write('\x03');
|
||||||
|
}
|
||||||
return true;
|
return true;
|
||||||
} else if (data === '^Z') {
|
} else if (data === '^Z') {
|
||||||
this.process.stdin.write('\x1A');
|
if (process.platform !== 'win32' && this.process.pid) {
|
||||||
|
try {
|
||||||
|
process.kill(-this.process.pid, 'SIGTSTP');
|
||||||
|
} catch (e) {
|
||||||
|
this.process.stdin.write('\x1A');
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
this.process.stdin.write('\x1A');
|
||||||
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -77,7 +102,25 @@ class StatVitTerm extends EventEmitter {
|
|||||||
|
|
||||||
kill() {
|
kill() {
|
||||||
if (this.process) {
|
if (this.process) {
|
||||||
this.process.kill('SIGTERM');
|
try {
|
||||||
|
if (process.platform !== 'win32' && this.process.pid) {
|
||||||
|
// SIGINT an Prozessgruppe
|
||||||
|
process.kill(-this.process.pid, 'SIGINT');
|
||||||
|
setTimeout(() => {
|
||||||
|
if (this.process) {
|
||||||
|
this.process.kill('SIGTERM');
|
||||||
|
}
|
||||||
|
}, 100);
|
||||||
|
} else {
|
||||||
|
this.process.kill('SIGTERM');
|
||||||
|
}
|
||||||
|
} catch (err) {
|
||||||
|
try {
|
||||||
|
this.process.kill('SIGKILL');
|
||||||
|
} catch (err2) {
|
||||||
|
this.emit('err', 'Could not kill process');
|
||||||
|
}
|
||||||
|
}
|
||||||
this.isRunning = false;
|
this.isRunning = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user