0.1.1: device names with brackets, reopening the app

A device named like "Sen setup (CLI)" made kept-both copies unreadable:
the name was cut at the inner bracket, so tidying wrote the kept version
into a new note instead of the original. Copy names are now read by
matching brackets, and names with unpaired brackets are refused.
Opening Lune again while it runs brings its window back.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-29 13:47:07 +07:00
parent 97dda528d3
commit 3f29810f96
8 changed files with 81 additions and 9 deletions

View File

@@ -53,7 +53,9 @@ pub fn parse_copy(path: &str) -> Option<(String, String)> {
let file_start = path.rfind('/').map_or(0, |i| i + 1);
let name = &path[file_start..];
let v = name.rfind(MARK)?;
let open = name[..v].rfind(" (")?;
// The " (" that matches the ")" after the mark: device labels may have brackets of their own, like
// "Sen setup (CLI)". Unbalanced labels fall back to the nearest " (".
let open = matching_open(&name[..v]).or_else(|| name[..v].rfind(" ("))?;
let label = &name[open + 2..v];
let rest = &name[v + MARK.len()..];
let close = rest.find(')')?;
@@ -66,18 +68,51 @@ pub fn parse_copy(path: &str) -> Option<(String, String)> {
Some((format!("{}{}{}", &path[..file_start], &name[..open], ext), label.to_string()))
}
/// Device labels end up inside kept-both names, so their brackets must pair up to be read back.
pub fn label_ok(label: &str) -> bool {
let mut depth = 0i32;
for c in label.chars() {
depth += match c {
'(' => 1,
')' => -1,
_ => 0,
};
if depth < 0 {
return false;
}
}
depth == 0
}
pub const LABEL_BRACKETS: &str = "a device name's brackets need to pair up, like \"Mac (work)\"";
/// Index of the " (" in `s` whose bracket is still open at the end of `s`.
fn matching_open(s: &str) -> Option<usize> {
let mut depth = 0usize;
for (i, c) in s.char_indices().rev() {
match c {
')' => depth += 1,
'(' if depth > 0 => depth -= 1,
'(' => return (i > 0 && s[..i].ends_with(' ')).then(|| i - 1),
_ => {}
}
}
None
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn copy_names_round_trip() {
for (p, l, n) in [("runbook.md", "Codex", 1), ("ops/vm list.md", "Hermes", 2), (".env", "VPS", 1), ("a.tar.gz", "x", 1), ("README", "Mac", 3)] {
for (p, l, n) in [("runbook.md", "Codex", 1), ("ops/vm list.md", "Hermes", 2), (".env", "VPS", 1), ("a.tar.gz", "x", 1), ("README", "Mac", 3), ("welcome.md", "Sen setup (CLI)", 1), ("a (b).md", "Mac (old) (2)", 2)] {
let c = copy_name(p, l, n);
assert_eq!(parse_copy(&c), Some((p.to_string(), l.to_string())), "{c}");
}
assert_eq!(copy_name("ops/runbook.md", "Codex", 1), "ops/runbook (Codex's version).md");
assert_eq!(parse_copy("notes/plain.md"), None);
assert_eq!(parse_copy("Sen's version.md"), None);
assert!(label_ok("Sen setup (CLI)") && !label_ok("Mac (old") && !label_ok("Mac )old("));
}
}

View File

@@ -226,6 +226,25 @@ fn tidy_theirs_and_both() {
assert!(tidy::list(&a).unwrap().is_empty());
}
#[test]
fn device_names_with_brackets_keep_both_and_tidy_cleanly() {
let w = World::new();
let a = w.device("Sen setup (CLI)");
write(&a, "welcome.md", "base\n");
save_and_sync(&a);
let b = w.device("Mac");
write(&a, "welcome.md", "tuesday\n");
write(&b, "welcome.md", "friday\n");
save_and_sync(&b);
save_and_sync(&a);
let kept = tidy::list(&a).unwrap();
assert_eq!(kept.len(), 1);
assert_eq!((kept[0].note.as_str(), kept[0].label.as_str()), ("welcome.md", "Sen setup (CLI)"));
tidy::keep(&a, &kept[0].copy, Keep::Mine).unwrap();
assert_eq!(read(&a, "welcome.md"), b"tuesday\n");
assert_eq!(snapshot(&a.root).len(), 1, "no stray notes");
}
#[test]
fn deleted_on_one_side_edited_on_the_other_keeps_the_edit() {
let w = World::new();