From f452f7196f6d092ab20814ce6dec4a1faf0c3ac0 Mon Sep 17 00:00:00 2001 From: iff Date: Sun, 6 Apr 2025 16:34:13 +0200 Subject: [PATCH] fix: extract inline environment variables --- CHANGELOG.md | 1 + core/src/shell.rs | 19 +++++++++++++++++++ core/src/suggestions.rs | 10 ++++++++++ 3 files changed, 30 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6e7caa9..56b14f1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed +- Parsing command environment variables (e.g. `LANG=ja_JP.UTF-8 pacman` will work as intended) - Not getting `command-not-found`'s output as it goes into `stderr` ## [0.7.0] - 2025-04-05 diff --git a/core/src/shell.rs b/core/src/shell.rs index 07a9d15..458d733 100644 --- a/core/src/shell.rs +++ b/core/src/shell.rs @@ -43,6 +43,7 @@ impl Init { pub struct Data { pub shell: String, + pub env: Option, pub command: String, pub suggest: Option, pub candidates: Vec, @@ -151,6 +152,7 @@ impl Data { let mut init = Data { shell, + env: None, command, suggest: None, candidates: vec![], @@ -165,6 +167,7 @@ impl Data { }; init.split(); + init.extract_env(); init.expand_command(); if init.mode != Mode::Cnf { init.update_error(None); @@ -174,6 +177,7 @@ impl Data { { eprintln!("/// data initialization"); eprintln!("shell: {}", init.shell); + eprintln!("env: {:?}", init.env); eprintln!("command: {}", init.command); eprintln!("error: {}", init.error); eprintln!("modules: {:?}", init.modules); @@ -218,6 +222,21 @@ impl Data { self.split = split; } + pub fn extract_env(&mut self) { + let mut envs = vec![]; + loop { + if self.split[0][1..].contains("=") { + envs.push(self.split.remove(0)); + } else { + break; + } + } + if !envs.is_empty() { + self.env = Some(envs.join(" ")); + self.command = self.split.join(" "); + } + } + pub fn update_error(&mut self, error: Option) { if let Some(error) = error { self.error = error diff --git a/core/src/suggestions.rs b/core/src/suggestions.rs index ffbf689..026bc36 100644 --- a/core/src/suggestions.rs +++ b/core/src/suggestions.rs @@ -164,6 +164,11 @@ pub fn confirm_suggestion(data: &Data) -> Result<(), String> { pub fn run_suggestion(data: &Data, command: &str) -> std::process::ExitStatus { let shell = &data.shell; let privilege = &data.privilege; + let command = if let Some(env) = &data.env { + format!("{env} {command}") + } else { + command.to_string() + }; match privilege { Some(sudo) => std::process::Command::new(sudo) .arg(shell) @@ -186,6 +191,11 @@ pub fn run_suggestion(data: &Data, command: &str) -> std::process::ExitStatus { fn suggestion_err(data: &Data, command: &str) -> Result<(), String> { let shell = &data.shell; let privilege = &data.privilege; + let command = if let Some(env) = &data.env { + format!("{env} {command}") + } else { + command.to_string() + }; let process = match privilege { Some(sudo) => std::process::Command::new(sudo) .arg(shell)