这是我在codereview - Colorful output on terminal上发布的问题的跟进,我试图在终端上输出彩色字符串并通过isatty()
调用来检测它。然而正如@Jerry Coffin指出的那样 -
使用isatty检查标准输出是否连接到终端,无论您要写入哪个流。这意味着如果您将
std::cout
作为要写入的流传递,则其余功能只能正常工作。否则,您可能在写入非TTY的内容时允许格式化,并且在写入TTY内容时可能禁止格式化。
这是我不知道的事情(读作没有经验),我甚至不知道cin / cout可以重定向到别处的事实。所以我试着阅读更多关于它的内容,并在SO上发现了一些现有的问题。以下是我一起入侵的内容:
// initialize them at start of program - mandatory
std::streambuf const *coutbuf = std::cout.rdbuf();
std::streambuf const *cerrbuf = std::cerr.rdbuf();
std::streambuf const *clogbuf = std::clog.rdbuf();
// ignore this, just checks for TERM env var
inline bool supportsColor()
{
if(const char *env_p = std::getenv("TERM")) {
const char *const term[8] = {
"xterm", "xterm-256", "xterm-256color", "vt100",
"color", "ansi", "cygwin", "linux"};
for(unsigned int i = 0; i < 8; ++i) {
if(std::strcmp(env_p, term[i]) == 0) return true;
}
}
return false;
}
rightTerm = supportsColor();
// would make necessary checks to ensure in terminal
inline bool isTerminal(const std::streambuf *osbuf)
{
FILE *currentStream = nullptr;
if(osbuf == coutbuf) {
currentStream = stdout;
}
else if(osbuf == cerrbuf || osbuf == clogbuf) {
currentStream = stderr;
}
else {
return false;
}
return isatty(fileno(currentStream));
}
// this would print checking rightTerm && isTerminal calls
inline std::ostream &operator<<(std::ostream &os, rang::style v)
{
std::streambuf const *osbuf = os.rdbuf();
return rightTerm && isTerminal(osbuf)
? os << "\e[" << static_cast<int>(v) << "m"
: os;
}
我的主要问题是,虽然我已经手动测试过,但我不知道这可能会失败的情况或可能包含的错误。这是做这件事的正确方法吗?有什么我可能会遗失的吗?
以下是运行的最小示例(您还需要随机数据in.txt
):
#include <iostream>
#include <fstream>
#include <string>
#include <unistd.h>
#include <cstdlib>
#include <cstring>
void f();
bool supportsColor();
// sample enum for foreground colors
enum class fg : unsigned char {
def = 39,
black = 30,
red = 31,
green = 32,
yellow = 33,
blue = 34,
magenta = 35,
cyan = 36,
gray = 37
};
// initialize them at start of program - mandatory
// so that even if user redirects, we've a copy
std::streambuf const *coutbuf = std::cout.rdbuf();
std::streambuf const *cerrbuf = std::cerr.rdbuf();
std::streambuf const *clogbuf = std::clog.rdbuf();
// check if TERM supports color
bool rightTerm = supportsColor();
// Here is the implementation of isTerminal
// which checks if program is writing to Terminal or not
bool isTerminal(const std::streambuf *osbuf)
{
FILE *currentStream = nullptr;
if(osbuf == coutbuf) {
currentStream = stdout;
}
else if(osbuf == cerrbuf || osbuf == clogbuf) {
currentStream = stderr;
}
else {
return false;
}
return isatty(fileno(currentStream));
}
// will check if TERM supports color and isTerminal()
inline std::ostream &operator<<(std::ostream &os, fg v)
{
std::streambuf const *osbuf = os.rdbuf();
return rightTerm && isTerminal(osbuf)
? os << "\e[" << static_cast<int>(v) << "m"
: os;
}
int main()
{
std::cout << fg::red << "ERROR HERE! " << std::endl
<< fg::blue << "ERROR INVERSE?" << std::endl;
std::ifstream in("in.txt");
std::streambuf *Orig_cinbuf = std::cin.rdbuf(); // save old buf
std::cin.rdbuf(in.rdbuf()); // redirect std::cin to in.txt!
std::ofstream out("out.txt");
std::streambuf *Orig_coutbuf = std::cout.rdbuf(); // save old buf
std::cout.rdbuf(out.rdbuf()); // redirect std::cout to out.txt!
std::string word;
std::cin >> word; // input from the file in.txt
std::cout << fg::blue << word << " "; // output to the file out.txt
f(); // call function
std::cin.rdbuf(Orig_cinbuf); // reset to standard input again
std::cout.rdbuf(Orig_coutbuf); // reset to standard output again
std::cin >> word; // input from the standard input
std::cout << word; // output to the standard input
return 0;
}
void f()
{
std::string line;
while(std::getline(std::cin, line)) // input from the file in.txt
{
std::cout << fg::green << line << "\n"; // output to the file out.txt
}
}
bool supportsColor()
{
if(const char *env_p = std::getenv("TERM")) {
const char *const term[8] = {"xterm", "xterm-256", "xterm-256color",
"vt100", "color", "ansi",
"cygwin", "linux"};
for(unsigned int i = 0; i < 8; ++i) {
if(std::strcmp(env_p, term[i]) == 0) return true;
}
}
return false;
}
我还标记了c
语言,虽然这是c++
代码,因为相关代码是共享的两个,我不想错过任何建议
答案 0 :(得分:5)
OP的问题:
我的主要问题是,虽然我已经手动测试过,但我不知道这可能会失败的情况或可能包含的错误。这是做这件事的正确方法吗?有什么我可能会失踪吗?
并非所有终端都支持所有功能;此外,TERM
变量最常用于选择特定的终端描述。
通常的方法是使用终端数据库而不是硬编码。这样做,你的方法
inline bool supportsColor()
inline std::ostream &operator<<(std::ostream &os, rang::style v)
将检查终端能力,例如,使用tigetnum
(对于颜色数量),tigetstr
(对于终端应该支持的实际转义序列)。您可以像isatty
函数一样轻松地包装它们。
进一步阅读:
答案 1 :(得分:3)
要检查POSIX标准输出是终端,只需使用isatty(3)
IndexReader
您也可以使用 if (isatty(STDOUT_FILENO)) {
/// handle the stdout is terminal case
}
,请参阅tty(4);例如如果您的程序/dev/tty
在myprog
之类的命令管道中启动,您仍然可以./myprog some arguments | less
输出到控制终端(即使 stdout 是管道)
有时,程序在没有任何控制终端的情况下运行,例如通过crontab(5)或at(1)