diff --git a/Cargo.lock b/Cargo.lock index b1ede2a..5dcad81 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2581,7 +2581,7 @@ checksum = "f9f8bd3e56ce4dfc153cf470fffbfa98c7620958b312ca5c3a4b8d5181fd13c6" [[package]] name = "lune-core" -version = "0.1.0" +version = "0.1.1" dependencies = [ "anyhow", "base64 0.23.1", @@ -2601,7 +2601,7 @@ dependencies = [ [[package]] name = "lune-desktop" -version = "0.1.0" +version = "0.1.1" dependencies = [ "anyhow", "chrono", @@ -2621,7 +2621,7 @@ dependencies = [ [[package]] name = "lunehub" -version = "0.1.0" +version = "0.1.1" dependencies = [ "anyhow", "axum", @@ -2642,7 +2642,7 @@ dependencies = [ [[package]] name = "lunesync" -version = "0.1.0" +version = "0.1.1" dependencies = [ "anyhow", "chrono", diff --git a/Cargo.toml b/Cargo.toml index 5d01fe8..483751c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,4 +4,4 @@ members = ["core", "cli", "hub", "desktop"] # One version for every part: the hub offers clients by it (D3). [workspace.package] -version = "0.1.0" +version = "0.1.1" diff --git a/cli/src/main.rs b/cli/src/main.rs index 6cff332..f1077d1 100644 --- a/cli/src/main.rs +++ b/cli/src/main.rs @@ -288,6 +288,9 @@ fn run_cli(cli: Cli) -> Result<()> { if label.is_empty() { bail!("the label can't be empty"); } + if !lune_core::names::label_ok(&label) { + bail!(lune_core::names::LABEL_BRACKETS); + } for h in &config.hubs { h.set_label(&label)?; } diff --git a/core/src/names.rs b/core/src/names.rs index 108fde4..9634889 100644 --- a/core/src/names.rs +++ b/core/src/names.rs @@ -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 { + 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(")); } } diff --git a/core/tests/sync.rs b/core/tests/sync.rs index 0c99c2d..c66799e 100644 --- a/core/tests/sync.rs +++ b/core/tests/sync.rs @@ -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(); diff --git a/desktop/src/commands.rs b/desktop/src/commands.rs index 7dd0ea9..1d54c1b 100644 --- a/desktop/src/commands.rs +++ b/desktop/src/commands.rs @@ -94,6 +94,9 @@ pub fn connect( if label.is_empty() { return Err("give this Mac a name your team will see".into()); } + if !lune_core::names::label_ok(label) { + return Err(lune_core::names::LABEL_BRACKETS.into()); + } let mut config = Config::load().map_err(plain)?; let request = match (hub_url, code) { (Some(hub), Some(code)) if !hub.trim().is_empty() && !code.trim().is_empty() => { diff --git a/desktop/src/main.rs b/desktop/src/main.rs index 9e51704..3ea83e4 100644 --- a/desktop/src/main.rs +++ b/desktop/src/main.rs @@ -410,8 +410,17 @@ fn main() { let _ = window.hide(); } }) - .run(tauri::generate_context!()) - .expect("Lune couldn't start"); + .build(tauri::generate_context!()) + .expect("Lune couldn't start") + .run(|app, event| { + // Opening Lune again (Finder, Launchpad) while it runs in the menu bar brings its window back. + #[cfg(target_os = "macos")] + if let tauri::RunEvent::Reopen { .. } = event { + show_window(app); + } + #[cfg(not(target_os = "macos"))] + let _ = (app, event); + }); } async fn tokio_sleep(d: Duration) { diff --git a/hub/src/api.rs b/hub/src/api.rs index ba7cc07..1d58f60 100644 --- a/hub/src/api.rs +++ b/hub/src/api.rs @@ -107,6 +107,9 @@ fn check_label(label: &str) -> Result { if label.is_empty() || label.len() > 40 { return Err(bad("a device label needs 1–40 characters")); } + if !lune_core::names::label_ok(label) { + return Err(bad(lune_core::names::LABEL_BRACKETS)); + } Ok(label.to_string()) }