Update package dependencies to include node-pty for improved terminal handling. Refactor stat-vit-term.js to utilize node-pty for process management and streamline input simulation. Clean up example.js by removing unnecessary input commands.

This commit is contained in:
sebseb7
2025-08-12 01:36:10 +02:00
parent 2c83d402fc
commit 91d28779d6
4 changed files with 57 additions and 136 deletions

View File

@@ -27,10 +27,6 @@ termSession.on('ready', () => {
console.log('\n--- Ping abbrechen mit ^C ---');
termSession.input('^C');
termSession.input("sdfsdfsdaf\n");
termSession.input("\n");
termSession.input("\n");
termSession.input("\n");
}, 3000);
// Nach 4 Sekunden beenden

View File

@@ -1,138 +1,45 @@
// stat-vit-term.js
import { EventEmitter } from 'events';
import { spawn } from 'child_process';
// src/stat-vit-term.js
import pty from 'node-pty';
class StatVitTerm extends EventEmitter {
constructor(options = {}) {
super();
this.options = {
shell: process.platform === 'win32' ? 'cmd.exe' : '/bin/bash',
cwd: process.cwd(),
env: {
...process.env,
PS1: '$ ',
TERM: 'xterm-256color'
},
...options
};
this.process = null;
this.isRunning = false;
this._init();
}
const shell = process.platform === 'win32' ? 'powershell.exe' : 'bash';
_init() {
try {
// Wichtige Änderung: Shell im richtigen Modus starten
const args = process.platform === 'win32' ? [] : ['-l']; // login shell
this.process = spawn(this.options.shell, args, {
stdio: ['pipe', 'pipe', 'pipe'],
cwd: this.options.cwd,
env: this.options.env,
detached: true // Wichtig für Signalbehandlung
});
const ptyProcess = pty.spawn(shell, [], {
name: 'xterm-256color',
cols: 120,
rows: 30,
cwd: process.cwd(),
env: {
...process.env,
TERM: 'xterm-256color',
COLORTERM: 'truecolor',
LANG: 'en_US.UTF-8',
PWD: process.cwd(),
},
});
this.process.stdout.on('data', (data) => {
this.emit('std', data.toString());
});
// Handle output
ptyProcess.onData((data) => {
process.stdout.write(data); // Preserve formatting
});
this.process.stderr.on('data', (data) => {
this.emit('std', data.toString());
});
// Handle exit
ptyProcess.onExit(({ exitCode, signal }) => {
console.log(`\n\n✨ Shell exited with code ${exitCode}${signal ? ` (via signal ${signal})` : ''}`);
});
this.process.on('close', (code) => {
this.isRunning = false;
this.emit('close', code);
});
// Simulate input
setTimeout(() => {
console.log('\n[INPUT] Starting ping...');
ptyProcess.write('ping 127.0.0.1\r');
}, 1000);
this.process.on('error', (err) => {
this.emit('err', err.message);
});
setTimeout(() => {
console.log('\n[INPUT] Sending Ctrl+C...');
ptyProcess.write('\x03'); // Ctrl+C
}, 5000);
this.isRunning = true;
setTimeout(() => {
this.emit('ready');
}, 500);
} catch (err) {
this.emit('err', err.message);
}
}
input(data) {
if (!this.isRunning || !this.process) {
this.emit('err', 'Terminal is not running');
return false;
}
try {
if (data === '^C') {
// 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');
}
} else {
this.process.stdin.write('\x03');
}
return true;
} 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');
}
} else {
this.process.stdin.write('\x1A');
}
return true;
}
this.process.stdin.write(data);
return true;
} catch (err) {
this.emit('err', err.message);
return false;
}
}
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) {
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;
}
}
get pid() {
return this.process ? this.process.pid : null;
}
get running() {
return this.isRunning;
}
}
export default StatVitTerm;
setTimeout(() => {
console.log('\n[INPUT] Exiting shell...');
ptyProcess.write('exit\r');
}, 7000);