gpt4 book ai didi

ruby - 我如何使 stdin 成为 tty?

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:17:13 32 4
gpt4 key购买 nike

有些程序会根据其标准输出是否为 tty 来更改其输出。因此,如果您将它们放入管道或重定向它们,输出将与您的 shell 中的不同。这是一个例子:

$ touch a b c

# when running ls alone, it places them on one line
$ ls
a b c

# when stdout is not a tty, it places them on their own line
$ ls > output

$ cat output
a
b
c
output

因此,如果我想向某人展示这样的命令应该是什么样子(例如,我正在编写教程),我必须突出显示并复制终端的输出,然后将其保存到文件中。

看来我应该能够做这样的事情:

ls | ttypipe > output

ttypepipe 是一个假设的程序,当被问及它是否是 tty 时,其标准输入(以及 ls 的标准输出)以 true 响应。

我知道在 Ruby 中,我可以做这样的事情:

require 'pty'         # => true
IO.pipe.map(&:tty?) # => [false, false]
PTY.open.map(&:tty?) # => [true, true]

但那是针对子进程,而不是当前进程,因此:

$stdin.tty?  # => false

我可以执行,但我想不出一种方法来影响标准输入文件描述符。

最佳答案

你不能写 ttypepipe 因为管道就是管道,永远不可能是 tty。但是,您可以使用稍微不同的语法编写 ttyexec:

ttyexec ls > output

它会打开一个伪终端,在其中运行 ls,然后将 ls 写入终端的所有内容复制到 ttyexec 的标准输出.

你看,已经有这样的工具了:script。它在一个新的、隐藏的 pty 中打开一个程序来记录与它的交互,但我们可以忽略日志记录部分,只使用它的终端打开属性:

$ touch a b c

$ ls
a b c

$ script -q -c 'ls' /dev/null > output

$ cat output
a b c output

关于ruby - 我如何使 stdin 成为 tty?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28372443/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com