c++之终端输出带颜色

目录

基础用法

#include <iostream>

int main(int argc, char** argv)
{
  std::cout << "\033[0;30m This is black color text ! \033[0m" << std::endl;
  std::cout << "\033[0;31m This is red color text ! \033[0m" << std::endl;
  std::cout << "\033[0;32m This is green color text ! \033[0m" << std::endl;
  std::cout << "\033[0;33m This is orang color text ! \033[0m" << std::endl;
  std::cout << "\033[0;34m This is blue color text ! \033[0m" << std::endl;
  std::cout << "\033[0;35m This is purple color text ! \033[0m" << std::endl;
  std::cout << "\033[0;36m This is cyan color text ! \033[0m" << std::endl;
  std::cout << "\033[0;37m This is light gray color text ! \033[0m" << std::endl;

  return 0;
}

ANSI escape codes

颜色 code 颜色 code
black 0;30 dark gray 1;30
red 0;31 light red 1;31
green 0;32 light green 1;32
brown/orange 0;33 yellow 1;33
blue 0;34 light blue 1;34
purple 0;35 light purple 1;35
cyan 0;36 light cyan 1;36
light vray 0;37 whilte 1;37

注:

  • 错误 -- 红色
  • 警告 -- 黄色
  • 信息 -- 绿色

#define

  • 方法一:
#define COUT(X) std::cout <<std::setiosflags(std::ios::fixed)<< X << "\033[3m" << "\r" << std::flush << std::endl
#define COUTR(X) std::cout <<std::setiosflags(std::ios::fixed)<< "\033[1;31m " << X << "\033[0m" << "\r" << std::flush << std::endl  // red
#define COUTG(X) std::cout <<std::setiosflags(std::ios::fixed)<< "\033[1;32m " << X << "\033[0m" << "\r" << std::flush << std::endl  // green
#define COUTY(X) std::cout <<std::setiosflags(std::ios::fixed)<< "\033[1;33m " << X << "\033[0m" << "\r" << std::flush << std::endl  // yellow
#define COUTB(X) std::cout <<std::setiosflags(std::ios::fixed)<< "\033[1;34m " << X << "\033[0m" << "\r" << std::flush << std::endl  // blue
#define COUTP(X) std::cout <<std::setiosflags(std::ios::fixed)<< "\033[1;35m " << X << "\033[0m" << "\r" << std::flush << std::endl  // purple

用法:

COUTG("~~ ================================================ ~~");
COUTG("~~ =====         Robosense Perception         ===== ~~");
COUTG("~~ ================================================ ~~");
COUTG("OpenCV version: " << CV_VERSION);
COUTG("ROS version: " << ROS_VERSION_MAJOR << "." << ROS_VERSION_MINOR << "." << ROS_VERSION_PATCH);
COUTG("PCL version: " << PCL_VERSION_PRETTY);
COUTG("Boost version: " << BOOST_LIB_VERSION);
COUTG("Eigen version: " << EIGEN_WORLD_VERSION << "." << EIGEN_MAJOR_VERSION << "." << EIGEN_MINOR_VERSION);
  • 方法二:
#define RESET "\033[0m"
#define BLACK "\033[30m"  /* Black */
#define RED "\033[31m"    /* Red */
#define GREEN "\033[32m"  /* Green */
#define YELLOW "\033[33m" /* Yellow */
#define BLUE "\033[34m"   /* Blue */
#define PURPLE "\033[35m" /* Purple */
#define CYAN "\033[36m"   /* Cyan */
#define WHITE "\033[37m"  /* White */

#define NORMAL (std::cout << RESET)
#define INFO (std::cout << GREEN)
#define WARN (std::cout << YELLOW)
#define ERROR (std::cout << RED)
#define DEBUG (std::cout << CYAN)
#define END (std::endl)
#define REND "\033[0m" << std::endl

个人感觉第二种方法用起来更方便些.

上一篇:Python输出带颜色文字


下一篇:linux系统每日巡检脚本