Skip to content

PdfCraft: An open-source PDF reader and editor written by scratch in pure Rust

fix: print: read lpstat untranslated so printers show on non-English macOS (#101)

Jakub6c8983754163
diff --git a/crates/print/src/spool.rs b/crates/print/src/spool.rs
index c744579d2..c9c0fcbf3 100644
--- a/crates/print/src/spool.rs
+++ b/crates/print/src/spool.rs
@@ -70,11 +70,21 @@ pub fn lp_args(job: &Job, file: &str) -> Vec<String> {
a
}
+/// `lpstat -p -d`, forced to print untranslated messages so [`parse_lpstat`] can read them.
+///
+/// `LC_ALL`/`LANG=C` is enough on Linux. macOS CUPS ignores them and follows the user's
+/// interface language (`AppleLanguages`) unless `SOFTWARE` is set, in which case it uses `LANG`.
+pub fn lpstat_command() -> std::process::Command {
+ let mut c = std::process::Command::new("lpstat");
+ c.args(["-p", "-d"]).env("LC_ALL", "C").env("LANG", "C").env("SOFTWARE", "PdfCraft");
+ c
+}
+
/// The printers the system knows (empty when there are none or no spooler).
pub fn printers() -> Vec<Printer> {
#[cfg(all(unix, not(target_arch = "wasm32")))]
{
- match std::process::Command::new("lpstat").args(["-p", "-d"]).output() {
+ match lpstat_command().output() {
Ok(o) => parse_lpstat(&String::from_utf8_lossy(&o.stdout)),
Err(_) => Vec::new(),
}
diff --git a/crates/print/src/tests.rs b/crates/print/src/tests.rs
index 9ef4ab7db..ee4a4a6fb 100644
--- a/crates/print/src/tests.rs
+++ b/crates/print/src/tests.rs
@@ -191,3 +191,16 @@ fn spooler_arguments_and_printer_list() {
);
assert_eq!(lp_args(&Job::default(), "f.pdf")[0], "-n", "no -d: the default printer");
}
+
+#[test]
+fn lpstat_output_is_untranslated() {
+ // A localized lpstat (here Polish) is unreadable to parse_lpstat...
+ assert!(parse_lpstat("drukarka Office_Laser jest bezczynna.\ndomyślny cel systemowy: Office_Laser\n").is_empty());
+ // ...so the command must force the C locale, including the SOFTWARE switch macOS CUPS needs.
+ let cmd = spool::lpstat_command();
+ let envs: Vec<_> = cmd.get_envs().map(|(k, v)| (k.to_string_lossy().into_owned(), v.map(|v| v.to_string_lossy().into_owned()))).collect();
+ for key in ["LC_ALL", "LANG"] {
+ assert!(envs.contains(&(key.into(), Some("C".into()))), "{key}=C missing: {envs:?}");
+ }
+ assert!(envs.iter().any(|(k, v)| k == "SOFTWARE" && v.as_deref().is_some_and(|v| !v.is_empty())), "SOFTWARE missing: {envs:?}");
+}