Linux Console Color (the "\033[" way)

This is a list of codes used in C++ to change the text color:

 black - 30
 red - 31
 green - 32
 brown - 33
 blue - 34
 magenta - 35
 cyan - 36
 lightgray - 37


Now these are the basic colours.


 Usage of "\033[":
 This is to handle the console cursor. I do not have a complete reference so I ask people who know about it to comment with what I do not have.

 * 'm' character at the end of each of the following sentences is used as a stop character, where the system should stop and parse the \033[ sintax.
 \033[0m - is the default color for the console
 \033[0;#m - is the color of the text, where # is one of the codes mentioned above
 \033[1m - makes text bold
 \033[1;#m - makes colored text bold**
 \033[2;#m - colors text according to # but a bit darker
 \033[4;#m - colors text in # and underlines
 \033[7;#m - colors the background according to #
 \033[9;#m - colors text and strikes it
 \033[A - moves cursor one line above (carfull: it does not erase the previously written line)
 \033[B - moves cursor one line under
 \033[C - moves cursor one spacing to the right
 \033[D - moves cursor one spacing to the left
 \033[E - don't know yet
 \033[F - don't know yet

 \033[2K - erases everything written on line before this.

 **(this could be usefull for those who want more colors. The efect given by bold will give a color change effect)

 As a short and simple example that colors text and the bold version:

 1 #include <stdio.h>
 2 
 3 int main(int argc, char ** argv)
 4 {     
 5     printf("\033[0mHello world!........................................[");
 6     printf("\033[1;32mOK");
 7     printf("\033[0m]\n");
 8     
 9     return 0;
10 }

 

  1 #include   <stdio.h> 
  2 #include   <stdlib.h> 
  3 #include   <string.h>
  4 
  5 /****************************************************************************
  6 the text color:
  7     black - 30
  8     red - 31
  9     green - 32
 10     brown - 33
 11     blue - 34
 12     magenta - 35
 13     cyan - 36
 14     lightgray - 37
 15 ****************************************************************************/
 16 
 17 #define COLOR_DEFAULT   "m"
 18 #define COLOR_BLACK     ";30m"
 19 #define COLOR_RED       ";31m"
 20 #define COLOR_GREEN     ";32m"
 21 #define COLOR_BROWN     ";33m"
 22 #define COLOR_BLUE      ";34m"
 23 #define COLOR_MAGANTA   ";35m"
 24 #define COLOR_CYAN      ";36m"
 25 #define COLOR_LIGHTGRAY ";37m"
 26 
 27 /****************************************************************************
 28 Usage of "\033[":
 29 
 30 This is to handle the console cursor. 
 31 
 32 I do not have a complete reference so I ask people who know about it to 
 33 comment with what I do not have.
 34 
 35 * 'm' character at the end of each of the following sentences is used as a 
 36 stop character, where the system should stop and parse the \033[ sintax.
 37 
 38 \033[0m - is the default color for the console
 39 \033[0;#m - is the color of the text, where # is one of the codes mentioned 
 40             above
 41 \033[1m - makes text bold
 42 \033[1;#m - makes colored text bold**
 43 \033[2;#m - colors text according to # but a bit darker
 44 \033[4;#m - colors text in # and underlines
 45 \033[7;#m - colors the background according to #
 46 \033[9;#m - colors text and strikes it
 47 \033[A - moves cursor one line above (carfull: it does not erase the 
 48          previously written line)
 49 \033[B - moves cursor one line under
 50 \033[C - moves cursor one spacing to the right
 51 \033[D - moves cursor one spacing to the left
 52 \033[E - don't know yet
 53 \033[F - don't know yet
 54 \033[2K - erases everything written on line before this.
 55 ****************************************************************************/
 56 
 57 #define MODE_0 "0"
 58 #define MODE_1 "1"
 59 #define MODE_2 "2"
 60 #define MODE_3 "3"
 61 #define MODE_4 "4"
 62 #define MODE_5 "5"
 63 #define MODE_6 "6"
 64 #define MODE_7 "7"
 65 #define MODE_8 "8"
 66 #define MODE_9 "9"
 67 #define MODE_A "A"
 68 #define MODE_B "B"
 69 #define MODE_C "C"
 70 #define MODE_D "D"
 71 #define MODE_E "E"
 72 #define MODE_F "F"
 73 #define MODE_2K "2K"
 74 
 75 #define MAX_LINE_LEN 70
 76 
 77 #define MAX_DESC_LEN 20
 78 
 79 typedef struct strProcessStepsInfo
 80 {
 81     char szStepDesc[MAX_DESC_LEN];
 82     int  nResult;
 83 }ProcessStepsInfo;
 84 
 85 void SetModeDefault()
 86 {
 87     printf("\033[0m");
 88 }
 89 
 90 void SetMode(const char *pszMode, const char *pszColor)
 91 {
 92     printf("\033[%s%s", pszMode, pszColor);
 93 }
 94 
 95 void ShowSpecial(const char *pszOut, const char *pszMode, const char *pszColor)
 96 {
 97     SetMode(pszMode, pszColor);
 98     printf("%s", pszOut);
 99 }
100 
101 void ShowProcess(const char *pszOut, int nResult)
102 {
103     // show steps information
104     printf("\n");
105     int  nLen = strlen(pszOut);
106     
107     int nProcess = 0;
108     
109     for (nProcess = 0; nProcess <= 100; nProcess++)
110     {
111         SetMode(MODE_A, "");
112         
113         if (nProcess>=100)
114         {
115             ShowSpecial(pszOut, MODE_0, COLOR_DEFAULT);
116         }
117         else
118         {
119             ShowSpecial(pszOut, MODE_1, COLOR_DEFAULT);
120         }
121         
122         int nDotCount = 0;
123         for (nDotCount = 0; nDotCount< MAX_LINE_LEN - nLen; nDotCount++)
124         {
125             int nTotal = MAX_LINE_LEN - nLen;
126             int nShowDotCount = nProcess * nTotal / 100;
127             
128             if (nDotCount < nShowDotCount)
129             {
130                 printf(".");
131             }
132             else
133             {
134                 printf(" ");
135             }
136         }
137         
138         if (nProcess>=100)
139         {
140             if (nResult)
141             {
142                 printf(" [");
143                 ShowSpecial("Failed", MODE_1, COLOR_RED);
144                 SetModeDefault();
145                 printf("]\n");
146             }
147             else
148             {
149                 printf(" [");
150                 ShowSpecial("OK", MODE_1, COLOR_GREEN);
151                 SetModeDefault();
152                 printf("]\n");
153             }
154         }
155         else
156         {
157             printf(" [%d]\n", nProcess);
158         }
159         
160         system("sleep 0.001");
161     }
162 }
163 
164 void ShowAllProcess()
165 {
166     // generate steps information
167     int nSteps = 0;
168     const int nMaxSteps = 10;
169     ProcessStepsInfo StepsInfo[nMaxSteps];
170     
171     for (nSteps = 1; nSteps <= nMaxSteps; nSteps++)
172     {
173         sprintf(StepsInfo[nSteps-1].szStepDesc, "Step %d ", nSteps);
174         StepsInfo[nSteps-1].nResult = rand() % 2;
175         
176         ShowProcess(StepsInfo[nSteps-1].szStepDesc, StepsInfo[nSteps-1].nResult);
177     }
178 }
179 
180 int main(int argc, char **argv)
181 {
182    ShowAllProcess();
183    
184    SetModeDefault();
185    
186    return 0;
187 }

 

转载于:https://www.cnblogs.com/yuanping/archive/2012/12/29/2838731.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值