| author | |
| committer | |
| log | e2b954c2738c683a85b864eb33530f0e3dbbc480 |
| tree | 47d90ba0a28ee7ce3e4e2f12b325567c31f307e2 |
| parent | 7bdeda82aea82b9a71378462f51a708b8ad88161 |
7 files changed, 42 insertions(+), 20 deletions(-)
lib/std/debug.zig+4-4| ... | ... | @@ -90,11 +90,11 @@ pub fn getSelfDebugInfo() !*DebugInfo { |
| 90 | 90 | } |
| 91 | 91 | |
| 92 | 92 | pub fn detectTTYConfig() TTY.Config { |
| 93 | var bytes: [128]u8 = undefined; | |
| 94 | const allocator = &std.heap.FixedBufferAllocator.init(bytes[0..]).allocator; | |
| 95 | if (process.getEnvVarOwned(allocator, "ZIG_DEBUG_COLOR")) |_| { | |
| 93 | if (process.hasEnvVarConstant("ZIG_DEBUG_COLOR")) { | |
| 96 | 94 | return .escape_codes; |
| 97 | } else |_| { | |
| 95 | } else if (process.hasEnvVarConstant("NO_COLOR")) { | |
| 96 | return .no_color; | |
| 97 | } else { | |
| 98 | 98 | const stderr_file = io.getStdErr(); |
| 99 | 99 | if (stderr_file.supportsAnsiEscapeCodes()) { |
| 100 | 100 | return .escape_codes; |
lib/std/process.zig+20| ... | ... | @@ -179,6 +179,26 @@ pub fn getEnvVarOwned(allocator: *mem.Allocator, key: []const u8) GetEnvVarOwned |
| 179 | 179 | } |
| 180 | 180 | } |
| 181 | 181 | |
| 182 | pub fn hasEnvVarConstant(comptime key: []const u8) bool { | |
| 183 | if (builtin.os.tag == .windows) { | |
| 184 | const key_w = comptime std.unicode.utf8ToUtf16LeStringLiteral(key); | |
| 185 | return std.os.getenvW(key_w) != null; | |
| 186 | } else { | |
| 187 | return os.getenv(key) != null; | |
| 188 | } | |
| 189 | } | |
| 190 | ||
| 191 | pub fn hasEnvVar(allocator: *Allocator, key: []const u8) error{OutOfMemory}!bool { | |
| 192 | if (builtin.os.tag == .windows) { | |
| 193 | var stack_alloc = std.heap.stackFallback(256 * @sizeOf(u16), allocator); | |
| 194 | const key_w = try std.unicode.utf8ToUtf16LeWithNull(&stack_alloc.allocator, key); | |
| 195 | defer stack_alloc.allocator.free(key_w); | |
| 196 | return std.os.getenvW(key_w) != null; | |
| 197 | } else { | |
| 198 | return os.getenv(key) != null; | |
| 199 | } | |
| 200 | } | |
| 201 | ||
| 182 | 202 | test "os.getEnvVarOwned" { |
| 183 | 203 | var ga = std.testing.allocator; |
| 184 | 204 | try testing.expectError(error.EnvironmentVariableNotFound, getEnvVarOwned(ga, "BADENV")); |
lib/std/special/build_runner.zig+5| ... | ... | @@ -63,6 +63,11 @@ pub fn main() !void { |
| 63 | 63 | var install_prefix: ?[]const u8 = null; |
| 64 | 64 | var dir_list = Builder.DirList{}; |
| 65 | 65 | |
| 66 | // before arg parsing, check for the NO_COLOR environment variable | |
| 67 | // if it exists, default the color setting to .off | |
| 68 | // explicit --color arguments will still override this setting. | |
| 69 | builder.color = if (std.process.hasEnvVarConstant("NO_COLOR")) .off else .auto; | |
| 70 | ||
| 66 | 71 | while (nextArg(args, &arg_idx)) |arg| { |
| 67 | 72 | if (mem.startsWith(u8, arg, "-D")) { |
| 68 | 73 | const option_contents = arg[2..]; |
src/main.zig+7-11| ... | ... | @@ -503,15 +503,6 @@ const Emit = union(enum) { |
| 503 | 503 | } |
| 504 | 504 | }; |
| 505 | 505 | |
| 506 | fn optionalBoolEnvVar(arena: *Allocator, name: []const u8) !bool { | |
| 507 | if (std.process.getEnvVarOwned(arena, name)) |_| { | |
| 508 | return true; | |
| 509 | } else |err| switch (err) { | |
| 510 | error.EnvironmentVariableNotFound => return false, | |
| 511 | else => |e| return e, | |
| 512 | } | |
| 513 | } | |
| 514 | ||
| 515 | 506 | fn optionalStringEnvVar(arena: *Allocator, name: []const u8) !?[]const u8 { |
| 516 | 507 | if (std.process.getEnvVarOwned(arena, name)) |value| { |
| 517 | 508 | return value; |
| ... | ... | @@ -548,8 +539,8 @@ fn buildOutputType( |
| 548 | 539 | var single_threaded = false; |
| 549 | 540 | var function_sections = false; |
| 550 | 541 | var watch = false; |
| 551 | var verbose_link = try optionalBoolEnvVar(arena, "ZIG_VERBOSE_LINK"); | |
| 552 | var verbose_cc = try optionalBoolEnvVar(arena, "ZIG_VERBOSE_CC"); | |
| 542 | var verbose_link = std.process.hasEnvVarConstant("ZIG_VERBOSE_LINK"); | |
| 543 | var verbose_cc = std.process.hasEnvVarConstant("ZIG_VERBOSE_CC"); | |
| 553 | 544 | var verbose_air = false; |
| 554 | 545 | var verbose_llvm_ir = false; |
| 555 | 546 | var verbose_cimport = false; |
| ... | ... | @@ -670,6 +661,11 @@ fn buildOutputType( |
| 670 | 661 | defer freePkgTree(gpa, &pkg_tree_root, false); |
| 671 | 662 | var cur_pkg: *Package = &pkg_tree_root; |
| 672 | 663 | |
| 664 | // before arg parsing, check for the NO_COLOR environment variable | |
| 665 | // if it exists, default the color setting to .off | |
| 666 | // explicit --color arguments will still override this setting. | |
| 667 | color = if (std.process.hasEnvVarConstant("NO_COLOR")) .off else .auto; | |
| 668 | ||
| 673 | 669 | switch (arg_mode) { |
| 674 | 670 | .build, .translate_c, .zig_test, .run => { |
| 675 | 671 | var optimize_mode_string: ?[]const u8 = null; |
src/stage1/errmsg.cpp+2-2| ... | ... | @@ -16,8 +16,8 @@ enum ErrType { |
| 16 | 16 | }; |
| 17 | 17 | |
| 18 | 18 | static void print_err_msg_type(ErrorMsg *err, ErrColor color, ErrType err_type) { |
| 19 | bool is_tty = os_stderr_tty(); | |
| 20 | bool use_colors = color == ErrColorOn || (color == ErrColorAuto && is_tty); | |
| 19 | bool supports_color = os_stderr_supports_color(); | |
| 20 | bool use_colors = color == ErrColorOn || (color == ErrColorAuto && supports_color); | |
| 21 | 21 | |
| 22 | 22 | // Show the error location, if available |
| 23 | 23 | if (err->path != nullptr) { |
src/stage1/os.cpp+3-2| ... | ... | @@ -834,13 +834,14 @@ static bool is_stderr_cyg_pty(void) { |
| 834 | 834 | } |
| 835 | 835 | #endif |
| 836 | 836 | |
| 837 | bool os_stderr_tty(void) { | |
| 837 | bool os_stderr_supports_color(void) { | |
| 838 | if (getenv("NO_COLOR") != NULL) return false; | |
| 838 | 839 | #if defined(ZIG_OS_WINDOWS) |
| 839 | 840 | return _isatty(_fileno(stderr)) != 0 || is_stderr_cyg_pty(); |
| 840 | 841 | #elif defined(ZIG_OS_POSIX) |
| 841 | 842 | return isatty(STDERR_FILENO) != 0; |
| 842 | 843 | #else |
| 843 | #error "missing os_stderr_tty implementation" | |
| 844 | #error "missing os_stderr_supports_color implementation" | |
| 844 | 845 | #endif |
| 845 | 846 | } |
| 846 | 847 |
src/stage1/os.hpp+1-1| ... | ... | @@ -99,7 +99,7 @@ Error ATTRIBUTE_MUST_USE os_fetch_file_path(Buf *full_path, Buf *out_contents); |
| 99 | 99 | |
| 100 | 100 | Error ATTRIBUTE_MUST_USE os_get_cwd(Buf *out_cwd); |
| 101 | 101 | |
| 102 | bool os_stderr_tty(void); | |
| 102 | bool os_stderr_supports_color(void); | |
| 103 | 103 | void os_stderr_set_color(TermColor color); |
| 104 | 104 | |
| 105 | 105 | Error os_rename(Buf *src_path, Buf *dest_path); |