Skip to content

Commit

Permalink
Handle terminal detection
Browse files Browse the repository at this point in the history
  • Loading branch information
theory committed Feb 24, 2025
1 parent 7f1f952 commit bb443bc
Showing 1 changed file with 22 additions and 5 deletions.
27 changes: 22 additions & 5 deletions src/exec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,21 +106,38 @@ where
});

// Read the lines from the spawned threads and format them as appropriate.
if self.color {
// color_print does not omit the colors if the buffer is not
// attached to a terminal.
if !self.color || (!self.out.is_terminal() && !self.err.is_terminal()) {
// No color wanted or allowed. Just write the unmodified output.
for output in rx {
if output.is_err {
writeln!(self.err, "{}", output.line)?;
} else {
writeln!(self.out, "{}", output.line)?;
}
}
} else if self.out.is_terminal() && self.err.is_terminal() {
// Write colored output to both outputs.
for output in rx {
if output.is_err {
cwriteln!(self.err, "<red>{}</red>", output.line)?;
} else {
cwriteln!(self.out, "<dim><244>{}</244></dim>", output.line)?;
}
}
} else {
// No colors wanted, just write the unmodified output.
} else if self.out.is_terminal() {
// Write colored output only to self.out.
for output in rx {
if output.is_err {
writeln!(self.err, "{}", output.line)?;
} else {
cwriteln!(self.out, "<dim><244>{}</244></dim>", output.line)?;
}
}
} else {
// Write colored output only to self.err.
for output in rx {
if output.is_err {
cwriteln!(self.err, "<red>{}</red>", output.line)?;
} else {
writeln!(self.out, "{}", output.line)?;
}
Expand Down

0 comments on commit bb443bc

Please sign in to comment.