From 33215b3228b1d71fa04aea285502636c57253bb2 Mon Sep 17 00:00:00 2001 From: rachancheet Date: Thu, 29 Feb 2024 01:10:12 +0530 Subject: [PATCH 1/2] feat: added --human-time flag --- Cargo.lock | 137 +++++++++++++++++++++++++++++++++++++++++ wayshot/Cargo.toml | 1 + wayshot/src/clap.rs | 7 +++ wayshot/src/utils.rs | 9 +++ wayshot/src/wayshot.rs | 6 +- 5 files changed, 159 insertions(+), 1 deletion(-) diff --git a/Cargo.lock b/Cargo.lock index 2d5bff5d..5a464187 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -8,6 +8,21 @@ version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe" +[[package]] +name = "android-tzdata" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e999941b234f3131b00bc13c22d06e8c5ff726d1b6318ac7eb276997bbb4fef0" + +[[package]] +name = "android_system_properties" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "819e7219dbd41043ac279b19830f2efc897156490d7fd6ea916720117ee66311" +dependencies = [ + "libc", +] + [[package]] name = "anstream" version = "0.6.5" @@ -74,6 +89,12 @@ version = "2.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "327762f6e5a765692301e5bb513e0d9fef63be86bbc14528052b1cd3e6f03e07" +[[package]] +name = "bumpalo" +version = "3.15.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8ea184aa71bb362a1157c896979544cc23974e08fd265f29ea96b59f0b4a555b" + [[package]] name = "bytemuck" version = "1.14.0" @@ -101,6 +122,20 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" +[[package]] +name = "chrono" +version = "0.4.34" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5bc015644b92d5890fab7489e49d21f879d5c990186827d42ec511919404f38b" +dependencies = [ + "android-tzdata", + "iana-time-zone", + "js-sys", + "num-traits", + "wasm-bindgen", + "windows-targets 0.52.0", +] + [[package]] name = "clap" version = "4.4.11" @@ -153,6 +188,12 @@ dependencies = [ "windows-sys 0.45.0", ] +[[package]] +name = "core-foundation-sys" +version = "0.8.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "06ea2b9bc92be3c2baa9334a323ebca2d6f074ff852cd1d7b11064035cd3868f" + [[package]] name = "crc32fast" version = "1.3.2" @@ -241,6 +282,29 @@ dependencies = [ "thread_local", ] +[[package]] +name = "iana-time-zone" +version = "0.1.60" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e7ffbb5a1b541ea2561f8c41c087286cc091e21e556a4f09a8f6cbf17b69b141" +dependencies = [ + "android_system_properties", + "core-foundation-sys", + "iana-time-zone-haiku", + "js-sys", + "wasm-bindgen", + "windows-core", +] + +[[package]] +name = "iana-time-zone-haiku" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f31827a206f56af32e590ba56d5d2d085f558508192593743f16b2306495269f" +dependencies = [ + "cc", +] + [[package]] name = "image" version = "0.24.7" @@ -263,6 +327,15 @@ version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bc0000e42512c92e31c2252315bda326620a4e034105e900c98ec492fa077b3e" +[[package]] +name = "js-sys" +version = "0.3.68" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "406cda4b368d531c842222cf9d2600a9a4acce8d29423695379c6868a143a9ee" +dependencies = [ + "wasm-bindgen", +] + [[package]] name = "lazy_static" version = "1.4.0" @@ -677,6 +750,60 @@ version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "830b7e5d4d90034032940e4ace0d9a9a057e7a45cd94e6c007832e39edb82f6d" +[[package]] +name = "wasm-bindgen" +version = "0.2.91" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c1e124130aee3fb58c5bdd6b639a0509486b0338acaaae0c84a5124b0f588b7f" +dependencies = [ + "cfg-if", + "wasm-bindgen-macro", +] + +[[package]] +name = "wasm-bindgen-backend" +version = "0.2.91" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c9e7e1900c352b609c8488ad12639a311045f40a35491fb69ba8c12f758af70b" +dependencies = [ + "bumpalo", + "log", + "once_cell", + "proc-macro2", + "quote", + "syn", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-macro" +version = "0.2.91" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b30af9e2d358182b5c7449424f017eba305ed32a7010509ede96cdc4696c46ed" +dependencies = [ + "quote", + "wasm-bindgen-macro-support", +] + +[[package]] +name = "wasm-bindgen-macro-support" +version = "0.2.91" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "642f325be6301eb8107a83d12a8ac6c1e1c54345a7ef1a9261962dfefda09e66" +dependencies = [ + "proc-macro2", + "quote", + "syn", + "wasm-bindgen-backend", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-shared" +version = "0.2.91" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4f186bd2dcf04330886ce82d6f33dd75a7bfcf69ecf5763b89fcde53b6ac9838" + [[package]] name = "wayland-backend" version = "0.3.2" @@ -754,6 +881,7 @@ dependencies = [ name = "wayshot" version = "1.3.2-dev" dependencies = [ + "chrono", "clap", "dialoguer", "flate2", @@ -785,6 +913,15 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" +[[package]] +name = "windows-core" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "33ab640c8d7e35bf8ba19b884ba838ceb4fba93a4e8c65a9059d08afcfc683d9" +dependencies = [ + "windows-targets 0.52.0", +] + [[package]] name = "windows-sys" version = "0.45.0" diff --git a/wayshot/Cargo.toml b/wayshot/Cargo.toml index 8d4aa528..8808a153 100644 --- a/wayshot/Cargo.toml +++ b/wayshot/Cargo.toml @@ -29,6 +29,7 @@ image = { version = "0.24", default-features = false, features = [ ] } dialoguer = { version = "0.11.0", features = ["fuzzy-select"] } +chrono = "0.4.34" [[bin]] name = "wayshot" diff --git a/wayshot/src/clap.rs b/wayshot/src/clap.rs index 37319334..afe51d67 100644 --- a/wayshot/src/clap.rs +++ b/wayshot/src/clap.rs @@ -37,6 +37,13 @@ pub fn set_flags() -> Command { .action(ArgAction::SetTrue) .help("Output the image data to standard out"), ) + .arg( + arg!(--human_time) + .required(false) + .conflicts_with("file") + .action(ArgAction::SetTrue) + .help("Uses human readable time for image file name"), + ) .arg( arg!(-e --extension ) .required(false) diff --git a/wayshot/src/utils.rs b/wayshot/src/utils.rs index e24caf74..b8ba9ee1 100644 --- a/wayshot/src/utils.rs +++ b/wayshot/src/utils.rs @@ -1,3 +1,4 @@ +use chrono::Local; use std::{ process::exit, time::{SystemTime, UNIX_EPOCH}, @@ -86,3 +87,11 @@ pub fn get_default_file_name(extension: EncodingFormat) -> String { time + "-wayshot." + extension.into() } + +pub fn get_human_time_file_name(extension: EncodingFormat) -> String { + let current_time = Local::now(); + + let time = String::from(current_time.format("%H:%M:%S").to_string()); + + time + "-wayshot." + extension.into() +} diff --git a/wayshot/src/wayshot.rs b/wayshot/src/wayshot.rs index b4b3c01f..2fefaf49 100644 --- a/wayshot/src/wayshot.rs +++ b/wayshot/src/wayshot.rs @@ -67,7 +67,11 @@ fn main() -> Result<(), Box> { } else if let Some(filepath) = args.get_one::("file") { file_path = Some(filepath.trim().to_string()); } else { - file_path = Some(utils::get_default_file_name(extension)); + if args.get_flag("human_time") { + file_path = Some(utils::get_human_time_file_name(extension)); + } else { + file_path = Some(utils::get_default_file_name(extension)); + } } let wayshot_conn = WayshotConnection::new()?; From 9148c4b1ef4173894e95121c9edab904591557ae Mon Sep 17 00:00:00 2001 From: rachancheet Date: Thu, 29 Feb 2024 02:50:29 +0530 Subject: [PATCH 2/2] Refactor: renamed flag and removed dep(chrono) --- Cargo.lock | 137 ----------------------------------------- wayshot/Cargo.toml | 1 - wayshot/src/clap.rs | 2 +- wayshot/src/utils.rs | 32 ++++++++-- wayshot/src/wayshot.rs | 31 +++++----- 5 files changed, 43 insertions(+), 160 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 5a464187..2d5bff5d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -8,21 +8,6 @@ version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe" -[[package]] -name = "android-tzdata" -version = "0.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e999941b234f3131b00bc13c22d06e8c5ff726d1b6318ac7eb276997bbb4fef0" - -[[package]] -name = "android_system_properties" -version = "0.1.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "819e7219dbd41043ac279b19830f2efc897156490d7fd6ea916720117ee66311" -dependencies = [ - "libc", -] - [[package]] name = "anstream" version = "0.6.5" @@ -89,12 +74,6 @@ version = "2.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "327762f6e5a765692301e5bb513e0d9fef63be86bbc14528052b1cd3e6f03e07" -[[package]] -name = "bumpalo" -version = "3.15.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8ea184aa71bb362a1157c896979544cc23974e08fd265f29ea96b59f0b4a555b" - [[package]] name = "bytemuck" version = "1.14.0" @@ -122,20 +101,6 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" -[[package]] -name = "chrono" -version = "0.4.34" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5bc015644b92d5890fab7489e49d21f879d5c990186827d42ec511919404f38b" -dependencies = [ - "android-tzdata", - "iana-time-zone", - "js-sys", - "num-traits", - "wasm-bindgen", - "windows-targets 0.52.0", -] - [[package]] name = "clap" version = "4.4.11" @@ -188,12 +153,6 @@ dependencies = [ "windows-sys 0.45.0", ] -[[package]] -name = "core-foundation-sys" -version = "0.8.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "06ea2b9bc92be3c2baa9334a323ebca2d6f074ff852cd1d7b11064035cd3868f" - [[package]] name = "crc32fast" version = "1.3.2" @@ -282,29 +241,6 @@ dependencies = [ "thread_local", ] -[[package]] -name = "iana-time-zone" -version = "0.1.60" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e7ffbb5a1b541ea2561f8c41c087286cc091e21e556a4f09a8f6cbf17b69b141" -dependencies = [ - "android_system_properties", - "core-foundation-sys", - "iana-time-zone-haiku", - "js-sys", - "wasm-bindgen", - "windows-core", -] - -[[package]] -name = "iana-time-zone-haiku" -version = "0.1.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f31827a206f56af32e590ba56d5d2d085f558508192593743f16b2306495269f" -dependencies = [ - "cc", -] - [[package]] name = "image" version = "0.24.7" @@ -327,15 +263,6 @@ version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bc0000e42512c92e31c2252315bda326620a4e034105e900c98ec492fa077b3e" -[[package]] -name = "js-sys" -version = "0.3.68" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "406cda4b368d531c842222cf9d2600a9a4acce8d29423695379c6868a143a9ee" -dependencies = [ - "wasm-bindgen", -] - [[package]] name = "lazy_static" version = "1.4.0" @@ -750,60 +677,6 @@ version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "830b7e5d4d90034032940e4ace0d9a9a057e7a45cd94e6c007832e39edb82f6d" -[[package]] -name = "wasm-bindgen" -version = "0.2.91" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c1e124130aee3fb58c5bdd6b639a0509486b0338acaaae0c84a5124b0f588b7f" -dependencies = [ - "cfg-if", - "wasm-bindgen-macro", -] - -[[package]] -name = "wasm-bindgen-backend" -version = "0.2.91" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c9e7e1900c352b609c8488ad12639a311045f40a35491fb69ba8c12f758af70b" -dependencies = [ - "bumpalo", - "log", - "once_cell", - "proc-macro2", - "quote", - "syn", - "wasm-bindgen-shared", -] - -[[package]] -name = "wasm-bindgen-macro" -version = "0.2.91" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b30af9e2d358182b5c7449424f017eba305ed32a7010509ede96cdc4696c46ed" -dependencies = [ - "quote", - "wasm-bindgen-macro-support", -] - -[[package]] -name = "wasm-bindgen-macro-support" -version = "0.2.91" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "642f325be6301eb8107a83d12a8ac6c1e1c54345a7ef1a9261962dfefda09e66" -dependencies = [ - "proc-macro2", - "quote", - "syn", - "wasm-bindgen-backend", - "wasm-bindgen-shared", -] - -[[package]] -name = "wasm-bindgen-shared" -version = "0.2.91" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4f186bd2dcf04330886ce82d6f33dd75a7bfcf69ecf5763b89fcde53b6ac9838" - [[package]] name = "wayland-backend" version = "0.3.2" @@ -881,7 +754,6 @@ dependencies = [ name = "wayshot" version = "1.3.2-dev" dependencies = [ - "chrono", "clap", "dialoguer", "flate2", @@ -913,15 +785,6 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" -[[package]] -name = "windows-core" -version = "0.52.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "33ab640c8d7e35bf8ba19b884ba838ceb4fba93a4e8c65a9059d08afcfc683d9" -dependencies = [ - "windows-targets 0.52.0", -] - [[package]] name = "windows-sys" version = "0.45.0" diff --git a/wayshot/Cargo.toml b/wayshot/Cargo.toml index 8808a153..8d4aa528 100644 --- a/wayshot/Cargo.toml +++ b/wayshot/Cargo.toml @@ -29,7 +29,6 @@ image = { version = "0.24", default-features = false, features = [ ] } dialoguer = { version = "0.11.0", features = ["fuzzy-select"] } -chrono = "0.4.34" [[bin]] name = "wayshot" diff --git a/wayshot/src/clap.rs b/wayshot/src/clap.rs index afe51d67..db61cd9f 100644 --- a/wayshot/src/clap.rs +++ b/wayshot/src/clap.rs @@ -38,7 +38,7 @@ pub fn set_flags() -> Command { .help("Output the image data to standard out"), ) .arg( - arg!(--human_time) + arg!(--time_stamp) .required(false) .conflicts_with("file") .action(ArgAction::SetTrue) diff --git a/wayshot/src/utils.rs b/wayshot/src/utils.rs index b8ba9ee1..ac1b4bf2 100644 --- a/wayshot/src/utils.rs +++ b/wayshot/src/utils.rs @@ -1,4 +1,3 @@ -use chrono::Local; use std::{ process::exit, time::{SystemTime, UNIX_EPOCH}, @@ -81,17 +80,40 @@ pub fn get_default_file_name(extension: EncodingFormat) -> String { Ok(n) => n.as_secs().to_string(), Err(_) => { tracing::error!("SystemTime before UNIX EPOCH!"); - exit(1); + // didn't think this place needed to exit + String::from("") } }; time + "-wayshot." + extension.into() } +fn get_hour_minute_from_unix_seconds(seconds: u64) -> String { + let total_minutes = seconds / 60; -pub fn get_human_time_file_name(extension: EncodingFormat) -> String { - let current_time = Local::now(); + let mut current_hour = (((total_minutes / 60) % 24) + 5) % 24; + + let mut current_minute = (total_minutes % 60) + 30; + // println!("{}", current_minute); + if current_minute > 60 { + current_hour += 1; + } + current_minute = current_minute % 60; + if current_hour == 24 { + current_hour = 0; + } + + // println!("{}", total_minutes as f64 / 60.0); - let time = String::from(current_time.format("%H:%M:%S").to_string()); + format!("{}:{}:{}", current_hour, current_minute, seconds % 60) +} +pub fn get_human_time_file_name(extension: EncodingFormat) -> String { + let time = match SystemTime::now().duration_since(UNIX_EPOCH) { + Ok(n) => get_hour_minute_from_unix_seconds(n.as_secs()), + Err(_) => { + tracing::error!("SystemTime before UNIX EPOCH!"); + String::from("") + } + }; time + "-wayshot." + extension.into() } diff --git a/wayshot/src/wayshot.rs b/wayshot/src/wayshot.rs index 2fefaf49..d8ea7478 100644 --- a/wayshot/src/wayshot.rs +++ b/wayshot/src/wayshot.rs @@ -58,22 +58,6 @@ fn main() -> Result<(), Box> { } else { EncodingFormat::Png }; - - let mut file_is_stdout: bool = false; - let mut file_path: Option = None; - - if args.get_flag("stdout") { - file_is_stdout = true; - } else if let Some(filepath) = args.get_one::("file") { - file_path = Some(filepath.trim().to_string()); - } else { - if args.get_flag("human_time") { - file_path = Some(utils::get_human_time_file_name(extension)); - } else { - file_path = Some(utils::get_default_file_name(extension)); - } - } - let wayshot_conn = WayshotConnection::new()?; if args.get_flag("listoutputs") { @@ -120,6 +104,21 @@ fn main() -> Result<(), Box> { wayshot_conn.screenshot_all(cursor_overlay)? }; + let mut file_is_stdout: bool = false; + let mut file_path: Option = None; + + if args.get_flag("stdout") { + file_is_stdout = true; + } else if let Some(filepath) = args.get_one::("file") { + file_path = Some(filepath.trim().to_string()); + } else { + if args.get_flag("time_stamp") { + file_path = Some(utils::get_human_time_file_name(extension)); + } else { + file_path = Some(utils::get_default_file_name(extension)); + } + } + if file_is_stdout { let stdout = stdout(); let mut buffer = Cursor::new(Vec::new());