LTSCALE typed at the command line asks for the scale factor (fixes #1194) (#1199)

This commit is contained in:
Marco Arnulfo authored and GitHub committed 2026-10-19 02:30:56 -04:00
1 parent c68420221c
commit a2bee8b49e
2 files changed
+54 -2

No files matched your search

+35 -2
View File
@@ -11,7 +11,7 @@ use cadcraft_geom::Vec3;
use serde_json::{Value, json};
use super::*;
use crate::{Result, Session};
use crate::{Accept, Input, Interactive, Prompt, Result, Session, Step};
pub fn specs() -> Vec<CommandSpec> {
vec![
@@ -30,7 +30,7 @@ pub fn specs() -> Vec<CommandSpec> {
.noundo(),
CommandSpec::new("-linetype", "Linetype", run_linetype).params("{current?, load?, file? | text?, reload?} (as LINETYPE)").interactive(|_| Ok(Box::new(linfile::DashLinetype::new()))),
CommandSpec::new("lweight", "Lineweight...", run_lweight).menu(&["Format", "Lineweight..."]).alias(&["lw", "lineweight"]).params("{lineweight: mm | ByLayer}"),
CommandSpec::new("ltscale", "Linetype Scale", run_ltscale).alias(&["lts"]).params("{scale}"),
CommandSpec::new("ltscale", "Linetype Scale", run_ltscale).alias(&["lts"]).params("{scale}").interactive(|_| Ok(Box::new(LtscaleM))),
CommandSpec::new("units", "Units...", run_units).menu(&["Format", "Units..."]).alias(&["un"]).params("{lunits?: 1..5, luprec?: 0..8, aunits?: 0..4, auprec?: 0..8, insunits?}"),
CommandSpec::new("limits", "Drawing Limits", run_limits).menu(&["Format", "Drawing Limits"]).params("{min: [x,y], max: [x,y]}"),
CommandSpec::new("style", "Text Style...", run_style)
@@ -471,6 +471,39 @@ fn run_ltscale(s: &mut Session, p: &Value) -> Result<Value> {
Ok(json!({ "message": "Regenerating model." }))
}
/// Typed LTSCALE: "Enter new linetype scale factor <1.0000>:".
struct LtscaleM;
impl Interactive for LtscaleM {
fn name(&self) -> &'static str {
"LTSCALE"
}
fn prompt(&self, s: &Session) -> Prompt {
let cur = s.doc().map(|d| d.header.f64("LTSCALE", 1.0)).unwrap_or(1.0);
Prompt::new("Enter new linetype scale factor", Accept::NUMBER).default(format!("{cur:.4}"))
}
fn input(&mut self, s: &mut Session, i: Input) -> Result<Step> {
let text = match i {
Input::Enter | Input::Cancel => return Ok(Step::Done),
Input::Text(t) => t,
_ => return Ok(Step::Continue),
};
let scale = text.trim().parse::<f64>().ok().filter(|v| v.is_finite() && *v > 0.0);
match scale.map(|v| run_ltscale(s, &json!({ "scale": v }))) {
Some(Ok(r)) => {
if let Some(m) = r.get("message").and_then(Value::as_str) {
s.echo(m.to_string());
}
Ok(Step::Done)
}
_ => {
s.echo("Requires a positive number.");
Ok(Step::Continue)
}
}
}
}
fn run_units(s: &mut Session, p: &Value) -> Result<Value> {
let d = s.doc_mut()?;
for (k, lo, hi) in [("lunits", 1, 5), ("luprec", 0, 8), ("aunits", 0, 4), ("auprec", 0, 8), ("insunits", 0, 24)] {
+19
View File
@@ -1274,6 +1274,25 @@ fn bare_variable_name_runs_setvar() {
assert!(s.cmdline("-pickadd").is_err());
}
#[test]
fn typed_ltscale_asks_for_the_scale() {
let mut s = Session::new();
s.execute("setvar", &json!({"name": "LTSCALE", "value": 2.5})).unwrap();
s.cmdline("ltscale").unwrap();
assert!(s.prompt_text().contains("Enter new linetype scale factor <2.5000>"), "{}", s.prompt_text());
// A scale that is not positive is refused and asked again.
s.cmdline("0").unwrap();
assert!(s.current_prompt().is_some(), "asked again");
s.cmdline("0.1").unwrap();
assert!(s.current_prompt().is_none());
assert_eq!(s.doc().unwrap().header.f64("LTSCALE", 0.0), 0.1);
// Enter keeps the current scale.
s.cmdline("lts").unwrap();
s.cmdline("").unwrap();
assert!(s.current_prompt().is_none());
assert_eq!(s.doc().unwrap().header.f64("LTSCALE", 0.0), 0.1);
}
fn setvar_prompt_open(s: &Session) -> bool {
s.running.as_ref().is_some_and(|r| r.id == "setvar")
}