Skip to content

Commit c633c9d

Browse files
authored
fix(chat): Parse EDITOR env var to support arguments (#1209)
The /editor command now properly parses the EDITOR environment variable to handle cases where it contains arguments (like 'code -w'). This fixes issue #1208 where the command would fail when EDITOR contained spaces. 🤖 Assisted by [Amazon Q Developer](https://aws.amazon.com/q/developer)
1 parent cc66436 commit c633c9d

File tree

4 files changed

+20
-6
lines changed

4 files changed

+20
-6
lines changed

Cargo.lock

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/figterm/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ semver.workspace = true
5555
serde.workspace = true
5656
serde_json.workspace = true
5757
shell-color.workspace = true
58-
shell-words = "1.1"
5958
shellexpand = "3.1.0"
6059
shlex.workspace = true
6160
sysinfo.workspace = true

crates/figterm/src/pty/cmdbuilder.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ impl CommandBuilder {
221221
.ok_or_else(|| anyhow::anyhow!("argument cannot be represented as utf8"))?;
222222
strs.push(s);
223223
}
224-
Ok(shell_words::join(strs))
224+
shlex::try_join(strs).map_err(|e| anyhow::anyhow!("Failed to join command arguments: {}", e))
225225
}
226226
}
227227

crates/q_cli/src/cli/chat/mod.rs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -493,15 +493,31 @@ where
493493
let temp_file_path = temp_dir.join(file_name);
494494

495495
// Get the editor from environment variable or use a default
496-
let editor = env::var("EDITOR").unwrap_or_else(|_| "vi".to_string());
496+
let editor_cmd = env::var("EDITOR").unwrap_or_else(|_| "vi".to_string());
497+
498+
// Parse the editor command to handle arguments
499+
let mut parts =
500+
shlex::split(&editor_cmd).ok_or_else(|| ChatError::Custom("Failed to parse EDITOR command".into()))?;
501+
502+
if parts.is_empty() {
503+
return Err(ChatError::Custom("EDITOR environment variable is empty".into()));
504+
}
505+
506+
let editor_bin = parts.remove(0);
497507

498508
// Write initial content to the file if provided
499509
let initial_content = initial_text.unwrap_or_default();
500510
fs::write(&temp_file_path, &initial_content)
501511
.map_err(|e| ChatError::Custom(format!("Failed to create temporary file: {}", e).into()))?;
502512

503-
// Open the editor
504-
let status = ProcessCommand::new(editor)
513+
// Open the editor with the parsed command and arguments
514+
let mut cmd = ProcessCommand::new(editor_bin);
515+
// Add any arguments that were part of the EDITOR variable
516+
for arg in parts {
517+
cmd.arg(arg);
518+
}
519+
// Add the file path as the last argument
520+
let status = cmd
505521
.arg(&temp_file_path)
506522
.status()
507523
.map_err(|e| ChatError::Custom(format!("Failed to open editor: {}", e).into()))?;

0 commit comments

Comments
 (0)