aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Alex Lam S.L <alexlamsl@gmail.com> 2023-02-26 22:14:51 +0200
committerGravatar GitHub <noreply@github.com> 2023-02-26 12:14:51 -0800
commit16898a23e6b48d4ff0e4cad7a660008c78e07a01 (patch)
tree6fb44e416307f3c17b66cffdde755f2f7936d5f5
parenta55ca4879732b74fa21b15e479f5ffaed7b0b88e (diff)
downloadbun-16898a23e6b48d4ff0e4cad7a660008c78e07a01.tar.gz
bun-16898a23e6b48d4ff0e4cad7a660008c78e07a01.tar.zst
bun-16898a23e6b48d4ff0e4cad7a660008c78e07a01.zip
fix ANSI escape codes piped from `stdout` to file (#2202)
-rw-r--r--src/cli/install_completions_command.zig2
-rw-r--r--src/output.zig55
2 files changed, 13 insertions, 44 deletions
diff --git a/src/cli/install_completions_command.zig b/src/cli/install_completions_command.zig
index 27e921526..9d1d0568a 100644
--- a/src/cli/install_completions_command.zig
+++ b/src/cli/install_completions_command.zig
@@ -340,7 +340,7 @@ pub const InstallCompletionsCommand = struct {
);
}
- Output.errorLn(
+ Output.printErrorln(
"Please either pipe it:\n bun completions > /to/a/file\n\n Or pass a directory:\n\n bun completions /my/completions/dir\n",
.{},
);
diff --git a/src/output.zig b/src/output.zig
index cb57343ad..72f8e57ed 100644
--- a/src/output.zig
+++ b/src/output.zig
@@ -131,8 +131,8 @@ pub const Source = struct {
var is_color_terminal: ?bool = null;
if (_source.stream.isTty()) {
stdout_descriptor_type = OutputStreamDescriptor.terminal;
- is_color_terminal = is_color_terminal orelse isColorTerminal();
- enable_ansi_colors_stdout = is_color_terminal.?;
+ enable_ansi_colors_stdout = isColorTerminal();
+ is_color_terminal = enable_ansi_colors_stdout;
} else if (isForceColor()) |val| {
enable_ansi_colors_stdout = val;
} else {
@@ -141,8 +141,7 @@ pub const Source = struct {
if (_source.error_stream.isTty()) {
stderr_descriptor_type = OutputStreamDescriptor.terminal;
- is_color_terminal = is_color_terminal orelse isColorTerminal();
- enable_ansi_colors_stderr = is_color_terminal.?;
+ enable_ansi_colors_stderr = is_color_terminal orelse isColorTerminal();
} else if (isForceColor()) |val| {
enable_ansi_colors_stderr = val;
} else {
@@ -328,11 +327,7 @@ pub inline fn debug(comptime fmt: string, args: anytype) void {
pub fn _debug(comptime fmt: string, args: anytype) void {
std.debug.assert(source_set);
- if (fmt.len == 0 or fmt[fmt.len - 1] != '\n') {
- return print(fmt ++ "\n", args);
- }
-
- return print(fmt, args);
+ println(fmt, args);
}
pub fn print(comptime fmt: string, args: anytype) void {
@@ -382,6 +377,10 @@ pub fn scoped(comptime tag: @Type(.EnumLiteral), comptime disabled: bool) _log_f
/// To enable all logs, set the environment variable
/// BUN_DEBUG_ALL=1
pub fn log(comptime fmt: string, args: anytype) void {
+ if (fmt.len == 0 or fmt[fmt.len - 1] != '\n') {
+ return log(fmt ++ "\n", args);
+ }
+
if (!evaluated_disable) {
evaluated_disable = true;
if (bun.getenvZ("BUN_DEBUG_ALL") != null or
@@ -404,10 +403,6 @@ pub fn scoped(comptime tag: @Type(.EnumLiteral), comptime disabled: bool) _log_f
out_set = true;
}
- if (fmt.len == 0 or fmt[fmt.len - 1] != '\n') {
- return log(fmt ++ "\n", args);
- }
-
if (Output.enable_ansi_colors_stderr) {
out.print(comptime prettyFmt("<r><d>[" ++ @tagName(tag) ++ "]<r> " ++ fmt, true), args) catch unreachable;
buffered_writer.flush() catch unreachable;
@@ -530,7 +525,7 @@ pub fn prettyWithPrinter(comptime fmt: string, args: anytype, comptime printer:
if (level == .Error) return;
}
- if (if (comptime l == Level.stdout) enable_ansi_colors_stdout else enable_ansi_colors_stderr) {
+ if (if (comptime l == .stdout) enable_ansi_colors_stdout else enable_ansi_colors_stderr) {
printer(comptime prettyFmt(fmt, true), args);
} else {
printer(comptime prettyFmt(fmt, false), args);
@@ -552,11 +547,7 @@ pub fn pretty(comptime fmt: string, args: anytype) void {
/// Like Output.println, except it will automatically strip ansi color codes if
/// the terminal doesn't support them.
pub fn prettyln(comptime fmt: string, args: anytype) void {
- if (enable_ansi_colors) {
- println(comptime prettyFmt(fmt, true), args);
- } else {
- println(comptime prettyFmt(fmt, false), args);
- }
+ prettyWithPrinter(fmt, args, println, .stdout);
}
pub fn printErrorln(comptime fmt: string, args: anytype) void {
@@ -574,21 +565,7 @@ pub fn prettyError(comptime fmt: string, args: anytype) void {
/// Print to stderr with ansi color codes automatically stripped out if the
/// terminal doesn't support them. Text is buffered
pub fn prettyErrorln(comptime fmt: string, args: anytype) void {
- if (fmt.len == 0 or fmt[fmt.len - 1] != '\n') {
- return prettyWithPrinter(
- fmt ++ "\n",
- args,
- printError,
- .Error,
- );
- }
-
- return prettyWithPrinter(
- fmt,
- args,
- printError,
- .Error,
- );
+ prettyWithPrinter(fmt, args, printErrorln, .Error);
}
pub const Level = enum(u8) {
@@ -604,15 +581,7 @@ pub fn prettyWarn(comptime fmt: string, args: anytype) void {
}
pub fn prettyWarnln(comptime fmt: string, args: anytype) void {
- if (fmt.len == 0 or fmt[fmt.len - 1] != '\n') {
- return prettyWithPrinter(fmt ++ "\n", args, printError, .Warn);
- }
-
- return prettyWithPrinter(fmt, args, printError, .Warn);
-}
-
-pub fn errorLn(comptime fmt: string, args: anytype) void {
- return printErrorln(fmt, args);
+ prettyWithPrinter(fmt, args, printErrorln, .Warn);
}
pub fn printError(comptime fmt: string, args: anytype) void {