调整由 php 分配的伪 tty 的大小

Resize pseudo tty allocated by php

我正在尝试使用 php 和 xterm.js、

开发网络控制台

我设法分配了伪 tty,并通过 websocket 将其附加到 xterm.js,但我无法告诉进程终端的大小是多少,以使其根据大小正确工作,我找不到这方面的任何文档。

// using react/child-process
$process = new Process('/usr/bin/env bash -l', null, null, [
    0 => ['pty', 'r'],
    1 => ['pty', 'w'],
    2 => ['pty', 'w'],
]);
$process->start($this->loop);

我发现 运行 分配的 devpts 上的 stty 可以解决问题

if (isset($this->processes[spl_object_id($conn)])) {
    $process = $this->processes[spl_object_id($conn)];
    $data = unpack('i3', $raw);
    $col = $data[2];
    $row = $data[3];
    $getstream = (function () {
        return $this->stream;
    });
    $stty = new Process("stty cols $col rows $row", null, null, [
        $getstream->call($process->stdin), // ex: /dev/pts/0
        $getstream->call($process->stdout),
        $getstream->call($process->stderr),
    ]);
    $stty->start($this->loop);
}

或者可以这样做

use React\ChildProcess\Process;
$gettty = escapeshellcmd(PHP_BINARY).' -r "echo posix_ttyname(STDIN).PHP_EOL;"';
$bash = "setsid bash -l"; // setsid is important here
$process = new Process(
    "$gettty && $bash",
    null, null,
    [
        ['pty', 'r'],
        ['pty', 'w'],
        ['pty', 'w'],
    ]
);
// TODO: read the first line and get the tty path so can run stty on it later.

这是一个简单的例子: https://gist.github.com/eslym/d3bd7809681aa9c1eb34913043df9bb6