diff --git a/crates/engine/src/cmd/blocks.rs b/crates/engine/src/cmd/blocks.rs index 0744ba9b8..8440e7d99 100644 --- a/crates/engine/src/cmd/blocks.rs +++ b/crates/engine/src/cmd/blocks.rs @@ -43,14 +43,14 @@ pub fn specs() -> Vec { .menu(&["Insert", "Block..."]) .alias(&["i", "-insert", "ddinsert"]) .params("{name (\"*NAME\": exploded), file?: DXF/DWG path (defines or redefines block `name`, default the file name, from its model space; base point $INSBASE), at: [x,y], scale?: number | [x, y], rotation? (degrees), basePoint?: [x,y] (block coordinates), attribs?: {TAG: value}, explode?: bool}") - .interactive(|_| Ok(Box::new(InsertM::default()))), + .interactive(|s| Ok(Box::new(InsertM { scripted: s.script_running(), ..InsertM::default() }))), CommandSpec::new("blockspalette", "Blocks Palette", run_blockspalette) .params("{references?: bool (default true; counting reads every object)} → {blocks: [{name, description, references?}], truncated} (the app opens the Blocks palette instead)") .enabled(has_doc) .noundo(), CommandSpec::new("minsert", "Multiple Insert", run_minsert) .params("{name, file?: DXF/DWG path (as INSERT), at: [x,y], scale?: number | [x, y], rotation? (degrees), basePoint?: [x,y], attribs?: {TAG: value}, rows?: 1..32767 (default 1), columns?: 1..32767 (default 1), rowSpacing?, columnSpacing? (along the reference's own axes, not scaled with it)}") - .interactive(|_| Ok(Box::new(InsertM { minsert: true, ..InsertM::default() }))), + .interactive(|s| Ok(Box::new(InsertM { minsert: true, scripted: s.script_running(), ..InsertM::default() }))), CommandSpec::new("attdef", "Define Attributes...", run_attdef) .menu(&["Draw", "Block", "Define Attributes..."]) .alias(&["att", "-attdef"]) @@ -778,6 +778,9 @@ struct InsertM { asked: usize, /// ATTDIA 1: the values are asked for in the Edit Attributes dialog. dialog: bool, + /// A script started this command ([`Session::script_running`]), fixed for its whole life: a + /// script never opens the dialog, even when it ends with INSERT still waiting. + scripted: bool, values: serde_json::Map, /// Verify attributes asked again so far. verified: usize, @@ -868,7 +871,8 @@ impl InsertM { s.echo(format!("Block \"{name}\" not found.")); return Ok(()); }; - self.dialog = s.settings.attdia; + // A script cannot click the dialog: it asks at the command line, as ATTDIA 0 does. + self.dialog = s.settings.attdia && !self.scripted; self.attdefs = if self.explode || !s.settings.attreq { Vec::new() } else { diff --git a/crates/engine/src/session/cmdline.rs b/crates/engine/src/session/cmdline.rs index 68a5e532b..f8c4f87a5 100644 --- a/crates/engine/src/session/cmdline.rs +++ b/crates/engine/src/session/cmdline.rs @@ -175,6 +175,13 @@ impl Session { self.input(Input::Text(tt.to_string())) } + /// Whether a script is running ([`Session::script`]). A command a script starts never opens a + /// dialog: every value it asks for stays at the command line, which is where the script (or + /// the user, once it ends) answers it. + pub fn script_running(&self) -> bool { + self.in_script + } + /// Run a script, as AutoCAD reads one: a space or a line end ends an input, and an empty /// input (two spaces in a row, a space at the end of a line, an empty line) is Enter. At /// free-text prompts spaces are part of the text. Lines starting with `;` are comments. diff --git a/crates/engine/tests/it/insert_attrib_entry.rs b/crates/engine/tests/it/insert_attrib_entry.rs index 7119f6ee2..3dad9d564 100644 --- a/crates/engine/tests/it/insert_attrib_entry.rs +++ b/crates/engine/tests/it/insert_attrib_entry.rs @@ -214,6 +214,25 @@ fn hostile_values_never_panic() { assert_eq!(values(&s), pairs(&[("P", "v")])); } +#[test] +fn a_script_never_asks_in_the_dialog() { + // A script cannot click a dialog: ATTDIA 1 keeps its prompts at the command line, even when + // the script ends with INSERT still waiting for the first attribute. + let mut s = session(None, Some(true)); + s.script("-INSERT T 0,50 1 1 0\n").unwrap(); + assert_eq!(s.prompt_text(), "INSERT First <1>:", "INSERT still waits after the script"); + assert!(s.attribute_entry().is_none(), "a scripted INSERT never opens the dialog"); + // The command the script started keeps answering at the command line. + s.input(Input::Text("a".into())).unwrap(); + s.input(Input::Text("c".into())).unwrap(); + s.input(Input::Text("a".into())).unwrap(); // A's Verify + assert!(s.running.is_none(), "{}", s.prompt_text()); + assert_eq!(values(&s), pairs(&[("A", "a"), ("B", "two"), ("C", "c")])); + // A typed INSERT with ATTDIA 1 still asks in the dialog. + insert_up_to_the_attributes(&mut s); + assert!(s.attribute_entry().is_some(), "a typed INSERT asks in the dialog"); +} + #[test] fn an_entry_belongs_to_the_drawing_and_command_that_asked() { let mut s = session(None, Some(true)); diff --git a/crates/ui-egui/tests/it/insert_attrib_dialog.rs b/crates/ui-egui/tests/it/insert_attrib_dialog.rs index e46dd8e81..6bbeac1aa 100644 --- a/crates/ui-egui/tests/it/insert_attrib_dialog.rs +++ b/crates/ui-egui/tests/it/insert_attrib_dialog.rs @@ -143,6 +143,16 @@ fn a_new_insert_does_not_inherit_a_cancelled_draft() { assert_eq!(values(app), Some(vec![("A".into(), "101".into()), ("B".into(), "1".into())])); } +#[test] +fn a_script_never_opens_the_dialog() { + let mut a = app(); + // A script that ends before the first attribute answer leaves INSERT waiting. + a.session.script("-INSERT T 50,0 1 1 0\n").unwrap(); + let h = harness(a); + assert!(h.query_by_label("Edit Attributes").is_none(), "a script never opens the dialog"); + assert!(h.state().session.prompt_text().contains("Room"), "asked at the command line"); +} + #[test] fn no_dialog_with_attdia_0_or_from_json() { let mut a = app();