diff options
| author | 魏曹先生 <1992414357@qq.com> | 2026-04-17 00:00:04 +0800 |
|---|---|---|
| committer | 魏曹先生 <1992414357@qq.com> | 2026-04-17 00:00:04 +0800 |
| commit | 0816230e8712948df451fee7aee48537efe043cb (patch) | |
| tree | 749d581dfd8f3e6c7e535108eb16803102466774 /src/edit.rs | |
| parent | 6e36fc3707e791c3c748133d648957706b54fd3a (diff) | |
Add edit command with table serialization support
Diffstat (limited to 'src/edit.rs')
| -rw-r--r-- | src/edit.rs | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/src/edit.rs b/src/edit.rs new file mode 100644 index 0000000..f9c751c --- /dev/null +++ b/src/edit.rs @@ -0,0 +1,55 @@ +use std::process::Command; + +pub 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 + std::fs::write(cache_path, default_content)?; + + // Open editor with cache file + let status = Command::new(editor).arg(cache_path).status()?; + + 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 = std::fs::read_to_string(cache_path)?; + + // 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 _ = std::fs::remove_file(cache_path); + + Ok(processed_content) +} + +pub fn get_default_editor() -> String { + if let Ok(editor) = std::env::var("EDITOR") { + return editor; + } + + "nano".to_string() +} |
