Skip to content

Commit

Permalink
fix issues when logging to file
Browse files Browse the repository at this point in the history
  • Loading branch information
gvilums committed Apr 9, 2024
1 parent 78814e6 commit b8abffd
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions src/cli/filter_run.zig
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,12 @@ pub const ProcessHandle = struct {

fn handleChunkBasic(this: *This, chunk: []const u8) !void {
var content = chunk;
this.state.draw_buf.clearRetainingCapacity();
if (this.buffer.items.len > 0) {
if (std.mem.indexOfScalar(u8, content, '\n')) |i| {
try this.buffer.appendSlice(content[0 .. i + 1]);
content = content[i + 1 ..];
try std.io.getStdOut().writer().print("{s}: {s}\n", .{ this.config.package_name, this.buffer.items });
try this.state.draw_buf.writer().print("{s}: {s}", .{ this.config.package_name, this.buffer.items });
this.buffer.clearRetainingCapacity();
} else {
try this.buffer.appendSlice(content);
Expand All @@ -78,12 +79,13 @@ pub const ProcessHandle = struct {
}
while (std.mem.indexOfScalar(u8, content, '\n')) |i| {
const line = content[0 .. i + 1];
try std.io.getStdOut().writer().print("{s}: {s}\n", .{ this.config.package_name, line });
try this.state.draw_buf.writer().print("{s}: {s}", .{ this.config.package_name, line });
content = content[i + 1 ..];
}
if (content.len > 0) {
try this.buffer.appendSlice(content);
}
try std.io.getStdOut().writeAll(this.state.draw_buf.items);
}

pub fn onReaderDone(this: *This) void {
Expand Down Expand Up @@ -233,10 +235,14 @@ const State = struct {

pub fn flush(this: *This) !void {
if (this.pretty_output) return;
this.draw_buf.clearRetainingCapacity();
for (this.handles) |*handle| {
try std.io.getStdOut().writer().print("{s}: {s}\n", .{ handle.config.package_name, handle.buffer.items });
handle.buffer.clearRetainingCapacity();
if (handle.buffer.items.len > 0) {
try this.draw_buf.writer().print("{s}: {s}\n", .{ handle.config.package_name, handle.buffer.items });
handle.buffer.clearRetainingCapacity();
}
}
try std.io.getStdOut().writeAll(this.draw_buf.items);
}

pub fn abort(this: *This) void {
Expand Down Expand Up @@ -290,6 +296,11 @@ const AbortHandler = struct {
}
};

fn windowsIsTerminal() bool {
const res = bun.windows.GetFileType(bun.STDOUT_FD.cast());
return res == bun.windows.FILE_TYPE_CHAR;
}

pub fn runScriptsWithFilter(ctx: Command.Context) !noreturn {
const script_name = if (ctx.positionals.len > 1) ctx.positionals[1] else brk: {
Output.prettyErrorln("<r><red>error<r>: No script name provided", .{});
Expand Down Expand Up @@ -384,7 +395,7 @@ pub fn runScriptsWithFilter(ctx: Command.Context) !noreturn {
.handles = try ctx.allocator.alloc(ProcessHandle, scripts.items.len),
.event_loop = event_loop,
.live_processes = scripts.items.len,
.pretty_output = Output.enable_ansi_colors_stdout,
.pretty_output = if (Environment.isWindows) windowsIsTerminal() else Output.enable_ansi_colors_stdout,
};

for (scripts.items, 0..) |*script, i| {
Expand Down

0 comments on commit b8abffd

Please sign in to comment.