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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
use crate::config::Config;
use crate::error::{Contextable, Error, Result};
use crate::path;
use crate::script::ScriptInfo;
use crate::script_type::{get_default_template, AsScriptFullTypeRef, ScriptType};
use chrono::{DateTime, Utc};
use colored::Color;
use std::borrow::Cow;
use std::ffi::OsStr;
use std::fs::{create_dir_all, remove_file, rename, File};
use std::io::{Read, Write};
use std::path::{Path, PathBuf};
use std::process::{Command, ExitStatus};
pub mod completion_util;
pub mod holder;
pub mod main_util;
pub mod serde;
pub mod shebang_handle;
pub mod init_repo;
pub use init_repo::*;
pub fn illegal_name(s: &str) -> bool {
s.starts_with('-')
|| s.starts_with('.')
|| s.contains("..")
|| s.contains(' ')
|| s.contains('@')
|| s.contains('!')
|| s.contains('?')
|| s.contains('=')
|| s.contains('/')
|| s.is_empty()
}
pub fn run_cmd(mut cmd: Command) -> Result<ExitStatus> {
let res = cmd.spawn();
let program = cmd.get_program();
let mut child = handle_fs_res(&[program], res)?;
handle_fs_res(&[program], child.wait())
}
#[cfg(not(target_os = "linux"))]
pub fn create_cmd(cmd_str: &str, args: &[impl AsRef<OsStr>]) -> Command {
log::debug!("在非 linux 上執行,用 sh -c 包一層");
let args: Vec<_> = args
.iter()
.map(|s| {
s.as_ref()
.to_str()
.unwrap()
.to_string()
.replace(r"\", r"\\\\")
})
.collect();
let arg = format!("{} {}", cmd_str, args.join(" "));
let mut cmd = Command::new("sh");
cmd.args(&["-c", &arg]);
cmd
}
#[cfg(target_os = "linux")]
pub fn create_cmd<I, S1, S2>(cmd_str: S2, args: I) -> Command
where
I: IntoIterator<Item = S1>,
S1: AsRef<OsStr>,
S2: AsRef<OsStr>,
{
let mut cmd = Command::new(&cmd_str);
cmd.args(args);
cmd
}
pub fn open_editor(path: &Path) -> Result {
let conf = Config::get();
let cmd = create_concat_cmd(&conf.editor, &[&path]);
let stat = run_cmd(cmd)?;
if !stat.success() {
let code = stat.code().unwrap_or_default();
return Err(Error::EditorError(code, conf.editor.clone()));
}
Ok(())
}
pub fn create_concat_cmd<'a, 'b, I1, S1, I2, S2>(arg1: I1, arg2: I2) -> Command
where
I1: IntoIterator<Item = &'a S1>,
I2: IntoIterator<Item = &'b S2>,
S1: AsRef<OsStr> + 'a,
S2: AsRef<OsStr> + 'b,
{
let mut arg1 = arg1.into_iter();
let cmd = arg1.next().unwrap();
let remaining = arg1
.map(|s| s.as_ref())
.chain(arg2.into_iter().map(|s| s.as_ref()));
create_cmd(cmd, remaining)
}
pub fn file_modify_time(path: &Path) -> Result<DateTime<Utc>> {
let meta = handle_fs_res(&[path], std::fs::metadata(path))?;
let modified = handle_fs_res(&[path], meta.modified())?;
Ok(modified.into())
}
pub fn read_file(path: &Path) -> Result<String> {
let mut file = handle_fs_res(&[path], File::open(path)).context("唯讀開啟檔案失敗")?;
let mut content = String::new();
handle_fs_res(&[path], file.read_to_string(&mut content)).context("讀取檔案失敗")?;
Ok(content)
}
pub fn write_file(path: &Path, content: &str) -> Result<()> {
let mut file = handle_fs_res(&[path], File::create(path))?;
handle_fs_res(&[path], file.write_all(content.as_bytes()))
}
pub fn remove(script_path: &Path) -> Result<()> {
handle_fs_res(&[&script_path], remove_file(&script_path))
}
pub fn mv(origin: &Path, new: &Path) -> Result<()> {
log::info!("修改 {:?} 為 {:?}", origin, new);
if let Some(parent) = new.parent() {
handle_fs_res(&[&new], create_dir_all(parent))?;
}
handle_fs_res(&[&new, &origin], rename(&origin, &new))
}
pub fn cp(origin: &Path, new: &Path) -> Result<()> {
if let Some(parent) = new.parent() {
handle_fs_res(&[parent], create_dir_all(parent))?;
}
let _copied = handle_fs_res(&[&origin, &new], std::fs::copy(&origin, &new))?;
Ok(())
}
pub fn handle_fs_err<P: AsRef<Path>>(path: &[P], err: std::io::Error) -> Error {
use std::sync::Arc;
let mut p = path.iter().map(|p| p.as_ref().to_owned()).collect();
log::warn!("檔案系統錯誤:{:?}, {:?}", p, err);
match err.kind() {
std::io::ErrorKind::PermissionDenied => Error::PermissionDenied(p),
std::io::ErrorKind::NotFound => Error::PathNotFound(p),
std::io::ErrorKind::AlreadyExists => Error::PathExist(p.remove(0)),
_ => Error::GeneralFS(p, Arc::new(err)),
}
}
pub fn handle_fs_res<T, P: AsRef<Path>>(path: &[P], res: std::io::Result<T>) -> Result<T> {
match res {
Ok(t) => Ok(t),
Err(e) => Err(handle_fs_err(path, e)),
}
}
pub fn get_or_create_template_path<T: AsScriptFullTypeRef>(
ty: &T,
force: bool,
check_subtype: bool,
) -> Result<(PathBuf, Option<&'static str>)> {
if !force {
Config::get().get_script_conf(ty.get_ty())?;
}
let tmpl_path = path::get_template_path(ty)?;
if !tmpl_path.exists() {
if check_subtype && ty.get_sub().is_some() {
return Err(Error::UnknownType(ty.display().to_string()));
}
let default_tmpl = get_default_template(ty);
return write_file(&tmpl_path, default_tmpl).map(|_| (tmpl_path, Some(default_tmpl)));
}
Ok((tmpl_path, None))
}
pub fn get_or_create_template<T: AsScriptFullTypeRef>(
ty: &T,
force: bool,
check_subtype: bool,
) -> Result<String> {
let (tmpl_path, default_tmpl) = get_or_create_template_path(ty, force, check_subtype)?;
if let Some(default_tmpl) = default_tmpl {
return Ok(default_tmpl.to_owned());
}
read_file(&tmpl_path)
}
pub fn to_display_args(arg: String) -> String {
let mut need_escape = false;
for ch in arg.chars() {
match ch {
' ' | '>' | '|' | '\'' | '#' | '<' | ';' | '(' | ')' | '{' | '}' | '$' => {
need_escape = true
}
_ => (),
}
}
let escaped: String = serde_json::to_string(&arg).unwrap();
if need_escape || arg != escaped[1..escaped.len() - 1] {
escaped
} else {
arg
}
}
fn relative_to_home(p: &Path) -> Option<&Path> {
const CUR_DIR: &str = ".";
let home = dirs::home_dir()?;
if p == home {
return Some(CUR_DIR.as_ref());
}
p.strip_prefix(&home).ok()
}
#[derive(Debug)]
pub enum PrepareRespond {
NoAfterProcess,
NoContent { is_new: bool, time: DateTime<Utc> },
}
pub fn prepare_script<T: AsRef<str>>(
path: &Path,
script: &ScriptInfo,
sub_type: Option<&ScriptType>,
no_template: bool,
content: &[T],
) -> Result<PrepareRespond> {
log::info!("開始準備 {} 腳本內容……", script.name);
let has_content = !content.is_empty();
let is_new = !path.exists();
if is_new {
let birthplace = path::normalize_path(".")?;
let birthplace_rel = relative_to_home(&birthplace);
let mut file = handle_fs_res(&[path], File::create(&path))?;
let content = content.iter().map(|s| s.as_ref().split('\n')).flatten();
if !no_template {
let content: Vec<_> = content.collect();
let info = json!({
"birthplace_in_home": birthplace_rel.is_some(),
"birthplace_rel": birthplace_rel,
"birthplace": birthplace,
"name": script.name.key().to_owned(),
"content": content,
});
log::debug!("編輯模版資訊:{:?}", info);
let template = get_or_create_template(&(&script.ty, sub_type), true, true)?;
handle_fs_res(&[path], write_prepare_script(file, &template, &info))?;
} else {
let mut first = true;
for line in content {
if !first {
writeln!(file, "")?;
}
first = false;
write!(file, "{}", line)?;
}
}
} else {
if has_content {
log::debug!("腳本已存在,往後接上給定的訊息");
let mut file = handle_fs_res(
&[path],
std::fs::OpenOptions::new()
.append(true)
.write(true)
.open(path),
)?;
for content in content.iter() {
handle_fs_res(&[path], writeln!(&mut file, "{}", content.as_ref()))?;
}
}
}
Ok(if has_content {
PrepareRespond::NoAfterProcess
} else {
PrepareRespond::NoContent {
is_new,
time: file_modify_time(path)?,
}
})
}
fn write_prepare_script<W: Write>(
w: W,
template: &str,
info: &serde_json::Value,
) -> std::io::Result<()> {
use handlebars::{Handlebars, TemplateRenderError};
let reg = Handlebars::new();
reg.render_template_to_write(&template, &info, w)
.map_err(|err| match err {
TemplateRenderError::IOError(err, ..) => err,
e => panic!("解析模版錯誤:{}", e),
})
}
pub struct DisplayType<'a> {
ty: &'a ScriptType,
color: Option<Color>,
}
impl<'a> DisplayType<'a> {
pub fn is_unknown(&self) -> bool {
self.color.is_none()
}
pub fn color(&self) -> Color {
self.color.unwrap_or(Color::BrightBlack)
}
pub fn display(&self) -> Cow<'a, str> {
if self.is_unknown() {
Cow::Owned(format!("{}, unknown", self.ty))
} else {
Cow::Borrowed(self.ty.as_ref())
}
}
}
pub fn get_display_type(ty: &ScriptType) -> DisplayType {
let conf = Config::get();
match conf.get_color(ty) {
Err(e) => {
log::warn!("取腳本顏色時出錯:{},視為未知類別", e);
DisplayType { ty, color: None }
}
Ok(c) => DisplayType { ty, color: Some(c) },
}
}
pub fn print_iter<T: std::fmt::Display>(iter: impl Iterator<Item = T>, sep: &str) -> bool {
let mut first = true;
for t in iter {
if !first {
print!("{}", sep);
}
first = false;
print!("{}", t);
}
!first
}
pub fn option_map_res<T, F: FnOnce(T) -> Result<T>>(opt: Option<T>, f: F) -> Result<Option<T>> {
Ok(match opt {
Some(t) => Some(f(t)?),
None => None,
})
}
pub fn hijack_ctrlc_once() {
use std::sync::Once;
static CTRLC_HANDLE: Once = Once::new();
log::debug!("劫持 ctrl-c 回調");
CTRLC_HANDLE.call_once(|| {
let res = ctrlc::set_handler(|| {});
if res.is_err() {
log::warn!("設置 ctrl-c 回調失敗 {:?}", res);
}
});
}