Simplify color & banner logic

This commit is contained in:
Philippe Teuwen
2019-04-30 13:02:27 +02:00
parent a91d0a7b19
commit 0a4b90ac20
12 changed files with 77 additions and 72 deletions

View File

@@ -265,13 +265,6 @@ static void PacketResponseReceived(PacketResponseNG *packet) {
// we got a packet, reset WaitForResponseTimeout timeout
timeout_start_time = msclock();
bool filter_ansi = true;
#if defined(__linux__) || (__APPLE__)
struct stat tmp_stat;
if ((fstat (STDOUT_FILENO, &tmp_stat) == 0) && (S_ISCHR (tmp_stat.st_mode)) && isatty(STDIN_FILENO))
filter_ansi = false;
#endif
switch (packet->cmd) {
// First check if we are handling a debug message
case CMD_DEBUG_PRINT_STRING: {
@@ -289,11 +282,11 @@ static void PacketResponseReceived(PacketResponseNG *packet) {
struct d *data = (struct d *)&packet->data.asBytes;
len = packet->length - sizeof(data->flag);
flag = data->flag;
memcpy_filter_ansi(s, data->buf, len, (flag & FLAG_ANSI) && filter_ansi);
memcpy(s, data->buf, len);
} else {
len = MIN(packet->oldarg[0], USB_CMD_DATA_SIZE);
flag = packet->oldarg[1];
memcpy_filter_ansi(s, packet->data.asBytes, len, (flag & FLAG_ANSI) && filter_ansi);
memcpy(s, packet->data.asBytes, len);
}
if (flag & FLAG_LOG) {

View File

@@ -438,9 +438,21 @@ int main(int argc, char *argv[]) {
return 1;
}
// ascii art
bool stdinOnPipe = !isatty(STDIN_FILENO);
if (!script_cmds_file && !stdinOnPipe)
session.supports_colors = false;
session.stdinOnTTY = isatty(STDIN_FILENO);
session.stdoutOnTTY = isatty(STDOUT_FILENO);
#if defined(__linux__) || (__APPLE__)
// it's okay to use color if:
// * Linux or OSX
// * Not redirected to a file but printed to term
// For info, grep --color=auto is doing sth like this, plus test getenv("TERM") != "dumb":
// struct stat tmp_stat;
// if ((fstat (STDOUT_FILENO, &tmp_stat) == 0) && (S_ISCHR (tmp_stat.st_mode)) && isatty(STDIN_FILENO))
if (session.stdinOnTTY && session.stdoutOnTTY)
session.supports_colors = true;
#endif
// ascii art only in interactive client
if (!script_cmds_file && !script_cmd && session.stdinOnTTY && session.stdoutOnTTY)
showBanner();
// Let's take a baudrate ok for real UART, USB-CDC & BT don't use that info anyway

View File

@@ -16,10 +16,7 @@
#endif
#include "ui.h"
#if defined(__linux__) || (__APPLE__)
#include <unistd.h>
#include <sys/stat.h>
#endif
session_arg_t session;
double CursorScaleFactor = 1;
int PlotGridX = 0, PlotGridY = 0, PlotGridXdefault = 64, PlotGridYdefault = 64;
@@ -179,12 +176,7 @@ void PrintAndLog(const char *fmt, ...) {
vsnprintf(buffer, sizeof(buffer), fmt, argptr);
va_end(argptr);
bool filter_ansi = true;
#if defined(__linux__) || (__APPLE__)
struct stat tmp_stat;
if ((fstat (STDOUT_FILENO, &tmp_stat) == 0) && (S_ISCHR (tmp_stat.st_mode)) && isatty(STDIN_FILENO))
filter_ansi = false;
#endif
bool filter_ansi = !session.supports_colors;
memcpy_filter_ansi(buffer2, buffer, sizeof(buffer), filter_ansi);
printf("%s", buffer2);
printf(" "); // cleaning prompt

View File

@@ -24,6 +24,14 @@
#include <complex.h>
#include "util.h"
typedef struct {
bool stdinOnTTY;
bool stdoutOnTTY;
bool supports_colors;
} session_arg_t;
extern session_arg_t session;
#ifndef M_PI
#define M_PI 3.14159265358979323846264338327
#endif