PhoenixMock  1.8.7
Tools to split/merge/print mock used in Phoenix
string_color.cpp
Go to the documentation of this file.
1 /***************************************
2  Auteur : Pierre Aubert
3  Mail : pierre.aubert@lapp.in2p3.fr
4  Licence : CeCILL-C
5 ****************************************/
6 
7 #include <stdlib.h>
8 #include <unistd.h>
9 #include <sys/ioctl.h>
10 
11 #include "string_function.h"
12 
13 #include "string_color.h"
14 
15 using namespace std;
16 
18 
20 std::string termDefault(){return "\033[0m";}
21 
23 
25 std::string termBright(){return "\033[1m";}
26 
28 
30 std::string termUnderlined(){return "\033[4m";}
31 
33 
35 std::string termBlink(){return "\033[5m";}
36 
38 
40 std::string termNegative(){return "\033[7m";}
41 
43 
45 std::string termPositive(){return "\033[27m";}
46 
48 
50 std::string termBlack(){return "\033[30m";}
51 
53 
55 std::string termRed(){return "\033[31m";}
56 
58 
60 std::string termGreen(){return "\033[32m";}
61 
63 
65 std::string termYellow(){return "\033[33m";}
66 
68 
70 std::string termBlue(){return "\033[34m";}
71 
73 
75 std::string termPurple(){return "\033[35m";}
76 
78 
80 std::string termCyan(){return "\033[36m";}
81 
83 
85 std::string termWhite(){return "\033[37m";}
86 
88 
90 std::string termBBlack(){return "\033[40m";}
91 
93 
95 std::string termBRed(){return "\033[41m";}
96 
98 
100 std::string termBGreen(){return "\033[42m";}
101 
103 
105 std::string termBYellow(){return "\033[43m";}
106 
108 
110 std::string termBBlue(){return "\033[44m";}
111 
113 
115 std::string termBPurple(){return "\033[45m";}
116 
118 
120 std::string termBCyan(){return "\033[46m";}
121 
123 
125 std::string termBWhite(){return "\033[47m";}
126 
129  printf("\033[H\033[2J");
130 }
131 
133 
135 void textColor(const char* color){
136  printf("\033[%sm",color);
137 }
138 
140 
142 void textColorCerr(const char* color){
143  fprintf(stderr, "\033[%sm", color);
144 }
145 
147 
150  printf("\033[%dm",color);
151 }
152 
154 
157  fprintf(stderr, "\033[%dm", color);
158 }
159 
161 
163 void printDelire(const std::string & str){
164  int couleur = 0;
165  char buffer[255];
166  for(string::const_iterator it = str.begin(); it != str.end(); it++){
167  couleur++;
168  if(couleur > 7){couleur = 1;}
169  sprintf(buffer, "3%d", couleur);
170  textColor(buffer);
171  printf("%c", *it);
172  }
173  textColor("0");
174 }
175 
177 
181 std::string colorString(const std::string & str, PTerminalStyle color){
182  char buffer[100];
183  sprintf(buffer, "\033[%dm", color);
184  return string(buffer) + str + "\033[0m";
185 }
186 
188 
191 void printColor(const std::string & str, PTerminalStyle color){
192  printf("\033[%dm%s\033[0m",color, str.c_str());
193 }
194 
196 
199 void printColorCerr(const std::string & str, PTerminalStyle color){
200  fprintf(stderr, "\033[%dm%s\033[0m",color, str.c_str());
201 }
202 
204 
206 short unsigned int getNbColTerminal(){
207  struct winsize w;
208  ioctl(0, TIOCGWINSZ, &w);
209  return w.ws_col;
210 }
211 
213 
215 short unsigned int getNbRowTerminal(){
216  struct winsize w;
217  ioctl(0, TIOCGWINSZ, &w);
218  return w.ws_row;
219 }
220 
222 
226 void printAllCol(const std::string & strLeft, char ch, const std::string & strRight){
227  long unsigned int longueurConsole((long unsigned int)getNbColTerminal());
228  long unsigned int sizeLeft(strLeft.size()), sizeRight(strRight.size());
229  long unsigned int sizeStr(sizeLeft - ((long unsigned int)countNbChar(strLeft ,'\033')*9)/2 + sizeRight - ((long unsigned int)countNbChar(strRight ,'\033')*9)/2);
230  if(sizeStr > longueurConsole){
231  cout << strLeft << strRight << endl;
232  }else{
233  cout << strLeft;
234  long unsigned int nbChar(longueurConsole - sizeStr);
235  for(long unsigned int i(0); i < nbChar; ++i){
236  cout << ch;
237  }
238  cout << strRight << endl;
239  }
240 }
241 
243 
247 void printAll(const std::string & strLeft, char ch, const std::string & strRight){
248  long unsigned int longueurConsole((long unsigned int)getNbColTerminal());
249  long unsigned int sizeLeft(strLeft.size()), sizeRight(strRight.size());
250  long unsigned int sizeStr(sizeLeft + sizeRight);
251  if(sizeStr > longueurConsole){
252  cout << strLeft << strRight << endl;
253  }else{
254  cout << strLeft;
255  long unsigned int nbChar(longueurConsole - sizeStr);
256  for(long unsigned int i(0); i < nbChar; ++i){
257  cout << ch;
258  }
259  cout << strRight << endl;
260  }
261 }
262 
264 
268 void printAllCerr(const std::string & strLeft, char ch, const std::string & strRight){
269  long unsigned int longueurConsole((long unsigned int)getNbColTerminal());
270  long unsigned int sizeLeft(strLeft.size()), sizeRight(strRight.size());
271  long unsigned int sizeStr(sizeLeft + sizeRight);
272  if(sizeStr > longueurConsole){
273  cerr << strLeft << strRight;
274  }else{
275  cerr << strLeft;
276  long unsigned int nbChar(longueurConsole - sizeStr);
277  for(long unsigned int i(0); i < nbChar; ++i){
278  cerr << ch;
279  }
280  cerr << strRight;
281  }
282 }
283 
285 
289 void printAllColCerr(const std::string & strLeft, char ch, const std::string & strRight){
290  long unsigned int longueurConsole((long unsigned int)getNbColTerminal());
291  long unsigned int sizeLeft(strLeft.size()), sizeRight(strRight.size());
292  long unsigned int sizeStr(sizeLeft - ((long unsigned int)countNbChar(strLeft ,'\033')*9)/2 + sizeRight - ((long unsigned int)countNbChar(strRight ,'\033')*9)/2);
293  if(sizeStr > longueurConsole){
294  cerr << strLeft << strRight;
295  }else{
296  cerr << strLeft;
297  long unsigned int nbChar(longueurConsole - sizeStr);
298  for(long unsigned int i(0); i < nbChar; ++i){
299  cerr << ch;
300  }
301  cerr << strRight;
302  }
303 }
304 
305 
clearTerminal
void clearTerminal()
fonction qui permet d'éffacer le terminal
Definition: string_color.cpp:128
termBWhite
std::string termBWhite()
affiche le fond du terminal en blanc
Definition: string_color.cpp:125
termBRed
std::string termBRed()
affiche le fond du terminal en rouge
Definition: string_color.cpp:95
getNbColTerminal
short unsigned int getNbColTerminal()
fonction qui renvoie le nombre de colonnes du terminal
Definition: string_color.cpp:206
termBlue
std::string termBlue()
affiche le terminal blue
Definition: string_color.cpp:70
termBYellow
std::string termBYellow()
affiche le fond du terminal en jaune
Definition: string_color.cpp:105
printAll
void printAll(const std::string &strLeft, char ch, const std::string &strRight)
fonction qui écrit un texte dans la console en l'alignant à droite (ne prend pas en compte la couleur...
Definition: string_color.cpp:247
termPurple
std::string termPurple()
affiche le terminal violet
Definition: string_color.cpp:75
termBCyan
std::string termBCyan()
affiche le fond du terminal en cyan
Definition: string_color.cpp:120
printColorCerr
void printColorCerr(const std::string &str, PTerminalStyle color)
fonction qui affiche un texte coloré dans le terminal
Definition: string_color.cpp:199
printAllColCerr
void printAllColCerr(const std::string &strLeft, char ch, const std::string &strRight)
fonction qui écrit un texte dans la console d'erreur en l'alignant à droite (prend en compte la coule...
Definition: string_color.cpp:289
termBGreen
std::string termBGreen()
affiche le fond du terminal en vert
Definition: string_color.cpp:100
termPositive
std::string termPositive()
affiche le terminal positif
Definition: string_color.cpp:45
termDefault
std::string termDefault()
affiche le terminal par défaut
Definition: string_color.cpp:20
termRed
std::string termRed()
affiche le terminal rouge
Definition: string_color.cpp:55
colorString
std::string colorString(const std::string &str, PTerminalStyle color)
fonction qui permet de colorier du texte dans une string
Definition: string_color.cpp:181
termBlink
std::string termBlink()
affiche le terminal clignotant
Definition: string_color.cpp:35
termYellow
std::string termYellow()
affiche le terminal jaune
Definition: string_color.cpp:65
printAllCol
void printAllCol(const std::string &strLeft, char ch, const std::string &strRight)
fonction qui écrit un texte dans la console en l'alignant à droite (prend en compte la couleur du tex...
Definition: string_color.cpp:226
termBright
std::string termBright()
affiche le terminal brillant
Definition: string_color.cpp:25
termUnderlined
std::string termUnderlined()
affiche le terminal souligné
Definition: string_color.cpp:30
termGreen
std::string termGreen()
affiche le terminal vert
Definition: string_color.cpp:60
textColorCerr
void textColorCerr(const char *color)
focntion qui permet de changer la couleur du texte dan le terminal (sortie d'erreur)
Definition: string_color.cpp:142
printAllCerr
void printAllCerr(const std::string &strLeft, char ch, const std::string &strRight)
fonction qui écrit un texte dans la console d'erreur en l'alignant à droite (ne prend pas en compte l...
Definition: string_color.cpp:268
textColor
void textColor(const char *color)
focntion qui permet de changer la couleur du texte dan le terminal
Definition: string_color.cpp:135
termWhite
std::string termWhite()
affiche le terminal blanc
Definition: string_color.cpp:85
getNbRowTerminal
short unsigned int getNbRowTerminal()
fonction qui renvoie le nombre de lignes du terminal
Definition: string_color.cpp:215
string_function.h
termBBlue
std::string termBBlue()
affiche le fond du terminal en bleu
Definition: string_color.cpp:110
PTerminalStyle
PTerminalStyle
enum qui permet de ne pas avoir à se rappeler les codes couleurs du terminal
Definition: string_color.h:16
createReleaseCurl.str
str
Definition: createReleaseCurl.py:128
termBBlack
std::string termBBlack()
affiche le fond du terminal en noir
Definition: string_color.cpp:90
string_color.h
countNbChar
size_t countNbChar(const std::string &str, char ch)
Count number of chararacters ch in string.
Definition: string_function.cpp:145
termBlack
std::string termBlack()
affiche le terminal noir
Definition: string_color.cpp:50
termCyan
std::string termCyan()
affiche le terminal cyan
Definition: string_color.cpp:80
printColor
void printColor(const std::string &str, PTerminalStyle color)
fonction qui affiche un texte coloré dans le terminal
Definition: string_color.cpp:191
termNegative
std::string termNegative()
affiche le terminal négatif
Definition: string_color.cpp:40
printDelire
void printDelire(const std::string &str)
fonction qui utilise toutes les couleur du terminal pour afficher un message
Definition: string_color.cpp:163
termBPurple
std::string termBPurple()
affiche le fond du terminal en violet
Definition: string_color.cpp:115