Allow loading arg-specified paths without needing to ask

This commit is contained in:
Olivier 'reivilibre' 2021-11-28 14:00:12 +00:00
parent a0f85a4fdf
commit a1208eed48
1 changed files with 15 additions and 8 deletions

View File

@ -6,7 +6,10 @@ use eframe::egui::{CentralPanel, CtxRef};
use eframe::epi::{App, Frame, Storage}; use eframe::epi::{App, Frame, Storage};
use eframe::NativeOptions; use eframe::NativeOptions;
use env_logger::Env; use env_logger::Env;
use log::info;
use std::fs::File; use std::fs::File;
use std::path::PathBuf;
use std::str::FromStr;
pub mod config; pub mod config;
pub mod graph; pub mod graph;
@ -45,13 +48,15 @@ impl App for MetricsGui {
fn main() -> anyhow::Result<()> { fn main() -> anyhow::Result<()> {
env_logger::Builder::from_env(Env::default().default_filter_or("info")).init(); env_logger::Builder::from_env(Env::default().default_filter_or("info")).init();
let path = rfd::FileDialog::new() let path = if let Some(path_arg) = &std::env::args().skip(1).next() {
PathBuf::from_str(path_arg)?
} else {
rfd::FileDialog::new()
.set_title("Open a Bare Metrics Log") .set_title("Open a Bare Metrics Log")
.add_filter("Bare Metrics Log", &["baremetrics"]) .add_filter("Bare Metrics Log", &["baremetrics"])
.pick_file() .pick_file()
.expect("You must pick a file!"); .expect("You must pick a file!")
};
//let path = PathBuf::from_str(&std::env::args().skip(1).next().unwrap()).unwrap();
let file = File::open(path).unwrap(); let file = File::open(path).unwrap();
@ -65,12 +70,14 @@ fn main() -> anyhow::Result<()> {
let filtered_log_app_name = log_app_name.to_lowercase().replace(' ', "_"); let filtered_log_app_name = log_app_name.to_lowercase().replace(' ', "_");
let dashboard_file = dashboard_dir.join(filtered_log_app_name + ".ron"); let dashboard_file = dashboard_dir.join(filtered_log_app_name + ".ron");
eprintln!("Looking for dashboard file at {:?}", dashboard_file); info!("Looking for dashboard file at {:?}", dashboard_file);
let dashboard = if dashboard_file.exists() { let dashboard = if dashboard_file.exists() {
let rules_data: DashboardConfig = ron::de::from_reader(File::open(dashboard_file)?)?; let rules_data: DashboardConfig = ron::de::from_reader(File::open(dashboard_file)?)?;
info!("Dashboard loaded successfully :)");
Some(rules_data) Some(rules_data)
} else { } else {
info!("No dashboard found.");
None None
}; };