Skip to content

Commit

Permalink
fixed path issue (saving file)
Browse files Browse the repository at this point in the history
  • Loading branch information
hacknus committed Feb 6, 2023
1 parent 3395947 commit 9722dae
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 19 deletions.
33 changes: 18 additions & 15 deletions src/gui.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -71,7 +72,7 @@ pub struct MyApp {
plotting_range: i32,
console: Vec<Print>,
dropped_files: Vec<egui::DroppedFile>,
picked_path: String,
picked_path: PathBuf,
data: DataContainer,
gui_conf: GuiSettingsContainer,
print_lock: Arc<RwLock<Vec<Print>>>,
Expand All @@ -80,7 +81,7 @@ pub struct MyApp {
baud_lock: Arc<RwLock<u32>>,
connected_lock: Arc<RwLock<bool>>,
data_lock: Arc<RwLock<DataContainer>>,
save_tx: Sender<String>,
save_tx: Sender<PathBuf>,
send_tx: Sender<String>,
clear_tx: Sender<bool>,
eol: String,
Expand All @@ -97,15 +98,15 @@ impl MyApp {
baud_lock: Arc<RwLock<u32>>,
connected_lock: Arc<RwLock<bool>>,
gui_conf: GuiSettingsContainer,
save_tx: Sender<String>,
save_tx: Sender<PathBuf>,
send_tx: Sender<String>,
clear_tx: Sender<bool>,
) -> Self {
Self {
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())],
Expand Down Expand Up @@ -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(_) => {}
Expand Down
3 changes: 2 additions & 1 deletion src/io.rs
Original file line number Diff line number Diff line change
@@ -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<dyn Error>> {
pub fn save_to_csv(data: &DataContainer, file_path: &PathBuf) -> Result<(), Box<dyn Error>> {
let mut wtr = WriterBuilder::new()
.has_headers(false)
.from_path(file_path)?;
Expand Down
7 changes: 4 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -62,11 +63,11 @@ fn split(payload: &str) -> Vec<&str> {
fn main_thread(data_lock: Arc<RwLock<DataContainer>>,
raw_data_lock: Arc<RwLock<Vec<Packet>>>,
print_lock: Arc<RwLock<Vec<Print>>>,
save_rx: Receiver<String>,
save_rx: Receiver<PathBuf>,
clear_rx: Receiver<bool>) {
// 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 {
Expand Down Expand Up @@ -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<String>, Receiver<String>) = mpsc::channel();
let (save_tx, save_rx): (Sender<PathBuf>, Receiver<PathBuf>) = mpsc::channel();
let (send_tx, send_rx): (Sender<String>, Receiver<String>) = mpsc::channel();
let (clear_tx, clear_rx): (Sender<bool>, Receiver<bool>) = mpsc::channel();

Expand Down

0 comments on commit 9722dae

Please sign in to comment.