📄 Source: tmux.php
<?php
/**
* tmux helpers — capture console output, send commands.
*/
function tmux_session_name(string $template, string $server): string {
return str_replace('%s', $server, $template);
}
function tmux_has_session(string $session): bool {
$out = shell_exec('tmux has-session -t ' . escapeshellarg($session) . ' 2>&1');
// has-session exits 0 if exists, 1 if not. shell_exec doesn't give us
// the exit code, so we check stderr / empty output.
// A cleaner way: use exec and inspect $code.
$code = 1;
exec('tmux has-session -t ' . escapeshellarg($session) . ' 2>/dev/null', $_, $code);
return $code === 0;
}
function tmux_capture(string $session, int $lines = 300): ?string {
if (!tmux_has_session($session)) {
return null;
}
$cmd = 'tmux capture-pane -t ' . escapeshellarg($session) . ' -p -S -' . (int)$lines . ' 2>&1';
return shell_exec($cmd);
}
function tmux_send_command(string $session, string $command): bool {
if (!tmux_has_session($session)) {
return false;
}
// Send the literal command, then Enter.
$cmd = 'tmux send-keys -t ' . escapeshellarg($session) . ' '
. escapeshellarg($command) . ' Enter';
exec($cmd, $_, $code);
return $code === 0;
}
← Back