1. 程式人生 > >Linux下實現彩色進度條

Linux下實現彩色進度條

#include <stdio.h>                                                                                                                                    
#include <unistd.h>                                                                                                                                   
#include <string.h>                                                                                                                                   
#define RED "\e[0;31m"                                                                                                                                
#define BLUE "\e[0;34m"                                                                                                                               
#define GREEN "\e[0;32m"                                                                                                                              
int main()                                                                                                                                            
{                                                                                                                                                     
  int i=0;                                                                                                                                            
  char bar[102]={0};                                                                                                                                  
  char circle[4]={'-','/','|','\\'};                                                                                                                  
  for(i=0;i<=100;i++)                                                                                                                                 
  {                                                                                                                                                   
    if(i<=30)                                                                                                                                         
      printf(RED "[%d%%] [%c] [%s]\r",i,circle[i%4],bar);                                                                                             
    else if(i>30&&i<=70)                                                                                                                              
      printf(BLUE "[%d%%] [%c] [%s]\r",i,circle[i%4],bar);                                                                                            
    else                                                                                                                                              
      printf(GREEN "[%d%%] [%c] [%s]\r",i,circle[i%4],bar);                                                                                           
    fflush(stdout);                                                                                                                                   
    bar[i]='=';                                                                                                                                       
    usleep(100*1000);                                                                                                                                 
  }                                                                                                                                                   
  printf("\n");                                                                                                                                       
  return 0;                                                                                                                                           
}