Skip to content

Commit

Permalink
Prefer write_str over write! (2)
Browse files Browse the repository at this point in the history
  • Loading branch information
taiki-e committed Feb 16, 2024
1 parent c8d5f28 commit d61918d
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 21 deletions.
35 changes: 17 additions & 18 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -881,17 +881,16 @@ impl fmt::Display for Help {
let size = term_size - indent;
for s in desc.split(' ') {
if written + s.len() + 1 >= size {
writeln!(f)?;
f.write_str("\n")?;
(0..indent).try_for_each(|_| f.write_str(" "))?;
f.write_str(s)?;
written = s.len();
written = 0;
} else if written == 0 {
f.write_str(s)?;
written += s.len();
} else {
write!(f, " {s}")?;
written += s.len() + 1;
f.write_str(" ")?;
written += 1;
}
f.write_str(s)?;
written += s.len();
}
Ok(())
}
Expand All @@ -912,16 +911,16 @@ OPTIONS:",
for &(short, long, value_name, desc, additional) in HELP {
write!(f, " {short:2}{} ", if short.is_empty() { " " } else { "," })?;
if self.long {
if value_name.is_empty() {
writeln!(f, "{long}")?;
} else {
writeln!(f, "{long} {value_name}")?;
f.write_str(long)?;
if !value_name.is_empty() {
write!(f, " {value_name}")?;
}
f.write_str("\n")?;
write(f, 12, true, self.term_size, desc)?;
writeln!(f, ".\n")?;
f.write_str(".\n\n")?;
for desc in additional {
write(f, 12, true, self.term_size, desc)?;
writeln!(f, "\n")?;
f.write_str("\n\n")?;
}
} else {
if value_name.is_empty() {
Expand All @@ -931,21 +930,21 @@ OPTIONS:",
write!(f, "{long:32} ")?;
}
write(f, 41, false, self.term_size, desc)?;
writeln!(f)?;
f.write_str("\n")?;
}
}
if !self.long {
writeln!(f)?;
f.write_str("\n")?;
}

writeln!(
f,
f.write_str(
"\
Some common cargo commands are (see all commands with --list):
build Compile the current package
check Analyze the current package and report errors, but don't build object files
run Run a binary or example of the local package
test Run the tests"
test Run the tests
",
)
}
}
Expand Down
3 changes: 1 addition & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -503,8 +503,7 @@ struct KeepGoing {

impl fmt::Display for KeepGoing {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
writeln!(f, "failed to run {} commands", self.count)?;
writeln!(f)?;
writeln!(f, "failed to run {} commands\n", self.count)?;
writeln!(f, "failed commands:")?;
for (pkg, commands) in &self.failed_commands {
writeln!(f, " {pkg}:")?;
Expand Down
2 changes: 1 addition & 1 deletion src/version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ impl fmt::Display for VersionRange {
if let MaybeVersion::Version(start) = self.start_inclusive {
write!(f, "{start}")?;
}
write!(f, "..")?;
f.write_str("..")?;
if let MaybeVersion::Version(end) = self.end_inclusive {
write!(f, "={end}")?;
}
Expand Down

0 comments on commit d61918d

Please sign in to comment.