Skip to content

A fast file manager for Windows and macOS that never freezes: tabs, dual panes, archives as folders, Quick Look-style previews. A File Explorer and Finder alternative written in Rust.

Hash compare shows where a pasted hash differs

gelenmedyaa391090d64eb
diff --git a/crates/rf-app/src/hash_panel.rs b/crates/rf-app/src/hash_panel.rs
index 0f7c4418..e5484c0a 100644
--- a/crates/rf-app/src/hash_panel.rs
+++ b/crates/rf-app/src/hash_panel.rs
@@ -204,6 +204,14 @@ impl HashPanel {
}
}
+/// How a pasted hash differs from the computed one, character by character, for showing it: each place holds the pasted character
+/// (or `None` where the paste is shorter) and whether it differs from the computed one there. A pasted hash longer than the computed
+/// one has its extra characters marked as different. (A Discord request: say WHERE it differs, not only that it does.)
+fn diff_marks(computed: &str, typed: &str) -> Vec<(Option<char>, bool)> {
+ let (a, b): (Vec<char>, Vec<char>) = (computed.chars().collect(), typed.chars().collect());
+ (0..a.len().max(b.len())).map(|i| (b.get(i).copied(), a.get(i) != b.get(i))).collect()
+}
+
/// Lowercases and strips whitespace / `0x` so pasted hashes compare cleanly.
fn normalize(s: &str) -> String {
let t: String = s.chars().filter(|c| !c.is_whitespace()).collect::<String>().to_lowercase();
@@ -361,10 +369,35 @@ impl Render for HashPanel {
.size(px(18.))
.text_color(if ok { green } else { p.danger })
})),
- ),
+ )
+ // where a pasted hash differs: its characters, the different ones in red, missing ones as a faint dot
+ .when(verdict == Some(false), |c| {
+ let marks = diff_marks(value.as_deref().unwrap_or_default(), &typed);
+ c.child(h_flex().flex_wrap().text_xs().font_family("Consolas").children(marks.into_iter().map(|(ch, differs)| {
+ let shown = ch.map_or_else(|| "·".to_string(), |c| c.to_string());
+ div().child(shown).text_color(match (ch, differs) {
+ (None, _) => p.text_tertiary,
+ (Some(_), true) => p.danger,
+ (Some(_), false) => p.text_secondary,
+ })
+ })))
+ }),
);
}
v_flex().w_full().gap_3().p_3().child(header).child(status).child(rows)
}
}
+
+#[cfg(test)]
+mod diff_tests {
+ #[test]
+ fn the_places_that_differ_are_marked_and_missing_ones_are_empty() {
+ let m = super::diff_marks("abcd", "abxd");
+ assert_eq!(m, vec![(Some('a'), false), (Some('b'), false), (Some('x'), true), (Some('d'), false)]);
+ let short = super::diff_marks("abcd", "ab");
+ assert_eq!(short, vec![(Some('a'), false), (Some('b'), false), (None, true), (None, true)]);
+ let long = super::diff_marks("ab", "abz");
+ assert_eq!(long, vec![(Some('a'), false), (Some('b'), false), (Some('z'), true)]);
+ }
+}