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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#![deny(missing_docs,
missing_debug_implementations, missing_copy_implementations,
trivial_casts, trivial_numeric_casts,
unstable_features, unsafe_code,
unused_import_braces, unused_qualifications)]
use std::{
fs,
env::{temp_dir, var},
path::{Path, PathBuf},
sync::atomic::{AtomicUsize, Ordering},
process::Command
};
pub mod error;
use error::ScrawlError as ScrawlError;
pub fn new() -> Result<String, ScrawlError> {
let temp_file = create_temp_file()?;
open_editor(&temp_file).and_then(|output| {
let _ = fs::remove_file(temp_file);
Ok(output)
})
}
pub fn with(content: &str) -> Result<String, ScrawlError> {
let temp_file = create_temp_file()?;
fs::write(&temp_file, content).map_err(|_| {
ScrawlError::FailedToCreateTempfile
})?;
open_editor(&temp_file).and_then(|output| {
let _ = fs::remove_file(temp_file);
Ok(output)
})
}
pub fn open(p: &Path) -> Result<String, ScrawlError> {
let temp_file = create_temp_file()?;
fs::copy(p, &temp_file).map_err(|_| {
let p = p.to_str().unwrap_or("<unknown>");
ScrawlError::FailedToCopyToTempFile(String::from(p))
})?;
open_editor(&temp_file).and_then(|output| {
let _ = fs::remove_file(temp_file);
Ok(output)
})
}
pub fn edit(p: &Path) -> Result<String, ScrawlError> {
open_editor(p)
}
fn get_editor_name() -> Result<String, ScrawlError> {
match var("EDITOR") {
Ok(s) => Ok(s),
_ => Err(ScrawlError::EditorNotFound)
}
}
const PREFIX: &str = "xvrqt_scrawl";
static TEMP_FILE_COUNT: AtomicUsize = AtomicUsize::new(0);
fn create_temp_file() -> Result<PathBuf, ScrawlError> {
let i = TEMP_FILE_COUNT.fetch_add(1, Ordering::SeqCst);
let process_id = std::process::id();
let temp_file = format!("{}_{}_{}", PREFIX, process_id, i);
let mut temp_dir = temp_dir();
temp_dir.push(temp_file);
match fs::File::create(&temp_dir) {
Err(_) => Err(ScrawlError::FailedToCreateTempfile),
_ => Ok(temp_dir)
}
}
fn open_editor(path: &Path) -> Result<String, ScrawlError> {
let editor_name = get_editor_name()?;
match Command::new(&editor_name)
.arg(&path)
.status() {
Ok(status) if status.success() => {
fs::read_to_string(path).map_err(|_| {
ScrawlError::FailedToReadIntoString
})
},
_ => Err(ScrawlError::FailedToOpenEditor(editor_name))
}
}