Skip to content

Refactor print_to_console #45

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 8 additions & 21 deletions src/gui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ pub enum Print {
Message(String),
Error(String),
Debug(String),
Task(String),
OK(String),
}

Expand Down Expand Up @@ -68,14 +67,6 @@ impl Print {
color,
})
}
Print::Task(s) => {
let color = egui::Color32::WHITE;
Some(ScrollAreaMessage {
label: "[ ] ".to_owned(),
content: s.to_owned(),
color,
})
}
Print::OK(s) => {
let color = egui::Color32::GREEN;
Some(ScrollAreaMessage {
Expand All @@ -94,18 +85,14 @@ pub struct ScrollAreaMessage {
color: egui::Color32,
}

pub fn print_to_console(print_lock: &Arc<RwLock<Vec<Print>>>, message: Print) -> usize {
let mut length: usize = 0;
if let Ok(mut write_guard) = print_lock.write() {
write_guard.push(message);
length = write_guard.len() - 1;
}
length
}

pub fn update_in_console(print_lock: &Arc<RwLock<Vec<Print>>>, message: Print, index: usize) {
if let Ok(mut write_guard) = print_lock.write() {
write_guard[index] = message;
pub fn print_to_console(print_lock: &Arc<RwLock<Vec<Print>>>, message: Print) {
match print_lock.write() {
Ok(mut write_guard) => {
write_guard.push(message);
}
Err(e) => {
println!("Error while writing to print_lock: {}", e);
}
}
}

Expand Down
11 changes: 3 additions & 8 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use std::sync::{mpsc, Arc, RwLock};
use std::thread;
use std::time::Duration;

use crate::gui::{load_gui_settings, print_to_console, update_in_console, MyApp, Print};
use crate::gui::{load_gui_settings, print_to_console, MyApp, Print};
use crate::io::save_to_csv;
use crate::serial::serial_thread;

Expand Down Expand Up @@ -92,22 +92,17 @@ fn main_thread(
}

if let Ok(file_path) = save_rx.recv_timeout(Duration::from_millis(10)) {
let print_index = print_to_console(
&print_lock,
Print::Task(format!("saving data file to {:?} ...", file_path)),
);
match save_to_csv(&data, &file_path) {
Ok(_) => {
update_in_console(
print_to_console(
&print_lock,
Print::OK(format!("saved data file to {:?} ", file_path)),
print_index,
);
}
Err(e) => {
print_to_console(
&print_lock,
Print::Error(format!("failed to save file: {e:?}")),
Print::Error(format!("failed to save file to {:?}: {:?}", file_path, e)),
);
}
}
Expand Down