diff options
| author | Ogulcan Celik <ogulcancelik@gmail.com> | 2026-07-02 01:16:21 +0300 |
|---|---|---|
| committer | Ogulcan Celik <ogulcancelik@gmail.com> | 2026-07-02 01:16:21 +0300 |
| commit | a9111cbab2a7550c7e84ec94c8b388de9a44daa3 (patch) | |
| tree | ed60b5baf7fd5d138aa5ab7d7d1b8892d3f41b73 /src/platform | |
| parent | 6a5b431e39c3a77652dc22e692925027cb69b965 (diff) | |
fix: open scrollback editor on windows without sh
refs #914
Diffstat (limited to 'src/platform')
| -rw-r--r-- | src/platform/fallback.rs | 8 | ||||
| -rw-r--r-- | src/platform/linux.rs | 35 | ||||
| -rw-r--r-- | src/platform/macos.rs | 35 | ||||
| -rw-r--r-- | src/platform/windows.rs | 60 |
4 files changed, 138 insertions, 0 deletions
diff --git a/src/platform/fallback.rs b/src/platform/fallback.rs index 25bc761c..e9134a8b 100644 --- a/src/platform/fallback.rs +++ b/src/platform/fallback.rs @@ -7,6 +7,14 @@ use super::{ClipboardImage, ForegroundJob, Signal}; pub fn raise_server_nofile_limit() {} /// Unsupported platform stub. +pub(crate) fn scrollback_editor_argv(_path: &std::path::Path) -> std::io::Result<Vec<String>> { + Err(std::io::Error::new( + std::io::ErrorKind::Unsupported, + "opening scrollback in an editor is not supported on this platform", + )) +} + +/// Unsupported platform stub. pub fn detach_server_daemon_command(_command: &mut Command) {} /// Unsupported platform stub. diff --git a/src/platform/linux.rs b/src/platform/linux.rs index 3cf643e5..539c0d93 100644 --- a/src/platform/linux.rs +++ b/src/platform/linux.rs @@ -12,6 +12,30 @@ use super::{ pub fn raise_server_nofile_limit() {} +pub(crate) fn scrollback_editor_argv(path: &std::path::Path) -> std::io::Result<Vec<String>> { + let quoted_path = shell_quote(&path.display().to_string()); + let command = format!( + r#"scrollback_file={quoted_path}; eval "${{EDITOR:-vi}} \"\$scrollback_file\""; status=$?; rm -f "$scrollback_file"; exit $status"# + ); + Ok(vec!["/bin/sh".to_string(), "-c".to_string(), command]) +} + +fn shell_quote(value: &str) -> String { + if !value.is_empty() + && value.chars().all(|ch| { + ch.is_ascii_alphanumeric() + || matches!( + ch, + '@' | '%' | '_' | '+' | '=' | ':' | ',' | '.' | '/' | '-' + ) + }) + { + return value.to_string(); + } + + format!("'{}'", value.replace('\'', "'\\''")) +} + /// Collect the foreground terminal job for a given child PID. pub fn foreground_job(child_pid: u32) -> Option<ForegroundJob> { let tpgid = foreground_process_group_id(child_pid)?; @@ -794,4 +818,15 @@ mod tests { let _ = std::fs::remove_file(&path); assert_eq!(args, "--\n-danger\nbody\n"); } + + #[test] + fn scrollback_editor_argv_preserves_unix_editor_shell_semantics() { + let path = std::path::Path::new("/tmp/herdr scrollback.txt"); + let argv = scrollback_editor_argv(path).unwrap(); + + assert_eq!(argv[0], "/bin/sh"); + assert_eq!(argv[1], "-c"); + assert!(argv[2].contains("EDITOR:-vi")); + assert!(argv[2].contains("/tmp/herdr scrollback.txt")); + } } diff --git a/src/platform/macos.rs b/src/platform/macos.rs index df957652..3770a3be 100644 --- a/src/platform/macos.rs +++ b/src/platform/macos.rs @@ -16,6 +16,30 @@ const PROC_PGRP_ONLY: u32 = 2; const SERVER_NOFILE_LIMIT_TARGET: libc::rlim_t = 8192; const CF_STRING_ENCODING_UTF8: u32 = 0x0800_0100; +pub(crate) fn scrollback_editor_argv(path: &Path) -> std::io::Result<Vec<String>> { + let quoted_path = shell_quote(&path.display().to_string()); + let command = format!( + r#"scrollback_file={quoted_path}; eval "${{EDITOR:-vi}} \"\$scrollback_file\""; status=$?; rm -f "$scrollback_file"; exit $status"# + ); + Ok(vec!["/bin/sh".to_string(), "-c".to_string(), command]) +} + +fn shell_quote(value: &str) -> String { + if !value.is_empty() + && value.chars().all(|ch| { + ch.is_ascii_alphanumeric() + || matches!( + ch, + '@' | '%' | '_' | '+' | '=' | ':' | ',' | '.' | '/' | '-' + ) + }) + { + return value.to_string(); + } + + format!("'{}'", value.replace('\'', "'\\''")) +} + #[repr(C)] struct TisInputSource { _private: [u8; 0], @@ -1117,4 +1141,15 @@ printf '%s\n' "$@" > "$HERDR_NOTIFY_ARGS" "-e\non run argv\n-e\ndisplay notification (item 2 of argv) with title (item 1 of argv)\n-e\nend run\ntitle\nbody\n" ); } + + #[test] + fn scrollback_editor_argv_preserves_unix_editor_shell_semantics() { + let path = std::path::Path::new("/tmp/herdr scrollback.txt"); + let argv = scrollback_editor_argv(path).unwrap(); + + assert_eq!(argv[0], "/bin/sh"); + assert_eq!(argv[1], "-c"); + assert!(argv[2].contains("EDITOR:-vi")); + assert!(argv[2].contains("/tmp/herdr scrollback.txt")); + } } diff --git a/src/platform/windows.rs b/src/platform/windows.rs index e7a77e35..50e0e905 100644 --- a/src/platform/windows.rs +++ b/src/platform/windows.rs @@ -46,6 +46,41 @@ struct WindowsProcessEntry { pub fn raise_server_nofile_limit() {} +pub(crate) fn scrollback_editor_argv(path: &std::path::Path) -> std::io::Result<Vec<String>> { + let editor = std::env::var("VISUAL") + .ok() + .filter(|value| !value.trim().is_empty()) + .or_else(|| { + std::env::var("EDITOR") + .ok() + .filter(|value| !value.trim().is_empty()) + }); + scrollback_editor_argv_with_env(path, editor.as_deref()) +} + +fn scrollback_editor_argv_with_env( + path: &std::path::Path, + editor: Option<&str>, +) -> std::io::Result<Vec<String>> { + let mut argv = match editor.filter(|value| !value.trim().is_empty()) { + Some(editor) => command_line_to_argv(editor).ok_or_else(|| { + std::io::Error::new( + std::io::ErrorKind::InvalidInput, + format!("failed to parse editor command {editor:?}"), + ) + })?, + None => vec!["notepad.exe".to_string()], + }; + if argv.is_empty() { + return Err(std::io::Error::new( + std::io::ErrorKind::InvalidInput, + "editor command must not be empty", + )); + } + argv.push(path.display().to_string()); + Ok(argv) +} + pub fn detach_server_daemon_command(_command: &mut std::process::Command) {} pub fn current_process_is_detached_server_daemon() -> bool { @@ -698,6 +733,31 @@ mod tests { assert_eq!(pids, vec![10, 20, 30]); } + #[test] + fn scrollback_editor_argv_uses_editor_env_and_appends_path() { + let path = std::path::Path::new(r"C:\Users\User\AppData\Local\Temp\herdr scrollback.txt"); + let argv = super::scrollback_editor_argv_with_env( + path, + Some(r#""C:\Program Files\Microsoft VS Code\Code.exe" --wait"#), + ) + .unwrap(); + + assert_eq!(argv[0], r"C:\Program Files\Microsoft VS Code\Code.exe"); + assert_eq!(argv[1], "--wait"); + assert_eq!(argv[2], path.display().to_string()); + } + + #[test] + fn scrollback_editor_argv_falls_back_to_notepad() { + let path = std::path::Path::new(r"C:\Temp\herdr-scrollback.txt"); + let argv = super::scrollback_editor_argv_with_env(path, None).unwrap(); + + assert_eq!( + argv, + vec!["notepad.exe".to_string(), path.display().to_string()] + ); + } + fn test_entry( pid: u32, parent_pid: u32, |
