From b1aa92f6ab05f7b53ed3bb9a8e04a11f917b26cd Mon Sep 17 00:00:00 2001 From: gigas002 Date: Sat, 23 Mar 2024 00:05:56 +0900 Subject: [PATCH 01/35] Add config example --- wayshot/config.toml | 26 ++++++++++++++ wayshot/src/config.rs | 82 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 108 insertions(+) create mode 100644 wayshot/config.toml create mode 100644 wayshot/src/config.rs diff --git a/wayshot/config.toml b/wayshot/config.toml new file mode 100644 index 00000000..24badbc7 --- /dev/null +++ b/wayshot/config.toml @@ -0,0 +1,26 @@ +# base screenshot properties +[screenshot] +# display to take screenshot +display = "default" +# should contain cursor? +cursor = false + +# properties, related to +# clipboard copy +[clipboard] +# should copy resulting screenshot +# to clipborad? +clipboard = true + +# properties related to +# writing screenshot to filesystem +[filesystem] +# should write resulting screenshot +# to filesystem? +filesystem = true +# output directory +path = "~/images/screenshots" +# output filename format +format = "%Y-%m-%d_%H:%M:%S" +# output file encoding +encoding = "png" \ No newline at end of file diff --git a/wayshot/src/config.rs b/wayshot/src/config.rs new file mode 100644 index 00000000..fc580c9a --- /dev/null +++ b/wayshot/src/config.rs @@ -0,0 +1,82 @@ +use serde::{Deserialize, Serialize}; +use std::{fs::File, io::Read, path::PathBuf}; +use toml; + +use crate::utils::EncodingFormat; + +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct Config { + pub screenshot: Option, + #[serde(rename = "clipboard")] + pub clipboard: Option, + #[serde(rename = "filesystem")] + pub filesystem: Option, +} + +impl Default for Config { + fn default() -> Self { + Config { + screenshot: Some(Screenshot::default()), + clipboard: Some(Clipboard::default()), + filesystem: Some(Filesystem::default()), + } + } +} + +impl Config { + pub fn load(path: &PathBuf) -> Option { + let mut config_file = File::open(path).ok()?; + let mut config_str = String::new(); + config_file.read_to_string(&mut config_str).ok()?; + + toml::from_str(&mut config_str).ok()? + } +} + +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct Screenshot { + pub display: Option, + pub cursor: Option, +} + +impl Default for Screenshot { + fn default() -> Self { + Screenshot { + display: None, + cursor: Some(false), + } + } +} + +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct Clipboard { + pub clipboard: Option, +} + +impl Default for Clipboard { + fn default() -> Self { + Clipboard { + clipboard: Some(true), + } + } +} + +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct Filesystem { + pub filesystem: Option, + pub path: Option, + pub format: Option, + pub encoding: Option, +} + +impl Default for Filesystem { + fn default() -> Self { + Filesystem { + filesystem: Some(true), + path: None, + // PR #93 + format: Some("wayshot-%Y_%m_%d-%H_%M_%S".to_string()), + encoding: Some(EncodingFormat::Png), + } + } +} From 86a0522e2b9d442f9695f1e50b89c087c5024bf7 Mon Sep 17 00:00:00 2001 From: gigas002 Date: Wed, 27 Mar 2024 19:56:13 +0900 Subject: [PATCH 02/35] Update config standard --- wayshot/config.toml | 23 +++++++++-------------- wayshot/src/config.rs | 34 ++++++++++------------------------ 2 files changed, 19 insertions(+), 38 deletions(-) diff --git a/wayshot/config.toml b/wayshot/config.toml index 24badbc7..7e44c613 100644 --- a/wayshot/config.toml +++ b/wayshot/config.toml @@ -4,23 +4,18 @@ display = "default" # should contain cursor? cursor = false - -# properties, related to -# clipboard copy -[clipboard] -# should copy resulting screenshot -# to clipborad? +# should copy screenshot to clipborad? clipboard = true +# should write screenshot as file in filesystem? +fs = true +# should write screenshot in stdout? +stdout = false -# properties related to -# writing screenshot to filesystem -[filesystem] -# should write resulting screenshot -# to filesystem? -filesystem = true +# properties related to writing screenshot as file in filesystem +[fs] # output directory -path = "~/images/screenshots" +path = "$HOME/images/screenshots" # output filename format format = "%Y-%m-%d_%H:%M:%S" # output file encoding -encoding = "png" \ No newline at end of file +encoding = "png" diff --git a/wayshot/src/config.rs b/wayshot/src/config.rs index fc580c9a..9c3c428f 100644 --- a/wayshot/src/config.rs +++ b/wayshot/src/config.rs @@ -7,18 +7,14 @@ use crate::utils::EncodingFormat; #[derive(Clone, Debug, Serialize, Deserialize)] pub struct Config { pub screenshot: Option, - #[serde(rename = "clipboard")] - pub clipboard: Option, - #[serde(rename = "filesystem")] - pub filesystem: Option, + pub fs: Option, } impl Default for Config { fn default() -> Self { Config { screenshot: Some(Screenshot::default()), - clipboard: Some(Clipboard::default()), - filesystem: Some(Filesystem::default()), + fs: Some(Fs::default()), } } } @@ -37,6 +33,9 @@ impl Config { pub struct Screenshot { pub display: Option, pub cursor: Option, + pub clipboard: Option, + pub fs: Option, + pub stdout: Option, } impl Default for Screenshot { @@ -44,37 +43,24 @@ impl Default for Screenshot { Screenshot { display: None, cursor: Some(false), - } - } -} - -#[derive(Clone, Debug, Serialize, Deserialize)] -pub struct Clipboard { - pub clipboard: Option, -} - -impl Default for Clipboard { - fn default() -> Self { - Clipboard { clipboard: Some(true), + fs: Some(true), + stdout: Some(false), } } } #[derive(Clone, Debug, Serialize, Deserialize)] -pub struct Filesystem { - pub filesystem: Option, +pub struct Fs { pub path: Option, pub format: Option, pub encoding: Option, } -impl Default for Filesystem { +impl Default for Fs { fn default() -> Self { - Filesystem { - filesystem: Some(true), + Fs { path: None, - // PR #93 format: Some("wayshot-%Y_%m_%d-%H_%M_%S".to_string()), encoding: Some(EncodingFormat::Png), } From 27ed084b87b4e59ff7c4ce884e82d89d766e9c37 Mon Sep 17 00:00:00 2001 From: gigas002 Date: Wed, 27 Mar 2024 19:57:12 +0900 Subject: [PATCH 03/35] Add toml and serde dependencies --- Cargo.lock | 74 ++++++++++++++++++++++++++++++++++++++++++++++ wayshot/Cargo.toml | 2 ++ 2 files changed, 76 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index 04fcbd3d..3a5630dc 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -700,6 +700,35 @@ version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e1cf6437eb19a8f4a6cc0f7dca544973b0b78843adbfeb3683d1a94a0024a294" +[[package]] +name = "serde" +version = "1.0.197" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3fb1c873e1b9b056a4dc4c0c198b24c3ffa059243875552b2bd0933b1aee4ce2" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.197" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7eb0b34b42edc17f6b7cac84a52a1c5f0e1bb2227e997ca9011ea3dd34e8610b" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "serde_spanned" +version = "0.6.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eb3622f419d1296904700073ea6cc23ad690adbd66f13ea683df73298736f0c1" +dependencies = [ + "serde", +] + [[package]] name = "sharded-slab" version = "0.1.7" @@ -786,6 +815,40 @@ dependencies = [ "once_cell", ] +[[package]] +name = "toml" +version = "0.8.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e9dd1545e8208b4a5af1aa9bbd0b4cf7e9ea08fabc5d0a5c67fcaafa17433aa3" +dependencies = [ + "serde", + "serde_spanned", + "toml_datetime", + "toml_edit", +] + +[[package]] +name = "toml_datetime" +version = "0.6.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3550f4e9685620ac18a50ed434eb3aec30db8ba93b0287467bca5826ea25baf1" +dependencies = [ + "serde", +] + +[[package]] +name = "toml_edit" +version = "0.22.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e40bb779c5187258fd7aad0eb68cb8706a0a81fa712fbea808ab43c4b8374c4" +dependencies = [ + "indexmap", + "serde", + "serde_spanned", + "toml_datetime", + "winnow", +] + [[package]] name = "tracing" version = "0.1.40" @@ -1020,6 +1083,8 @@ dependencies = [ "image", "libwayshot", "nix 0.28.0", + "serde", + "toml", "tracing", "tracing-subscriber", "wl-clipboard-rs", @@ -1131,6 +1196,15 @@ version = "0.52.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "32b752e52a2da0ddfbdbcc6fceadfeede4c939ed16d13e648833a61dfb611ed8" +[[package]] +name = "winnow" +version = "0.6.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dffa400e67ed5a4dd237983829e66475f0a4a26938c4b04c21baede6262215b8" +dependencies = [ + "memchr", +] + [[package]] name = "wl-clipboard-rs" version = "0.8.1" diff --git a/wayshot/Cargo.toml b/wayshot/Cargo.toml index 2835b246..eeb2cc83 100644 --- a/wayshot/Cargo.toml +++ b/wayshot/Cargo.toml @@ -37,6 +37,8 @@ chrono = "0.4.35" wl-clipboard-rs = "0.8.0" nix = { version = "0.28.0", features = ["process"] } +toml = { version = "0.8.12", default-features = false, features = ["parse"] } +serde = { version = "1.0.197", features = ["derive"] } [[bin]] name = "wayshot" From fd872fa04d039007350078e68710a090979c71b9 Mon Sep 17 00:00:00 2001 From: gigas002 Date: Wed, 27 Mar 2024 19:58:04 +0900 Subject: [PATCH 04/35] Derive from Serialize and Deserialize for EncodingFormat --- wayshot/src/utils.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/wayshot/src/utils.rs b/wayshot/src/utils.rs index 1f8bf8b4..b50c8f8e 100644 --- a/wayshot/src/utils.rs +++ b/wayshot/src/utils.rs @@ -1,5 +1,6 @@ use clap::ValueEnum; use eyre::{bail, ContextCompat, Error, Result}; +use serde::{Deserialize, Serialize}; use std::{fmt::Display, path::PathBuf, str::FromStr}; @@ -48,7 +49,8 @@ pub fn parse_geometry(g: &str) -> Result { } /// Supported image encoding formats. -#[derive(Debug, Copy, Clone, PartialEq, Eq, ValueEnum)] +#[derive(Debug, Copy, Clone, PartialEq, Eq, ValueEnum, Serialize, Deserialize)] +#[serde(rename_all = "snake_case")] pub enum EncodingFormat { /// JPG/JPEG encoder. Jpg, From 33b905ab659c1bf7c807fa30f58a7369a0efbca8 Mon Sep 17 00:00:00 2001 From: gigas002 Date: Wed, 27 Mar 2024 19:58:44 +0900 Subject: [PATCH 05/35] Add config module for compilation --- wayshot/src/wayshot.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/wayshot/src/wayshot.rs b/wayshot/src/wayshot.rs index b8a9420b..3f4da455 100644 --- a/wayshot/src/wayshot.rs +++ b/wayshot/src/wayshot.rs @@ -9,6 +9,7 @@ use eyre::{bail, Result}; use libwayshot::{region::LogicalRegion, WayshotConnection}; mod cli; +mod config; mod utils; use dialoguer::{theme::ColorfulTheme, FuzzySelect}; From dab054419bedea1b6f563e57c1e6ccea1ee7c0c5 Mon Sep 17 00:00:00 2001 From: gigas002 Date: Wed, 27 Mar 2024 19:59:15 +0900 Subject: [PATCH 06/35] Add config parameter for cli --- wayshot/src/cli.rs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/wayshot/src/cli.rs b/wayshot/src/cli.rs index 0ad61c23..dc1cb282 100644 --- a/wayshot/src/cli.rs +++ b/wayshot/src/cli.rs @@ -51,4 +51,12 @@ pub struct Cli { /// Present a fuzzy selector for output/display selection #[arg(long, alias = "chooseoutput", conflicts_with_all = ["slurp", "output"])] pub choose_output: bool, + + /// Path to your config file + /// defaults to: + /// 1. `$XDG_CONFIG_HOME/wayshot/config.toml` + /// 2. `$HOME/wayshot/config.toml` -- if `$XDG_CONFIG_HOME` variable doesn't exist + /// 3. None -- if the config isn't found, the `Config::default()` will be used + #[arg(long)] + pub config: Option, } From 602cae2ed78d7137560ff0c1cc1766caac47aa00 Mon Sep 17 00:00:00 2001 From: gigas002 Date: Wed, 27 Mar 2024 20:00:06 +0900 Subject: [PATCH 07/35] Add config loading with dirs dependency --- Cargo.lock | 180 ++++++++++++++++++++++++++++++++++++----- wayshot/Cargo.toml | 1 + wayshot/src/wayshot.rs | 13 +++ 3 files changed, 175 insertions(+), 19 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 3a5630dc..7870e087 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -58,7 +58,7 @@ version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e28923312444cdd728e4738b3f9c9cac739500909bb3d3c94b43551b16517648" dependencies = [ - "windows-sys", + "windows-sys 0.52.0", ] [[package]] @@ -68,7 +68,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1cd54b81ec8d6180e24654d0b371ad22fc3dd083b6ff8ba325b72e00c87660a7" dependencies = [ "anstyle", - "windows-sys", + "windows-sys 0.52.0", ] [[package]] @@ -140,7 +140,7 @@ dependencies = [ "js-sys", "num-traits", "wasm-bindgen", - "windows-targets", + "windows-targets 0.52.4", ] [[package]] @@ -205,7 +205,7 @@ dependencies = [ "lazy_static", "libc", "unicode-width", - "windows-sys", + "windows-sys 0.52.0", ] [[package]] @@ -248,6 +248,27 @@ dependencies = [ "zeroize", ] +[[package]] +name = "dirs" +version = "5.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "44c45a9d03d6676652bcb5e724c7e988de1acad23a711b5217ab9cbecbec2225" +dependencies = [ + "dirs-sys", +] + +[[package]] +name = "dirs-sys" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "520f05a5cbd335fae5a99ff7a6ab8627577660ee5cfd6a94a6a929b52ff0321c" +dependencies = [ + "libc", + "option-ext", + "redox_users", + "windows-sys 0.48.0", +] + [[package]] name = "dlib" version = "0.5.2" @@ -282,7 +303,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a258e46cdc063eb8519c00b9fc845fc47bcfca4130e2f08e88665ceda8474245" dependencies = [ "libc", - "windows-sys", + "windows-sys 0.52.0", ] [[package]] @@ -341,6 +362,17 @@ dependencies = [ "thread_local", ] +[[package]] +name = "getrandom" +version = "0.2.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "190092ea657667030ac6a35e305e62fc4dd69fd98ac98631e5d3a2b1575a12b5" +dependencies = [ + "cfg-if", + "libc", + "wasi", +] + [[package]] name = "glob" version = "0.3.1" @@ -365,7 +397,7 @@ version = "0.5.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e3d1354bf6b7235cb4a0576c2619fd4ed18183f689b12b006a0ee7329eeff9a5" dependencies = [ - "windows-sys", + "windows-sys 0.52.0", ] [[package]] @@ -466,7 +498,18 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0c2a198fb6b0eada2a8df47933734e6d35d350665a33a3593d7164fa52c75c19" dependencies = [ "cfg-if", - "windows-targets", + "windows-targets 0.52.4", +] + +[[package]] +name = "libredox" +version = "0.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "85c833ca1e66078851dba29046874e38f08b2c883700aa29a03ddd3b23814ee8" +dependencies = [ + "bitflags 2.5.0", + "libc", + "redox_syscall", ] [[package]] @@ -594,6 +637,12 @@ version = "1.19.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3fdb12b2476b595f9358c5161aa467c2438859caa136dec86c26fdd2efe17b92" +[[package]] +name = "option-ext" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "04744f49eae99ab78e0d5c0b603ab218f515ea8cfe5a456d7629ad883a3b6e7d" + [[package]] name = "os_pipe" version = "1.1.5" @@ -601,7 +650,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "57119c3b893986491ec9aa85056780d3a0f3cf4da7cc09dd3650dbd6c6738fb9" dependencies = [ "libc", - "windows-sys", + "windows-sys 0.52.0", ] [[package]] @@ -681,6 +730,26 @@ dependencies = [ "proc-macro2", ] +[[package]] +name = "redox_syscall" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4722d768eff46b75989dd134e5c353f0d6296e5aaa3132e776cbdb56be7731aa" +dependencies = [ + "bitflags 1.3.2", +] + +[[package]] +name = "redox_users" +version = "0.4.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a18479200779601e498ada4e8c1e1f50e3ee19deb0259c25825a98b5603b2cb4" +dependencies = [ + "getrandom", + "libredox", + "thiserror", +] + [[package]] name = "rustix" version = "0.38.32" @@ -691,7 +760,7 @@ dependencies = [ "errno", "libc", "linux-raw-sys", - "windows-sys", + "windows-sys 0.52.0", ] [[package]] @@ -782,7 +851,7 @@ dependencies = [ "cfg-if", "fastrand", "rustix", - "windows-sys", + "windows-sys 0.52.0", ] [[package]] @@ -944,6 +1013,12 @@ version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "830b7e5d4d90034032940e4ace0d9a9a057e7a45cd94e6c007832e39edb82f6d" +[[package]] +name = "wasi" +version = "0.11.0+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" + [[package]] name = "wasm-bindgen" version = "0.2.92" @@ -1078,6 +1153,7 @@ dependencies = [ "chrono", "clap", "dialoguer", + "dirs", "eyre", "flate2", "image", @@ -1127,7 +1203,16 @@ version = "0.52.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "33ab640c8d7e35bf8ba19b884ba838ceb4fba93a4e8c65a9059d08afcfc683d9" dependencies = [ - "windows-targets", + "windows-targets 0.52.4", +] + +[[package]] +name = "windows-sys" +version = "0.48.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "677d2418bec65e3338edb076e806bc1ec15693c5d0104683f2efe857f61056a9" +dependencies = [ + "windows-targets 0.48.5", ] [[package]] @@ -1136,7 +1221,22 @@ version = "0.52.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d" dependencies = [ - "windows-targets", + "windows-targets 0.52.4", +] + +[[package]] +name = "windows-targets" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a2fa6e2155d7247be68c096456083145c183cbbbc2764150dda45a87197940c" +dependencies = [ + "windows_aarch64_gnullvm 0.48.5", + "windows_aarch64_msvc 0.48.5", + "windows_i686_gnu 0.48.5", + "windows_i686_msvc 0.48.5", + "windows_x86_64_gnu 0.48.5", + "windows_x86_64_gnullvm 0.48.5", + "windows_x86_64_msvc 0.48.5", ] [[package]] @@ -1145,51 +1245,93 @@ version = "0.52.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7dd37b7e5ab9018759f893a1952c9420d060016fc19a472b4bb20d1bdd694d1b" dependencies = [ - "windows_aarch64_gnullvm", - "windows_aarch64_msvc", - "windows_i686_gnu", - "windows_i686_msvc", - "windows_x86_64_gnu", - "windows_x86_64_gnullvm", - "windows_x86_64_msvc", + "windows_aarch64_gnullvm 0.52.4", + "windows_aarch64_msvc 0.52.4", + "windows_i686_gnu 0.52.4", + "windows_i686_msvc 0.52.4", + "windows_x86_64_gnu 0.52.4", + "windows_x86_64_gnullvm 0.52.4", + "windows_x86_64_msvc 0.52.4", ] +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2b38e32f0abccf9987a4e3079dfb67dcd799fb61361e53e2882c3cbaf0d905d8" + [[package]] name = "windows_aarch64_gnullvm" version = "0.52.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bcf46cf4c365c6f2d1cc93ce535f2c8b244591df96ceee75d8e83deb70a9cac9" +[[package]] +name = "windows_aarch64_msvc" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc35310971f3b2dbbf3f0690a219f40e2d9afcf64f9ab7cc1be722937c26b4bc" + [[package]] name = "windows_aarch64_msvc" version = "0.52.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "da9f259dd3bcf6990b55bffd094c4f7235817ba4ceebde8e6d11cd0c5633b675" +[[package]] +name = "windows_i686_gnu" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a75915e7def60c94dcef72200b9a8e58e5091744960da64ec734a6c6e9b3743e" + [[package]] name = "windows_i686_gnu" version = "0.52.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b474d8268f99e0995f25b9f095bc7434632601028cf86590aea5c8a5cb7801d3" +[[package]] +name = "windows_i686_msvc" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f55c233f70c4b27f66c523580f78f1004e8b5a8b659e05a4eb49d4166cca406" + [[package]] name = "windows_i686_msvc" version = "0.52.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1515e9a29e5bed743cb4415a9ecf5dfca648ce85ee42e15873c3cd8610ff8e02" +[[package]] +name = "windows_x86_64_gnu" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "53d40abd2583d23e4718fddf1ebec84dbff8381c07cae67ff7768bbf19c6718e" + [[package]] name = "windows_x86_64_gnu" version = "0.52.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5eee091590e89cc02ad514ffe3ead9eb6b660aedca2183455434b93546371a03" +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b7b52767868a23d5bab768e390dc5f5c55825b6d30b86c844ff2dc7414044cc" + [[package]] name = "windows_x86_64_gnullvm" version = "0.52.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "77ca79f2451b49fa9e2af39f0747fe999fcda4f5e241b2898624dca97a1f2177" +[[package]] +name = "windows_x86_64_msvc" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538" + [[package]] name = "windows_x86_64_msvc" version = "0.52.4" diff --git a/wayshot/Cargo.toml b/wayshot/Cargo.toml index eeb2cc83..3aee22f8 100644 --- a/wayshot/Cargo.toml +++ b/wayshot/Cargo.toml @@ -39,6 +39,7 @@ wl-clipboard-rs = "0.8.0" nix = { version = "0.28.0", features = ["process"] } toml = { version = "0.8.12", default-features = false, features = ["parse"] } serde = { version = "1.0.197", features = ["derive"] } +dirs = "5.0.1" [[bin]] name = "wayshot" diff --git a/wayshot/src/wayshot.rs b/wayshot/src/wayshot.rs index 3f4da455..15484c5e 100644 --- a/wayshot/src/wayshot.rs +++ b/wayshot/src/wayshot.rs @@ -5,6 +5,7 @@ use std::{ }; use clap::Parser; +use config::Config; use eyre::{bail, Result}; use libwayshot::{region::LogicalRegion, WayshotConnection}; @@ -35,12 +36,24 @@ where } fn main() -> Result<()> { + // cli args let cli = cli::Cli::parse(); tracing_subscriber::fmt() .with_max_level(cli.log_level) .with_writer(std::io::stderr) .init(); + // config path + let config_path = dirs::config_local_dir() + .and_then(|path| Some(path.join("wayshot").join("config.toml"))) + .unwrap_or_default(); + let config_path = cli.config.unwrap_or(config_path); + + // config + let config = Config::load(&config_path).unwrap_or_default(); + let screenshot = config.screenshot.unwrap_or_default(); + let fs = config.fs.unwrap_or_default(); + let input_encoding = cli .file .as_ref() From 1b988d7b6cc5de8de0f609634e0045a5bead6614 Mon Sep 17 00:00:00 2001 From: gigas002 Date: Wed, 27 Mar 2024 20:01:22 +0900 Subject: [PATCH 08/35] Make clipboard arg optional, defaulting to config value --- wayshot/src/cli.rs | 6 ++++-- wayshot/src/wayshot.rs | 18 ++++++++++++------ 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/wayshot/src/cli.rs b/wayshot/src/cli.rs index dc1cb282..b9a1a945 100644 --- a/wayshot/src/cli.rs +++ b/wayshot/src/cli.rs @@ -20,8 +20,9 @@ pub struct Cli { /// Copy image to clipboard. Can be used simultaneously with [OUTPUT] or stdout. /// Wayshot persists in the background offering the image till the clipboard is overwritten. + /// Defaults to config value #[arg(long, verbatim_doc_comment)] - pub clipboard: bool, + pub clipboard: Option, /// Log level to be used for printing to stderr #[arg(long, default_value = "info", value_parser = clap::builder::PossibleValuesParser::new(["trace", "debug", "info", "warn", "error"]).map(|s| -> tracing::Level{ s.parse().wrap_err_with(|| format!("Failed to parse log level: {}", s)).unwrap()}))] @@ -32,8 +33,9 @@ pub struct Cli { pub slurp: Option, /// Enable cursor in screenshots + /// defaults to config value #[arg(short, long)] - pub cursor: bool, + pub cursor: Option, /// Set image encoder, by default uses the file extension from the OUTPUT /// positional argument. Otherwise defaults to png. diff --git a/wayshot/src/wayshot.rs b/wayshot/src/wayshot.rs index 15484c5e..2262c955 100644 --- a/wayshot/src/wayshot.rs +++ b/wayshot/src/wayshot.rs @@ -54,6 +54,12 @@ fn main() -> Result<()> { let screenshot = config.screenshot.unwrap_or_default(); let fs = config.fs.unwrap_or_default(); + // pre-work vars definitions + let cursor = cli.cursor.unwrap_or(screenshot.cursor.unwrap_or_default()); + let clipboard = cli + .clipboard + .unwrap_or(screenshot.clipboard.unwrap_or_default()); + let input_encoding = cli .file .as_ref() @@ -95,12 +101,12 @@ fn main() -> Result<()> { }() .map_err(|_| libwayshot::Error::FreezeCallbackError) }), - cli.cursor, + cursor, )? } else if let Some(output_name) = cli.output { let outputs = wayshot_conn.get_all_outputs(); if let Some(output) = outputs.iter().find(|output| output.name == output_name) { - wayshot_conn.screenshot_single_output(output, cli.cursor)? + wayshot_conn.screenshot_single_output(output, cursor)? } else { bail!("No output found!"); } @@ -111,12 +117,12 @@ fn main() -> Result<()> { .map(|display| display.name.as_str()) .collect(); if let Some(index) = select_ouput(&output_names) { - wayshot_conn.screenshot_single_output(&outputs[index], cli.cursor)? + wayshot_conn.screenshot_single_output(&outputs[index], cursor)? } else { bail!("No output found!"); } } else { - wayshot_conn.screenshot_all(cli.cursor)? + wayshot_conn.screenshot_all(cursor)? }; let mut stdout_print = false; @@ -133,7 +139,7 @@ fn main() -> Result<()> { } } None => { - if cli.clipboard { + if clipboard { None } else { Some(utils::get_default_file_name(requested_encoding)) @@ -153,7 +159,7 @@ fn main() -> Result<()> { image_buf = Some(buffer); } - if cli.clipboard { + if clipboard { clipboard_daemonize(match image_buf { Some(buf) => buf, None => { From d77a923e613fede8077384a5bace39948226a965 Mon Sep 17 00:00:00 2001 From: gigas002 Date: Wed, 27 Mar 2024 20:02:13 +0900 Subject: [PATCH 09/35] Introduce filename_format cli arg --- wayshot/config.toml | 2 +- wayshot/src/cli.rs | 19 ++++++++++++------- wayshot/src/wayshot.rs | 3 +++ 3 files changed, 16 insertions(+), 8 deletions(-) diff --git a/wayshot/config.toml b/wayshot/config.toml index 7e44c613..41b435d8 100644 --- a/wayshot/config.toml +++ b/wayshot/config.toml @@ -16,6 +16,6 @@ stdout = false # output directory path = "$HOME/images/screenshots" # output filename format -format = "%Y-%m-%d_%H:%M:%S" +format = "wayshot-%Y_%m_%d-%H_%M_%S" # output file encoding encoding = "png" diff --git a/wayshot/src/cli.rs b/wayshot/src/cli.rs index b9a1a945..5b0f42a4 100644 --- a/wayshot/src/cli.rs +++ b/wayshot/src/cli.rs @@ -32,9 +32,9 @@ pub struct Cli { #[arg(short, long, value_name = "SLURP_ARGS")] pub slurp: Option, - /// Enable cursor in screenshots - /// defaults to config value - #[arg(short, long)] + /// Enable cursor in screenshots. + /// Defaults to config value + #[arg(short, long, verbatim_doc_comment)] pub cursor: Option, /// Set image encoder, by default uses the file extension from the OUTPUT @@ -54,11 +54,16 @@ pub struct Cli { #[arg(long, alias = "chooseoutput", conflicts_with_all = ["slurp", "output"])] pub choose_output: bool, - /// Path to your config file - /// defaults to: + /// Path to your config file. + /// Defaults to: /// 1. `$XDG_CONFIG_HOME/wayshot/config.toml` /// 2. `$HOME/wayshot/config.toml` -- if `$XDG_CONFIG_HOME` variable doesn't exist - /// 3. None -- if the config isn't found, the `Config::default()` will be used - #[arg(long)] + /// 3. `None` -- if the config isn't found, the `Config::default()` will be used + #[arg(long, verbatim_doc_comment)] pub config: Option, + + /// Output filename's formatting. + /// Defaults to config value, or `wayshot-%Y_%m_%d-%H_%M_%S` + #[arg(long, verbatim_doc_comment)] + pub filename_format: Option, } diff --git a/wayshot/src/wayshot.rs b/wayshot/src/wayshot.rs index 2262c955..36ef08c1 100644 --- a/wayshot/src/wayshot.rs +++ b/wayshot/src/wayshot.rs @@ -59,6 +59,9 @@ fn main() -> Result<()> { let clipboard = cli .clipboard .unwrap_or(screenshot.clipboard.unwrap_or_default()); + let filename_format = cli + .filename_format + .unwrap_or(fs.format.unwrap_or("wayshot-%Y_%m_%d-%H_%M_%S".to_string())); let input_encoding = cli .file From d35e1adf9baca1854e5f10681811c5767facf1da Mon Sep 17 00:00:00 2001 From: gigas002 Date: Wed, 27 Mar 2024 20:02:49 +0900 Subject: [PATCH 10/35] Modify get_default_file_name fn to take filename_format parameter --- wayshot/src/utils.rs | 23 ++++++++++++++++++----- wayshot/src/wayshot.rs | 10 ++++++++-- 2 files changed, 26 insertions(+), 7 deletions(-) diff --git a/wayshot/src/utils.rs b/wayshot/src/utils.rs index b50c8f8e..90383446 100644 --- a/wayshot/src/utils.rs +++ b/wayshot/src/utils.rs @@ -2,7 +2,7 @@ use clap::ValueEnum; use eyre::{bail, ContextCompat, Error, Result}; use serde::{Deserialize, Serialize}; -use std::{fmt::Display, path::PathBuf, str::FromStr}; +use std::{fmt::Display, fmt::Write, path::PathBuf, str::FromStr}; use chrono::{DateTime, Local}; use libwayshot::region::{LogicalRegion, Position, Region, Size}; @@ -135,9 +135,22 @@ impl FromStr for EncodingFormat { } } -pub fn get_default_file_name(extension: EncodingFormat) -> PathBuf { - let current_datetime: DateTime = Local::now(); - let formated_time = format!("{}", current_datetime.format("%Y_%m_%d-%H_%M_%S")); +pub fn get_default_file_name(filename_format: &str, extension: EncodingFormat) -> PathBuf { + let now = Local::now(); + let format = now.format(filename_format); - format!("wayshot-{formated_time}.{extension}").into() + let mut file_name = String::new(); + let write_result = write!(file_name, "{format}.{extension}"); + + if let Ok(_) = write_result { + file_name.into() + } else { + tracing::warn!( + "Couldn't write proposed filename_format: '{filename_format}', using default value." + ); + + let format = now.format("wayshot-%Y_%m_%d-%H_%M_%S"); + + format!("{format}.{extension}").into() + } } diff --git a/wayshot/src/wayshot.rs b/wayshot/src/wayshot.rs index 36ef08c1..b32b812c 100644 --- a/wayshot/src/wayshot.rs +++ b/wayshot/src/wayshot.rs @@ -136,7 +136,10 @@ fn main() -> Result<()> { None } else { if pathbuf.is_dir() { - pathbuf.push(utils::get_default_file_name(requested_encoding)); + pathbuf.push(utils::get_default_file_name( + &filename_format, + requested_encoding, + )); } Some(pathbuf) } @@ -145,7 +148,10 @@ fn main() -> Result<()> { if clipboard { None } else { - Some(utils::get_default_file_name(requested_encoding)) + Some(utils::get_default_file_name( + &filename_format, + requested_encoding, + )) } } }; From 81655a7e7067bcdb34e2bbd109448dfd4da831d6 Mon Sep 17 00:00:00 2001 From: gigas002 Date: Wed, 27 Mar 2024 20:03:37 +0900 Subject: [PATCH 11/35] Fallback encoding value to config, and then to default --- wayshot/src/utils.rs | 8 ++++---- wayshot/src/wayshot.rs | 23 ++++++++--------------- 2 files changed, 12 insertions(+), 19 deletions(-) diff --git a/wayshot/src/utils.rs b/wayshot/src/utils.rs index 90383446..e14583c4 100644 --- a/wayshot/src/utils.rs +++ b/wayshot/src/utils.rs @@ -4,7 +4,7 @@ use serde::{Deserialize, Serialize}; use std::{fmt::Display, fmt::Write, path::PathBuf, str::FromStr}; -use chrono::{DateTime, Local}; +use chrono::Local; use libwayshot::region::{LogicalRegion, Position, Region, Size}; pub fn parse_geometry(g: &str) -> Result { @@ -135,12 +135,12 @@ impl FromStr for EncodingFormat { } } -pub fn get_default_file_name(filename_format: &str, extension: EncodingFormat) -> PathBuf { +pub fn get_default_file_name(filename_format: &str, encoding: EncodingFormat) -> PathBuf { let now = Local::now(); let format = now.format(filename_format); let mut file_name = String::new(); - let write_result = write!(file_name, "{format}.{extension}"); + let write_result = write!(file_name, "{format}.{encoding}"); if let Ok(_) = write_result { file_name.into() @@ -151,6 +151,6 @@ pub fn get_default_file_name(filename_format: &str, extension: EncodingFormat) - let format = now.format("wayshot-%Y_%m_%d-%H_%M_%S"); - format!("{format}.{extension}").into() + format!("{format}.{encoding}").into() } } diff --git a/wayshot/src/wayshot.rs b/wayshot/src/wayshot.rs index b32b812c..5c354cb8 100644 --- a/wayshot/src/wayshot.rs +++ b/wayshot/src/wayshot.rs @@ -14,7 +14,6 @@ mod config; mod utils; use dialoguer::{theme::ColorfulTheme, FuzzySelect}; -use utils::EncodingFormat; use wl_clipboard_rs::copy::{MimeType, Options, Source}; @@ -67,15 +66,15 @@ fn main() -> Result<()> { .file .as_ref() .and_then(|pathbuf| pathbuf.try_into().ok()); - let requested_encoding = cli + let encoding = cli .encoding .or(input_encoding) - .unwrap_or(EncodingFormat::default()); + .unwrap_or(fs.encoding.unwrap_or_default()); if let Some(input_encoding) = input_encoding { - if input_encoding != requested_encoding { + if input_encoding != encoding { tracing::warn!( - "The encoding requested '{requested_encoding}' does not match the output file's encoding '{input_encoding}'. Still using the requested encoding however.", + "The encoding requested '{encoding}' does not match the output file's encoding '{input_encoding}'. Still using the requested encoding however.", ); } } @@ -136,10 +135,7 @@ fn main() -> Result<()> { None } else { if pathbuf.is_dir() { - pathbuf.push(utils::get_default_file_name( - &filename_format, - requested_encoding, - )); + pathbuf.push(utils::get_default_file_name(&filename_format, encoding)); } Some(pathbuf) } @@ -148,10 +144,7 @@ fn main() -> Result<()> { if clipboard { None } else { - Some(utils::get_default_file_name( - &filename_format, - requested_encoding, - )) + Some(utils::get_default_file_name(&filename_format, encoding)) } } }; @@ -161,7 +154,7 @@ fn main() -> Result<()> { image_buffer.save(file)?; } else if stdout_print { let mut buffer = Cursor::new(Vec::new()); - image_buffer.write_to(&mut buffer, requested_encoding)?; + image_buffer.write_to(&mut buffer, encoding)?; let stdout = stdout(); let mut writer = BufWriter::new(stdout.lock()); writer.write_all(buffer.get_ref())?; @@ -173,7 +166,7 @@ fn main() -> Result<()> { Some(buf) => buf, None => { let mut buffer = Cursor::new(Vec::new()); - image_buffer.write_to(&mut buffer, requested_encoding)?; + image_buffer.write_to(&mut buffer, encoding)?; buffer } })?; From ebdbb1b3064432e126a4f1e9dbe6468d987f385a Mon Sep 17 00:00:00 2001 From: gigas002 Date: Wed, 27 Mar 2024 20:05:16 +0900 Subject: [PATCH 12/35] Introduce log configuration --- wayshot/config.toml | 5 +++++ wayshot/src/config.rs | 30 ++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/wayshot/config.toml b/wayshot/config.toml index 41b435d8..a292596b 100644 --- a/wayshot/config.toml +++ b/wayshot/config.toml @@ -19,3 +19,8 @@ path = "$HOME/images/screenshots" format = "wayshot-%Y_%m_%d-%H_%M_%S" # output file encoding encoding = "png" + +# logging properties +[log] +# max logging level +level = "info" diff --git a/wayshot/src/config.rs b/wayshot/src/config.rs index 9c3c428f..5239f0c8 100644 --- a/wayshot/src/config.rs +++ b/wayshot/src/config.rs @@ -1,6 +1,7 @@ use serde::{Deserialize, Serialize}; use std::{fs::File, io::Read, path::PathBuf}; use toml; +use tracing::Level; use crate::utils::EncodingFormat; @@ -8,6 +9,7 @@ use crate::utils::EncodingFormat; pub struct Config { pub screenshot: Option, pub fs: Option, + pub log: Option, } impl Default for Config { @@ -15,6 +17,7 @@ impl Default for Config { Config { screenshot: Some(Screenshot::default()), fs: Some(Fs::default()), + log: Some(Log::default()), } } } @@ -66,3 +69,30 @@ impl Default for Fs { } } } + +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct Log { + pub level: Option, +} + +impl Default for Log { + fn default() -> Self { + Log { + level: Some("info".to_string()), + } + } +} + +impl Log { + pub fn get_level(self) -> Level { + self.level + .map_or(Level::INFO, |level| match level.as_str() { + "trace" => Level::TRACE, + "debug" => Level::DEBUG, + "info" => Level::INFO, + "warn" => Level::WARN, + "error" => Level::ERROR, + _ => Level::INFO, + }) + } +} From dcc70bb6f5e65cf7564d49e935d4d2b6427b9e35 Mon Sep 17 00:00:00 2001 From: gigas002 Date: Wed, 27 Mar 2024 20:06:16 +0900 Subject: [PATCH 13/35] Simplify config loading a bit --- wayshot/src/config.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/wayshot/src/config.rs b/wayshot/src/config.rs index 5239f0c8..5ab2d141 100644 --- a/wayshot/src/config.rs +++ b/wayshot/src/config.rs @@ -1,6 +1,5 @@ use serde::{Deserialize, Serialize}; use std::{fs::File, io::Read, path::PathBuf}; -use toml; use tracing::Level; use crate::utils::EncodingFormat; @@ -28,7 +27,7 @@ impl Config { let mut config_str = String::new(); config_file.read_to_string(&mut config_str).ok()?; - toml::from_str(&mut config_str).ok()? + toml::from_str(&config_str).ok()? } } From ae327c46054bce0942271a73ca599c07743770b8 Mon Sep 17 00:00:00 2001 From: gigas002 Date: Wed, 27 Mar 2024 20:07:02 +0900 Subject: [PATCH 14/35] Make current_dir default directory --- wayshot/src/config.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/wayshot/src/config.rs b/wayshot/src/config.rs index 5ab2d141..a009ed59 100644 --- a/wayshot/src/config.rs +++ b/wayshot/src/config.rs @@ -1,5 +1,5 @@ use serde::{Deserialize, Serialize}; -use std::{fs::File, io::Read, path::PathBuf}; +use std::{env, fs::File, io::Read, path::PathBuf}; use tracing::Level; use crate::utils::EncodingFormat; @@ -62,7 +62,7 @@ pub struct Fs { impl Default for Fs { fn default() -> Self { Fs { - path: None, + path: Some(env::current_dir().unwrap_or_default()), format: Some("wayshot-%Y_%m_%d-%H_%M_%S".to_string()), encoding: Some(EncodingFormat::Png), } From 07081f4c4c07b3a53e155aec60a7b81021c77166 Mon Sep 17 00:00:00 2001 From: gigas002 Date: Wed, 27 Mar 2024 20:08:03 +0900 Subject: [PATCH 15/35] Add missing webp from_str impl for EncodingFormat --- wayshot/src/utils.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/wayshot/src/utils.rs b/wayshot/src/utils.rs index e14583c4..c8309f68 100644 --- a/wayshot/src/utils.rs +++ b/wayshot/src/utils.rs @@ -130,6 +130,7 @@ impl FromStr for EncodingFormat { "png" => Self::Png, "ppm" => Self::Ppm, "qoi" => Self::Qoi, + "webp" => Self::Webp, _ => bail!("unsupported extension '{s}'"), }) } From bceb11354ffc2dd0038338c1ce7562fd69e0784c Mon Sep 17 00:00:00 2001 From: gigas002 Date: Wed, 27 Mar 2024 20:10:22 +0900 Subject: [PATCH 16/35] Introduce utils::get_full_file_name fn --- wayshot/src/utils.rs | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/wayshot/src/utils.rs b/wayshot/src/utils.rs index c8309f68..480b2abd 100644 --- a/wayshot/src/utils.rs +++ b/wayshot/src/utils.rs @@ -1,11 +1,13 @@ +use chrono::Local; use clap::ValueEnum; use eyre::{bail, ContextCompat, Error, Result}; -use serde::{Deserialize, Serialize}; - -use std::{fmt::Display, fmt::Write, path::PathBuf, str::FromStr}; - -use chrono::Local; use libwayshot::region::{LogicalRegion, Position, Region, Size}; +use serde::{Deserialize, Serialize}; +use std::{ + fmt::{Display, Write}, + path::{Path, PathBuf}, + str::FromStr, +}; pub fn parse_geometry(g: &str) -> Result { let tail = g.trim(); @@ -136,6 +138,12 @@ impl FromStr for EncodingFormat { } } +pub fn get_full_file_name(dir: &Path, filename_format: &str, encoding: EncodingFormat) -> PathBuf { + let filename = get_default_file_name(filename_format, encoding); + + dir.join(filename) +} + pub fn get_default_file_name(filename_format: &str, encoding: EncodingFormat) -> PathBuf { let now = Local::now(); let format = now.format(filename_format); @@ -143,7 +151,7 @@ pub fn get_default_file_name(filename_format: &str, encoding: EncodingFormat) -> let mut file_name = String::new(); let write_result = write!(file_name, "{format}.{encoding}"); - if let Ok(_) = write_result { + if write_result.is_ok() { file_name.into() } else { tracing::warn!( From 0cff93df3d0cf2fd24de5710307b579bd7a4ef13 Mon Sep 17 00:00:00 2001 From: gigas002 Date: Wed, 27 Mar 2024 20:12:44 +0900 Subject: [PATCH 17/35] Sort use statements with cargo fmt --- wayshot/src/cli.rs | 10 +++------- wayshot/src/config.rs | 3 +-- wayshot/src/wayshot.rs | 19 +++++++------------ 3 files changed, 11 insertions(+), 21 deletions(-) diff --git a/wayshot/src/cli.rs b/wayshot/src/cli.rs index 5b0f42a4..582101e9 100644 --- a/wayshot/src/cli.rs +++ b/wayshot/src/cli.rs @@ -1,12 +1,8 @@ -use std::path::PathBuf; - -use clap::arg; - -use clap::Parser; -use eyre::WrapErr; - use crate::utils::EncodingFormat; use clap::builder::TypedValueParser; +use clap::Parser; +use eyre::WrapErr; +use std::path::PathBuf; #[derive(Parser)] #[command(version, about)] diff --git a/wayshot/src/config.rs b/wayshot/src/config.rs index a009ed59..0db1d473 100644 --- a/wayshot/src/config.rs +++ b/wayshot/src/config.rs @@ -1,9 +1,8 @@ +use crate::utils::EncodingFormat; use serde::{Deserialize, Serialize}; use std::{env, fs::File, io::Read, path::PathBuf}; use tracing::Level; -use crate::utils::EncodingFormat; - #[derive(Clone, Debug, Serialize, Deserialize)] pub struct Config { pub screenshot: Option, diff --git a/wayshot/src/wayshot.rs b/wayshot/src/wayshot.rs index 5c354cb8..a6be87c3 100644 --- a/wayshot/src/wayshot.rs +++ b/wayshot/src/wayshot.rs @@ -1,24 +1,19 @@ -use std::{ - fs::File, - io::{stdout, BufWriter, Cursor, Write}, - process::Command, -}; - use clap::Parser; use config::Config; +use dialoguer::{theme::ColorfulTheme, FuzzySelect}; use eyre::{bail, Result}; use libwayshot::{region::LogicalRegion, WayshotConnection}; +use nix::unistd::{fork, ForkResult}; +use std::{ + io::{stdout, BufWriter, Cursor, Write}, + process::Command, +}; +use wl_clipboard_rs::copy::{MimeType, Options, Source}; mod cli; mod config; mod utils; -use dialoguer::{theme::ColorfulTheme, FuzzySelect}; - -use wl_clipboard_rs::copy::{MimeType, Options, Source}; - -use nix::unistd::{fork, ForkResult}; - fn select_ouput(ouputs: &[T]) -> Option where T: ToString, From ed33e00a71804deda6f3fa2af55b351fc91d08a6 Mon Sep 17 00:00:00 2001 From: gigas002 Date: Wed, 27 Mar 2024 20:13:37 +0900 Subject: [PATCH 18/35] Clipboard default to true --- wayshot/src/wayshot.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/wayshot/src/wayshot.rs b/wayshot/src/wayshot.rs index a6be87c3..f0c2caef 100644 --- a/wayshot/src/wayshot.rs +++ b/wayshot/src/wayshot.rs @@ -52,7 +52,7 @@ fn main() -> Result<()> { let cursor = cli.cursor.unwrap_or(screenshot.cursor.unwrap_or_default()); let clipboard = cli .clipboard - .unwrap_or(screenshot.clipboard.unwrap_or_default()); + .unwrap_or(screenshot.clipboard.unwrap_or(true)); let filename_format = cli .filename_format .unwrap_or(fs.format.unwrap_or("wayshot-%Y_%m_%d-%H_%M_%S".to_string())); From c446b68ab7a56395e326e332aefe40145ab31965 Mon Sep 17 00:00:00 2001 From: gigas002 Date: Wed, 27 Mar 2024 20:16:11 +0900 Subject: [PATCH 19/35] Use log_level config variable --- wayshot/src/cli.rs | 9 ++++----- wayshot/src/wayshot.rs | 11 +++++++++-- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/wayshot/src/cli.rs b/wayshot/src/cli.rs index 582101e9..a50b3633 100644 --- a/wayshot/src/cli.rs +++ b/wayshot/src/cli.rs @@ -1,8 +1,7 @@ use crate::utils::EncodingFormat; -use clap::builder::TypedValueParser; use clap::Parser; -use eyre::WrapErr; use std::path::PathBuf; +use tracing::Level; #[derive(Parser)] #[command(version, about)] @@ -20,9 +19,9 @@ pub struct Cli { #[arg(long, verbatim_doc_comment)] pub clipboard: Option, - /// Log level to be used for printing to stderr - #[arg(long, default_value = "info", value_parser = clap::builder::PossibleValuesParser::new(["trace", "debug", "info", "warn", "error"]).map(|s| -> tracing::Level{ s.parse().wrap_err_with(|| format!("Failed to parse log level: {}", s)).unwrap()}))] - pub log_level: tracing::Level, + /// Defaults to config value (`info`) + #[arg(long, verbatim_doc_comment)] + pub log_level: Option, /// Arguments to call slurp with for selecting a region #[arg(short, long, value_name = "SLURP_ARGS")] diff --git a/wayshot/src/wayshot.rs b/wayshot/src/wayshot.rs index f0c2caef..a8823a39 100644 --- a/wayshot/src/wayshot.rs +++ b/wayshot/src/wayshot.rs @@ -5,7 +5,7 @@ use eyre::{bail, Result}; use libwayshot::{region::LogicalRegion, WayshotConnection}; use nix::unistd::{fork, ForkResult}; use std::{ - io::{stdout, BufWriter, Cursor, Write}, + io::{self, stdout, BufWriter, Cursor, Write}, process::Command, }; use wl_clipboard_rs::copy::{MimeType, Options, Source}; @@ -39,16 +39,23 @@ fn main() -> Result<()> { // config path let config_path = dirs::config_local_dir() - .and_then(|path| Some(path.join("wayshot").join("config.toml"))) + .map(|path| path.join("wayshot").join("config.toml")) .unwrap_or_default(); let config_path = cli.config.unwrap_or(config_path); // config let config = Config::load(&config_path).unwrap_or_default(); + let log = config.log.unwrap_or_default(); let screenshot = config.screenshot.unwrap_or_default(); let fs = config.fs.unwrap_or_default(); // pre-work vars definitions + let log_level = cli.log_level.unwrap_or(log.get_level()); + tracing_subscriber::fmt() + .with_max_level(log_level) + .with_writer(io::stderr) + .init(); + let cursor = cli.cursor.unwrap_or(screenshot.cursor.unwrap_or_default()); let clipboard = cli .clipboard From 9818d6e61c8c2ac7e799710f430bf5d707af8ba7 Mon Sep 17 00:00:00 2001 From: gigas002 Date: Wed, 27 Mar 2024 20:17:56 +0900 Subject: [PATCH 20/35] Update cli docs a bit --- wayshot/src/cli.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/wayshot/src/cli.rs b/wayshot/src/cli.rs index a50b3633..3cdb2e81 100644 --- a/wayshot/src/cli.rs +++ b/wayshot/src/cli.rs @@ -15,7 +15,7 @@ pub struct Cli { /// Copy image to clipboard. Can be used simultaneously with [OUTPUT] or stdout. /// Wayshot persists in the background offering the image till the clipboard is overwritten. - /// Defaults to config value + /// Defaults to config value (`true`) #[arg(long, verbatim_doc_comment)] pub clipboard: Option, @@ -28,12 +28,12 @@ pub struct Cli { pub slurp: Option, /// Enable cursor in screenshots. - /// Defaults to config value + /// Defaults to config value (`false`) #[arg(short, long, verbatim_doc_comment)] pub cursor: Option, /// Set image encoder, by default uses the file extension from the OUTPUT - /// positional argument. Otherwise defaults to png. + /// positional argument. Otherwise defaults to config value (`png`). #[arg(long, verbatim_doc_comment, visible_aliases = ["extension", "format", "output-format"], value_name = "FILE_EXTENSION")] pub encoding: Option, @@ -58,7 +58,7 @@ pub struct Cli { pub config: Option, /// Output filename's formatting. - /// Defaults to config value, or `wayshot-%Y_%m_%d-%H_%M_%S` + /// Defaults to config value (`wayshot-%Y_%m_%d-%H_%M_%S`) #[arg(long, verbatim_doc_comment)] pub filename_format: Option, } From cc92489fd6d3fd96aab3bb425b30c950a91db6e7 Mon Sep 17 00:00:00 2001 From: gigas002 Date: Wed, 27 Mar 2024 20:30:25 +0900 Subject: [PATCH 21/35] Whoops, remove unneeded tracing_subscriber --- wayshot/src/wayshot.rs | 4 ---- 1 file changed, 4 deletions(-) diff --git a/wayshot/src/wayshot.rs b/wayshot/src/wayshot.rs index a8823a39..9896fb4e 100644 --- a/wayshot/src/wayshot.rs +++ b/wayshot/src/wayshot.rs @@ -32,10 +32,6 @@ where fn main() -> Result<()> { // cli args let cli = cli::Cli::parse(); - tracing_subscriber::fmt() - .with_max_level(cli.log_level) - .with_writer(std::io::stderr) - .init(); // config path let config_path = dirs::config_local_dir() From 1ebcbffa236bbc3a5c4ccde472be06ad1537a877 Mon Sep 17 00:00:00 2001 From: gigas002 Date: Thu, 28 Mar 2024 20:56:10 +0900 Subject: [PATCH 22/35] Rename display in config to output --- wayshot/config.toml | 10 +++++----- wayshot/src/cli.rs | 1 + wayshot/src/config.rs | 4 ++-- wayshot/src/wayshot.rs | 5 ++++- 4 files changed, 12 insertions(+), 8 deletions(-) diff --git a/wayshot/config.toml b/wayshot/config.toml index a292596b..8361cc9f 100644 --- a/wayshot/config.toml +++ b/wayshot/config.toml @@ -1,7 +1,7 @@ # base screenshot properties [screenshot] -# display to take screenshot -display = "default" +# output to take screenshot +# output = "default" # should contain cursor? cursor = false # should copy screenshot to clipborad? @@ -13,11 +13,11 @@ stdout = false # properties related to writing screenshot as file in filesystem [fs] -# output directory +# screenshots directory path = "$HOME/images/screenshots" -# output filename format +# screenshot filename format format = "wayshot-%Y_%m_%d-%H_%M_%S" -# output file encoding +# screenshot file encoding encoding = "png" # logging properties diff --git a/wayshot/src/cli.rs b/wayshot/src/cli.rs index 3cdb2e81..3727e1b3 100644 --- a/wayshot/src/cli.rs +++ b/wayshot/src/cli.rs @@ -19,6 +19,7 @@ pub struct Cli { #[arg(long, verbatim_doc_comment)] pub clipboard: Option, + /// Log level to be used for printing to stderr /// Defaults to config value (`info`) #[arg(long, verbatim_doc_comment)] pub log_level: Option, diff --git a/wayshot/src/config.rs b/wayshot/src/config.rs index 0db1d473..6be3fa54 100644 --- a/wayshot/src/config.rs +++ b/wayshot/src/config.rs @@ -32,7 +32,7 @@ impl Config { #[derive(Clone, Debug, Serialize, Deserialize)] pub struct Screenshot { - pub display: Option, + pub output: Option, pub cursor: Option, pub clipboard: Option, pub fs: Option, @@ -42,7 +42,7 @@ pub struct Screenshot { impl Default for Screenshot { fn default() -> Self { Screenshot { - display: None, + output: None, cursor: Some(false), clipboard: Some(true), fs: Some(true), diff --git a/wayshot/src/wayshot.rs b/wayshot/src/wayshot.rs index 9896fb4e..f74917c9 100644 --- a/wayshot/src/wayshot.rs +++ b/wayshot/src/wayshot.rs @@ -77,6 +77,8 @@ fn main() -> Result<()> { } } + let output = cli.output.or(screenshot.output); + let wayshot_conn = WayshotConnection::new()?; if cli.list_outputs { @@ -103,7 +105,7 @@ fn main() -> Result<()> { }), cursor, )? - } else if let Some(output_name) = cli.output { + } else if let Some(output_name) = output { let outputs = wayshot_conn.get_all_outputs(); if let Some(output) = outputs.iter().find(|output| output.name == output_name) { wayshot_conn.screenshot_single_output(output, cursor)? @@ -147,6 +149,7 @@ fn main() -> Result<()> { } }; + // save the screenshot data let mut image_buf: Option>> = None; if let Some(file) = file { image_buffer.save(file)?; From 895bcf7600505958b265100ddb21c7ed15a98fd5 Mon Sep 17 00:00:00 2001 From: gigas002 Date: Thu, 28 Mar 2024 21:02:08 +0900 Subject: [PATCH 23/35] Rename output file related OUTPUT parameter to FILE --- wayshot/src/cli.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/wayshot/src/cli.rs b/wayshot/src/cli.rs index 3727e1b3..af585f4c 100644 --- a/wayshot/src/cli.rs +++ b/wayshot/src/cli.rs @@ -6,14 +6,14 @@ use tracing::Level; #[derive(Parser)] #[command(version, about)] pub struct Cli { - /// Custom output path can be of the following types: - /// 1. Directory (Default naming scheme is used for the image output). + /// Custom screenshot file path can be of the following types: + /// 1. Directory (Default naming scheme is used for the image screenshot file). /// 2. Path (Encoding is automatically inferred from the extension). /// 3. `-` (Indicates writing to terminal [stdout]). - #[arg(value_name = "OUTPUT", verbatim_doc_comment)] + #[arg(value_name = "FILE", verbatim_doc_comment)] pub file: Option, - /// Copy image to clipboard. Can be used simultaneously with [OUTPUT] or stdout. + /// Copy image to clipboard. Can be used simultaneously with [FILE] or stdout. /// Wayshot persists in the background offering the image till the clipboard is overwritten. /// Defaults to config value (`true`) #[arg(long, verbatim_doc_comment)] @@ -33,9 +33,9 @@ pub struct Cli { #[arg(short, long, verbatim_doc_comment)] pub cursor: Option, - /// Set image encoder, by default uses the file extension from the OUTPUT + /// Set image encoder, by default uses the file extension from the FILE /// positional argument. Otherwise defaults to config value (`png`). - #[arg(long, verbatim_doc_comment, visible_aliases = ["extension", "format", "output-format"], value_name = "FILE_EXTENSION")] + #[arg(long, verbatim_doc_comment, visible_aliases = ["extension", "format", "file-format"], value_name = "FILE_EXTENSION")] pub encoding: Option, /// List all valid outputs From ef9e96ecfe2f4a1026e7546dc3cc49c4443df820 Mon Sep 17 00:00:00 2001 From: gigas002 Date: Thu, 28 Mar 2024 21:07:56 +0900 Subject: [PATCH 24/35] Separate stdout and file options --- wayshot/src/cli.rs | 7 +++++- wayshot/src/wayshot.rs | 56 ++++++++++++++++++++++-------------------- 2 files changed, 35 insertions(+), 28 deletions(-) diff --git a/wayshot/src/cli.rs b/wayshot/src/cli.rs index af585f4c..2bab1942 100644 --- a/wayshot/src/cli.rs +++ b/wayshot/src/cli.rs @@ -9,10 +9,15 @@ pub struct Cli { /// Custom screenshot file path can be of the following types: /// 1. Directory (Default naming scheme is used for the image screenshot file). /// 2. Path (Encoding is automatically inferred from the extension). - /// 3. `-` (Indicates writing to terminal [stdout]). + /// 3. None (no screenshot will be saved on filesystem/drive) #[arg(value_name = "FILE", verbatim_doc_comment)] pub file: Option, + /// Write screenshot to terminal/stdout. + /// Defaults to config value (`false`) + #[arg(long, verbatim_doc_comment)] + pub stdout: Option, + /// Copy image to clipboard. Can be used simultaneously with [FILE] or stdout. /// Wayshot persists in the background offering the image till the clipboard is overwritten. /// Defaults to config value (`true`) diff --git a/wayshot/src/wayshot.rs b/wayshot/src/wayshot.rs index f74917c9..0510b4aa 100644 --- a/wayshot/src/wayshot.rs +++ b/wayshot/src/wayshot.rs @@ -5,7 +5,8 @@ use eyre::{bail, Result}; use libwayshot::{region::LogicalRegion, WayshotConnection}; use nix::unistd::{fork, ForkResult}; use std::{ - io::{self, stdout, BufWriter, Cursor, Write}, + env, + io::{self, BufWriter, Cursor, Write}, process::Command, }; use wl_clipboard_rs::copy::{MimeType, Options, Source}; @@ -77,6 +78,26 @@ fn main() -> Result<()> { } } + let stdout_print = cli.stdout.unwrap_or(screenshot.stdout.unwrap_or_default()); + let file = match cli.file { + Some(f) => { + if f.is_dir() { + Some(utils::get_full_file_name(&f, &filename_format, encoding)) + } else { + Some(f) + } + } + _ => { + if screenshot.fs.unwrap_or_default() { + // default to current dir + let dir = fs.path.unwrap_or(env::current_dir().unwrap_or_default()); + Some(utils::get_full_file_name(&dir, &filename_format, encoding)) + } else { + None + } + } + }; + let output = cli.output.or(screenshot.output); let wayshot_conn = WayshotConnection::new()?; @@ -89,6 +110,7 @@ fn main() -> Result<()> { return Ok(()); } + // take a screenshot let image_buffer = if let Some(slurp_region) = cli.slurp { let slurp_region = slurp_region.clone(); wayshot_conn.screenshot_freeze( @@ -127,36 +149,16 @@ fn main() -> Result<()> { wayshot_conn.screenshot_all(cursor)? }; - let mut stdout_print = false; - let file = match cli.file { - Some(mut pathbuf) => { - if pathbuf.to_string_lossy() == "-" { - stdout_print = true; - None - } else { - if pathbuf.is_dir() { - pathbuf.push(utils::get_default_file_name(&filename_format, encoding)); - } - Some(pathbuf) - } - } - None => { - if clipboard { - None - } else { - Some(utils::get_default_file_name(&filename_format, encoding)) - } - } - }; - // save the screenshot data let mut image_buf: Option>> = None; - if let Some(file) = file { - image_buffer.save(file)?; - } else if stdout_print { + if let Some(file_path) = file { + image_buffer.save(file_path)?; + } + + if stdout_print { let mut buffer = Cursor::new(Vec::new()); image_buffer.write_to(&mut buffer, encoding)?; - let stdout = stdout(); + let stdout = io::stdout(); let mut writer = BufWriter::new(stdout.lock()); writer.write_all(buffer.get_ref())?; image_buf = Some(buffer); From 8ac93d7f3aabc2fa452b2c51d6a38b072d31f6bd Mon Sep 17 00:00:00 2001 From: gigas002 Date: Thu, 28 Mar 2024 21:11:29 +0900 Subject: [PATCH 25/35] Handle HOME env var/tilde in config paths with shellexpand --- Cargo.lock | 10 ++++++++++ wayshot/Cargo.toml | 1 + wayshot/src/utils.rs | 14 +++++++++++++- wayshot/src/wayshot.rs | 2 +- 4 files changed, 25 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 7870e087..fd1b0933 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -813,6 +813,15 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "24188a676b6ae68c3b2cb3a01be17fbf7240ce009799bb56d5b1409051e78fde" +[[package]] +name = "shellexpand" +version = "3.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "da03fa3b94cc19e3ebfc88c4229c49d8f08cdbd1228870a45f0ffdf84988e14b" +dependencies = [ + "dirs", +] + [[package]] name = "simd-adler32" version = "0.3.7" @@ -1160,6 +1169,7 @@ dependencies = [ "libwayshot", "nix 0.28.0", "serde", + "shellexpand", "toml", "tracing", "tracing-subscriber", diff --git a/wayshot/Cargo.toml b/wayshot/Cargo.toml index 3aee22f8..41688da3 100644 --- a/wayshot/Cargo.toml +++ b/wayshot/Cargo.toml @@ -40,6 +40,7 @@ nix = { version = "0.28.0", features = ["process"] } toml = { version = "0.8.12", default-features = false, features = ["parse"] } serde = { version = "1.0.197", features = ["derive"] } dirs = "5.0.1" +shellexpand = "3.1.0" [[bin]] name = "wayshot" diff --git a/wayshot/src/utils.rs b/wayshot/src/utils.rs index 480b2abd..b4f3a040 100644 --- a/wayshot/src/utils.rs +++ b/wayshot/src/utils.rs @@ -4,6 +4,7 @@ use eyre::{bail, ContextCompat, Error, Result}; use libwayshot::region::{LogicalRegion, Position, Region, Size}; use serde::{Deserialize, Serialize}; use std::{ + env, fmt::{Display, Write}, path::{Path, PathBuf}, str::FromStr, @@ -138,10 +139,21 @@ impl FromStr for EncodingFormat { } } +pub fn get_checked_path(path: &str) -> PathBuf { + let checked_path = shellexpand::full(path); + + if let Ok(checked_path) = checked_path { + PathBuf::from(checked_path.into_owned()) + } else { + env::current_dir().unwrap_or_default() + } +} + pub fn get_full_file_name(dir: &Path, filename_format: &str, encoding: EncodingFormat) -> PathBuf { let filename = get_default_file_name(filename_format, encoding); - dir.join(filename) + let checked_path = get_checked_path(dir.to_str().unwrap_or_default()); + checked_path.join(filename) } pub fn get_default_file_name(filename_format: &str, encoding: EncodingFormat) -> PathBuf { diff --git a/wayshot/src/wayshot.rs b/wayshot/src/wayshot.rs index 0510b4aa..6b912f11 100644 --- a/wayshot/src/wayshot.rs +++ b/wayshot/src/wayshot.rs @@ -84,7 +84,7 @@ fn main() -> Result<()> { if f.is_dir() { Some(utils::get_full_file_name(&f, &filename_format, encoding)) } else { - Some(f) + Some(utils::get_checked_path(f.to_str().unwrap_or_default())) } } _ => { From 8d75acf96db9d1741dfc8371e4abf4e09a20ce5a Mon Sep 17 00:00:00 2001 From: gigas002 Date: Mon, 1 Apr 2024 22:18:04 +0900 Subject: [PATCH 26/35] Fix comments for cli --- wayshot/src/cli.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/wayshot/src/cli.rs b/wayshot/src/cli.rs index 2bab1942..0a159a58 100644 --- a/wayshot/src/cli.rs +++ b/wayshot/src/cli.rs @@ -9,7 +9,7 @@ pub struct Cli { /// Custom screenshot file path can be of the following types: /// 1. Directory (Default naming scheme is used for the image screenshot file). /// 2. Path (Encoding is automatically inferred from the extension). - /// 3. None (no screenshot will be saved on filesystem/drive) + /// 3. None (the screenshot file with default filename_format will be created in current directory) #[arg(value_name = "FILE", verbatim_doc_comment)] pub file: Option, @@ -20,7 +20,7 @@ pub struct Cli { /// Copy image to clipboard. Can be used simultaneously with [FILE] or stdout. /// Wayshot persists in the background offering the image till the clipboard is overwritten. - /// Defaults to config value (`true`) + /// Defaults to config value (`false`) #[arg(long, verbatim_doc_comment)] pub clipboard: Option, From 9f87c12ee69afafa376c8ecc52eed51356096e94 Mon Sep 17 00:00:00 2001 From: gigas002 Date: Mon, 1 Apr 2024 22:26:06 +0900 Subject: [PATCH 27/35] Make log_level property of screenshot --- wayshot/src/config.rs | 19 +++++++++++++++++-- wayshot/src/wayshot.rs | 3 +-- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/wayshot/src/config.rs b/wayshot/src/config.rs index 6be3fa54..a9e33b4d 100644 --- a/wayshot/src/config.rs +++ b/wayshot/src/config.rs @@ -7,7 +7,6 @@ use tracing::Level; pub struct Config { pub screenshot: Option, pub fs: Option, - pub log: Option, } impl Default for Config { @@ -15,7 +14,6 @@ impl Default for Config { Config { screenshot: Some(Screenshot::default()), fs: Some(Fs::default()), - log: Some(Log::default()), } } } @@ -37,6 +35,7 @@ pub struct Screenshot { pub clipboard: Option, pub fs: Option, pub stdout: Option, + pub log_level: Option, } impl Default for Screenshot { @@ -47,10 +46,26 @@ impl Default for Screenshot { clipboard: Some(true), fs: Some(true), stdout: Some(false), + log_level: Some("info".to_string()), } } } +impl Screenshot { + pub fn get_log_level(&self) -> Level { + self.log_level + .as_ref() + .map_or(Level::INFO, |level| match level.as_str() { + "trace" => Level::TRACE, + "debug" => Level::DEBUG, + "info" => Level::INFO, + "warn" => Level::WARN, + "error" => Level::ERROR, + _ => Level::INFO, + }) + } +} + #[derive(Clone, Debug, Serialize, Deserialize)] pub struct Fs { pub path: Option, diff --git a/wayshot/src/wayshot.rs b/wayshot/src/wayshot.rs index 6b912f11..d6795149 100644 --- a/wayshot/src/wayshot.rs +++ b/wayshot/src/wayshot.rs @@ -42,12 +42,11 @@ fn main() -> Result<()> { // config let config = Config::load(&config_path).unwrap_or_default(); - let log = config.log.unwrap_or_default(); let screenshot = config.screenshot.unwrap_or_default(); let fs = config.fs.unwrap_or_default(); // pre-work vars definitions - let log_level = cli.log_level.unwrap_or(log.get_level()); + let log_level = cli.log_level.unwrap_or(screenshot.get_log_level()); tracing_subscriber::fmt() .with_max_level(log_level) .with_writer(io::stderr) From da1aa222ec7826d5460cbff1eb31ec09fea262f8 Mon Sep 17 00:00:00 2001 From: gigas002 Date: Mon, 1 Apr 2024 22:27:11 +0900 Subject: [PATCH 28/35] Add Config::get_default_path fn --- wayshot/src/config.rs | 6 ++++++ wayshot/src/wayshot.rs | 5 +---- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/wayshot/src/config.rs b/wayshot/src/config.rs index a9e33b4d..0a977a24 100644 --- a/wayshot/src/config.rs +++ b/wayshot/src/config.rs @@ -26,6 +26,12 @@ impl Config { toml::from_str(&config_str).ok()? } + + pub fn get_default_path() -> PathBuf { + dirs::config_local_dir() + .map(|path| path.join("wayshot").join("config.toml")) + .unwrap_or_default() + } } #[derive(Clone, Debug, Serialize, Deserialize)] diff --git a/wayshot/src/wayshot.rs b/wayshot/src/wayshot.rs index d6795149..004e004d 100644 --- a/wayshot/src/wayshot.rs +++ b/wayshot/src/wayshot.rs @@ -35,10 +35,7 @@ fn main() -> Result<()> { let cli = cli::Cli::parse(); // config path - let config_path = dirs::config_local_dir() - .map(|path| path.join("wayshot").join("config.toml")) - .unwrap_or_default(); - let config_path = cli.config.unwrap_or(config_path); + let config_path = cli.config.unwrap_or(Config::get_default_path()); // config let config = Config::load(&config_path).unwrap_or_default(); From 3cce69d7e69c9564cd354b5f7f4d53807fb776fc Mon Sep 17 00:00:00 2001 From: gigas002 Date: Mon, 1 Apr 2024 22:30:07 +0900 Subject: [PATCH 29/35] Some options fixes --- wayshot/src/config.rs | 29 +---------------------------- wayshot/src/wayshot.rs | 27 ++++++++++++++++----------- 2 files changed, 17 insertions(+), 39 deletions(-) diff --git a/wayshot/src/config.rs b/wayshot/src/config.rs index 0a977a24..3f6baeca 100644 --- a/wayshot/src/config.rs +++ b/wayshot/src/config.rs @@ -49,7 +49,7 @@ impl Default for Screenshot { Screenshot { output: None, cursor: Some(false), - clipboard: Some(true), + clipboard: Some(false), fs: Some(true), stdout: Some(false), log_level: Some("info".to_string()), @@ -88,30 +88,3 @@ impl Default for Fs { } } } - -#[derive(Clone, Debug, Serialize, Deserialize)] -pub struct Log { - pub level: Option, -} - -impl Default for Log { - fn default() -> Self { - Log { - level: Some("info".to_string()), - } - } -} - -impl Log { - pub fn get_level(self) -> Level { - self.level - .map_or(Level::INFO, |level| match level.as_str() { - "trace" => Level::TRACE, - "debug" => Level::DEBUG, - "info" => Level::INFO, - "warn" => Level::WARN, - "error" => Level::ERROR, - _ => Level::INFO, - }) - } -} diff --git a/wayshot/src/wayshot.rs b/wayshot/src/wayshot.rs index 004e004d..21804ba3 100644 --- a/wayshot/src/wayshot.rs +++ b/wayshot/src/wayshot.rs @@ -61,18 +61,23 @@ fn main() -> Result<()> { .file .as_ref() .and_then(|pathbuf| pathbuf.try_into().ok()); - let encoding = cli - .encoding - .or(input_encoding) - .unwrap_or(fs.encoding.unwrap_or_default()); - - if let Some(input_encoding) = input_encoding { - if input_encoding != encoding { - tracing::warn!( - "The encoding requested '{encoding}' does not match the output file's encoding '{input_encoding}'. Still using the requested encoding however.", - ); + let requested_encoding = cli.encoding.or(input_encoding); + let encoding = match requested_encoding { + Some(e) => { + if let Some(input_encoding) = input_encoding { + if input_encoding.ne(&e) { + tracing::warn!( + "The encoding requested '{e}' does not match the output file's encoding '{input_encoding}'. Still using the requested encoding however.", + ); + } + + input_encoding + } else { + e + } } - } + _ => fs.encoding.unwrap_or_default(), + }; let stdout_print = cli.stdout.unwrap_or(screenshot.stdout.unwrap_or_default()); let file = match cli.file { From 629d3801a3670307ea8fb57fd428ffa8a0b37a6f Mon Sep 17 00:00:00 2001 From: gigas002 Date: Mon, 1 Apr 2024 22:33:27 +0900 Subject: [PATCH 30/35] Fix file cli file argument behavior --- wayshot/src/wayshot.rs | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/wayshot/src/wayshot.rs b/wayshot/src/wayshot.rs index 21804ba3..021c8851 100644 --- a/wayshot/src/wayshot.rs +++ b/wayshot/src/wayshot.rs @@ -52,7 +52,7 @@ fn main() -> Result<()> { let cursor = cli.cursor.unwrap_or(screenshot.cursor.unwrap_or_default()); let clipboard = cli .clipboard - .unwrap_or(screenshot.clipboard.unwrap_or(true)); + .unwrap_or(screenshot.clipboard.unwrap_or_default()); let filename_format = cli .filename_format .unwrap_or(fs.format.unwrap_or("wayshot-%Y_%m_%d-%H_%M_%S".to_string())); @@ -81,16 +81,21 @@ fn main() -> Result<()> { let stdout_print = cli.stdout.unwrap_or(screenshot.stdout.unwrap_or_default()); let file = match cli.file { - Some(f) => { + Some(mut f) => { if f.is_dir() { Some(utils::get_full_file_name(&f, &filename_format, encoding)) } else { - Some(utils::get_checked_path(f.to_str().unwrap_or_default())) + f.set_extension(""); + let dir = fs.path.unwrap_or(env::current_dir().unwrap_or_default()); + Some(utils::get_full_file_name( + &dir, + &f.to_str().unwrap(), + encoding, + )) } } _ => { if screenshot.fs.unwrap_or_default() { - // default to current dir let dir = fs.path.unwrap_or(env::current_dir().unwrap_or_default()); Some(utils::get_full_file_name(&dir, &filename_format, encoding)) } else { From 60f61a0534a396f6b45519daef1b8c21aa2584de Mon Sep 17 00:00:00 2001 From: gigas002 Date: Mon, 1 Apr 2024 22:35:03 +0900 Subject: [PATCH 31/35] Update config.toml --- wayshot/config.toml | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/wayshot/config.toml b/wayshot/config.toml index 8361cc9f..43150755 100644 --- a/wayshot/config.toml +++ b/wayshot/config.toml @@ -5,22 +5,19 @@ # should contain cursor? cursor = false # should copy screenshot to clipborad? -clipboard = true +clipboard = false # should write screenshot as file in filesystem? fs = true # should write screenshot in stdout? stdout = false +# max logging level +log_level = "info" # properties related to writing screenshot as file in filesystem [fs] # screenshots directory -path = "$HOME/images/screenshots" +# path = "$HOME/images/screenshots" # screenshot filename format format = "wayshot-%Y_%m_%d-%H_%M_%S" # screenshot file encoding encoding = "png" - -# logging properties -[log] -# max logging level -level = "info" From 175398f061368df5932dc22fe558e3a634bfd36e Mon Sep 17 00:00:00 2001 From: gigas002 Date: Tue, 2 Apr 2024 21:49:11 +0900 Subject: [PATCH 32/35] Rename screenshot to base --- wayshot/config.toml | 2 +- wayshot/src/config.rs | 12 ++++++------ wayshot/src/wayshot.rs | 14 +++++++------- 3 files changed, 14 insertions(+), 14 deletions(-) diff --git a/wayshot/config.toml b/wayshot/config.toml index 43150755..617ab937 100644 --- a/wayshot/config.toml +++ b/wayshot/config.toml @@ -1,5 +1,5 @@ # base screenshot properties -[screenshot] +[base] # output to take screenshot # output = "default" # should contain cursor? diff --git a/wayshot/src/config.rs b/wayshot/src/config.rs index 3f6baeca..00d87e43 100644 --- a/wayshot/src/config.rs +++ b/wayshot/src/config.rs @@ -5,14 +5,14 @@ use tracing::Level; #[derive(Clone, Debug, Serialize, Deserialize)] pub struct Config { - pub screenshot: Option, + pub base: Option, pub fs: Option, } impl Default for Config { fn default() -> Self { Config { - screenshot: Some(Screenshot::default()), + base: Some(Base::default()), fs: Some(Fs::default()), } } @@ -35,7 +35,7 @@ impl Config { } #[derive(Clone, Debug, Serialize, Deserialize)] -pub struct Screenshot { +pub struct Base { pub output: Option, pub cursor: Option, pub clipboard: Option, @@ -44,9 +44,9 @@ pub struct Screenshot { pub log_level: Option, } -impl Default for Screenshot { +impl Default for Base { fn default() -> Self { - Screenshot { + Base { output: None, cursor: Some(false), clipboard: Some(false), @@ -57,7 +57,7 @@ impl Default for Screenshot { } } -impl Screenshot { +impl Base { pub fn get_log_level(&self) -> Level { self.log_level .as_ref() diff --git a/wayshot/src/wayshot.rs b/wayshot/src/wayshot.rs index 021c8851..adda0226 100644 --- a/wayshot/src/wayshot.rs +++ b/wayshot/src/wayshot.rs @@ -39,20 +39,20 @@ fn main() -> Result<()> { // config let config = Config::load(&config_path).unwrap_or_default(); - let screenshot = config.screenshot.unwrap_or_default(); + let base = config.base.unwrap_or_default(); let fs = config.fs.unwrap_or_default(); // pre-work vars definitions - let log_level = cli.log_level.unwrap_or(screenshot.get_log_level()); + let log_level = cli.log_level.unwrap_or(base.get_log_level()); tracing_subscriber::fmt() .with_max_level(log_level) .with_writer(io::stderr) .init(); - let cursor = cli.cursor.unwrap_or(screenshot.cursor.unwrap_or_default()); + let cursor = cli.cursor.unwrap_or(base.cursor.unwrap_or_default()); let clipboard = cli .clipboard - .unwrap_or(screenshot.clipboard.unwrap_or_default()); + .unwrap_or(base.clipboard.unwrap_or_default()); let filename_format = cli .filename_format .unwrap_or(fs.format.unwrap_or("wayshot-%Y_%m_%d-%H_%M_%S".to_string())); @@ -79,7 +79,7 @@ fn main() -> Result<()> { _ => fs.encoding.unwrap_or_default(), }; - let stdout_print = cli.stdout.unwrap_or(screenshot.stdout.unwrap_or_default()); + let stdout_print = cli.stdout.unwrap_or(base.stdout.unwrap_or_default()); let file = match cli.file { Some(mut f) => { if f.is_dir() { @@ -95,7 +95,7 @@ fn main() -> Result<()> { } } _ => { - if screenshot.fs.unwrap_or_default() { + if base.fs.unwrap_or_default() { let dir = fs.path.unwrap_or(env::current_dir().unwrap_or_default()); Some(utils::get_full_file_name(&dir, &filename_format, encoding)) } else { @@ -104,7 +104,7 @@ fn main() -> Result<()> { } }; - let output = cli.output.or(screenshot.output); + let output = cli.output.or(base.output); let wayshot_conn = WayshotConnection::new()?; From aa4fdab983014e34af8f7c57c5b28cb0422f6ea6 Mon Sep 17 00:00:00 2001 From: gigas002 Date: Tue, 2 Apr 2024 21:51:35 +0900 Subject: [PATCH 33/35] Rename fs to file --- wayshot/config.toml | 4 ++-- wayshot/src/config.rs | 14 +++++++------- wayshot/src/wayshot.rs | 14 +++++++------- 3 files changed, 16 insertions(+), 16 deletions(-) diff --git a/wayshot/config.toml b/wayshot/config.toml index 617ab937..082d41b8 100644 --- a/wayshot/config.toml +++ b/wayshot/config.toml @@ -7,14 +7,14 @@ cursor = false # should copy screenshot to clipborad? clipboard = false # should write screenshot as file in filesystem? -fs = true +file = true # should write screenshot in stdout? stdout = false # max logging level log_level = "info" # properties related to writing screenshot as file in filesystem -[fs] +[file] # screenshots directory # path = "$HOME/images/screenshots" # screenshot filename format diff --git a/wayshot/src/config.rs b/wayshot/src/config.rs index 00d87e43..9a2ee0e6 100644 --- a/wayshot/src/config.rs +++ b/wayshot/src/config.rs @@ -1,26 +1,26 @@ use crate::utils::EncodingFormat; use serde::{Deserialize, Serialize}; -use std::{env, fs::File, io::Read, path::PathBuf}; +use std::{env, io::Read, path::PathBuf}; use tracing::Level; #[derive(Clone, Debug, Serialize, Deserialize)] pub struct Config { pub base: Option, - pub fs: Option, + pub file: Option, } impl Default for Config { fn default() -> Self { Config { base: Some(Base::default()), - fs: Some(Fs::default()), + file: Some(File::default()), } } } impl Config { pub fn load(path: &PathBuf) -> Option { - let mut config_file = File::open(path).ok()?; + let mut config_file = std::fs::File::open(path).ok()?; let mut config_str = String::new(); config_file.read_to_string(&mut config_str).ok()?; @@ -73,15 +73,15 @@ impl Base { } #[derive(Clone, Debug, Serialize, Deserialize)] -pub struct Fs { +pub struct File { pub path: Option, pub format: Option, pub encoding: Option, } -impl Default for Fs { +impl Default for File { fn default() -> Self { - Fs { + File { path: Some(env::current_dir().unwrap_or_default()), format: Some("wayshot-%Y_%m_%d-%H_%M_%S".to_string()), encoding: Some(EncodingFormat::Png), diff --git a/wayshot/src/wayshot.rs b/wayshot/src/wayshot.rs index adda0226..3971da9a 100644 --- a/wayshot/src/wayshot.rs +++ b/wayshot/src/wayshot.rs @@ -40,7 +40,7 @@ fn main() -> Result<()> { // config let config = Config::load(&config_path).unwrap_or_default(); let base = config.base.unwrap_or_default(); - let fs = config.fs.unwrap_or_default(); + let file = config.file.unwrap_or_default(); // pre-work vars definitions let log_level = cli.log_level.unwrap_or(base.get_log_level()); @@ -55,7 +55,7 @@ fn main() -> Result<()> { .unwrap_or(base.clipboard.unwrap_or_default()); let filename_format = cli .filename_format - .unwrap_or(fs.format.unwrap_or("wayshot-%Y_%m_%d-%H_%M_%S".to_string())); + .unwrap_or(file.format.unwrap_or("wayshot-%Y_%m_%d-%H_%M_%S".to_string())); let input_encoding = cli .file @@ -76,17 +76,17 @@ fn main() -> Result<()> { e } } - _ => fs.encoding.unwrap_or_default(), + _ => file.encoding.unwrap_or_default(), }; let stdout_print = cli.stdout.unwrap_or(base.stdout.unwrap_or_default()); - let file = match cli.file { + let file_path = match cli.file { Some(mut f) => { if f.is_dir() { Some(utils::get_full_file_name(&f, &filename_format, encoding)) } else { f.set_extension(""); - let dir = fs.path.unwrap_or(env::current_dir().unwrap_or_default()); + let dir = file.path.unwrap_or(env::current_dir().unwrap_or_default()); Some(utils::get_full_file_name( &dir, &f.to_str().unwrap(), @@ -96,7 +96,7 @@ fn main() -> Result<()> { } _ => { if base.fs.unwrap_or_default() { - let dir = fs.path.unwrap_or(env::current_dir().unwrap_or_default()); + let dir = file.path.unwrap_or(env::current_dir().unwrap_or_default()); Some(utils::get_full_file_name(&dir, &filename_format, encoding)) } else { None @@ -157,7 +157,7 @@ fn main() -> Result<()> { // save the screenshot data let mut image_buf: Option>> = None; - if let Some(file_path) = file { + if let Some(file_path) = file_path { image_buffer.save(file_path)?; } From 78fb18a4634e29da800802b019d881b0f56cc1fa Mon Sep 17 00:00:00 2001 From: gigas002 Date: Tue, 2 Apr 2024 21:56:22 +0900 Subject: [PATCH 34/35] cargo fmt + clippy --- wayshot/src/wayshot.rs | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/wayshot/src/wayshot.rs b/wayshot/src/wayshot.rs index 3971da9a..b31d2494 100644 --- a/wayshot/src/wayshot.rs +++ b/wayshot/src/wayshot.rs @@ -50,12 +50,11 @@ fn main() -> Result<()> { .init(); let cursor = cli.cursor.unwrap_or(base.cursor.unwrap_or_default()); - let clipboard = cli - .clipboard - .unwrap_or(base.clipboard.unwrap_or_default()); - let filename_format = cli - .filename_format - .unwrap_or(file.format.unwrap_or("wayshot-%Y_%m_%d-%H_%M_%S".to_string())); + let clipboard = cli.clipboard.unwrap_or(base.clipboard.unwrap_or_default()); + let filename_format = cli.filename_format.unwrap_or( + file.format + .unwrap_or("wayshot-%Y_%m_%d-%H_%M_%S".to_string()), + ); let input_encoding = cli .file @@ -89,7 +88,7 @@ fn main() -> Result<()> { let dir = file.path.unwrap_or(env::current_dir().unwrap_or_default()); Some(utils::get_full_file_name( &dir, - &f.to_str().unwrap(), + f.to_str().unwrap(), encoding, )) } From 866942830e263d46be0417e2788a16bfcd74f400 Mon Sep 17 00:00:00 2001 From: gigas002 Date: Wed, 3 Apr 2024 20:14:18 +0900 Subject: [PATCH 35/35] Fix base.fs->file naming --- wayshot/src/config.rs | 4 ++-- wayshot/src/wayshot.rs | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/wayshot/src/config.rs b/wayshot/src/config.rs index 9a2ee0e6..f9b8bde9 100644 --- a/wayshot/src/config.rs +++ b/wayshot/src/config.rs @@ -39,7 +39,7 @@ pub struct Base { pub output: Option, pub cursor: Option, pub clipboard: Option, - pub fs: Option, + pub file: Option, pub stdout: Option, pub log_level: Option, } @@ -50,7 +50,7 @@ impl Default for Base { output: None, cursor: Some(false), clipboard: Some(false), - fs: Some(true), + file: Some(true), stdout: Some(false), log_level: Some("info".to_string()), } diff --git a/wayshot/src/wayshot.rs b/wayshot/src/wayshot.rs index b31d2494..78641a01 100644 --- a/wayshot/src/wayshot.rs +++ b/wayshot/src/wayshot.rs @@ -94,7 +94,7 @@ fn main() -> Result<()> { } } _ => { - if base.fs.unwrap_or_default() { + if base.file.unwrap_or_default() { let dir = file.path.unwrap_or(env::current_dir().unwrap_or_default()); Some(utils::get_full_file_name(&dir, &filename_format, encoding)) } else {