| Directory: | ./ |
|---|---|
| File: | tmp_project/PhoenixString/src/string_color.cpp |
| Date: | 2024-12-09 15:28:54 |
| Exec | Total | Coverage | |
|---|---|---|---|
| Lines: | 102 | 110 | 92.7% |
| Branches: | 42 | 48 | 87.5% |
| Line | Branch | Exec | Source |
|---|---|---|---|
| 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 | |||
| 17 | ///affiche le terminal par défaut | ||
| 18 | /** @return string de modification du terminal | ||
| 19 | */ | ||
| 20 |
1/1✓ Branch 2 taken 46 times.
|
46 | std::string termDefault(){return "\033[0m";} |
| 21 | |||
| 22 | ///affiche le terminal brillant | ||
| 23 | /** @return string de modification du terminal | ||
| 24 | */ | ||
| 25 |
1/1✓ Branch 2 taken 1 times.
|
1 | std::string termBright(){return "\033[1m";} |
| 26 | |||
| 27 | ///affiche le terminal souligné | ||
| 28 | /** @return string de modification du terminal | ||
| 29 | */ | ||
| 30 |
1/1✓ Branch 2 taken 1 times.
|
1 | std::string termUnderlined(){return "\033[4m";} |
| 31 | |||
| 32 | ///affiche le terminal clignotant | ||
| 33 | /** @return string de modification du terminal | ||
| 34 | */ | ||
| 35 |
1/1✓ Branch 2 taken 1 times.
|
1 | std::string termBlink(){return "\033[5m";} |
| 36 | |||
| 37 | ///affiche le terminal négatif | ||
| 38 | /** @return string de modification du terminal | ||
| 39 | */ | ||
| 40 |
1/1✓ Branch 2 taken 1 times.
|
1 | std::string termNegative(){return "\033[7m";} |
| 41 | |||
| 42 | ///affiche le terminal positif | ||
| 43 | /** @return string de modification du terminal | ||
| 44 | */ | ||
| 45 |
1/1✓ Branch 2 taken 1 times.
|
1 | std::string termPositive(){return "\033[27m";} |
| 46 | |||
| 47 | ///affiche le terminal noir | ||
| 48 | /** @return string de modification du terminal | ||
| 49 | */ | ||
| 50 |
1/1✓ Branch 2 taken 1 times.
|
1 | std::string termBlack(){return "\033[30m";} |
| 51 | |||
| 52 | ///affiche le terminal rouge | ||
| 53 | /** @return string de modification du terminal | ||
| 54 | */ | ||
| 55 |
1/1✓ Branch 2 taken 1 times.
|
1 | std::string termRed(){return "\033[31m";} |
| 56 | |||
| 57 | ///affiche le terminal vert | ||
| 58 | /** @return string de modification du terminal | ||
| 59 | */ | ||
| 60 |
1/1✓ Branch 2 taken 1 times.
|
1 | std::string termGreen(){return "\033[32m";} |
| 61 | |||
| 62 | ///affiche le terminal jaune | ||
| 63 | /** @return string de modification du terminal | ||
| 64 | */ | ||
| 65 |
1/1✓ Branch 2 taken 1 times.
|
1 | std::string termYellow(){return "\033[33m";} |
| 66 | |||
| 67 | ///affiche le terminal blue | ||
| 68 | /** @return string de modification du terminal | ||
| 69 | */ | ||
| 70 |
1/1✓ Branch 2 taken 1 times.
|
1 | std::string termBlue(){return "\033[34m";} |
| 71 | |||
| 72 | ///affiche le terminal violet | ||
| 73 | /** @return string de modification du terminal | ||
| 74 | */ | ||
| 75 |
1/1✓ Branch 2 taken 1 times.
|
1 | std::string termPurple(){return "\033[35m";} |
| 76 | |||
| 77 | ///affiche le terminal cyan | ||
| 78 | /** @return string de modification du terminal | ||
| 79 | */ | ||
| 80 |
1/1✓ Branch 2 taken 1 times.
|
1 | std::string termCyan(){return "\033[36m";} |
| 81 | |||
| 82 | ///affiche le terminal blanc | ||
| 83 | /** @return string de modification du terminal | ||
| 84 | */ | ||
| 85 |
1/1✓ Branch 2 taken 1 times.
|
1 | std::string termWhite(){return "\033[37m";} |
| 86 | |||
| 87 | ///affiche le fond du terminal en noir | ||
| 88 | /** @return string de modification du terminal | ||
| 89 | */ | ||
| 90 |
1/1✓ Branch 2 taken 1 times.
|
1 | std::string termBBlack(){return "\033[40m";} |
| 91 | |||
| 92 | ///affiche le fond du terminal en rouge | ||
| 93 | /** @return string de modification du terminal | ||
| 94 | */ | ||
| 95 |
1/1✓ Branch 2 taken 1 times.
|
1 | std::string termBRed(){return "\033[41m";} |
| 96 | |||
| 97 | ///affiche le fond du terminal en vert | ||
| 98 | /** @return string de modification du terminal | ||
| 99 | */ | ||
| 100 |
1/1✓ Branch 2 taken 1 times.
|
1 | std::string termBGreen(){return "\033[42m";} |
| 101 | |||
| 102 | ///affiche le fond du terminal en jaune | ||
| 103 | /** @return string de modification du terminal | ||
| 104 | */ | ||
| 105 |
1/1✓ Branch 2 taken 1 times.
|
1 | std::string termBYellow(){return "\033[43m";} |
| 106 | |||
| 107 | ///affiche le fond du terminal en bleu | ||
| 108 | /** @return string de modification du terminal | ||
| 109 | */ | ||
| 110 |
1/1✓ Branch 2 taken 1 times.
|
1 | std::string termBBlue(){return "\033[44m";} |
| 111 | |||
| 112 | ///affiche le fond du terminal en violet | ||
| 113 | /** @return string de modification du terminal | ||
| 114 | */ | ||
| 115 |
1/1✓ Branch 2 taken 1 times.
|
1 | std::string termBPurple(){return "\033[45m";} |
| 116 | |||
| 117 | ///affiche le fond du terminal en cyan | ||
| 118 | /** @return string de modification du terminal | ||
| 119 | */ | ||
| 120 |
1/1✓ Branch 2 taken 1 times.
|
1 | std::string termBCyan(){return "\033[46m";} |
| 121 | |||
| 122 | ///affiche le fond du terminal en blanc | ||
| 123 | /** @return string de modification du terminal | ||
| 124 | */ | ||
| 125 |
1/1✓ Branch 2 taken 1 times.
|
1 | std::string termBWhite(){return "\033[47m";} |
| 126 | |||
| 127 | ///fonction qui permet d'éffacer le terminal | ||
| 128 | 1 | void clearTerminal(){ | |
| 129 | 1 | printf("\033[H\033[2J"); | |
| 130 | 1 | } | |
| 131 | |||
| 132 | ///focntion qui permet de changer la couleur du texte dan le terminal | ||
| 133 | /** @param color : couleur des caractères dans le terminal | ||
| 134 | */ | ||
| 135 | 73 | void textColor(const char* color){ | |
| 136 | 73 | printf("\033[%sm",color); | |
| 137 | 73 | } | |
| 138 | |||
| 139 | ///focntion qui permet de changer la couleur du texte dan le terminal (sortie d'erreur) | ||
| 140 | /** @param color : couleur des caractères dans le terminal (sortie d'erreur) | ||
| 141 | */ | ||
| 142 | 1 | void textColorCerr(const char* color){ | |
| 143 | 1 | fprintf(stderr, "\033[%sm", color); | |
| 144 | 1 | } | |
| 145 | |||
| 146 | ///focntion qui permet de changer la couleur du texte dan le terminal | ||
| 147 | /** @param color : couleur des caractères dans le terminal | ||
| 148 | */ | ||
| 149 | 22 | void textColor(PTerminalStyle color){ | |
| 150 | 22 | printf("\033[%dm",color); | |
| 151 | 22 | } | |
| 152 | |||
| 153 | ///focntion qui permet de changer la couleur du texte dan le terminal (sortie d'erreur) | ||
| 154 | /** @param color : couleur des caractères dans le terminal (sortie d'erreur) | ||
| 155 | */ | ||
| 156 | 22 | void textColorCerr(PTerminalStyle color){ | |
| 157 | 22 | fprintf(stderr, "\033[%dm", color); | |
| 158 | 22 | } | |
| 159 | |||
| 160 | ///fonction qui utilise toutes les couleur du terminal pour afficher un message | ||
| 161 | /** @param str : méssage à afficher dans le terminal | ||
| 162 | */ | ||
| 163 | 1 | void printDelire(const std::string & str){ | |
| 164 | 1 | int couleur = 0; | |
| 165 | char buffer[255]; | ||
| 166 |
2/2✓ Branch 4 taken 71 times.
✓ Branch 5 taken 1 times.
|
72 | for(string::const_iterator it = str.begin(); it != str.end(); it++){ |
| 167 | 71 | couleur++; | |
| 168 |
2/2✓ Branch 0 taken 10 times.
✓ Branch 1 taken 61 times.
|
71 | if(couleur > 7){couleur = 1;} |
| 169 | 71 | sprintf(buffer, "3%d", couleur); | |
| 170 |
1/1✓ Branch 1 taken 71 times.
|
71 | textColor(buffer); |
| 171 |
1/1✓ Branch 2 taken 71 times.
|
71 | printf("%c", *it); |
| 172 | } | ||
| 173 |
1/1✓ Branch 1 taken 1 times.
|
1 | textColor("0"); |
| 174 | 1 | } | |
| 175 | |||
| 176 | ///fonction qui permet de colorier du texte dans une string | ||
| 177 | /** @param str : méssage à afficher dans le terminal | ||
| 178 | * @param color : couleur des caractères dans le terminal | ||
| 179 | * @return string avec la couleur | ||
| 180 | */ | ||
| 181 | 22 | std::string colorString(const std::string & str, PTerminalStyle color){ | |
| 182 | char buffer[100]; | ||
| 183 | 22 | sprintf(buffer, "\033[%dm", color); | |
| 184 |
3/3✓ Branch 2 taken 22 times.
✓ Branch 5 taken 22 times.
✓ Branch 8 taken 22 times.
|
44 | return string(buffer) + str + "\033[0m"; |
| 185 | } | ||
| 186 | |||
| 187 | ///fonction qui affiche un texte coloré dans le terminal | ||
| 188 | /** @param str : méssage à afficher dans le terminal | ||
| 189 | * @param color : couleur des caractères dans le terminal | ||
| 190 | */ | ||
| 191 | 22 | void printColor(const std::string & str, PTerminalStyle color){ | |
| 192 | 22 | printf("\033[%dm%s\033[0m",color, str.c_str()); | |
| 193 | 22 | } | |
| 194 | |||
| 195 | ///fonction qui affiche un texte coloré dans le terminal | ||
| 196 | /** @param str : méssage à afficher dans le terminal | ||
| 197 | * @param color : couleur des caractères dans le terminal | ||
| 198 | */ | ||
| 199 | 22 | void printColorCerr(const std::string & str, PTerminalStyle color){ | |
| 200 | 22 | fprintf(stderr, "\033[%dm%s\033[0m",color, str.c_str()); | |
| 201 | 22 | } | |
| 202 | |||
| 203 | ///fonction qui renvoie le nombre de colonnes du terminal | ||
| 204 | /** @return nombre de colonnes du terminal | ||
| 205 | */ | ||
| 206 | 8 | short unsigned int getNbColTerminal(){ | |
| 207 | struct winsize w; | ||
| 208 | 8 | ioctl(0, TIOCGWINSZ, &w); | |
| 209 | 8 | return w.ws_col; | |
| 210 | } | ||
| 211 | |||
| 212 | ///fonction qui renvoie le nombre de lignes du terminal | ||
| 213 | /** @return nombre de lignes du terminal | ||
| 214 | */ | ||
| 215 | 1 | short unsigned int getNbRowTerminal(){ | |
| 216 | struct winsize w; | ||
| 217 | 1 | ioctl(0, TIOCGWINSZ, &w); | |
| 218 | 1 | return w.ws_row; | |
| 219 | } | ||
| 220 | |||
| 221 | ///fonction qui écrit un texte dans la console en l'alignant à droite (prend en compte la couleur du texte) | ||
| 222 | /** @param strLeft : texte à écrire aligné à gauche | ||
| 223 | * @param ch : caractère à afficher entre les deux | ||
| 224 | * @param strRight : texte à écrire aligné à droite | ||
| 225 | */ | ||
| 226 | 1 | void printAllCol(const std::string & strLeft, char ch, const std::string & strRight){ | |
| 227 | 1 | long unsigned int longueurConsole((long unsigned int)getNbColTerminal()); | |
| 228 | 1 | long unsigned int sizeLeft(strLeft.size()), sizeRight(strRight.size()); | |
| 229 | 1 | long unsigned int sizeStr(sizeLeft - ((long unsigned int)countNbChar(strLeft ,'\033')*9)/2 + sizeRight - ((long unsigned int)countNbChar(strRight ,'\033')*9)/2); | |
| 230 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if(sizeStr > longueurConsole){ |
| 231 | ✗ | cout << strLeft << strRight << endl; | |
| 232 | }else{ | ||
| 233 | 1 | cout << strLeft; | |
| 234 | 1 | long unsigned int nbChar(longueurConsole - sizeStr); | |
| 235 |
2/2✓ Branch 0 taken 18612 times.
✓ Branch 1 taken 1 times.
|
18613 | for(long unsigned int i(0); i < nbChar; ++i){ |
| 236 | 18612 | cout << ch; | |
| 237 | } | ||
| 238 | 1 | cout << strRight << endl; | |
| 239 | } | ||
| 240 | 1 | } | |
| 241 | |||
| 242 | ///fonction qui écrit un texte dans la console en l'alignant à droite (ne prend pas en compte la couleur du texte) | ||
| 243 | /** @param strLeft : texte à écrire aligné à gauche | ||
| 244 | * @param ch : caractère à afficher entre les deux | ||
| 245 | * @param strRight : texte à écrire aligné à droite | ||
| 246 | */ | ||
| 247 | 1 | void printAll(const std::string & strLeft, char ch, const std::string & strRight){ | |
| 248 | 1 | long unsigned int longueurConsole((long unsigned int)getNbColTerminal()); | |
| 249 | 1 | long unsigned int sizeLeft(strLeft.size()), sizeRight(strRight.size()); | |
| 250 | 1 | long unsigned int sizeStr(sizeLeft + sizeRight); | |
| 251 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if(sizeStr > longueurConsole){ |
| 252 | ✗ | cout << strLeft << strRight << endl; | |
| 253 | }else{ | ||
| 254 | 1 | cout << strLeft; | |
| 255 | 1 | long unsigned int nbChar(longueurConsole - sizeStr); | |
| 256 |
2/2✓ Branch 0 taken 4951 times.
✓ Branch 1 taken 1 times.
|
4952 | for(long unsigned int i(0); i < nbChar; ++i){ |
| 257 | 4951 | cout << ch; | |
| 258 | } | ||
| 259 | 1 | cout << strRight << endl; | |
| 260 | } | ||
| 261 | 1 | } | |
| 262 | |||
| 263 | ///fonction qui écrit un texte dans la console d'erreur en l'alignant à droite (ne prend pas en compte la couleur du texte) | ||
| 264 | /** @param strLeft : texte à écrire aligné à gauche | ||
| 265 | * @param ch : caractère à afficher entre les deux | ||
| 266 | * @param strRight : texte à écrire aligné à droite | ||
| 267 | */ | ||
| 268 | 1 | void printAllCerr(const std::string & strLeft, char ch, const std::string & strRight){ | |
| 269 | 1 | long unsigned int longueurConsole((long unsigned int)getNbColTerminal()); | |
| 270 | 1 | long unsigned int sizeLeft(strLeft.size()), sizeRight(strRight.size()); | |
| 271 | 1 | long unsigned int sizeStr(sizeLeft + sizeRight); | |
| 272 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if(sizeStr > longueurConsole){ |
| 273 | ✗ | cerr << strLeft << strRight; | |
| 274 | }else{ | ||
| 275 | 1 | cerr << strLeft; | |
| 276 | 1 | long unsigned int nbChar(longueurConsole - sizeStr); | |
| 277 |
2/2✓ Branch 0 taken 18615 times.
✓ Branch 1 taken 1 times.
|
18616 | for(long unsigned int i(0); i < nbChar; ++i){ |
| 278 | 18615 | cerr << ch; | |
| 279 | } | ||
| 280 | 1 | cerr << strRight; | |
| 281 | } | ||
| 282 | 1 | } | |
| 283 | |||
| 284 | ///fonction qui écrit un texte dans la console d'erreur en l'alignant à droite (prend en compte la couleur du texte) | ||
| 285 | /** @param strLeft : texte à écrire aligné à gauche | ||
| 286 | * @param ch : caractère à afficher entre les deux | ||
| 287 | * @param strRight : texte à écrire aligné à droite | ||
| 288 | */ | ||
| 289 | 1 | void printAllColCerr(const std::string & strLeft, char ch, const std::string & strRight){ | |
| 290 | 1 | long unsigned int longueurConsole((long unsigned int)getNbColTerminal()); | |
| 291 | 1 | long unsigned int sizeLeft(strLeft.size()), sizeRight(strRight.size()); | |
| 292 | 1 | long unsigned int sizeStr(sizeLeft - ((long unsigned int)countNbChar(strLeft ,'\033')*9)/2 + sizeRight - ((long unsigned int)countNbChar(strRight ,'\033')*9)/2); | |
| 293 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | if(sizeStr > longueurConsole){ |
| 294 | 1 | 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 | 1 | } | |
| 304 | |||
| 305 | |||
| 306 |