diff --git a/src/gui.rs b/src/gui.rs index db84965..2c29ad1 100644 --- a/src/gui.rs +++ b/src/gui.rs @@ -1,5 +1,6 @@ use core::f32; use std::ops::RangeInclusive; +use std::path::PathBuf; use std::sync::mpsc::{Sender}; use std::sync::{Arc, RwLock}; use std::time::Duration; @@ -71,7 +72,7 @@ pub struct MyApp { plotting_range: i32, console: Vec, dropped_files: Vec, - picked_path: String, + picked_path: PathBuf, data: DataContainer, gui_conf: GuiSettingsContainer, print_lock: Arc>>, @@ -80,7 +81,7 @@ pub struct MyApp { baud_lock: Arc>, connected_lock: Arc>, data_lock: Arc>, - save_tx: Sender, + save_tx: Sender, send_tx: Sender, clear_tx: Sender, eol: String, @@ -97,7 +98,7 @@ impl MyApp { baud_lock: Arc>, connected_lock: Arc>, gui_conf: GuiSettingsContainer, - save_tx: Sender, + save_tx: Sender, send_tx: Sender, clear_tx: Sender, ) -> Self { @@ -105,7 +106,7 @@ impl MyApp { dark_mode: true, ready: false, dropped_files: vec![], - picked_path: "".to_string(), + picked_path: PathBuf::new(), device: "".to_string(), data: DataContainer::default(), console: vec![Print::MESSAGE(format!("waiting for serial connection..,").to_string())], @@ -381,20 +382,22 @@ impl eframe::App for MyApp { ui.end_row(); if ui.button("Save to file").clicked() { match rfd::FileDialog::new().save_file() { - Some(path) => - // TODO: here we should really include .csv as extension! + Some(mut path) => { - let extension = ".csv".to_string(); - let mut final_path: String; - if path.display().to_string().ends_with(".csv") { - final_path = path.display().to_string(); - } else { - final_path = path.display().to_string(); - final_path.push_str(&extension); + let extension = "csv"; + match path.extension() { + None => { + path.set_extension(&extension); + } + Some(ext) => { + if ext != "csv" { + path.set_extension(&extension); + } + } } - self.picked_path = final_path; + self.picked_path = path; } - None => self.picked_path = "".to_string() + None => self.picked_path = PathBuf::new() } match self.save_tx.send(self.picked_path.clone()) { Ok(_) => {} diff --git a/src/io.rs b/src/io.rs index 63ddfce..dee5a5e 100644 --- a/src/io.rs +++ b/src/io.rs @@ -1,9 +1,10 @@ use std::error::Error; +use std::path::PathBuf; use csv::{WriterBuilder}; use crate::DataContainer; -pub fn save_to_csv(data: &DataContainer, file_path: &String) -> Result<(), Box> { +pub fn save_to_csv(data: &DataContainer, file_path: &PathBuf) -> Result<(), Box> { let mut wtr = WriterBuilder::new() .has_headers(false) .from_path(file_path)?; diff --git a/src/main.rs b/src/main.rs index b59842c..948484e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -12,6 +12,7 @@ mod serial; mod data; use std::cmp::max; +use std::path::PathBuf; use std::thread; use eframe::egui::{vec2, Visuals}; use std::sync::mpsc::{Receiver, Sender}; @@ -62,11 +63,11 @@ fn split(payload: &str) -> Vec<&str> { fn main_thread(data_lock: Arc>, raw_data_lock: Arc>>, print_lock: Arc>>, - save_rx: Receiver, + save_rx: Receiver, clear_rx: Receiver) { // reads data from mutex, samples and saves if needed let mut acquire = false; - let mut file_path = "serial_monitor_test.csv".to_string(); + let mut file_path = PathBuf::from("serial_monitor_test.csv"); let mut data = DataContainer::default(); let mut failed_format_counter = 0; loop { @@ -185,7 +186,7 @@ fn main() { let print_lock = Arc::new(RwLock::new(vec![Print::EMPTY])); let connected_lock = Arc::new(RwLock::new(false)); - let (save_tx, save_rx): (Sender, Receiver) = mpsc::channel(); + let (save_tx, save_rx): (Sender, Receiver) = mpsc::channel(); let (send_tx, send_rx): (Sender, Receiver) = mpsc::channel(); let (clear_tx, clear_rx): (Sender, Receiver) = mpsc::channel();