switch color字符串简单显示颜色

参考:
http://www.runoob.com/cplusplus/cpp-switch.html
Git shell coloring:https://gist.github.com/vratiu/9780109

#include <iostream>
#include <string>
using namespace std;    
void printcolor(string color, string str)
{
  int clr=0;
//  int clr;
  string head;
  string tail;
  string display;
  if(color=="red") clr=1;
  if(color=="purple") clr=2;
  switch(clr){
    case 1:{
      head="\033[31m";
      tail="\033[0m";
      display=head+str+tail;
      break;
    }
    case 2:{
      head="\033[35m";
      tail="\033[0m";
      display=head+str+tail;
      break;
    }
    default:{
//	    display=str;
//若clr变量未初始化,则将上一次的选择结果作为default
//blue
      head="\033[34m";
      tail="\033[0m";
      display=head+str+tail;
      break;
    }
  }
  cout << display << endl;
}
 int main ()
{
   printcolor("default", "unknown str!");
   printcolor("red", "helloworld!");
   printcolor("purple", "helloworld!");
   printcolor("default", "unknown str2!");
   return 0;
}

int clr=0;
int clr =0; 初始化结果
int clr;
int clr ; 结果

猜你喜欢

转载自blog.csdn.net/MoonShapedPool/article/details/86573878
今日推荐