简体   繁体   English

#tty是什么? 在STDIN上意味着/在红宝石中做?

[英]What does #tty? on STDIN mean / do in ruby?

Reading the ruby docs isn't overly helpful here : 这里阅读ruby文档并不是很有帮助:

Returns true if ios is associated with a terminal device (tty), false otherwise. 如果ios与终端设备(tty)关联,则返回true,否则返回false。

I was hoping to get some additional resources or explanation to help me understand this better. 我希望获得一些其他资源或解释,以帮助我更好地理解这一点。

For context, I'm writing a little command line program that accepts either a file path or piped content into the ruby executable and am using #tty? 对于上下文,我正在编写一个小的命令行程序,该程序接受文件路径或管道内容到ruby可执行文件中,并且正在使用#tty? to determine what is coming in. 确定即将发生的事情。

Thanks! 谢谢!

Seems like http://www.jstorimer.com/blogs/workingwithcode/7766125-writing-ruby-scripts-that-respect-pipelines supplies the most concise description of what #tty? 好像http://www.jstorimer.com/blogs/workingwithcode/7766125-writing-ruby-scripts-that-respect-pipelines提供了关于#tty?的最简洁的描述#tty? does: 确实:

Ruby's IO#isatty method (aliased as IO#tty?) will tell you whether or not the IO in question is attached to a terminal. Ruby的IO#isatty方法(别名为IO#tty?)将告诉您所涉及的IO是否已连接到终端。 Calling it on $stdout, for instance, when it's being piped will return false. 例如,在管道传输时,在$ stdout上调用它会返回false。

Here is some relevant info that you may find useful: 以下是一些有用的相关信息:

Background meaning via What do pty and tty mean? pty和tty是什么意思? :

In UNIX, /dev/tty* is any device that acts like a "teletype", ie, terminal. 在UNIX中,/ dev / tty *是任何充当“ teletype”(即终端)的设备。 (Called teletype because that's what we had for terminals in those benighted days.) (之所以称为电传打字机,是因为那是我们在那些陷入困境的日子里为终端所用的。)

In the spirit of the question, here's an example of writing to /dev/tty from http://zetcode.com/lang/rubytutorial/io/ : 本着问题的精神,下面是一个从http://zetcode.com/lang/rubytutorial/io/写入/dev/tty的示例:

#!/usr/bin/ruby

fd = IO.sysopen "/dev/tty", "w"
ios = IO.new(fd, "w")
ios.puts "ZetCode"
ios.close

Look at how grep handles this situation for the traditional UNIX approach: No file specified? 看一下grep如何处理传统UNIX方法的这种情况:没有指定文件? Default to $stdin and don't worry about TTY status, maybe someone wants to paste into the terminal. 默认为$stdin ,不必担心TTY状态,也许有人想粘贴到终端中。 If a filename is specified, read from that and ignore STDIN. 如果指定了文件名,请从中读取并忽略STDIN。

The tty? tty? function is there so you can know if you should send things like ANSI escape codes to colour your output. 函数在那里,所以您可以知道是否应该发送诸如ANSI转义码之类的颜色来为输出着色。 It's not generally a reliable signal of if someone wants to supply input over STDIN. 如果有人想通过STDIN提供输入,通常这不是一个可靠的信号。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM