当我运行ag
命令以搜索文本文件时,在命令行上会得到如下所示的输出(未显示完整结果):
❯ ag dependency
140317155505.md
27:The Bundler dependencies API ...
140423193022_python_packages.md
61:`distutils` is part of the Python standard ....
输出中有换行符。但是,当我通过管道传输或重定向到文件时,会得到如下输出:
140317155505.md:27:The Bundler dependencies API...
140423193022_python_packages.md:61:`distutils` is part of the Python standard...
这里有一个多余的冒号而不是换行符。这是如何运作的?我认为这ag
只是写到STDOUT而不知道STDOUT的实际目标-我以为它无法访问此信息是错误的吗?
该程序ag
(我不熟悉),可以使用系统调用
stdout_is_tty = isatty(1);
isatty()函数测试fd是否是指向终端的打开文件描述符。
这允许它根据写入的位置来修改输出。
我也认为我找到了相关的源代码部分
/* If we're not outputting to a terminal. change output to:
* turn off colors
* print filenames on every line
*/
if (!isatty(fileno(stdout))) {
opts.color = 0;
group = 0;
....
另请参阅man isatty
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句