Added console color support

This commit is contained in:
luca0N! 2020-06-27 13:04:46 -03:00
parent b1e196443d
commit df325b783a
No known key found for this signature in database
GPG Key ID: 9F1B99B522287CAB
2 changed files with 32 additions and 23 deletions

View File

@ -20,12 +20,15 @@
#ifndef COLORS_DEF #ifndef COLORS_DEF
#define COLORS_DEF #define COLORS_DEF
#define ANSI_COLOR_RED "\x1b[31m" /*#define ANSI_COLOR_RED "\x1b[31m"
#define ANSI_COLOR_GREEN "\x1b[32m" #define ANSI_COLOR_GREEN "\x1b[32m"
#define ANSI_COLOR_YELLOW "\x1b[33m" #define ANSI_COLOR_YELLOW "\x1b[33m"
#define ANSI_COLOR_BLUE "\x1b[34m" #define ANSI_COLOR_BLUE "\x1b[34m"
#define ANSI_COLOR_MAGENTA "\x1b[35m" #define ANSI_COLOR_MAGENTA "\x1b[35m"
#define ANSI_COLOR_CYAN "\x1b[36m" #define ANSI_COLOR_CYAN "\x1b[36m"
#define ANSI_COLOR_RESET "\033[0m" #define ANSI_COLOR_RESET "\033[0m"*/
#define RESET_CONSOLE_COLOR 7
#define FOREGROUND_YELLOW FOREGROUND_RED | FOREGROUND_GREEN
#endif #endif

42
main.c
View File

@ -22,7 +22,7 @@
#include "cfg.h" #include "cfg.h"
// Because this program is currently targeted at Windows only, the use of ANSI colors are no longer used as of May 5, 2020. // Because this program is currently targeted at Windows only, the use of ANSI colors are no longer used as of May 5, 2020.
// #include "colors.h" #include "colors.h"
#include <windows.h> #include <windows.h>
#include <tchar.h> #include <tchar.h>
@ -31,6 +31,16 @@
#include <string.h> #include <string.h>
#include <math.h> #include <math.h>
void setConsoleColor(int consoleColor){
HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
WORD wColor;
CONSOLE_SCREEN_BUFFER_INFO info;
GetConsoleScreenBufferInfo(h, &info);
wColor = info.wAttributes;
SetConsoleTextAttribute(h, consoleColor);
}
// Prints the program license to the user. // Prints the program license to the user.
int printLicense(int full){ int printLicense(int full){
if (full == 0) { if (full == 0) {
@ -112,15 +122,19 @@ void cleanDir(const char* path, unsigned* objectCount, unsigned long* totalDelet
} }
if (DeleteFile(fullPath) == 0){ if (DeleteFile(fullPath) == 0){
setConsoleColor(FOREGROUND_INTENSITY | FOREGROUND_RED);
int lastError = GetLastError(); int lastError = GetLastError();
printf(" %s\n", STR_FAILED_FILE); printf(" %s\n", STR_FAILED_FILE);
printf(" --> %s\n", fullPath); printf(" --> %s\n", fullPath);
printf(" --> %#010x\n", lastError); printf(" --> %#010x\n", lastError);
setConsoleColor(RESET_CONSOLE_COLOR);
} else { } else {
setConsoleColor(FOREGROUND_INTENSITY | FOREGROUND_BLUE);
printf(" %s\n", STR_DELETED_FILE); printf(" %s\n", STR_DELETED_FILE);
// Update the deleted object count if the process was successful. // Update the deleted object count if the process was successful.
(*objectCount)++; (*objectCount)++;
(*totalDeletedBytes) += length; (*totalDeletedBytes) += length;
setConsoleColor(RESET_CONSOLE_COLOR);
} }
} while(FindNextFile(hFind, &data)); } while(FindNextFile(hFind, &data));
} else { } else {
@ -157,20 +171,6 @@ void getAdditionalDirs(bool safeMode){
} }
if (containsVars){ if (containsVars){
/*HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
WORD wColor;
CONSOLE_SCREEN_BUFFER_INFO info;
GetConsoleScreenBufferInfo(h, &info);
wColor = info.wAttributes;
SetConsoleTextAttribute(h, FOREGROUND_RED);
printf("%s\n\n", STR_ERROR_VAR_CURRENTLY_NOT_SUPPORTED);
SetConsoleTextAttribute(h, wColor);*/
/*char varBuffer[sizeof(currentPath)];
memcpy(varBuffer, &currentPath[1], strlen(currentPath)-3);
varBuffer[sizeof(varBuffer)] = '\0';*/
char* buffer; char* buffer;
char* varBuffer; char* varBuffer;
@ -221,7 +221,8 @@ void getAdditionalDirs(bool safeMode){
if (!safeMode) if (!safeMode)
cleanDir(currentPath, &objectCount, &totalDeletedBytes); cleanDir(currentPath, &objectCount, &totalDeletedBytes);
} }
setConsoleColor(FOREGROUND_INTENSITY | FOREGROUND_GREEN);
printf(FMT_STR_OBJECT_COUNT, objectCount);
// Determine whether we should output the size as bytes, KiB, MiB or GiB. // Determine whether we should output the size as bytes, KiB, MiB or GiB.
if (totalDeletedBytes < 1024){ if (totalDeletedBytes < 1024){
printf(FMT_STR_TOTAL_DELETED_BYTES, totalDeletedBytes); printf(FMT_STR_TOTAL_DELETED_BYTES, totalDeletedBytes);
@ -230,22 +231,26 @@ void getAdditionalDirs(bool safeMode){
printf(FMT_STR_TOTAL_DELETED_KIBIBYTES, kib); printf(FMT_STR_TOTAL_DELETED_KIBIBYTES, kib);
} else if (totalDeletedBytes < (unsigned long) pow(1024, 3)){ } else if (totalDeletedBytes < (unsigned long) pow(1024, 3)){
float mib = (unsigned long) totalDeletedBytes / pow(1024, 2);// / (float) 1024 / (float) 1024; float mib = (unsigned long) totalDeletedBytes / pow(1024, 2);// / (float) 1024 / (float) 1024;
printf("%lu\n", totalDeletedBytes);
printf(FMT_STR_TOTAL_DELETED_MEBIBYTES, mib); printf(FMT_STR_TOTAL_DELETED_MEBIBYTES, mib);
} else { } else {
float gib = (unsigned long) totalDeletedBytes / pow(1024, 3); float gib = (unsigned long) totalDeletedBytes / pow(1024, 3);
printf(FMT_STR_TOTAL_DELETED_GIBIBYTES, gib); printf(FMT_STR_TOTAL_DELETED_GIBIBYTES, gib);
} }
setConsoleColor(RESET_CONSOLE_COLOR);
} }
int main(void){ int main(void){
SetConsoleOutputCP(CP_UTF8); SetConsoleOutputCP(CP_UTF8);
setConsoleColor(FOREGROUND_INTENSITY | FOREGROUND_YELLOW);
printLicense(0); printLicense(0);
setConsoleColor(RESET_CONSOLE_COLOR);
setConsoleColor(FOREGROUND_INTENSITY | FOREGROUND_BLUE);
printf("%s\n%s %d, %s\n", STR_LN, PROGRAM_NAME, PROGRAM_SEASON, PROGRAM_VERSION); printf("%s\n%s %d, %s\n", STR_LN, PROGRAM_NAME, PROGRAM_SEASON, PROGRAM_VERSION);
printf("%s\n%s\n\n", PROGRAM_COPYRIGHT, STR_LN); printf("%s\n%s\n\n", PROGRAM_COPYRIGHT, STR_LN);
setConsoleColor(FOREGROUND_INTENSITY | FOREGROUND_YELLOW);
unsigned int ignoreWarning = GetPrivateProfileInt("Geral", "IgnorarAlerta", 0, PATH_CFG); unsigned int ignoreWarning = GetPrivateProfileInt("Geral", "IgnorarAlerta", 0, PATH_CFG);
unsigned int licenseAccepted = GetPrivateProfileInt("Geral", "AceitarLicença", 0, PATH_CFG); unsigned int licenseAccepted = GetPrivateProfileInt("Geral", "AceitarLicença", 0, PATH_CFG);
if (!ignoreWarning || !licenseAccepted){ if (!ignoreWarning || !licenseAccepted){
@ -253,6 +258,7 @@ int main(void){
char in; char in;
fflush(stdout); fflush(stdout);
setConsoleColor(RESET_CONSOLE_COLOR);
scanf("%c", &in); scanf("%c", &in);
if (in == 'w' || in == 'c') if (in == 'w' || in == 'c')
@ -261,7 +267,7 @@ int main(void){
if (in != 's' && in != 'S' && in != 'y' && in != 'Y') if (in != 's' && in != 'S' && in != 'y' && in != 'Y')
return 0; return 0;
} }
setConsoleColor(RESET_CONSOLE_COLOR);
unsigned int safeMode = GetPrivateProfileInt("Geral", "ModoSeguro", 1, PATH_CFG); unsigned int safeMode = GetPrivateProfileInt("Geral", "ModoSeguro", 1, PATH_CFG);
getAdditionalDirs(safeMode); getAdditionalDirs(safeMode);