设置另一个进程的终端大小(类似于 `stty columns`)?

huangapple go评论121阅读模式
英文:

Setting the terminal size (ala `stty columns` ) of another process?

问题

我正在使用github.com/kr/pty包来为我生成的外部进程创建一个伪终端(pseudo-TTY)。然而,它们的终端大小似乎比终端仿真器窗口的大小要小(例如,ncurses和其他终端用户界面只会在xterm / Konsole / 等的左上角绘制)。我已经向pty包报告了一个错误,因为修复此问题的理想方式是通过该包本身,但作为一种解决方法,如果我可以在代码中自己设置TTY的尺寸,那将非常方便。我应该如何做到这一点?注意:该项目是用Go(Golang)编写的,所以最好能够提供使用C或Go进行此操作的建议。另外,我正在开发的项目非常注重跨平台兼容性,所以如果需要任何特定于操作系统的系统调用,了解一下也会很方便。

英文:

I'm using the github.com/kr/pty package to create a pseudo-TTY for external processes I spawn off. However the terminal size for them seems to be smaller than the terminal emulator window size (ie ncurses and other terminal UIs will only draw in the top left corner of xterm / Konsole / whatever).

I have raised a bug with the pty package as the idea way to fix this issue would be with the package itself, but as a work around it might be handy if I could set the dimensions of the TTY myself (in code).

How would I go about doing this?

NB The project is written in Go (Golang) so ideally I'd need advice in doing this in C or Go. Also the project I'm working on has a strong emphasis on cross platform compatibility so it would be handy to know if any syscalls required are OS specific.

答案1

得分: 0

我找到了解决方案。原来创建一个新的伪终端是错误的方法,我实际上可以使用标准的Go库来实现我想要的效果:

cmd := exec.Command(name, parameters...)

cmd.SysProcAttr = &syscall.SysProcAttr{
    Ctty: int(os.Stdout.Fd()) // 设置终端
}

// 这些必须是相应的标准 os.File,因为如果你尝试为终端仿真器分配一个终端,则使用 Reader/Writer 是行不通的。
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
英文:

I found the solution. Turns out creating a new pseudo-TTY was the wrong approach and I can actually use the standard Go libraries to achieve what I wanted:

cmd := exec.Command(name, parameters...)

cmd.SysProcAttr = &syscall.SysProcAttr{
    Ctty: int(os.Stdout.Fd()) // set the TTY
}

// These must be the respective Std os.File as using a Reader/Writer 
// wouldn't work if you're trying to assign a TTY to your terminal
// emulator.
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr

huangapple
  • 本文由 发表于 2017年7月28日 19:16:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/45372101.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定