diff options
| author | 魏曹先生 <1992414357@qq.com> | 2026-03-12 15:54:59 +0800 |
|---|---|---|
| committer | 魏曹先生 <1992414357@qq.com> | 2026-03-12 15:54:59 +0800 |
| commit | 9d812580557cdc343378816cd65678b8aa75d944 (patch) | |
| tree | b1a3397e38d9620a487aed409fc94310f101bc27 /utils/src/input/editor.rs | |
| parent | 0a95bae451c1847f4f0b9601e60959f4e8e6b669 (diff) | |
Add lang field to command context and reorganize utils modules
Diffstat (limited to 'utils/src/input/editor.rs')
| -rw-r--r-- | utils/src/input/editor.rs | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/utils/src/input/editor.rs b/utils/src/input/editor.rs new file mode 100644 index 0000000..34377fd --- /dev/null +++ b/utils/src/input/editor.rs @@ -0,0 +1,66 @@ +use tokio::{fs, process::Command}; + +use crate::env::editor::get_default_editor; + +/// Input text using the system editor +/// Opens the system editor (from EDITOR environment variable) with default text in a cache file, +/// then reads back the modified content after the editor closes, removing comment lines +pub async fn input_with_editor( + default_text: impl AsRef<str>, + cache_file: impl AsRef<std::path::Path>, + comment_char: impl AsRef<str>, +) -> Result<String, std::io::Error> { + input_with_editor_cutsom( + default_text, + cache_file, + comment_char, + get_default_editor().await, + ) + .await +} + +pub async fn input_with_editor_cutsom( + default_text: impl AsRef<str>, + cache_file: impl AsRef<std::path::Path>, + comment_char: impl AsRef<str>, + editor: String, +) -> Result<String, std::io::Error> { + let cache_path = cache_file.as_ref(); + let default_content = default_text.as_ref(); + let comment_prefix = comment_char.as_ref(); + + // Write default text to cache file + fs::write(cache_path, default_content).await?; + + // Open editor with cache file + let status = Command::new(editor).arg(cache_path).status().await?; + + if !status.success() { + return Err(std::io::Error::new( + std::io::ErrorKind::Other, + "Editor exited with non-zero status", + )); + } + + // Read the modified content + let content = fs::read_to_string(cache_path).await?; + + // Remove comment lines and trim + let processed_content: String = content + .lines() + .filter_map(|line| { + let trimmed = line.trim(); + if trimmed.starts_with(comment_prefix) { + None + } else { + Some(line) + } + }) + .collect::<Vec<&str>>() + .join("\n"); + + // Delete the cache file + let _ = fs::remove_file(cache_path).await; + + Ok(processed_content) +} |
