Skip to content

Commit

Permalink
Make Debug format of Vec2/Pos2/Rot2 respect user precision (#4671)
Browse files Browse the repository at this point in the history
* closes #4665

pretty self explanatory, i opted for 3 instead of 2 since i think that's
what display does
  • Loading branch information
murl-digital authored Jun 18, 2024
1 parent 44d7aab commit dd52291
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 8 deletions.
6 changes: 5 additions & 1 deletion crates/emath/src/pos2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,11 @@ impl Div<f32> for Pos2 {

impl fmt::Debug for Pos2 {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "[{:.1} {:.1}]", self.x, self.y)
if let Some(precision) = f.precision() {
write!(f, "[{1:.0$} {2:.0$}]", precision, self.x, self.y)
} else {
write!(f, "[{:.1} {:.1}]", self.x, self.y)
}
}
}

Expand Down
22 changes: 16 additions & 6 deletions crates/emath/src/rot2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,22 @@ impl Rot2 {

impl std::fmt::Debug for Rot2 {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"Rot2 {{ angle: {:.1}°, length: {} }}",
self.angle().to_degrees(),
self.length()
)
if let Some(precision) = f.precision() {
write!(
f,
"Rot2 {{ angle: {:.2$}°, length: {} }}",
self.angle().to_degrees(),
self.length(),
precision
)
} else {
write!(
f,
"Rot2 {{ angle: {:.1}°, length: {} }}",
self.angle().to_degrees(),
self.length(),
)
}
}
}

Expand Down
6 changes: 5 additions & 1 deletion crates/emath/src/vec2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,11 @@ impl Div<f32> for Vec2 {

impl fmt::Debug for Vec2 {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "[{:.1} {:.1}]", self.x, self.y)
if let Some(precision) = f.precision() {
write!(f, "[{1:.0$} {2:.0$}]", precision, self.x, self.y)
} else {
write!(f, "[{:.1} {:.1}]", self.x, self.y)
}
}
}

Expand Down

0 comments on commit dd52291

Please sign in to comment.