Skip to content

Commit 9c6e528

Browse files
authored
Avoid rounding 99.995% matches up to 100.00% in the function diff view (#427)
* Avoid rounding 99.995% matches up to 100% in the function diff view * Fix objdiff-wasm build
1 parent e5c0c43 commit 9c6e528

3 files changed

Lines changed: 14 additions & 4 deletions

File tree

‎objdiff-cli/src/views/function_diff.rs‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use objdiff_core::{
1010
display::{DiffText, DiffTextColor, HighlightKind, display_row},
1111
},
1212
obj::Object,
13+
util::format_percent,
1314
};
1415
use ratatui::{
1516
Frame,
@@ -88,7 +89,7 @@ impl UiView for FunctionDiffUi {
8889
.and_then(|(_, _, d)| d.match_percent)
8990
{
9091
line_r.spans.push(Span::styled(
91-
format!("{percent:.2}% "),
92+
format!("{} ", format_percent(percent)),
9293
Style::new().fg(match_percent_color(percent)),
9394
));
9495
}

‎objdiff-core/src/util.rs‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use alloc::{format, vec::Vec};
1+
use alloc::{format, string::String, vec::Vec};
22
use core::fmt;
33

44
use anyhow::{Result, ensure};
@@ -26,6 +26,15 @@ impl<N: PrimInt> fmt::UpperHex for ReallySigned<N> {
2626
}
2727
}
2828

29+
/// Formats a progress percentage as a string, and prevents partial matches from being rounded to 0.00% or 100.00%.
30+
pub fn format_percent(value: f32) -> String {
31+
let mut value = value;
32+
if value > 0.0 && value < 100.0 {
33+
value = value.clamp(0.01, 99.99);
34+
}
35+
format!("{value:.2}%")
36+
}
37+
2938
pub fn read_u32(obj_file: &object::File, reader: &mut &[u8]) -> Result<u32> {
3039
ensure!(reader.len() >= 4, "Not enough bytes to read u32");
3140
let value = reader[..4].try_into()?;

‎objdiff-gui/src/views/diff.rs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use objdiff_core::{
1212
},
1313
},
1414
obj::{InstructionArgValue, Object, Symbol},
15-
util::ReallySigned,
15+
util::{ReallySigned, format_percent},
1616
};
1717
use time::format_description;
1818

@@ -535,7 +535,7 @@ pub fn diff_view_ui(
535535
let mut needs_separator = false;
536536
if let Some(match_percent) = symbol_diff.match_percent {
537537
let response = ui.label(
538-
RichText::new(format!("{match_percent:.2}%"))
538+
RichText::new(format_percent(match_percent))
539539
.font(appearance.code_font.clone())
540540
.color(match_color_for_symbol(match_percent, appearance)),
541541
);

0 commit comments

Comments
 (0)