diff --git a/Cargo.toml b/Cargo.toml index 6dfe953..bece8e6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,6 +10,12 @@ repository = "https://github.com/PiyushXCoder/post_maker" keywords = ["posts", "instagram", "facebook", "twitter"] # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html +[profile.dev] +opt-level = 1 + +[profile.release] +opt-level = 3 + [dependencies] clap = { version = "3.0", features = ["derive"] } log = "0.4" diff --git a/src/draw_thread.rs b/src/draw_thread.rs index e88ac22..8932a08 100644 --- a/src/draw_thread.rs +++ b/src/draw_thread.rs @@ -34,7 +34,6 @@ use fltk::{ }; use std::{ fs, - path::{Path}, sync::{mpsc, Arc, RwLock}, }; @@ -54,6 +53,8 @@ pub(crate) enum DrawMessage { Clone, /// Delete file Delete, + /// Show details about images linke count of quotes + ShowImagesDetails } /// Spawn thread to manage all actions related to image, like: edit, save, delete @@ -86,7 +87,7 @@ pub(crate) fn spawn_image_thread( let mut status = main_win.status.clone(); let mut count = main_win.count.clone(); let mut dimension = main_win.dimension.clone(); - let images_path = Arc::clone(&main_win.images_list); + let images_list = Arc::clone(&main_win.images_list); let mut _container: Option = None; std::thread::spawn(move || loop { @@ -96,7 +97,7 @@ pub(crate) fn spawn_image_thread( status.set_label("Loading..."); load_image( &mut file_choice, - Arc::clone(&images_path), + Arc::clone(&images_list), None, &mut quote, &mut subquote, @@ -129,7 +130,7 @@ pub(crate) fn spawn_image_thread( status.set_label("Loading..."); load_image( &mut file_choice, - Arc::clone(&images_path), + Arc::clone(&images_list), Some((x, y)), &mut quote, &mut subquote, @@ -182,7 +183,7 @@ pub(crate) fn spawn_image_thread( win.deactivate(); if let Some(image_info) = cont.clone_img() { let idx = file_choice.value(); - let mut imgs = images_path.write().unwrap(); + let mut imgs = images_list.write().unwrap(); imgs.insert(idx as usize, image_info.clone()); file_choice.insert( idx, @@ -204,7 +205,7 @@ pub(crate) fn spawn_image_thread( status.set_label("Deleting..."); win.deactivate(); cont.delete(); - let mut imgs = images_path.write().unwrap(); + let mut imgs = images_list.write().unwrap(); imgs.remove(file_choice.value() as usize); file_choice.remove(file_choice.value()); if file_choice.value() != imgs.len() as i32 { @@ -218,6 +219,9 @@ pub(crate) fn spawn_image_thread( app::awake(); } } + DrawMessage::ShowImagesDetails => { + show_images_details(Arc::clone(&images_list)) + } } } }); @@ -264,8 +268,7 @@ fn load_image( *container = Some(ImageContainer::new(&image_info, Arc::clone(&properties))); if let Some(cont) = container { - let file = Path::new(&image_info.path); - let properties_file = file.with_extension("prop"); + let properties_file = utils::get_properties_file(&image_info); let read = fs::read_to_string(&properties_file).unwrap_or("{}".to_owned()); let read = match serde_json::from_str::(&read) { @@ -346,6 +349,37 @@ fn load_image( flush_buffer(&app_sender, &container); } +fn show_images_details(images_list: Arc>>) { + let mut image_with_quote: usize = 0; + let mut image_without_quote: usize = 0; + + let list = images_list.read().unwrap(); + for image_info in list.iter() { + let properties_file = utils::get_properties_file(&image_info); + + let read = fs::read_to_string(&properties_file).unwrap_or("{}".to_owned()); + let read = match serde_json::from_str::(&read) { + Ok(r) => r, + Err(_) => { + image_without_quote += 1; + continue; + } + }; + + if let Some(t) = read.quote { + if t.trim().len() == 0 { + image_without_quote += 1; + } else { + image_with_quote += 1; + } + }else { + image_without_quote += 1; + } + } + + dialog::message_default(&format!("With Quote: {}\nWithout Quote: {}", image_with_quote, image_without_quote)); +} + /// Flush the Buffer from image container to drawing buffer for fltk // for drawing buffer for fltk (check in main.rs) fn flush_buffer(app_sender: &app::Sender, container: &Option) { diff --git a/src/main_window.rs b/src/main_window.rs index 13eea25..7578505 100644 --- a/src/main_window.rs +++ b/src/main_window.rs @@ -432,7 +432,7 @@ impl MainWindow { let sender = self.sender.clone(); let properties = Arc::clone(&self.properties); self.menubar.add( - "&File/Save...\t", + "&File/Save Image...\t", Shortcut::Ctrl | 's', menu::MenuFlag::Normal, move |_| { @@ -442,6 +442,16 @@ impl MainWindow { }, ); + let sender = self.sender.clone(); + self.menubar.add( + "&Actions/Show Details...\t", + Shortcut::None, + menu::MenuFlag::Normal, + move |_| { + sender.send(DrawMessage::ShowImagesDetails).unwrap(); + }, + ); + let mut config_window = ConfigWindow::new(); let sender = self.sender.clone(); let mut image = self.page.image.clone(); diff --git a/src/utils.rs b/src/utils.rs index 483be13..2dd1156 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -210,12 +210,11 @@ impl ImageContainer { /// Save image and properities pub(crate) fn save(&self) { let prop = self.properties.read().unwrap(); - - let (path_original, mut original_image) = match &prop.image_info { - Some(p) => (Path::new(&p.path), load_image(p)), + let image_info = &prop.image_info; + let (path_original, path_properties, mut original_image) = match image_info { + Some(p) => (Path::new(&p.path), get_properties_file(p), load_image(p)), None => return, }; - let path_properties = path_original.with_extension("prop"); let config = globals::CONFIG.read().unwrap(); let export_format = &config.image_format; let export = path_original.parent().unwrap().join("export").join( @@ -292,30 +291,27 @@ impl ImageContainer { match &prop.image_info { Some(image_info) => { - let name = image_info.path.file_stem().unwrap().to_string_lossy(); - let ext = image_info.path.extension().unwrap().to_string_lossy(); + let stem = image_info.path.file_stem().unwrap().to_string_lossy(); + let extension = image_info.path.extension().unwrap().to_string_lossy(); let mut i = 1; - let mut new_path = image_info.path.clone(); - while new_path.exists() { - let new_file = format!("{}{}.{}", name, "-copy".repeat(i), ext); - new_path = image_info.path.with_file_name(&new_file); + let mut new_image_info = image_info.clone(); + while new_image_info.path.exists() { + let new_file = format!("{}{}.{}", stem, "-copy".repeat(i), extension); + new_image_info.path = image_info.path.with_file_name(&new_file); i += 1; } - let path_properties = image_info.path.with_extension("prop"); - let path_properties_new = new_path.with_extension("prop"); + let path_properties = get_properties_file(&image_info); + let path_properties_new = get_properties_file(&new_image_info); if image_info.path.exists() { - fs::copy(&image_info.path, &new_path).warn_log("Failed to clone image!"); + fs::copy(&image_info.path, &new_image_info.path).warn_log("Failed to clone image!"); } if path_properties.exists() { fs::copy(path_properties, &path_properties_new).warn_log("Failed to clone image properties!"); } - Some(ImageInfo { - path: new_path, - image_type: image_info.image_type.clone() - }) + Some(new_image_info) } None => None, } @@ -326,13 +322,13 @@ impl ImageContainer { let config = globals::CONFIG.read().unwrap(); let export_format = config.image_format.as_extension(); - let path_original = match &prop.image_info { - Some(p) => Path::new(&p.path), + let image_info = &prop.image_info; + let (path_image, path_properties) = match image_info { + Some(p) => (Path::new(&p.path), get_properties_file(p)), None => return, }; - let path_properties = path_original.with_extension("prop"); - let export = path_original.parent().unwrap().join("export").join( - path_original + let export = path_image.parent().unwrap().join("export").join( + path_image .with_extension(export_format) .file_name() .unwrap() @@ -340,8 +336,8 @@ impl ImageContainer { .unwrap(), ); - if path_original.exists() { - fs::remove_file(path_original).warn_log("Failed to delete image!"); + if path_image.exists() { + fs::remove_file(path_image).warn_log("Failed to delete image!"); } if path_properties.exists() { @@ -629,6 +625,34 @@ pub(crate) fn measure_line( Coord::from((width, height)).into() } +/// path of properties files +pub(crate) fn get_properties_file(image_info: &ImageInfo) -> PathBuf { + let img = &image_info.path; + let stem = String::from(img.file_stem().unwrap_or_default().to_string_lossy()); + let extension = String::from(img.extension().unwrap_or_default().to_string_lossy()); + + let mut default_path = img.with_file_name(format!("{}-{}", stem, extension)); + default_path.set_extension("prop"); + + if default_path.exists() { + return default_path; + } + + let path = img.with_extension("prop"); + + if path.exists() { + match std::fs::copy(&path, &default_path){ + Ok(_) => { + std::fs::remove_file(&path).warn_log("Failed to delete depricated prop file") + } + Err(e) => Result::<(), _>::Err(e).warn_log("Failed to copy depricated prop file") + } + } + + default_path +} + + /// small hack because 0,0,0 rgb, because can't be set on fltk theme pub(crate) fn set_color_btn_rgba(rgba: [u8; 4], btn: &mut Button) { let [mut r, g, b, _] = rgba;