diff --git a/objdiff-cli/src/views/function_diff.rs b/objdiff-cli/src/views/function_diff.rs index e7cab796..9d330d68 100644 --- a/objdiff-cli/src/views/function_diff.rs +++ b/objdiff-cli/src/views/function_diff.rs @@ -10,6 +10,7 @@ use objdiff_core::{ display::{DiffText, DiffTextColor, HighlightKind, display_row}, }, obj::Object, + util::format_percent, }; use ratatui::{ Frame, @@ -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)), )); } diff --git a/objdiff-core/src/util.rs b/objdiff-core/src/util.rs index a6783c98..129dd985 100644 --- a/objdiff-core/src/util.rs +++ b/objdiff-core/src/util.rs @@ -1,4 +1,4 @@ -use alloc::{format, vec::Vec}; +use alloc::{format, string::String, vec::Vec}; use core::fmt; use anyhow::{Result, ensure}; @@ -26,6 +26,15 @@ impl fmt::UpperHex for ReallySigned { } } +/// 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 { ensure!(reader.len() >= 4, "Not enough bytes to read u32"); let value = reader[..4].try_into()?; diff --git a/objdiff-gui/src/views/diff.rs b/objdiff-gui/src/views/diff.rs index 173abf29..bb2fdbe9 100644 --- a/objdiff-gui/src/views/diff.rs +++ b/objdiff-gui/src/views/diff.rs @@ -12,7 +12,7 @@ use objdiff_core::{ }, }, obj::{InstructionArgValue, Object, Symbol}, - util::ReallySigned, + util::{ReallySigned, format_percent}, }; use time::format_description; @@ -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)), );