Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,7 @@ log = { version = "0.4.29", features = ["std"] }
owo-colors = "4.2.3"
serde_json = "1.0.132"
toml_edit = "0.23"
supports-color = "3.0.2"
strip-ansi-escapes = "0.2.1"

[dev-dependencies]
54 changes: 38 additions & 16 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,39 @@ use owo_colors::{OwoColorize, Style};
use std::os::unix::fs::PermissionsExt;
use std::sync::mpsc;
use std::{
borrow::Cow,
ffi::OsStr,
fs,
io::{self, BufRead, BufReader, Write},
path::{Path, PathBuf},
process::{Command, Stdio},
time::{Duration, Instant},
};
use supports_color::{self, Stream as ColorStream};
use toml_edit::{Array, DocumentMut, Item, Table, Value, value};

mod readme;

fn terminal_supports_color(stream: ColorStream) -> bool {
supports_color::on_cached(stream).is_some()
}

fn maybe_strip_bytes<'a>(data: &'a [u8], stream: ColorStream) -> Cow<'a, [u8]> {
if terminal_supports_color(stream) {
Cow::Borrowed(data)
} else {
Cow::Owned(strip_ansi_escapes::strip(data))
}
}

fn maybe_strip_str<'a>(line: &'a str, stream: ColorStream) -> Cow<'a, str> {
if terminal_supports_color(stream) {
Cow::Borrowed(line)
} else {
Cow::Owned(strip_ansi_escapes::strip_str(line))
}
}

fn apply_color_env(cmd: &mut Command) {
cmd.env("FORCE_COLOR", "1");
cmd.env("CARGO_TERM_COLOR", "always");
Expand Down Expand Up @@ -766,12 +788,14 @@ fn enqueue_rustfmt_jobs(sender: std::sync::mpsc::Sender<Job>, staged_files: &Sta
);

if !output.status.success() {
let stderr_clean = maybe_strip_bytes(&output.stderr, ColorStream::Stderr);
let stdout_clean = maybe_strip_bytes(&output.stdout, ColorStream::Stdout);
error!(
"{} {}: rustfmt failed\n{}\n{}",
"❌".red(),
path.display().to_string().blue(),
String::from_utf8_lossy(&output.stderr).dimmed(),
String::from_utf8_lossy(&output.stdout).dimmed()
String::from_utf8_lossy(&stderr_clean).dimmed(),
String::from_utf8_lossy(&stdout_clean).dimmed()
);
continue;
}
Expand Down Expand Up @@ -1051,15 +1075,13 @@ fn install_cargo_shear() {
exit_with_command_failure(&binstall_command, &[], binstall_output, None);
}

fn print_stream(label: &str, data: &[u8]) {
fn print_stream(label: &str, data: &[u8], stream: ColorStream) {
if data.is_empty() {
println!(" {}: <empty>", label);
} else {
println!(
" {}:\n{}",
label,
String::from_utf8_lossy(data).trim_end()
);
let cleaned = maybe_strip_bytes(data, stream);
let text = String::from_utf8_lossy(&cleaned);
println!(" {}:\n{}", label, text.trim_end());
}
}

Expand All @@ -1083,8 +1105,8 @@ fn exit_with_command_failure(
Some(code) => println!(" exit code: {}", code),
None => println!(" exit code: terminated by signal"),
}
print_stream("stdout", &output.stdout);
print_stream("stderr", &output.stderr);
print_stream("stdout", &output.stdout, ColorStream::Stdout);
print_stream("stderr", &output.stderr, ColorStream::Stderr);
if let Some(action) = hint {
action();
}
Expand Down Expand Up @@ -1179,13 +1201,13 @@ fn run_command_with_streaming(
// Process has finished, collect remaining output
while let Ok(line) = stdout_rx.try_recv() {
if streaming {
println!("{}", line);
println!("{}", maybe_strip_str(&line, ColorStream::Stdout));
}
stdout_buffer.push(line);
}
while let Ok(line) = stderr_rx.try_recv() {
if streaming {
eprintln!("{}", line);
eprintln!("{}", maybe_strip_str(&line, ColorStream::Stderr));
}
stderr_buffer.push(line);
}
Expand Down Expand Up @@ -1223,25 +1245,25 @@ fn run_command_with_streaming(
);
// Flush buffered output
for line in &stdout_buffer {
println!("{}", line);
println!("{}", maybe_strip_str(line, ColorStream::Stdout));
}
for line in &stderr_buffer {
eprintln!("{}", line);
eprintln!("{}", maybe_strip_str(line, ColorStream::Stderr));
}
}

// Collect new output
let mut got_output = false;
while let Ok(line) = stdout_rx.try_recv() {
if streaming {
println!("{}", line);
println!("{}", maybe_strip_str(&line, ColorStream::Stdout));
}
stdout_buffer.push(line);
got_output = true;
}
while let Ok(line) = stderr_rx.try_recv() {
if streaming {
eprintln!("{}", line);
eprintln!("{}", maybe_strip_str(&line, ColorStream::Stderr));
}
stderr_buffer.push(line);
got_output = true;
Expand Down
Loading