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
use std::collections::BTreeMap;
use std::fs;
use std::path::PathBuf;
use serde::{Deserialize, Serialize};
/// A macro is a named sequence of commands to send.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Macro {
pub name: String,
pub description: String,
pub commands: Vec<MacroCommand>,
}
/// A single command within a macro.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MacroCommand {
/// The text to send.
pub text: String,
/// Delay in milliseconds before sending this command.
#[serde(default)]
pub delay_ms: u64,
}
/// Manages macros stored in a TOML file.
pub struct MacroManager {
macros: BTreeMap<String, Macro>,
file_path: Option<PathBuf>,
}
impl MacroManager {
pub fn new() -> Self {
let file_path = dirs::config_dir().map(|d| d.join("yapper").join("macros.toml"));
let mut mgr = Self {
macros: BTreeMap::new(),
file_path,
};
mgr.load();
mgr
}
/// Get all macros sorted by name.
pub fn list(&self) -> Vec<&Macro> {
self.macros.values().collect()
}
/// Get a macro by name.
pub fn get(&self, name: &str) -> Option<&Macro> {
self.macros.get(name)
}
/// Add or update a macro.
pub fn save_macro(&mut self, m: Macro) {
self.macros.insert(m.name.clone(), m);
self.save();
}
/// Remove a macro.
pub fn remove(&mut self, name: &str) {
self.macros.remove(name);
self.save();
}
/// Load macros from file.
fn load(&mut self) {
let path = match &self.file_path {
Some(p) => p,
None => return,
};
if !path.exists() {
// Create default example macros
self.create_defaults();
return;
}
if let Ok(content) = fs::read_to_string(path) {
if let Ok(macros) = toml::from_str::<BTreeMap<String, MacroFile>>(&content) {
for (name, mf) in macros {
self.macros.insert(
name.clone(),
Macro {
name,
description: mf.description,
commands: mf
.commands
.into_iter()
.map(|c| MacroCommand {
text: c.text,
delay_ms: c.delay_ms.unwrap_or(0),
})
.collect(),
},
);
}
}
}
}
/// Save macros to file.
fn save(&self) {
let path = match &self.file_path {
Some(p) => p,
None => return,
};
if let Some(parent) = path.parent() {
let _ = fs::create_dir_all(parent);
}
let mut map: BTreeMap<String, MacroFile> = BTreeMap::new();
for (name, m) in &self.macros {
map.insert(
name.clone(),
MacroFile {
description: m.description.clone(),
commands: m
.commands
.iter()
.map(|c| MacroCommandFile {
text: c.text.clone(),
delay_ms: if c.delay_ms > 0 {
Some(c.delay_ms)
} else {
None
},
})
.collect(),
},
);
}
if let Ok(content) = toml::to_string_pretty(&map) {
let _ = fs::write(path, content);
}
}
fn create_defaults(&mut self) {
self.macros.insert(
"reset".to_string(),
Macro {
name: "reset".to_string(),
description: "Send reset command".to_string(),
commands: vec![MacroCommand {
text: "reset".to_string(),
delay_ms: 0,
}],
},
);
self.macros.insert(
"version".to_string(),
Macro {
name: "version".to_string(),
description: "Query firmware version".to_string(),
commands: vec![MacroCommand {
text: "version".to_string(),
delay_ms: 0,
}],
},
);
self.save();
}
}
/// File format for macros (slightly different from in-memory for serde flexibility).
#[derive(Debug, Serialize, Deserialize)]
struct MacroFile {
description: String,
commands: Vec<MacroCommandFile>,
}
#[derive(Debug, Serialize, Deserialize)]
struct MacroCommandFile {
text: String,
delay_ms: Option<u64>,
}