From edd6e2bbdad5930899a93db8fa73c3b61c7b9bcc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adrien=20Gallou=C3=ABt?= Date: Sat, 3 Oct 2026 15:48:24 +0200 Subject: [PATCH] common : add common_is_tty() helper and fix deprecated warnings on Windows (#29860) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Adrien Gallouët --- common/common.cpp | 13 +++++++++---- common/common.h | 4 ++++ common/download.cpp | 13 +------------ common/log.cpp | 13 ------------- 4 files changed, 14 insertions(+), 29 deletions(-) diff --git a/common/common.cpp b/common/common.cpp index 0269bc895..d3617fbf1 100644 --- a/common/common.cpp +++ b/common/common.cpp @@ -1078,6 +1078,14 @@ std::vector fs_list(const std::string & path, bool include_dir // TTY utils // +bool common_is_tty(FILE * file) { +#if defined(_WIN32) + return _isatty(_fileno(file)); +#else + return isatty(fileno(file)); +#endif +} + bool tty_can_use_colors() { // Check NO_COLOR environment variable (https://no-color.org/) if (const char * no_color = std::getenv("NO_COLOR")) { @@ -1095,10 +1103,7 @@ bool tty_can_use_colors() { // Check if stdout and stderr are connected to a terminal // We check both because log messages can go to either - bool stdout_is_tty = isatty(fileno(stdout)); - bool stderr_is_tty = isatty(fileno(stderr)); - - return stdout_is_tty || stderr_is_tty; + return common_is_tty(stdout) || common_is_tty(stderr); } // diff --git a/common/common.h b/common/common.h index 27be5c058..4308b0703 100644 --- a/common/common.h +++ b/common/common.h @@ -19,6 +19,7 @@ #include #include #include +#include #if defined(_WIN32) && !defined(_WIN32_WINNT) #define _WIN32_WINNT 0x0A00 @@ -946,6 +947,9 @@ void fs_write_atomic(const std::filesystem::path & path, const std::string & dat // Auto-detect if colors can be enabled based on terminal and environment bool tty_can_use_colors(); +// Check if the given file is attached to a terminal +bool common_is_tty(FILE * file); + // // Model utils // diff --git a/common/download.cpp b/common/download.cpp index 6b7123e47..ae0f7ffed 100644 --- a/common/download.cpp +++ b/common/download.cpp @@ -35,13 +35,6 @@ #endif #endif -// isatty -#if defined(_WIN32) -#include -#else -#include -#endif - // // downloader // @@ -97,11 +90,7 @@ class ProgressBar : public common_download_callback { } static bool is_output_a_tty() { -#if defined(_WIN32) - return _isatty(_fileno(stdout)); -#else - return isatty(1); -#endif + return common_is_tty(stdout); } public: diff --git a/common/log.cpp b/common/log.cpp index 0a9a4eb9e..b60e8d268 100644 --- a/common/log.cpp +++ b/common/log.cpp @@ -14,19 +14,6 @@ #include #include -#if defined(_WIN32) -# define WIN32_LEAN_AND_MEAN -# ifndef NOMINMAX -# define NOMINMAX -# endif -# include -# include -# define isatty _isatty -# define fileno _fileno -#else -# include -#endif // defined(_WIN32) - int common_log_verbosity_thold = LOG_DEFAULT_LLAMA; int common_log_get_verbosity_thold(void) {