Skip to content

Commit

Permalink
Fix pretty printing long units with long prefixes
Browse files Browse the repository at this point in the history
Before this, we had:

```
>>> @metric_prefixes
unit points

  @metric_prefixes
  unit points

>>> megapoints

  megapoints

    = 1 Mpoints
```

Now we have:

```
>>> @metric_prefixes
unit points

  @metric_prefixes
  unit points

>>> megapoints

  megapoints

    = 1 megapoints
```

I've also added a roundtrip test.
  • Loading branch information
do-you-dare committed Feb 4, 2024
1 parent aaa6a36 commit 9a5fcce
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
4 changes: 4 additions & 0 deletions numbat/src/typed_ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -616,6 +616,9 @@ mod tests {
@aliases(in: short)
unit inch = 0.0254 m
@metric_prefixes
unit points
let a = 1
let b = 1
let c = 1
Expand Down Expand Up @@ -731,6 +734,7 @@ mod tests {
roundtrip_check("2^3!");
roundtrip_check("-3!");
roundtrip_check("(-3)!");
roundtrip_check("megapoints");
}

#[test]
Expand Down
9 changes: 7 additions & 2 deletions numbat/src/unit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,13 +193,18 @@ impl Power for UnitFactor {
}
}

// TODO: Use the AcceptsPrefix here to format the string correctly
impl Display for UnitFactor {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let prefix = if self.unit_id.canonical_name.accepts_prefix.short {
self.prefix.as_string_short()
} else {
self.prefix.as_string_long()
};

write!(
f,
"{}{}{}",
self.prefix.as_string_short(),
prefix,
self.unit_id.canonical_name.name,
pretty_exponent(&self.exponent)
)
Expand Down

0 comments on commit 9a5fcce

Please sign in to comment.