1) changed name format of prop files

2) Added feature to count number of images with quote and without
This commit is contained in:
Piyush मिश्रः 2022-03-25 13:57:11 +05:30
parent c0b31a9b31
commit 59aec4ab3b
4 changed files with 107 additions and 33 deletions

View File

@ -10,6 +10,12 @@ repository = "https://github.com/PiyushXCoder/post_maker"
keywords = ["posts", "instagram", "facebook", "twitter"] keywords = ["posts", "instagram", "facebook", "twitter"]
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # 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] [dependencies]
clap = { version = "3.0", features = ["derive"] } clap = { version = "3.0", features = ["derive"] }
log = "0.4" log = "0.4"

View File

@ -34,7 +34,6 @@ use fltk::{
}; };
use std::{ use std::{
fs, fs,
path::{Path},
sync::{mpsc, Arc, RwLock}, sync::{mpsc, Arc, RwLock},
}; };
@ -54,6 +53,8 @@ pub(crate) enum DrawMessage {
Clone, Clone,
/// Delete file /// Delete file
Delete, Delete,
/// Show details about images linke count of quotes
ShowImagesDetails
} }
/// Spawn thread to manage all actions related to image, like: edit, save, delete /// 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 status = main_win.status.clone();
let mut count = main_win.count.clone(); let mut count = main_win.count.clone();
let mut dimension = main_win.dimension.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<ImageContainer> = None; let mut _container: Option<ImageContainer> = None;
std::thread::spawn(move || loop { std::thread::spawn(move || loop {
@ -96,7 +97,7 @@ pub(crate) fn spawn_image_thread(
status.set_label("Loading..."); status.set_label("Loading...");
load_image( load_image(
&mut file_choice, &mut file_choice,
Arc::clone(&images_path), Arc::clone(&images_list),
None, None,
&mut quote, &mut quote,
&mut subquote, &mut subquote,
@ -129,7 +130,7 @@ pub(crate) fn spawn_image_thread(
status.set_label("Loading..."); status.set_label("Loading...");
load_image( load_image(
&mut file_choice, &mut file_choice,
Arc::clone(&images_path), Arc::clone(&images_list),
Some((x, y)), Some((x, y)),
&mut quote, &mut quote,
&mut subquote, &mut subquote,
@ -182,7 +183,7 @@ pub(crate) fn spawn_image_thread(
win.deactivate(); win.deactivate();
if let Some(image_info) = cont.clone_img() { if let Some(image_info) = cont.clone_img() {
let idx = file_choice.value(); 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()); imgs.insert(idx as usize, image_info.clone());
file_choice.insert( file_choice.insert(
idx, idx,
@ -204,7 +205,7 @@ pub(crate) fn spawn_image_thread(
status.set_label("Deleting..."); status.set_label("Deleting...");
win.deactivate(); win.deactivate();
cont.delete(); cont.delete();
let mut imgs = images_path.write().unwrap(); let mut imgs = images_list.write().unwrap();
imgs.remove(file_choice.value() as usize); imgs.remove(file_choice.value() as usize);
file_choice.remove(file_choice.value()); file_choice.remove(file_choice.value());
if file_choice.value() != imgs.len() as i32 { if file_choice.value() != imgs.len() as i32 {
@ -218,6 +219,9 @@ pub(crate) fn spawn_image_thread(
app::awake(); 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))); *container = Some(ImageContainer::new(&image_info, Arc::clone(&properties)));
if let Some(cont) = container { if let Some(cont) = container {
let file = Path::new(&image_info.path); let properties_file = utils::get_properties_file(&image_info);
let properties_file = file.with_extension("prop");
let read = fs::read_to_string(&properties_file).unwrap_or("{}".to_owned()); let read = fs::read_to_string(&properties_file).unwrap_or("{}".to_owned());
let read = match serde_json::from_str::<ImagePropertiesFile>(&read) { let read = match serde_json::from_str::<ImagePropertiesFile>(&read) {
@ -346,6 +349,37 @@ fn load_image(
flush_buffer(&app_sender, &container); flush_buffer(&app_sender, &container);
} }
fn show_images_details(images_list: Arc<RwLock<Vec<ImageInfo>>>) {
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::<ImagePropertiesFile>(&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 /// Flush the Buffer from image container to drawing buffer for fltk
// for drawing buffer for fltk (check in main.rs) // for drawing buffer for fltk (check in main.rs)
fn flush_buffer(app_sender: &app::Sender<crate::AppMessage>, container: &Option<ImageContainer>) { fn flush_buffer(app_sender: &app::Sender<crate::AppMessage>, container: &Option<ImageContainer>) {

View File

@ -432,7 +432,7 @@ impl MainWindow {
let sender = self.sender.clone(); let sender = self.sender.clone();
let properties = Arc::clone(&self.properties); let properties = Arc::clone(&self.properties);
self.menubar.add( self.menubar.add(
"&File/Save...\t", "&File/Save Image...\t",
Shortcut::Ctrl | 's', Shortcut::Ctrl | 's',
menu::MenuFlag::Normal, menu::MenuFlag::Normal,
move |_| { 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 mut config_window = ConfigWindow::new();
let sender = self.sender.clone(); let sender = self.sender.clone();
let mut image = self.page.image.clone(); let mut image = self.page.image.clone();

View File

@ -210,12 +210,11 @@ impl ImageContainer {
/// Save image and properities /// Save image and properities
pub(crate) fn save(&self) { pub(crate) fn save(&self) {
let prop = self.properties.read().unwrap(); let prop = self.properties.read().unwrap();
let image_info = &prop.image_info;
let (path_original, mut original_image) = match &prop.image_info { let (path_original, path_properties, mut original_image) = match image_info {
Some(p) => (Path::new(&p.path), load_image(p)), Some(p) => (Path::new(&p.path), get_properties_file(p), load_image(p)),
None => return, None => return,
}; };
let path_properties = path_original.with_extension("prop");
let config = globals::CONFIG.read().unwrap(); let config = globals::CONFIG.read().unwrap();
let export_format = &config.image_format; let export_format = &config.image_format;
let export = path_original.parent().unwrap().join("export").join( let export = path_original.parent().unwrap().join("export").join(
@ -292,30 +291,27 @@ impl ImageContainer {
match &prop.image_info { match &prop.image_info {
Some(image_info) => { Some(image_info) => {
let name = image_info.path.file_stem().unwrap().to_string_lossy(); let stem = image_info.path.file_stem().unwrap().to_string_lossy();
let ext = image_info.path.extension().unwrap().to_string_lossy(); let extension = image_info.path.extension().unwrap().to_string_lossy();
let mut i = 1; let mut i = 1;
let mut new_path = image_info.path.clone(); let mut new_image_info = image_info.clone();
while new_path.exists() { while new_image_info.path.exists() {
let new_file = format!("{}{}.{}", name, "-copy".repeat(i), ext); let new_file = format!("{}{}.{}", stem, "-copy".repeat(i), extension);
new_path = image_info.path.with_file_name(&new_file); new_image_info.path = image_info.path.with_file_name(&new_file);
i += 1; i += 1;
} }
let path_properties = image_info.path.with_extension("prop"); let path_properties = get_properties_file(&image_info);
let path_properties_new = new_path.with_extension("prop"); let path_properties_new = get_properties_file(&new_image_info);
if image_info.path.exists() { 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() { if path_properties.exists() {
fs::copy(path_properties, &path_properties_new).warn_log("Failed to clone image properties!"); fs::copy(path_properties, &path_properties_new).warn_log("Failed to clone image properties!");
} }
Some(ImageInfo { Some(new_image_info)
path: new_path,
image_type: image_info.image_type.clone()
})
} }
None => None, None => None,
} }
@ -326,13 +322,13 @@ impl ImageContainer {
let config = globals::CONFIG.read().unwrap(); let config = globals::CONFIG.read().unwrap();
let export_format = config.image_format.as_extension(); let export_format = config.image_format.as_extension();
let path_original = match &prop.image_info { let image_info = &prop.image_info;
Some(p) => Path::new(&p.path), let (path_image, path_properties) = match image_info {
Some(p) => (Path::new(&p.path), get_properties_file(p)),
None => return, None => return,
}; };
let path_properties = path_original.with_extension("prop"); let export = path_image.parent().unwrap().join("export").join(
let export = path_original.parent().unwrap().join("export").join( path_image
path_original
.with_extension(export_format) .with_extension(export_format)
.file_name() .file_name()
.unwrap() .unwrap()
@ -340,8 +336,8 @@ impl ImageContainer {
.unwrap(), .unwrap(),
); );
if path_original.exists() { if path_image.exists() {
fs::remove_file(path_original).warn_log("Failed to delete image!"); fs::remove_file(path_image).warn_log("Failed to delete image!");
} }
if path_properties.exists() { if path_properties.exists() {
@ -629,6 +625,34 @@ pub(crate) fn measure_line(
Coord::from((width, height)).into() 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 /// 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) { pub(crate) fn set_color_btn_rgba(rgba: [u8; 4], btn: &mut Button) {
let [mut r, g, b, _] = rgba; let [mut r, g, b, _] = rgba;