Fix colors detection under Windows (when no ref & when redirected IOs)

This commit is contained in:
Philippe Teuwen 2020-06-12 18:17:39 +02:00
commit bb5c588935

View file

@ -32,9 +32,6 @@
#include "flash.h" #include "flash.h"
#include "preferences.h" #include "preferences.h"
// Used to enable/disable use of preferences json file
#define USE_PREFERENCE_FILE
#define BANNERMSG1 " :snowflake: iceman@icesql.net" #define BANNERMSG1 " :snowflake: iceman@icesql.net"
#define BANNERMSG2 " https://github.com/rfidresearchgroup/proxmark3/" #define BANNERMSG2 " https://github.com/rfidresearchgroup/proxmark3/"
#define BANNERMSG3 " bleeding edge :coffee:" #define BANNERMSG3 " bleeding edge :coffee:"
@ -632,8 +629,7 @@ finish2:
return ret; return ret;
} }
#ifndef USE_PREFERENCE_FILE /*
// Check if windows AnsiColor Support is enabled in the registery // Check if windows AnsiColor Support is enabled in the registery
// [HKEY_CURRENT_USER\Console] // [HKEY_CURRENT_USER\Console]
// "VirtualTerminalLevel"=dword:00000001 // "VirtualTerminalLevel"=dword:00000001
@ -688,8 +684,7 @@ static bool DetectWindowsAnsiSupport(void) {
#endif #endif
return ret; return ret;
} }
*/
#endif
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
srand(time(0)); srand(time(0));
@ -912,7 +907,6 @@ int main(int argc, char *argv[]) {
return 1; return 1;
} }
#ifdef USE_PREFERENCE_FILE
// Load Settings and assign // Load Settings and assign
// This will allow the command line to override the settings.json values // This will allow the command line to override the settings.json values
preferences_load(); preferences_load();
@ -921,28 +915,24 @@ int main(int argc, char *argv[]) {
g_debugMode = session.client_debug_level; g_debugMode = session.client_debug_level;
// settings_save (); // settings_save ();
// End Settings // End Settings
#endif
#ifndef USE_PREFERENCE_FILE
// comment next 2 lines to use session values set from settings_load
session.supports_colors = DetectWindowsAnsiSupport();
session.emoji_mode = ALTTEXT;
#endif
session.stdinOnTTY = isatty(STDIN_FILENO); session.stdinOnTTY = isatty(STDIN_FILENO);
session.stdoutOnTTY = isatty(STDOUT_FILENO); session.stdoutOnTTY = isatty(STDOUT_FILENO);
#if defined(__linux__) || defined(__APPLE__)
// it's okay to use color if: // it's okay to use color if:
// * Linux or OSX // * Linux or OSX
// * Not redirected to a file but printed to term // * Not redirected to a file but printed to term
// For info, grep --color=auto is doing sth like this, plus test getenv("TERM") != "dumb": // For info, grep --color=auto is doing sth like this, plus test getenv("TERM") != "dumb":
// struct stat tmp_stat; // struct stat tmp_stat;
// if ((fstat (STDOUT_FILENO, &tmp_stat) == 0) && (S_ISCHR (tmp_stat.st_mode)) && isatty(STDIN_FILENO)) // if ((fstat (STDOUT_FILENO, &tmp_stat) == 0) && (S_ISCHR (tmp_stat.st_mode)) && isatty(STDIN_FILENO))
#ifdef USE_PREFERENCE_FILE
if (!session.preferences_loaded) { if (!session.preferences_loaded) {
if (session.stdinOnTTY && session.stdoutOnTTY) { if (session.stdinOnTTY && session.stdoutOnTTY) {
#if defined(__linux__) || defined(__APPLE__)
session.supports_colors = true; session.supports_colors = true;
session.emoji_mode = EMOJI; session.emoji_mode = EMOJI;
#else
session.supports_colors = DetectWindowsAnsiSupport();
session.emoji_mode = ALTTEXT;
#endif
} }
} else { } else {
// even if prefs, we disable colors if stdin or stdout is not a TTY // even if prefs, we disable colors if stdin or stdout is not a TTY
@ -951,14 +941,7 @@ int main(int argc, char *argv[]) {
session.emoji_mode = ALTTEXT; session.emoji_mode = ALTTEXT;
} }
} }
#else
if (session.stdinOnTTY && session.stdoutOnTTY) {
session.supports_colors = true;
session.emoji_mode = EMOJI;
}
#endif
#endif
// Let's take a baudrate ok for real UART, USB-CDC & BT don't use that info anyway // Let's take a baudrate ok for real UART, USB-CDC & BT don't use that info anyway
if (speed == 0) if (speed == 0)
speed = USART_BAUD_RATE; speed = USART_BAUD_RATE;
@ -1013,7 +996,6 @@ int main(int argc, char *argv[]) {
if (!script_cmds_file && !script_cmd && session.stdinOnTTY && session.stdoutOnTTY && !flash_mode) if (!script_cmds_file && !script_cmd && session.stdinOnTTY && session.stdoutOnTTY && !flash_mode)
showBanner(); showBanner();
#ifdef USE_PREFERENCE_FILE
// Save settings if not loaded from settings json file. // Save settings if not loaded from settings json file.
// Doing this here will ensure other checks and updates are saved to over rule default // Doing this here will ensure other checks and updates are saved to over rule default
// e.g. Linux color use check // e.g. Linux color use check
@ -1034,7 +1016,6 @@ int main(int argc, char *argv[]) {
PrintAndLogEx(WARNING,"Proxmark3 not ready to set debug level"); PrintAndLogEx(WARNING,"Proxmark3 not ready to set debug level");
} }
*/ */
#endif
#ifdef HAVE_GUI #ifdef HAVE_GUI
@ -1061,9 +1042,7 @@ int main(int argc, char *argv[]) {
CloseProxmark(); CloseProxmark();
} }
#ifdef USE_PREFERENCE_FILE
if (session.window_changed) // Plot/Overlay moved or resized if (session.window_changed) // Plot/Overlay moved or resized
preferences_save(); preferences_save();
#endif
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);
} }