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

我会带着你远行 2021-12-20 09:01 255阅读 0赞

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

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

发表评论

表情:
评论列表 (有 0 条评论,255人围观)

还没有评论,来说两句吧...

相关阅读