-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnem.rs
240 lines (212 loc) · 6.84 KB
/
nem.rs
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
use std::env;
use std::fs;
use std::process;
use std::str;
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize, Debug)]
struct NemFile {
version: String,
#[serde(rename = "cmds")]
entries: Vec<Entry>,
#[serde(skip)]
file_name: String,
}
impl NemFile {
fn codes(&self) -> Vec<&String> {
self.entries.iter().map(|c| &c.code).collect()
}
fn sort(&mut self) {
self.entries.sort_by(|a, b| a.cmd.cmp(&b.cmd))
}
fn add_entry(&mut self, entry: Entry) {
self.entries.push(entry);
}
fn rm_entry(&mut self, code: &String) -> Option<Entry> {
self.entries
.iter()
.position(|c| c.code == *code)
.map(|index| self.entries.remove(index))
}
fn find_entry_by_code(&self, code: &String) -> Option<&Entry> {
self.entries.iter().find(|c| c.code == *code)
}
fn find_entry_by_code_mut(&mut self, code: &String) -> Option<&mut Entry> {
self.entries.iter_mut().find(|c| c.code == *code)
}
fn edit_code(&mut self, old_code: &String, new_code: &String) -> Option<&Entry> {
self.find_entry_by_code_mut(old_code).map(|entry| {
entry.code = new_code.clone();
&*entry
})
}
fn write_to_file(&self) {
let s = toml::to_string(&self).unwrap();
fs::write(&self.file_name, s).expect("unable to write to file");
}
}
#[derive(Serialize, Deserialize, Debug)]
struct Entry {
cmd: String,
code: String,
desc: String,
}
#[derive(Debug)]
struct NemFiles {
nem_files: Vec<NemFile>,
}
impl NemFiles {
fn cur_nem_file_mut(&mut self) -> &mut NemFile {
&mut self.nem_files[0]
}
fn codes(&self) -> Vec<&String> {
self.nem_files.iter().flat_map(NemFile::codes).collect()
}
fn sort(&mut self) {
for nem_file in &mut self.nem_files {
nem_file.sort();
}
}
fn add_entry(&mut self, entry: Entry) {
let nem_file = self.cur_nem_file_mut();
nem_file.add_entry(entry);
}
fn rm_entry(&mut self, code: &String) -> Option<Entry> {
self.nem_files
.iter_mut()
.find(|nf| nf.find_entry_by_code(code).is_some())
.and_then(|nf| nf.rm_entry(code))
}
fn find_entry_by_code(&self, code: &String) -> Option<&Entry> {
self.nem_files
.iter()
.filter_map(|nf| nf.find_entry_by_code(code))
.next()
}
fn edit_code(&mut self, old_code: &String, new_code: &String) -> Option<&Entry> {
self.nem_files
.iter_mut()
.find(|nf| nf.find_entry_by_code(old_code).is_some())
.and_then(|nf| nf.edit_code(old_code, new_code))
}
fn write(&mut self) {
for nem_file in &self.nem_files {
nem_file.write_to_file()
}
}
}
fn mnemonic(v: &[String], existing: Vec<&String>) -> String {
let mut n = v
.iter()
.filter_map(|w| w.chars().filter(|c| *c != '-').next())
.collect();
while existing.iter().any(|s| **s == n) {
n = n + "1";
}
n
}
fn main() {
let mut args: Vec<_> = env::args().collect();
let mut nem_files = NemFiles {
nem_files: Vec::new(),
};
let mut path_buf = env::current_dir().expect("couldn't get CWD");
loop {
path_buf.push(".nem.toml");
if path_buf.exists() {
match fs::read_to_string(&path_buf) {
Ok(contents) => {
let mut nem_file: NemFile =
toml::from_str(&contents).expect("couldn't parse toml file");
nem_file.file_name = String::from(path_buf.to_str().unwrap());
nem_file.sort();
nem_files.nem_files.push(nem_file);
}
Err(err) => {
eprintln!("{}", err);
}
}
}
path_buf.pop();
if !path_buf.pop() {
break;
}
}
if args.len() < 2 {
args.push("/cl".to_string());
}
match args[1].as_str() {
"/cc" | "/command-create" => {
if args.len() < 3 {
eprintln!("please specify a command to create");
process::exit(1);
}
let nem = mnemonic(&args[2..], nem_files.codes());
nem_files.add_entry(Entry {
cmd: args[2..].join(" "),
code: nem,
desc: "".to_string(),
});
}
"/ce" | "/command-edit" => {
if args.len() != 4 {
eprintln!("unexpected arguments. expected <existing code> <replacement code>");
process::exit(1);
}
if let Some(entry) = nem_files.find_entry_by_code(&args[3]) {
eprintln!(
"collision: code '{}' already exists for `{}`",
&args[3], entry.cmd
);
process::exit(1);
} else {
let entry = nem_files.edit_code(&args[2], &args[3]).unwrap();
println!(
"edited code for `{}` from '{}' to '{}'",
&entry.cmd, &args[2], &entry.code
);
}
}
"/cl" | "/command-list" => {
for nem_file in nem_files.nem_files.iter().rev() {
println!("file: {}", &nem_file.file_name);
for entry in &nem_file.entries {
println!("\t{}\t{}", entry.code, entry.cmd);
}
}
}
"/cr" | "/command-remove" => {
if args.len() != 3 {
eprintln!("unexpected number of arguments. expected <code to remove>");
process::exit(1);
}
if let Some(entry) = nem_files.rm_entry(&args[2]) {
println!("removed command `{}` for code '{}'", entry.cmd, &args[2]);
} else {
eprintln!("no command for code '{}' found", &args[2]);
process::exit(1);
}
}
_ => {
let cmd = nem_files
.find_entry_by_code(&args[1])
.map(|cmd| cmd.cmd.split(" ").collect::<Vec<&str>>())
.unwrap_or_else(|| vec![&args[0], "/cl"]);
let exec = process::Command::new("sh")
.arg("-c")
.arg(format!("which {}", cmd[0]))
.output()
.expect("failed to look up command");
let location = str::from_utf8(&exec.stdout)
.expect("couldn't convert to string")
.trim();
let _ = process::Command::new(&location)
.args(&cmd[1..])
.args(&args[2..])
.spawn()
.expect("failed to execute command")
.wait();
}
}
nem_files.sort();
nem_files.write()
}