Remove ansi_color, move its implementation to line_printer

This commit is contained in:
Brayden Krus
2026-04-30 10:04:57 -04:00
parent 61b029a1f3
commit d4f11df90f
7 changed files with 25 additions and 146 deletions

View File

@@ -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

View File

@@ -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',

View File

@@ -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 <stdlib.h>
#include <string>
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";
}

View File

@@ -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

View File

@@ -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 <windows.h>
#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
}

View File

@@ -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";
}

View File

@@ -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_;