From 9ad95acdabff0fcd20e7aba52cad201f1347214c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jorge=20Gregorio=20Rinc=C3=B3n?= Date: Wed, 18 Nov 2026 07:32:18 -0500 Subject: [PATCH 1/3] feat(script): load a script file into the Script pane (#2397) --- apps/rustgis-mobile/src/lib.rs | 2 + apps/rustgis-web/src/web.rs | 3 + crates/ui-egui/src/analysispane.rs | 168 ++++++++++++++++++++++++++++- crates/ui-egui/src/lib.rs | 15 ++- docs/user-guide/analysis.md | 7 +- 5 files changed, 190 insertions(+), 5 deletions(-) diff --git a/apps/rustgis-mobile/src/lib.rs b/apps/rustgis-mobile/src/lib.rs index 4db7b8633a..dd0b109e8e 100644 --- a/apps/rustgis-mobile/src/lib.rs +++ b/apps/rustgis-mobile/src/lib.rs @@ -55,6 +55,7 @@ fn mark(purpose: &str) -> String { } match purpose { "shortcuts" => rustgis_ui_egui::SHORTCUT_PICK, + "script" => rustgis_ui_egui::SCRIPT_PICK, "text" => rustgis_ui_egui::DELIMITED_PICK, "shapefile" => rustgis_ui_egui::SHAPEFILE_PICK, p if p.starts_with(rustgis_ui_egui::TOOL_FILE_PURPOSE) => rustgis_ui_egui::TOOL_FILE_PICK, @@ -240,6 +241,7 @@ mod tests { fn a_dxf_request_keeps_its_id_in_its_mark() { assert_eq!(super::mark("dxf:7"), format!("{}7:", rustgis_ui_egui::DXF_PICK)); assert_eq!(super::mark("shapefile"), rustgis_ui_egui::SHAPEFILE_PICK); + assert_eq!(super::mark("script"), rustgis_ui_egui::SCRIPT_PICK); assert_eq!(super::mark("data"), ""); } diff --git a/apps/rustgis-web/src/web.rs b/apps/rustgis-web/src/web.rs index 834c099661..829c657f99 100644 --- a/apps/rustgis-web/src/web.rs +++ b/apps/rustgis-web/src/web.rs @@ -1250,6 +1250,7 @@ fn services(inbox: Inbox, font_inbox: Inbox, notes: Notes, ctx: egui::Context, e } else { match (purpose, tool_exts) { ("shortcuts", _) => rustgis_ui_egui::SHORTCUT_PICK, + ("script", _) => rustgis_ui_egui::SCRIPT_PICK, ("text", _) => rustgis_ui_egui::DELIMITED_PICK, ("shapefile", _) => rustgis_ui_egui::SHAPEFILE_PICK, (_, Some(_)) => rustgis_ui_egui::TOOL_FILE_PICK, @@ -1262,6 +1263,8 @@ fn services(inbox: Inbox, font_inbox: Inbox, notes: Notes, ctx: egui::Context, e if exts.is_empty() { rfd::AsyncFileDialog::new() } else { rfd::AsyncFileDialog::new().add_filter(exts.join(", "), &exts) } } else if purpose == "shortcuts" { rfd::AsyncFileDialog::new().add_filter(rustgis_ui_egui::tl!("Keyboard shortcuts"), &["json", "rgisshortcuts"]) + } else if purpose == "script" { + rfd::AsyncFileDialog::new().add_filter(rustgis_ui_egui::tl!("Script"), &["txt"]) } else if purpose == "shapefile" { rfd::AsyncFileDialog::new().add_filter("Shapefile", &["shp", "dbf", "shx", "prj", "cpg", "zip"]) } else if purpose == "project" { diff --git a/crates/ui-egui/src/analysispane.rs b/crates/ui-egui/src/analysispane.rs index 4f4445b019..ffaad5e6c2 100644 --- a/crates/ui-egui/src/analysispane.rs +++ b/crates/ui-egui/src/analysispane.rs @@ -41,6 +41,10 @@ pub struct Workflow { pub script_last: Option, /// The last run's Details window is open. pub script_details: bool, + /// A script file picked with Load… in the browser or on a phone ([`script_picked`]), for + /// [`RustGisApp::logic`] to open in the pane once the inbox is read ([`open_script`]): what + /// keeps its undo needs the frame's context, which `open_picked` hasn't. + pub(crate) script_picked: Option, /// When each Script pane run under way or waiting started (seconds since 1970, and the /// instant), and the project it was asked in ([`rustgis_engine::Session::opened`]), in the /// order asked, for [`script_ran`] to tell as each lands. @@ -1851,6 +1855,50 @@ pub(crate) fn open_script(app: &mut RustGisApp, ctx: &egui::Context, script: Str ctx.memory_mut(|m| m.request_focus(box_id)); } +/// Load…: a script file read into the Script pane in place of its script ([`open_script`]). In +/// the browser and on a phone the shell's picker answers later, through [`script_picked`]. +fn load_script(app: &mut RustGisApp, ctx: &egui::Context) { + if let Some(open) = &app.services.open_async { + open("script"); + return; + } + let Some(path) = crate::dialogs::pick_open(&["txt"], tl!("Script")) else { return }; + // One byte past the limit is enough to refuse it, whatever the file's size. + let read = std::fs::File::open(&path).and_then(|file| { + use std::io::Read; + let mut bytes = Vec::new(); + file.take(SCRIPT_FILE_MAX_BYTES as u64 + 1).read_to_end(&mut bytes).map(|_| bytes) + }); + match read.map_err(|e| format!("{path}: {e}")).and_then(|bytes| script_text(app, &bytes)) { + Ok(script) => open_script(app, ctx, script), + Err(e) => app.session.notify(e, true), + } +} + +/// A script file the shell's picker read for Load…, kept for [`RustGisApp::logic`] to open once +/// the inbox is read. A file that isn't UTF-8 text, or one landing while a run is under +/// way, is refused and said, and the pane's script stays. +pub(crate) fn script_picked(app: &mut RustGisApp, bytes: &[u8]) -> Result { + let script = script_text(app, bytes).inspect_err(|e| app.session.notify(e.clone(), true))?; + app.workflow.script_picked = Some(script); + Ok(Value::Null) +} + +/// The largest script file Load… reads, as the assistant's command file. +const SCRIPT_FILE_MAX_BYTES: usize = 1024 * 1024; + +/// A script file's text for the Script pane: refused when it is over [`SCRIPT_FILE_MAX_BYTES`] or +/// isn't UTF-8 text, or while a run is under way (its results would land in the replaced script). +fn script_text(app: &RustGisApp, bytes: &[u8]) -> Result { + if script_running(app) { + return Err(tl!("Wait for the run to finish").to_string()); + } + if bytes.len() > SCRIPT_FILE_MAX_BYTES { + return Err(tl!("The script file is larger than 1 MB").to_string()); + } + std::str::from_utf8(bytes).map(str::to_owned).map_err(|_| tl!("The file isn't UTF-8 text").to_string()) +} + /// The Script pane emptied: its script, results and count, so the next run is `Run #1`, and the /// Details window with the run it showed. Clear does it, and another project opening. fn clear_script(app: &mut RustGisApp) { @@ -2027,11 +2075,20 @@ fn script(app: &mut RustGisApp, ui: &mut Ui, t: &Tokens, fit: bool) { if ui.button(tl!("Commands…")).on_hover_text(tl!("Search every command and its parameters")).clicked() { let _ = app.run("ui.commandSearch", Value::Null); } - // Not while a run is under way: its results would land in the emptied pane. + // Not while a run is under way: its results would land in the replaced or emptied pane. + let running = script_running(app); + let undo = if mac { "⌘+Z" } else { "Ctrl+Z" }; + let mut load = ui + .add_enabled(!running, egui::Button::new(tl!("Load…"))) + .on_hover_text(crate::i18n::fmt("Read a script from a file in place of this one ({keys} brings it back)", &[("keys", undo)])); + if running { + load = load.on_disabled_hover_text(tl!("Wait for the run to finish")); + } + if load.clicked() { + load_script(app, ui.ctx()); + } let w = &app.workflow; - let running = !w.script_started.is_empty(); let anything = !w.script.is_empty() || !w.script_out.is_empty() || w.script_last.is_some(); - let undo = if mac { "⌘+Z" } else { "Ctrl+Z" }; let mut clear = ui .add_enabled(anything && !running, egui::Button::new(tl!("Clear"))) .on_hover_text(crate::i18n::fmt("Empty the script and its results ({keys} brings the script back)", &[("keys", undo)])); @@ -2498,6 +2555,111 @@ mod tests { assert!(!on(&h)); } + /// Load… reads a script file in place of the pane's script, emptying its results as Clear + /// does, and Ctrl+Z brings the old script back; a cancelled picker and a file that isn't UTF-8 + /// text leave it (the latter said), and it is off while a run is under way. + #[test] + fn load_reads_a_script_file_in_place_of_the_panes() { + let dir = std::env::temp_dir().join(format!("rustgis-script-load-{}", std::process::id())); + std::fs::create_dir_all(&dir).unwrap(); + let (good, bad) = (dir.join("zoom.txt"), dir.join("latin1.txt")); + std::fs::write(&good, "map.zoomFull\n").unwrap(); + std::fs::write(&bad, b"map.new {\"name\": \"Espa\xf1a\"}").unwrap(); + let mut app = RustGisApp::with_sample(); + app.workflow.script = "kept".into(); + script_ran(&mut app, crate::Landed::Ran(Ok(json!({"ran": [{"line": 1, "command": "map.zoomFull", "result": null}]})))); + let mut fonts = false; + let mut h = egui_kittest::Harness::builder().with_size(vec2(900.0, 800.0)).build_ui_state( + move |ui, app: &mut RustGisApp| { + if !fonts { + crate::theme::install_fonts(ui.ctx(), &[], None); + fonts = true; + return; + } + script_pane(app, ui, &Tokens::light()); + }, + app, + ); + h.run(); + let load = |h: &mut egui_kittest::Harness<'_, RustGisApp>, path: Option<&std::path::Path>| { + crate::dialogs::PICKED.with(|c| *c.borrow_mut() = path.map(|p| p.display().to_string())); + h.get_by_label("Load…").click(); + h.run(); + }; + load(&mut h, None); + assert_eq!(h.state().workflow.script, "kept", "a cancelled picker changes nothing"); + assert!(h.state().workflow.script_last.is_some()); + load(&mut h, Some(&bad)); + assert_eq!(h.state().workflow.script, "kept", "a file that isn't UTF-8 leaves the script"); + assert!(h.state().workflow.script_last.is_some()); + assert!(h.state().session.notices.iter().any(|n| n.text == "The file isn't UTF-8 text"), "{:?}", h.state().session.notices); + load(&mut h, Some(&good)); + let w = &h.state().workflow; + assert_eq!(w.script, "map.zoomFull\n"); + assert!(w.script_out.is_empty() && w.script_last.is_none(), "the results go, as with Clear"); + let id = egui::Id::unique("script_text"); + let state = egui::widgets::text_edit::TextEditState::load(&h.ctx, id).expect("the box's state"); + let at = state.cursor.char_range().expect("a cursor"); + assert_eq!( + state.undoer().undo(&(at, "map.zoomFull\n".to_string())).map(|(_, t)| t.as_str()), + Some("kept"), + "Ctrl+Z brings it back" + ); + // A run under way: off, as its results would land in the replaced script. + h.state_mut().workflow.script_started.push_back((0, web_time::Instant::now(), 0)); + h.run(); + assert!(egui_kittest::kittest::NodeT::accesskit_node(&h.get_by_label("Load…")).is_disabled()); + std::fs::remove_dir_all(dir).unwrap(); + } + + /// In the browser and on a phone, Load… asks the shell's `"script"` picker; its file opens in + /// the pane on the next frame, a cancelled one (an empty answer) changes nothing, and one + /// landing while a run is under way is refused and said. + #[test] + fn a_script_from_the_shells_picker_opens_in_the_pane() { + use std::sync::{Arc, Mutex}; + let mut app = RustGisApp::with_sample(); + app.workflow.script = "kept".into(); + let asked = Arc::new(Mutex::new(Vec::::new())); + let ask = asked.clone(); + app.services.open_async = Some(Box::new(move |purpose| ask.lock().unwrap().push(purpose.to_string()))); + let mut fonts = false; + let mut h = egui_kittest::Harness::builder().with_size(vec2(900.0, 800.0)).build_ui_state( + move |ui, app: &mut RustGisApp| { + if !fonts { + crate::theme::install_fonts(ui.ctx(), &[], None); + fonts = true; + return; + } + app.logic(ui.ctx()); + script_pane(app, ui, &Tokens::light()); + }, + app, + ); + h.run(); + h.get_by_label("Load…").click(); + h.run(); + assert_eq!(*asked.lock().unwrap(), ["script"]); + h.state_mut().open_picked(crate::SCRIPT_PICK, &[]).unwrap(); + h.run(); + assert_eq!(h.state().workflow.script, "kept", "a cancelled picker changes nothing"); + assert!(h.state_mut().open_picked(&format!("{}bad.txt", crate::SCRIPT_PICK), b"\xff").is_err()); + let big = vec![b'#'; SCRIPT_FILE_MAX_BYTES + 1]; + assert!(h.state_mut().open_picked(&format!("{}big.txt", crate::SCRIPT_PICK), &big).is_err()); + assert!(h.state().session.notices.iter().any(|n| n.text == "The script file is larger than 1 MB")); + h.run(); + assert_eq!(h.state().workflow.script, "kept"); + h.state_mut().open_picked(&format!("{}zoom.txt", crate::SCRIPT_PICK), b"map.zoomFull").unwrap(); + h.run(); + assert_eq!(h.state().workflow.script, "map.zoomFull"); + assert_eq!(h.state().ui.right, Some(crate::RightPane::Script)); + h.state_mut().workflow.script_started.push_back((0, web_time::Instant::now(), 0)); + assert!(h.state_mut().open_picked(&format!("{}other.txt", crate::SCRIPT_PICK), b"map.new").is_err()); + h.run(); + assert_eq!(h.state().workflow.script, "map.zoomFull", "not while a run is under way"); + assert!(h.state().session.notices.iter().any(|n| n.text == "Wait for the run to finish"), "{:?}", h.state().session.notices); + } + /// Details… opens the last run in a window of its own, each line folded: a result the pane cuts /// shows whole there, laid out to read, Copy copies it and Copy All the run; a run that stopped /// shows why. diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs index 07a2b8dbb0..40ce13f3f2 100644 --- a/crates/ui-egui/src/lib.rs +++ b/crates/ui-egui/src/lib.rs @@ -195,6 +195,10 @@ pub const TOOL_FILE_PURPOSE: &str = "toolfile:"; /// Marks a returned shortcut picker file; cancellation leaves no pending routing state. pub const SHORTCUT_PICK: &str = "\u{1}shortcuts:"; +/// Starts the name of a script file the shell's `"script"` picker put in the inbox, for the +/// Script pane's Load…. +pub const SCRIPT_PICK: &str = "\u{1}script:"; + /// Maximum portable shortcut document size: saved overrides plus room for version metadata. pub const SHORTCUT_FILE_MAX_BYTES: usize = keymap::MAX_PREF_BYTES + 100; @@ -2028,12 +2032,17 @@ impl RustGisApp { /// Opens a file from the inbox: one the `"text"` picker marked ([`DELIMITED_PICK`]) opens Add /// Delimited Text for it, or is read with a script's options; one the tool file picker marked /// ([`TOOL_FILE_PICK`]) goes to the Tools pane. Source-CRS picks carry [`SHAPEFILE_PICK`]; - /// shortcut picks carry [`SHORTCUT_PICK`] and open the validated merge preview; DXF picks + /// shortcut picks carry [`SHORTCUT_PICK`] and open the validated merge preview; script picks + /// carry [`SCRIPT_PICK`] and open in the Script pane; DXF picks /// carry [`DXF_PICK`] and open Import DXF. pub fn open_picked(&mut self, name: &str, bytes: &[u8]) -> Result { if name.starts_with(SHORTCUT_PICK) { return shortcutfile::picked(self, bytes); } + if let Some(name) = name.strip_prefix(SCRIPT_PICK) { + // An answer with no name is a cancelled picker. + return if name.is_empty() { Ok(Value::Null) } else { analysispane::script_picked(self, bytes) }; + } if let Some(name) = name.strip_prefix(TOOL_FILE_PICK) { panes::received_file(self, name, bytes); return Ok(Value::Null); @@ -6528,6 +6537,10 @@ impl RustGisApp { for (name, bytes) in arrived { let _ = self.open_picked(&name, &bytes); } + // A script file for the Script pane's Load…, opened with the context its undo needs. + if let Some(script) = self.workflow.script_picked.take() { + analysispane::open_script(self, ctx, script); + } // Files dropped on the desktop window arrive as paths. (The web shell reads browser drops // asynchronously and passes them through the inbox instead.) if !cfg!(target_arch = "wasm32") { diff --git a/docs/user-guide/analysis.md b/docs/user-guide/analysis.md index afa9a23be5..73b40e3ede 100644 --- a/docs/user-guide/analysis.md +++ b/docs/user-guide/analysis.md @@ -1574,7 +1574,12 @@ read, **Copy** for one and **Copy All** for the run, and why it stopped, if it d empties the pane: the script, its results, the count (the next run is Run #1 again) and the Details window. It asks nothing: the box keeps the focus, so Ctrl+Z (⌘+Z) brings the script back; after a click elsewhere, Ctrl+Z undoes in the project instead, so click into the box first. It is -off with nothing to clear, and while a run is under way. Opening a +off with nothing to clear, and while a run is under way. **Load…** reads a script from a file +(a `.txt` file; on the desktop any other too) in place of the pane's, emptying its results as +Clear does, and Ctrl+Z (⌘+Z) brings the old script back; in the browser it uses the browser's file +picker. A file that isn't UTF-8 text, or is over 1 MB, is refused with a notice and the script +stays. It is off while a run is +under way too. Opening a project (New, Open, the sample, a template, or the same one again), from the app, a script, the assistant or an agent, starts the pane blank: the script, its results, the count and the Details window go with the old project. A script that opens a project itself stays, with its results, as From 36682d8cec720a5e7927d5fc055c869e54d71798 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jorge=20Gregorio=20Rinc=C3=B3n?= Date: Wed, 18 Nov 2026 09:00:24 -0500 Subject: [PATCH 2/3] fix(script): drop a UTF-8 byte-order mark, refuse a large browser pick before reading it, name the file in notices --- apps/rustgis-web/src/web.rs | 5 ++++ crates/ui-egui/src/analysispane.rs | 39 +++++++++++++++--------------- crates/ui-egui/src/lib.rs | 6 ++++- docs/user-guide/analysis.md | 11 ++++----- 4 files changed, 35 insertions(+), 26 deletions(-) diff --git a/apps/rustgis-web/src/web.rs b/apps/rustgis-web/src/web.rs index 829c657f99..e8c00d8565 100644 --- a/apps/rustgis-web/src/web.rs +++ b/apps/rustgis-web/src/web.rs @@ -494,6 +494,11 @@ async fn read_together(files: Vec, mark: &str, inbox: &Inbox, not .push((rustgis_ui_egui::tl!("Shortcut file exceeds the size limit.").into(), true)); continue; } + if mark == rustgis_ui_egui::SCRIPT_PICK && file.size() > rustgis_ui_egui::SCRIPT_FILE_MAX_BYTES as f64 { + let text = rustgis_ui_egui::i18n::fmt("{file} is larger than 1 MB", &[("file", &name)]); + notes.lock().unwrap_or_else(|e| e.into_inner()).push((text, true)); + continue; + } if let Some(bytes) = crate::parts::read_file(file, notes.clone()).await { read.push((name, bytes)); } else if mark.starts_with(rustgis_ui_egui::DXF_PICK) { diff --git a/crates/ui-egui/src/analysispane.rs b/crates/ui-egui/src/analysispane.rs index ffaad5e6c2..18c8efb7b5 100644 --- a/crates/ui-egui/src/analysispane.rs +++ b/crates/ui-egui/src/analysispane.rs @@ -1867,36 +1867,36 @@ fn load_script(app: &mut RustGisApp, ctx: &egui::Context) { let read = std::fs::File::open(&path).and_then(|file| { use std::io::Read; let mut bytes = Vec::new(); - file.take(SCRIPT_FILE_MAX_BYTES as u64 + 1).read_to_end(&mut bytes).map(|_| bytes) + file.take(crate::SCRIPT_FILE_MAX_BYTES as u64 + 1).read_to_end(&mut bytes).map(|_| bytes) }); - match read.map_err(|e| format!("{path}: {e}")).and_then(|bytes| script_text(app, &bytes)) { + let name = std::path::Path::new(&path).file_name().map_or_else(|| path.clone(), |n| n.to_string_lossy().into_owned()); + match read.map_err(|e| format!("{path}: {e}")).and_then(|bytes| script_text(app, &name, &bytes)) { Ok(script) => open_script(app, ctx, script), Err(e) => app.session.notify(e, true), } } /// A script file the shell's picker read for Load…, kept for [`RustGisApp::logic`] to open once -/// the inbox is read. A file that isn't UTF-8 text, or one landing while a run is under -/// way, is refused and said, and the pane's script stays. -pub(crate) fn script_picked(app: &mut RustGisApp, bytes: &[u8]) -> Result { - let script = script_text(app, bytes).inspect_err(|e| app.session.notify(e.clone(), true))?; +/// the inbox is read. A file [`script_text`] refuses is said, and the pane's script stays. +pub(crate) fn script_picked(app: &mut RustGisApp, name: &str, bytes: &[u8]) -> Result { + let script = script_text(app, name, bytes).inspect_err(|e| app.session.notify(e.clone(), true))?; app.workflow.script_picked = Some(script); Ok(Value::Null) } -/// The largest script file Load… reads, as the assistant's command file. -const SCRIPT_FILE_MAX_BYTES: usize = 1024 * 1024; - -/// A script file's text for the Script pane: refused when it is over [`SCRIPT_FILE_MAX_BYTES`] or -/// isn't UTF-8 text, or while a run is under way (its results would land in the replaced script). -fn script_text(app: &RustGisApp, bytes: &[u8]) -> Result { +/// Script file `name`'s text for the Script pane, without the byte-order mark some editors +/// start a UTF-8 file with (it would make the first line's command unknown): refused when it is +/// over [`crate::SCRIPT_FILE_MAX_BYTES`] or isn't UTF-8 text, or while a run is under way (its +/// results would land in the replaced script). +fn script_text(app: &RustGisApp, name: &str, bytes: &[u8]) -> Result { if script_running(app) { return Err(tl!("Wait for the run to finish").to_string()); } - if bytes.len() > SCRIPT_FILE_MAX_BYTES { - return Err(tl!("The script file is larger than 1 MB").to_string()); + if bytes.len() > crate::SCRIPT_FILE_MAX_BYTES { + return Err(crate::i18n::fmt("{file} is larger than 1 MB", &[("file", name)])); } - std::str::from_utf8(bytes).map(str::to_owned).map_err(|_| tl!("The file isn't UTF-8 text").to_string()) + let bytes = bytes.strip_prefix(b"\xEF\xBB\xBF").unwrap_or(bytes); + std::str::from_utf8(bytes).map(str::to_owned).map_err(|_| crate::i18n::fmt("{file} isn't UTF-8 text", &[("file", name)])) } /// The Script pane emptied: its script, results and count, so the next run is `Run #1`, and the @@ -2592,7 +2592,7 @@ mod tests { load(&mut h, Some(&bad)); assert_eq!(h.state().workflow.script, "kept", "a file that isn't UTF-8 leaves the script"); assert!(h.state().workflow.script_last.is_some()); - assert!(h.state().session.notices.iter().any(|n| n.text == "The file isn't UTF-8 text"), "{:?}", h.state().session.notices); + assert!(h.state().session.notices.iter().any(|n| n.text == "latin1.txt isn't UTF-8 text"), "{:?}", h.state().session.notices); load(&mut h, Some(&good)); let w = &h.state().workflow; assert_eq!(w.script, "map.zoomFull\n"); @@ -2644,12 +2644,13 @@ mod tests { h.run(); assert_eq!(h.state().workflow.script, "kept", "a cancelled picker changes nothing"); assert!(h.state_mut().open_picked(&format!("{}bad.txt", crate::SCRIPT_PICK), b"\xff").is_err()); - let big = vec![b'#'; SCRIPT_FILE_MAX_BYTES + 1]; + let big = vec![b'#'; crate::SCRIPT_FILE_MAX_BYTES + 1]; assert!(h.state_mut().open_picked(&format!("{}big.txt", crate::SCRIPT_PICK), &big).is_err()); - assert!(h.state().session.notices.iter().any(|n| n.text == "The script file is larger than 1 MB")); + assert!(h.state().session.notices.iter().any(|n| n.text == "big.txt is larger than 1 MB")); h.run(); assert_eq!(h.state().workflow.script, "kept"); - h.state_mut().open_picked(&format!("{}zoom.txt", crate::SCRIPT_PICK), b"map.zoomFull").unwrap(); + // A byte-order mark (Notepad's UTF-8) isn't kept: the first line's command stays known. + h.state_mut().open_picked(&format!("{}zoom.txt", crate::SCRIPT_PICK), b"\xEF\xBB\xBFmap.zoomFull").unwrap(); h.run(); assert_eq!(h.state().workflow.script, "map.zoomFull"); assert_eq!(h.state().ui.right, Some(crate::RightPane::Script)); diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs index 40ce13f3f2..dcf920385b 100644 --- a/crates/ui-egui/src/lib.rs +++ b/crates/ui-egui/src/lib.rs @@ -199,6 +199,10 @@ pub const SHORTCUT_PICK: &str = "\u{1}shortcuts:"; /// Script pane's Load…. pub const SCRIPT_PICK: &str = "\u{1}script:"; +/// The largest script file the Script pane's Load… reads, as the assistant's command file; the +/// browser's shell refuses a larger one before reading it. +pub const SCRIPT_FILE_MAX_BYTES: usize = 1024 * 1024; + /// Maximum portable shortcut document size: saved overrides plus room for version metadata. pub const SHORTCUT_FILE_MAX_BYTES: usize = keymap::MAX_PREF_BYTES + 100; @@ -2041,7 +2045,7 @@ impl RustGisApp { } if let Some(name) = name.strip_prefix(SCRIPT_PICK) { // An answer with no name is a cancelled picker. - return if name.is_empty() { Ok(Value::Null) } else { analysispane::script_picked(self, bytes) }; + return if name.is_empty() { Ok(Value::Null) } else { analysispane::script_picked(self, name, bytes) }; } if let Some(name) = name.strip_prefix(TOOL_FILE_PICK) { panes::received_file(self, name, bytes); diff --git a/docs/user-guide/analysis.md b/docs/user-guide/analysis.md index 73b40e3ede..249da1f23b 100644 --- a/docs/user-guide/analysis.md +++ b/docs/user-guide/analysis.md @@ -1574,12 +1574,11 @@ read, **Copy** for one and **Copy All** for the run, and why it stopped, if it d empties the pane: the script, its results, the count (the next run is Run #1 again) and the Details window. It asks nothing: the box keeps the focus, so Ctrl+Z (⌘+Z) brings the script back; after a click elsewhere, Ctrl+Z undoes in the project instead, so click into the box first. It is -off with nothing to clear, and while a run is under way. **Load…** reads a script from a file -(a `.txt` file; on the desktop any other too) in place of the pane's, emptying its results as -Clear does, and Ctrl+Z (⌘+Z) brings the old script back; in the browser it uses the browser's file -picker. A file that isn't UTF-8 text, or is over 1 MB, is refused with a notice and the script -stays. It is off while a run is -under way too. Opening a +off with nothing to clear, and while a run is under way. **Load…**, off then too, reads a script +from a file (a `.txt` file; on the desktop any other too) in place of the pane's, emptying its +results as Clear does, and Ctrl+Z (⌘+Z) brings the old script back; in the browser it uses the +browser's file picker. A file that isn't UTF-8 text, or is over 1 MB, is refused with a notice +naming it, and the script stays. Opening a project (New, Open, the sample, a template, or the same one again), from the app, a script, the assistant or an agent, starts the pane blank: the script, its results, the count and the Details window go with the old project. A script that opens a project itself stays, with its results, as From d34ec59edc43c847e018f4be3b959ca6964a28f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jorge=20Gregorio=20Rinc=C3=B3n?= Date: Wed, 18 Nov 2026 09:32:44 -0500 Subject: [PATCH 3/3] fix(script): refuse a large phone pick before reading it, translate the read-error notice --- apps/rustgis-mobile/src/shell.rs | 26 ++++++++++++++++++++++++++ crates/ui-egui/src/analysispane.rs | 3 ++- 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/apps/rustgis-mobile/src/shell.rs b/apps/rustgis-mobile/src/shell.rs index 817848077d..0e05e92f0b 100644 --- a/apps/rustgis-mobile/src/shell.rs +++ b/apps/rustgis-mobile/src/shell.rs @@ -128,6 +128,8 @@ struct App { enum Problem { /// The shortcut file `name` is over `SHORTCUT_FILE_MAX_BYTES`. ShortcutTooLarge { name: String }, + /// The script file `name`, picked for the Script pane's Load…, is over `SCRIPT_FILE_MAX_BYTES`. + ScriptTooLarge { name: String }, /// The file `name` is over the size read into memory (`rustgis_data::io::MAX_BYTES`), by /// itself or `together` with the files picked with it and already read. TooLarge { name: String, together: bool }, @@ -213,6 +215,7 @@ impl App

{ let limit = rustgis_ui_egui::SHORTCUT_FILE_MAX_BYTES.to_string(); i18n::fmt("{name} wasn't opened: a shortcut file is at most {limit} bytes.", &[("name", &name), ("limit", &limit)]) } + Problem::ScriptTooLarge { name } => i18n::fmt("{file} is larger than 1 MB", &[("file", &name)]), Problem::TooLarge { name, together } => { let limit = (rustgis_engine::data::io::MAX_BYTES >> 20).to_string(); let template = if together { @@ -352,6 +355,9 @@ fn read_batch(purpose: &str, files: Vec<(String, PathBuf)>, inbox: &Inbox, probl let size = std::fs::metadata(&path).map(|m| m.len()).unwrap_or(0); if shortcut && size > rustgis_ui_egui::SHORTCUT_FILE_MAX_BYTES as u64 { say(Problem::ShortcutTooLarge { name }); + } else if mark == rustgis_ui_egui::SCRIPT_PICK && size > rustgis_ui_egui::SCRIPT_FILE_MAX_BYTES as u64 { + // Refused before it is read, as the pane would refuse it after. + say(Problem::ScriptTooLarge { name }); } else if held.saturating_add(size) > rustgis_engine::data::io::MAX_BYTES { // Refused before it is read: no more than the cap is held in memory at once, in // one file or in the files picked together. @@ -764,4 +770,24 @@ mod tests { assert_eq!(reading.load(Ordering::Relaxed), 0); assert_eq!(*inbox.lock().unwrap(), vec![(crate::mark("dxf:7"), Vec::::new())]); } + + /// A script picked for Load… over the pane's limit is refused before it is read, naming it; one + /// within it reaches the inbox marked for the pane. + #[test] + fn a_script_over_the_limit_is_refused_before_it_is_read() { + let dir = std::env::temp_dir().join(format!("rustgis-mobile-script-{}", std::process::id())); + std::fs::create_dir_all(&dir).unwrap(); + let (big, small) = (dir.join("big.txt"), dir.join("small.txt")); + std::fs::write(&big, vec![b'#'; rustgis_ui_egui::SCRIPT_FILE_MAX_BYTES + 1]).unwrap(); + std::fs::write(&small, "map.zoomFull").unwrap(); + let (inbox, problems) = (Inbox::default(), Problems::default()); + let files = vec![("big.txt".to_string(), big), ("small.txt".to_string(), small)]; + read_batch("script", files, &inbox, &problems, &mut 0); + let said = problems.lock().unwrap(); + assert!(matches!(said.as_slice(), [Problem::ScriptTooLarge { name }] if name == "big.txt")); + let names: Vec = inbox.lock().unwrap().iter().map(|(n, _)| n.clone()).collect(); + assert_eq!(names, [format!("{}small.txt", rustgis_ui_egui::SCRIPT_PICK)]); + // The copies are gone, read or not, and their folder with them (`remove_copy`). + assert!(!dir.exists()); + } } diff --git a/crates/ui-egui/src/analysispane.rs b/crates/ui-egui/src/analysispane.rs index 18c8efb7b5..1b44d4bbcd 100644 --- a/crates/ui-egui/src/analysispane.rs +++ b/crates/ui-egui/src/analysispane.rs @@ -1870,7 +1870,8 @@ fn load_script(app: &mut RustGisApp, ctx: &egui::Context) { file.take(crate::SCRIPT_FILE_MAX_BYTES as u64 + 1).read_to_end(&mut bytes).map(|_| bytes) }); let name = std::path::Path::new(&path).file_name().map_or_else(|| path.clone(), |n| n.to_string_lossy().into_owned()); - match read.map_err(|e| format!("{path}: {e}")).and_then(|bytes| script_text(app, &name, &bytes)) { + let unread = |e: std::io::Error| crate::i18n::fmt("{path}: {error}", &[("path", &path), ("error", &e.to_string())]); + match read.map_err(unread).and_then(|bytes| script_text(app, &name, &bytes)) { Ok(script) => open_script(app, ctx, script), Err(e) => app.session.notify(e, true), }