diff --git a/CMakeLists.txt b/CMakeLists.txt index 3c1ea991..c777afc2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -125,7 +125,6 @@ check_platform_supports_browse_mode(platform_supports_ninja_browse) # Core source files all build into ninja library. add_library(libninja OBJECT - src/ansi_color.cc src/build_log.cc src/build.cc src/clean.cc @@ -274,7 +273,6 @@ if(BUILD_TESTING) # Tests all build into ninja_test executable. add_executable(ninja_test - src/ansi_color_test.cc src/build_log_test.cc src/build_test.cc src/clean_test.cc diff --git a/configure.py b/configure.py index a3cf094b..db5c4dc7 100755 --- a/configure.py +++ b/configure.py @@ -537,8 +537,7 @@ n.newline() n.comment('Core source files all build into ninja library.') objs.extend(re2c_objs) -for name in ['ansi_color', - 'build', +for name in ['build', 'build_log', 'clean', 'clparser', @@ -644,7 +643,6 @@ if gtest_src_dir: test_variables += [('pdb', 'ninja_test.pdb')] test_names = [ - 'ansi_color_test', 'build_log_test', 'build_test', 'clean_test', diff --git a/src/ansi_color.cc b/src/ansi_color.cc deleted file mode 100644 index ba99c77d..00000000 --- a/src/ansi_color.cc +++ /dev/null @@ -1,33 +0,0 @@ -// Copyright 2026 Google Inc. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include "ansi_color.h" - -#include -#include - -bool EnvHasNoColor() { - char* no_color = std::getenv("NO_COLOR"); - return no_color && std::string(no_color) != "0"; -} - -bool EnvHasCliColorForce() { - char* clicolor_force = std::getenv("CLICOLOR_FORCE"); - return clicolor_force && std::string(clicolor_force) != "0"; -} - -bool EnvHasForceColor() { - char* force_color = std::getenv("FORCE_COLOR"); - return force_color && std::string(force_color) != "0"; -} diff --git a/src/ansi_color.h b/src/ansi_color.h deleted file mode 100644 index 8f3c5a00..00000000 --- a/src/ansi_color.h +++ /dev/null @@ -1,27 +0,0 @@ -// Copyright 2026 Google Inc. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#ifndef NINJA_ANSI_COLOR_H -#define NINJA_ANSI_COLOR_H - -/// Check if environment variable NO_COLOR is set. -bool EnvHasNoColor(); - -/// Check if environment variable CLICOLOR_FORCE is set. -bool EnvHasCliColorForce(); - -/// Check if environment variable FORCE_COLOR is set. -bool EnvHasForceColor(); - -#endif // NINJA_ANSI_COLOR_H diff --git a/src/ansi_color_test.cc b/src/ansi_color_test.cc deleted file mode 100644 index 2ed776ed..00000000 --- a/src/ansi_color_test.cc +++ /dev/null @@ -1,80 +0,0 @@ -// Copyright 2026 Google Inc. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include "ansi_color.h" -#include "test.h" - -#ifdef _WIN32 -#include -#endif - -TEST(AnsiColorTest, CliColorForce) { - #ifdef _WIN32 - const char* temp_value = std::getenv("CLICOLOR_FORCE"); - std::string original_value = temp_value ? temp_value : "0"; - _putenv_s("CLICOLOR_FORCE", "1"); - ASSERT_EQ(true, EnvHasCliColorForce()); - _putenv_s("CLICOLOR_FORCE", "0"); - ASSERT_EQ(false, EnvHasCliColorForce()); - _putenv_s("CLICOLOR_FORCE", original_value.c_str()); - #else - const char* temp_value = std::getenv("CLICOLOR_FORCE"); - std::string original_value = temp_value ? temp_value : "0"; - setenv("CLICOLOR_FORCE", "1", 1); - ASSERT_EQ(true, EnvHasCliColorForce()); - setenv("CLICOLOR_FORCE", "0", 1); - ASSERT_EQ(false, EnvHasCliColorForce()); - setenv("CLICOLOR_FORCE", original_value.c_str(), 1); - #endif -} - -TEST(AnsiColorTest, ForceColor) { - #ifdef _WIN32 - const char* temp_value = std::getenv("FORCE_COLOR"); - std::string original_value = temp_value ? temp_value : "0"; - _putenv_s("FORCE_COLOR", "1"); - ASSERT_EQ(true, EnvHasForceColor()); - _putenv_s("FORCE_COLOR", "0"); - ASSERT_EQ(false, EnvHasForceColor()); - _putenv_s("FORCE_COLOR", original_value.c_str()); - #else - const char* temp_value = std::getenv("FORCE_COLOR"); - std::string original_value = temp_value ? temp_value : "0"; - setenv("FORCE_COLOR", "1", 1); - ASSERT_EQ(true, EnvHasForceColor()); - setenv("FORCE_COLOR", "0", 1); - ASSERT_EQ(false, EnvHasForceColor()); - setenv("FORCE_COLOR", original_value.c_str(), 1); - #endif -} - -TEST(AnsiColorTest, NoColor) { - #ifdef _WIN32 - const char* temp_value = std::getenv("NO_COLOR"); - std::string original_value = temp_value ? temp_value : "0"; - _putenv_s("NO_COLOR", "1"); - ASSERT_EQ(true, EnvHasNoColor()); - _putenv_s("NO_COLOR", "0"); - ASSERT_EQ(false, EnvHasNoColor()); - _putenv_s("NO_COLOR", original_value.c_str()); - #else - const char* temp_value = std::getenv("NO_COLOR"); - std::string original_value = temp_value ? temp_value : "0"; - setenv("NO_COLOR", "1", 1); - ASSERT_EQ(true, EnvHasNoColor()); - setenv("NO_COLOR", "0", 1); - ASSERT_EQ(false, EnvHasNoColor()); - setenv("NO_COLOR", original_value.c_str(), 1); - #endif -} diff --git a/src/line_printer.cc b/src/line_printer.cc index 79b8642c..50309e74 100644 --- a/src/line_printer.cc +++ b/src/line_printer.cc @@ -30,7 +30,6 @@ #include "elide_middle.h" #include "util.h" -#include "ansi_color.h" using namespace std; @@ -185,3 +184,18 @@ void LinePrinter::SetConsoleLocked(bool locked) { line_buffer_.clear(); } } + +bool LinePrinter::EnvHasNoColor() { + char* no_color = std::getenv("NO_COLOR"); + return no_color && std::string(no_color) != "0"; +} + +bool LinePrinter::EnvHasCliColorForce() { + char* clicolor_force = std::getenv("CLICOLOR_FORCE"); + return clicolor_force && std::string(clicolor_force) != "0"; +} + +bool LinePrinter::EnvHasForceColor() { + char* force_color = std::getenv("FORCE_COLOR"); + return force_color && std::string(force_color) != "0"; +} diff --git a/src/line_printer.h b/src/line_printer.h index a8ec9ff4..ac1a7c67 100644 --- a/src/line_printer.h +++ b/src/line_printer.h @@ -43,6 +43,15 @@ struct LinePrinter { /// console is locked will not be printed until it is unlocked. void SetConsoleLocked(bool locked); + /// Check if environment variable NO_COLOR is set. + bool EnvHasNoColor(); + + /// Check if environment variable CLICOLOR_FORCE is set. + bool EnvHasCliColorForce(); + + /// Check if environment variable FORCE_COLOR is set. + bool EnvHasForceColor(); + private: /// Whether we can do fancy terminal control codes. bool smart_terminal_;