blob: f9c751c92e62e79baa21cfd6fd6bf9f984beef9f (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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()
}
|