【问题标题】:Colour output of program run under BASH [closed]在 BASH 下运行的程序的颜色输出 [关闭]
【发布时间】:2012-04-14 03:37:42
【问题描述】:

我需要能够使终端上的一些文本更加引人注目,而我的想法是使文本着色。无论是实际文本,还是每个字母矩形中的空间(想想 vi 的光标)。我认为对我的应用程序很重要的唯一两个额外规范是:程序应该是独立于发行版的(可以肯定的是代码只会在 BASH 下运行),并且在写入文件时不应该输出额外的字符(无论是来自实际代码,还是在管道输出时)

我在网上搜索了一些信息,但我只能找到已弃用的 cstdlib (stdlib.h) 的信息,我需要(实际上,它更像是“想要”)使用 iostream 的功能来完成它。

【问题讨论】:

    标签: c++ linux bash colors


    【解决方案1】:

    你可能想看看VT100 control codes。

    【讨论】:

      【解决方案2】:

      大多数终端都遵循 ASCII 颜色序列。它们的工作方式是输出ESC,然后是[,然后是分号分隔的颜色值列表,然后是m。这些是常见的值:

      Special
      0  Reset all attributes
      1  Bright
      2  Dim
      4  Underscore   
      5  Blink
      7  Reverse
      8  Hidden
      
      Foreground colors
      30  Black
      31  Red
      32  Green
      33  Yellow
      34  Blue
      35  Magenta
      36  Cyan
      37  White
      
      Background colors
      40  Black
      41  Red
      42  Green
      43  Yellow
      44  Blue
      45  Magenta
      46  Cyan
      47  White
      

      所以输出"\033[31;47m" 应该使终端正面(文本)颜色为红色,背景颜色为白色。

      你可以用 C++ 形式很好地包装它:

      enum Color {
          NONE = 0,
          BLACK, RED, GREEN,
          YELLOW, BLUE, MAGENTA,
          CYAN, WHITE
      }
      
      std::string set_color(Color foreground = 0, Color background = 0) {
          char num_s[3];
          std::string s = "\033[";
      
          if (!foreground && ! background) s += "0"; // reset colors if no params
      
          if (foreground) {
              itoa(29 + foreground, num_s, 10);
              s += num_s;
      
              if (background) s += ";";
          }
      
          if (background) {
              itoa(39 + background, num_s, 10);
              s += num_s;
          }
      
          return s + "m";
      }
      

      【讨论】:

      • 不要忘记序列的终止'm',如"\033]31;47m"。
      • @JoachimPileborg:已修复。
      【解决方案3】:

      您还可以制作自定义函数,例如:

      void textcolor(int color)
      {
          std::cout<<"\033]"<<color;
      }
      

      更多信息请阅读http://en.wikipedia.org/wiki/ANSI_escape_code

      【讨论】:

        【解决方案4】:

        这是来自@nightcracker 的上述代码的一个版本,使用stringstream 而不是itoa。 (使用 clang++、C++11、OS X 10.7、iTerm2、bash 运行)

        #include <iostream>
        #include <string>
        #include <sstream>
        
        enum Color
        {
            NONE = 0,
            BLACK, RED, GREEN,
            YELLOW, BLUE, MAGENTA,
            CYAN, WHITE
        };
        
        static std::string set_color(Color foreground = NONE, Color background = NONE)
        {
            std::stringstream s;
            s << "\033[";
            if (!foreground && ! background){
                s << "0"; // reset colors if no params
            }
            if (foreground) {
                s << 29 + foreground;
                if (background) s << ";";
            }
            if (background) {
                s << 39 + background;
            }
            s << "m";
            return s.str();
        }
        
        int main(int agrc, char* argv[])
        {
            std::cout << "These words should be colored [ " <<
                set_color(RED) << "red " <<
                set_color(GREEN) << "green " <<
                set_color(BLUE) << "blue" <<
                set_color() <<  " ]" << 
                std::endl;
            return EXIT_SUCCESS;
        }
        

        【讨论】:

          【解决方案5】:

          您可以使用来自 github (https://github.com/Spezialcoder/libcolor) 的 libcolor

          #include "libcolor/libcolor.h"
          #include <iostream>
          using namespace std; 
          int main()
          {
               cout << font_color::green << "Hello World" << endl;
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2018-12-13
            • 2013-04-05
            • 1970-01-01
            • 2015-05-09
            • 2014-03-22
            相关资源
            最近更新 更多