1use std::env;
12use std::process::Command;
13
14pub fn open(file: String, opts: Option<String>) {
16 let application = editor(opts);
17
18 Command::new(&application)
19 .arg(file)
20 .output()
21 .expect("failed to execute process");
22}
23
24fn default_editor() -> String {
26 if env::consts::OS == "windows" {
27 String::from("notepad")
28 } else {
29 String::from("vi")
30 }
31}
32
33fn editor(opts: Option<String>) -> String {
35 let visual = match env::var("VISUAL") {
36 Ok(env) => env,
37 Err(_) => String::new(),
38 };
39
40 let editor = match env::var("EDITOR") {
41 Ok(env) => env,
42 Err(_) => String::new(),
43 };
44
45 let default = default_editor();
46
47 if !opts.is_none() {
48 opts.unwrap()
49 } else if !visual.is_empty() {
50 visual
51 } else if !editor.is_empty() {
52 editor
53 } else {
54 default
55 }
56}
57
58#[cfg(test)]
59mod tests {
60 use super::*;
61
62 #[test]
63 #[cfg(target_family = "unix")]
64 fn test_default_unix() {
65 assert_eq!(default_editor(), String::from("vi"));
66 }
67
68 #[test]
69 #[cfg(target_family = "windows")]
70 fn test_default_windows() {
71 assert_eq!(default_editor(), String::from("notepad"));
72 }
73
74 #[test]
75 fn test_editor() {
76 assert_eq!(editor(Some(String::from("nano"))), String::from("nano"));
77 }
78
79 #[test]
80 fn test_editor_env() {
81 env::set_var("EDITOR", "nano");
82 assert_eq!(editor(None), String::from("nano"));
83 }
84}