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:
sebseb7
2025-08-12 00:23:04 +02:00
parent 697cf74cc3
commit 83c912d8e0
2 changed files with 62 additions and 24 deletions

View File

@@ -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);
}); });

View File

@@ -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
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'); this.process.stdin.write('\x03');
}
} else {
this.process.stdin.write('\x03');
}
return true; return true;
} else if (data === '^Z') { } else if (data === '^Z') {
if (process.platform !== 'win32' && this.process.pid) {
try {
process.kill(-this.process.pid, 'SIGTSTP');
} catch (e) {
this.process.stdin.write('\x1A'); this.process.stdin.write('\x1A');
}
} else {
this.process.stdin.write('\x1A');
}
return true; return true;
} }
@@ -76,8 +101,26 @@ class StatVitTerm extends EventEmitter {
} }
kill() { kill() {
if (this.process) {
try {
if (process.platform !== 'win32' && this.process.pid) {
// SIGINT an Prozessgruppe
process.kill(-this.process.pid, 'SIGINT');
setTimeout(() => {
if (this.process) { if (this.process) {
this.process.kill('SIGTERM'); 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;
} }
} }