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
use crate::Templates::*;
use std::io::Write;
use std::path::Path;
use std::fs::create_dir;
pub fn const_conv(main: &str, structure: &str)-> (String, String) {
(main.to_string(), structure.to_string())
}
pub fn load(template: &str) -> (String, String) {
match template {
"Basic" => const_conv(basic::BASIC_MAIN, basic::BASIC_STRUCTURE),
"Code" => const_conv(code::CODE_MAIN, code::CODE_STRUCTURE),
"Math" => const_conv(math::MATH_MAIN, math::MATH_STRUCTURE),
"Theatre" => const_conv(theatre::THEATRE_MAIN, theatre::THEATRE_STRUCTURE),
"Novel" => const_conv(novel::NOVEL_MAIN, novel::NOVEL_STRUCTURE),
"Beamer" => const_conv(beamer::BEAMER_MAIN, beamer::BEAMER_STRUCTURE),
"Lachaise" => const_conv(lachaise::LACHAISE_MAIN, lachaise::LACHAISE_STRUCTURE),
_ => panic!("Unknown template: {}", template),
}
}
pub fn create(name: &str, file_path: &Option<String>, template: &str) {
let file_path = match file_path{
Some(path) => path,
None => "."
};
println!("Loading {} template...", template);
let (main, structure) = load(template);
let path = format!("{}/{}", file_path, name);
let def_path = format!("{}/{}_texcreate", file_path, name);
let mut p = String::new();
if Path::new(&path).exists(){
let f = format!("Note: {} exists, creating {} instead\n", &path, &def_path);
println!("{}",f);
match create_dir(&def_path){
Ok(dir) => dir,
Err(e) => eprintln!("{}", e)
};
p = def_path;
} else {
println!("Creating {}", &path);
match create_dir(&path){
Ok(dir) => dir,
Err(e) => eprintln!("{}", e)
};
p = path;
}
println!("Creating {}/{}.tex", &p, name);
let mut main_file =
std::fs::File::create(format!("{}/{}.tex", &p, name)).expect("Couldn't create file");
match main_file.write_all(main.as_bytes()){
Ok(write) => write,
Err(e) => eprintln!("{}", e)
};
println!("Creating {}/structure.tex", &p);
let mut structure_file =
std::fs::File::create(format!("{}/structure.tex", &p)).expect("Couldn't create file");
match structure_file.write_all(structure.as_bytes()){
Ok(write) => write,
Err(e) => eprintln!("{}", e)
};
}