Improve handling of pe tool toml files

This commit is contained in:
2Shirt 2025-05-05 21:15:50 -07:00
parent 15b5d5e131
commit 2aede33db6
Signed by: 2Shirt
GPG key ID: 152FAC923B0E132C

View file

@ -76,7 +76,7 @@ impl App {
let disk_list_arc = Arc::new(Mutex::new(Vec::new()));
let tasks = Tasks::new(action_tx.clone(), disk_list_arc.clone());
let mut list = StatefulList::default();
list.set_items(load_tools());
list.set_items(load_tools(action_tx.clone()));
Ok(Self {
// TUI
action_rx,
@ -441,21 +441,45 @@ fn build_right_items(app: &App) -> Action {
Action::UpdateRight(labels, start_index, items)
}
pub fn load_tools() -> Vec<Tool> {
pub fn load_tools(action_tx: mpsc::UnboundedSender<Action>) -> Vec<Tool> {
let mut entries: Vec<PathBuf>;
let mut tools: Vec<Tool> = Vec::new();
let exe_path = env::current_exe().expect("Failed to find main executable");
let tool_config_path = exe_path.parent().unwrap().join("menu_entries");
let mut entries: Vec<PathBuf> = std::fs::read_dir(tool_config_path)
.expect("Failed to find any tool configs")
.map(|res| res.map(|e| e.path()))
.filter_map(Result::ok)
.collect();
entries.sort();
entries
.iter()
.map(|entry| {
let contents = fs::read_to_string(entry).expect("Failed to read tool config file");
let tool: Tool = toml::from_str(&contents).expect("Failed to parse tool config file");
tool
})
.collect()
if let Ok(read_dir) = std::fs::read_dir(tool_config_path) {
entries = read_dir
.map(|res| res.map(|e| e.path()))
.filter_map(Result::ok)
.collect();
entries.sort();
} else {
action_tx
.send(Action::Error(String::from(
"Failed to find any tool configs",
)))
.unwrap();
entries = Vec::new();
}
entries.iter().for_each(|entry| {
if let Ok(toml_str) = fs::read_to_string(entry) {
if let Ok(tool) = toml::from_str(&toml_str) {
tools.push(tool);
} else {
action_tx
.send(Action::Error(format!(
"Failed to parse tool config file: {:?}",
&entry,
)))
.unwrap();
}
} else {
action_tx
.send(Action::Error(format!(
"Failed to read tool config file: {:?}",
&entry,
)))
.unwrap();
}
});
tools
}