Skip to content

Commit 131983e

Browse files
committed
Define a should_color library
Honors NO_COLOR, FORCE_COLOR, and defaults to whether the terminal is interactive. Bug: b/533162913
1 parent 4bf9a98 commit 131983e

3 files changed

Lines changed: 77 additions & 0 deletions

File tree

base/cvd/cuttlefish/ansi_codes/BUILD.bazel

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,15 @@ cf_cc_library(
1313
],
1414
)
1515

16+
cf_cc_library(
17+
name = "should_color",
18+
srcs = ["should_color.cc"],
19+
hdrs = ["should_color.h"],
20+
deps = [
21+
"//cuttlefish/common/libs/utils:environment",
22+
],
23+
)
24+
1625
cf_cc_library(
1726
name = "terminal_colors",
1827
srcs = ["terminal_colors.cc"],
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Copyright (C) 2026 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#include "cuttlefish/ansi_codes/should_color.h"
18+
19+
#include <unistd.h>
20+
21+
#include "cuttlefish/common/libs/utils/environment.h"
22+
23+
namespace cuttlefish {
24+
namespace {
25+
26+
bool ColorDefault(int fd) {
27+
// https://no-color.org/
28+
if (!StringFromEnv("NO_COLOR").value_or("").empty()) {
29+
return false;
30+
}
31+
// https://force-color.org/
32+
if (!StringFromEnv("FORCE_COLOR").value_or("").empty()) {
33+
return true;
34+
}
35+
return isatty(fd);
36+
}
37+
38+
} // namespace
39+
40+
bool ShouldColorStdout() { return ColorDefault(STDOUT_FILENO); }
41+
42+
bool ShouldColorStderr() { return ColorDefault(STDERR_FILENO); }
43+
44+
} // namespace cuttlefish
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* Copyright (C) 2026 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#pragma once
18+
19+
namespace cuttlefish {
20+
21+
bool ShouldColorStdout();
22+
bool ShouldColorStderr();
23+
24+
} // namespace cuttlefish

0 commit comments

Comments
 (0)