Fix non-strings also being escaped when copied (#307)

* Fix non-strings also being escaped when copied

* Move copy escape logic into objdiff-core
This commit is contained in:
LagoLunatic
2025-12-23 14:31:36 -05:00
committed by GitHub
parent 67b28b7da1
commit 976e629f08
6 changed files with 75 additions and 49 deletions
+2 -21
View File
@@ -865,24 +865,6 @@ pub fn hover_items_ui(ui: &mut Ui, items: Vec<HoverItem>, appearance: &Appearanc
}
}
/// Escape ASCII characters such as \n or \t, but not Unicode characters such as \u{3000}.
/// Suitable for copying to clipboard.
fn escape_special_ascii_characters(value: String) -> String {
let mut escaped = String::new();
escaped.push('"');
for c in value.chars() {
if c.is_ascii() {
for e in c.escape_default() {
escaped.push(e);
}
} else {
escaped.push(c);
}
}
escaped.push('"');
escaped
}
pub fn context_menu_items_ui(
ui: &mut Ui,
items: Vec<ContextItem>,
@@ -892,7 +874,7 @@ pub fn context_menu_items_ui(
let mut ret = None;
for item in items {
match item {
ContextItem::Copy { value, label } => {
ContextItem::Copy { value, label, copy_string } => {
let mut job = LayoutJob::default();
write_text("Copy ", appearance.text_color, &mut job, appearance.code_font.clone());
write_text(
@@ -912,8 +894,7 @@ pub fn context_menu_items_ui(
write_text(")", appearance.text_color, &mut job, appearance.code_font.clone());
}
if ui.button(job).clicked() {
let escaped = escape_special_ascii_characters(value);
ui.ctx().copy_text(escaped);
ui.ctx().copy_text(copy_string.unwrap_or(value));
ui.close();
}
}