如何检查程序正在写入终端

问题描述 投票:0回答:3

这是我在 codereview - 终端上的彩色输出上发布的问题的后续内容,我试图在终端上输出彩色字符串并通过

isatty()
调用检测它。然而正如@Jerry Coffin 指出的 -

您可以使用 isatty 检查标准输出是否连接到终端,无论您正在写入哪个流。这意味着其余函数只有在您将

std::cout
作为它们要写入的流传递时才能正常工作。否则,您可以在写入非 TTY 的内容时允许格式化,并且在写入 TTY 的内容时可以禁止格式化。

这是我不知道的事情(读作没有经验),我什至不知道 cin/cout 可以重定向到其他地方。所以我尝试阅读更多相关内容,并发现了一些现有的问题。这是我一起破解的:

// 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++
代码,因为相关代码是黑白共享的,我不想错过任何建议

c++ linux c++11 terminal
3个回答
4
投票

OP的问题:

我的主要问题是,虽然我已经手动测试了这一点,但我不知道这可能会失败的情况或它可能包含的错误。这是做这件事的正确方法吗?有什么我可能遗漏的吗?

并非所有终端都支持所有功能;此外,

TERM
变量最常用于选择特定的终端描述

通常的方法是使用终端数据库而不是硬编码。这样做,你的方法

inline bool supportsColor()

inline std::ostream &operator<<(std::ostream &os, rang::style v)

将检查终端功能,例如,使用

tigetnum
(用于颜色数量)、
tigetstr
(用于终端应该支持的实际转义序列)。您可以像使用
isatty
函数一样轻松地包装它们。

进一步阅读:


4
投票

要检查 POSIX 标准输出是否为终端,只需使用 isatty(3)

 if (isatty(STDOUT_FILENO)) {
   /// handle the stdout is terminal case
 }

您也可以使用

/dev/tty
,请参阅 tty(4);例如如果您的程序
myprog
在像
./myprog some arguments | less
这样的命令管道中启动,您仍然可以
fopen("/dev/tty","w")
输出到控制终端(即使 stdout 是一个管道)。

有时,程序在没有任何控制终端的情况下运行,例如通过 crontab(5)at(1)


0
投票

我想知道为什么8年来没有人提出解决方案:/

我们可以使用 std::ostream 的继承并尝试使用动态转换升级到 std::ofstream :

void write(std::ostream& os = std::cout)
{   
    std::ofstream* fileStream = dynamic_cast<std::ofstream*>(&os);
    if (fileStream)
        std::cout << "It's a file\n";
    else 
        std::cout << "It's not a file\n";

    os << "Hello, StackOverflow\n";
};


std::ofstream file("./build/test.txt");
write();
write(file);
© www.soinside.com 2019 - 2024. All rights reserved.