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:


#include <stdio.h>

int main(int argc, char ** argv)
{     
    printf("\033[0mHello world!........................................[");
    printf("\033[1;32mOK");
    printf("\033[0m]\n");
    
    return 0;
}


#include   <stdio.h> 
#include   <stdlib.h> 
#include   <string.h>

/****************************************************************************
the text color:
    black - 30
    red - 31
    green - 32
    brown - 33
    blue - 34
    magenta - 35
    cyan - 36
    lightgray - 37
****************************************************************************/

#define COLOR_DEFAULT   "m"
#define COLOR_BLACK     ";30m"
#define COLOR_RED       ";31m"
#define COLOR_GREEN     ";32m"
#define COLOR_BROWN     ";33m"
#define COLOR_BLUE      ";34m"
#define COLOR_MAGANTA   ";35m"
#define COLOR_CYAN      ";36m"
#define COLOR_LIGHTGRAY ";37m"

/****************************************************************************
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.
****************************************************************************/

#define MODE_0 "0"
#define MODE_1 "1"
#define MODE_2 "2"
#define MODE_3 "3"
#define MODE_4 "4"
#define MODE_5 "5"
#define MODE_6 "6"
#define MODE_7 "7"
#define MODE_8 "8"
#define MODE_9 "9"
#define MODE_A "A"
#define MODE_B "B"
#define MODE_C "C"
#define MODE_D "D"
#define MODE_E "E"
#define MODE_F "F"
#define MODE_2K "2K"

#define MAX_LINE_LEN 70

#define MAX_DESC_LEN 20

typedef struct strProcessStepsInfo
{
    char szStepDesc[MAX_DESC_LEN];
    int  nResult;
}ProcessStepsInfo;

void SetModeDefault()
{
    printf("\033[0m");
}

void SetMode(const char *pszMode, const char *pszColor)
{
    printf("\033[%s%s", pszMode, pszColor);
}

void ShowSpecial(const char *pszOut, const char *pszMode, const char *pszColor)
{
    SetMode(pszMode, pszColor);
    printf("%s", pszOut);
}

void ShowProcess(const char *pszOut, int nResult)
{
    // show steps information
    printf("\n");
    int  nLen = strlen(pszOut);
    
    int nProcess = 0;
    
    for (nProcess = 0; nProcess <= 100; nProcess++)
    {
        SetMode(MODE_A, "");
        
        if (nProcess>=100)
        {
            ShowSpecial(pszOut, MODE_0, COLOR_DEFAULT);
        }
        else
        {
            ShowSpecial(pszOut, MODE_1, COLOR_DEFAULT);
        }
        
        int nDotCount = 0;
        for (nDotCount = 0; nDotCount< MAX_LINE_LEN - nLen; nDotCount++)
        {
            int nTotal = MAX_LINE_LEN - nLen;
            int nShowDotCount = nProcess * nTotal / 100;
            
            if (nDotCount < nShowDotCount)
            {
                printf(".");
            }
            else
            {
                printf(" ");
            }
        }
        
        if (nProcess>=100)
        {
            if (nResult)
            {
                printf(" [");
                ShowSpecial("Failed", MODE_1, COLOR_RED);
                SetModeDefault();
                printf("]\n");
            }
            else
            {
                printf(" [");
                ShowSpecial("OK", MODE_1, COLOR_GREEN);
                SetModeDefault();
                printf("]\n");
            }
        }
        else
        {
            printf(" [%d]\n", nProcess);
        }
        
        system("sleep 0.001");
    }
}

void ShowAllProcess()
{
    // generate steps information
    int nSteps = 0;
    const int nMaxSteps = 10;
    ProcessStepsInfo StepsInfo[nMaxSteps];
    
    for (nSteps = 1; nSteps <= nMaxSteps; nSteps++)
    {
        sprintf(StepsInfo[nSteps-1].szStepDesc, "Step %d ", nSteps);
        StepsInfo[nSteps-1].nResult = rand() % 2;
        
        ShowProcess(StepsInfo[nSteps-1].szStepDesc, StepsInfo[nSteps-1].nResult);
    }
}

int main(int argc, char **argv)
{
   ShowAllProcess();
   
   SetModeDefault();
   
   return 0;
}





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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值