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
3 changes: 2 additions & 1 deletion objdiff-cli/src/views/function_diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use objdiff_core::{
display::{DiffText, DiffTextColor, HighlightKind, display_row},
},
obj::Object,
util::format_percent,
};
use ratatui::{
Frame,
Expand Down Expand Up @@ -88,7 +89,7 @@ impl UiView for FunctionDiffUi {
.and_then(|(_, _, d)| d.match_percent)
{
line_r.spans.push(Span::styled(
format!("{percent:.2}% "),
format!("{} ", format_percent(percent)),
Style::new().fg(match_percent_color(percent)),
));
}
Expand Down
11 changes: 10 additions & 1 deletion objdiff-core/src/util.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use alloc::{format, vec::Vec};
use alloc::{format, string::String, vec::Vec};
use core::fmt;

use anyhow::{Result, ensure};
Expand Down Expand Up @@ -26,6 +26,15 @@ impl<N: PrimInt> fmt::UpperHex for ReallySigned<N> {
}
}

/// Formats a progress percentage as a string, and prevents partial matches from being rounded to 0.00% or 100.00%.
pub fn format_percent(value: f32) -> String {
let mut value = value;
if value > 0.0 && value < 100.0 {
value = value.clamp(0.01, 99.99);
}
format!("{value:.2}%")
}

pub fn read_u32(obj_file: &object::File, reader: &mut &[u8]) -> Result<u32> {
ensure!(reader.len() >= 4, "Not enough bytes to read u32");
let value = reader[..4].try_into()?;
Expand Down
4 changes: 2 additions & 2 deletions objdiff-gui/src/views/diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use objdiff_core::{
},
},
obj::{InstructionArgValue, Object, Symbol},
util::ReallySigned,
util::{ReallySigned, format_percent},
};
use time::format_description;

Expand Down Expand Up @@ -535,7 +535,7 @@ pub fn diff_view_ui(
let mut needs_separator = false;
if let Some(match_percent) = symbol_diff.match_percent {
let response = ui.label(
RichText::new(format!("{match_percent:.2}%"))
RichText::new(format_percent(match_percent))
.font(appearance.code_font.clone())
.color(match_color_for_symbol(match_percent, appearance)),
);
Expand Down
Loading