#include <string> #include <stdarg.h> #include <sstream> #include <iostream> typedef enum ShellColor { //使用时先输出 "\033[" ,然后输出下面的枚举值,多个枚举值用分号隔开,最后输 出"m" 即可 SC_DEFAULT = 0, //重新设置属性到缺省设置 FONT_B = 1, //粗体 FONT_HALF_LIGHT = 2, //一半亮度 FONT_U = 4, //下划线 FONT_FLICKER = 5, //闪烁 SC_REVERSE = 7, //将背景与字体颜色相换 FONT_BLACK = 30, //黑色字体 FONT_RED = 31, //红色字体 FONT_GREEN = 32, //绿色字体 FONT_BROWN = 33, //棕色字体 FONT_BLUE = 34, //蓝色字体 FONT_PURPLE = 35, //紫色字体 FONT_BLUEST = 36, //青色字体 FONT_WHITE = 37, //白色字体 BG_BLACK = 40, //黑色背景 BG_RED = 41, //红色背景 BG_GREEN = 42, //绿色背景 BG_BROWN = 44, //棕色背景 BG_BLUE = 44, //蓝色背景 BG_PURPLE = 45, //紫色背景 BG_BLUEST = 46, //青色背景 BG_WHITE = 47, //白色背景 } SHELL_COLOR; //下面的方法使用可变参数 std::string set_shell_color(int first,...) { va_list lt; va_start(lt,first); const int flag = static_cast<int>(-1); std::string ret; std::stringstream ss; ss<<"\033["<<first; int i; while((i = va_arg(lt,int)) != flag) { ss<<";"<<i; } va_end(lt); ss<<"m"; ss>>ret; return ret; } //下面的方法使用 c++0X 特性。 std::string set_shell_color_new(std::initializer_list<int> list) { std::string ret; std::stringstream ss; ss<<"\033["; for(int i : list) { ss<<";"<<i; } ss<<"m"; ss>>ret; return ret; } ///* int main() { std::cout<<set_shell_color(FONT_BLUEST,FONT_U,-1)<<"set_shell_color"<<std::endl; std::cout<<set_shell_color_new({FONT_PURPLE,FONT_B})<<"set_shell_color_new"<<set_shell_color_new({0})<<std::endl; } //*/