From 0b2b3b6ae57e507735e1aa4a125e05652d14b8f6 Mon Sep 17 00:00:00 2001 From: Uzair Aftab Date: Wed, 24 Jul 2024 13:23:39 +0200 Subject: [PATCH] [Feature]: Use xdg dir as a default if possible --- generator/Cargo.lock | 66 +++++++++++++++++++++++++++++++++++++++++ generator/Cargo.toml | 1 + generator/src/lib.rs | 3 ++ generator/src/save.rs | 14 +++++++++ lua/codesnap/config.lua | 8 ++++- 5 files changed, 91 insertions(+), 1 deletion(-) diff --git a/generator/Cargo.lock b/generator/Cargo.lock index 9cb50a2..5528d98 100644 --- a/generator/Cargo.lock +++ b/generator/Cargo.lock @@ -351,6 +351,27 @@ dependencies = [ "syn 2.0.52", ] +[[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" @@ -488,6 +509,7 @@ dependencies = [ "arboard", "cached", "cosmic-text", + "dirs", "nvim-oxi", "regex", "serde", @@ -507,6 +529,17 @@ dependencies = [ "windows-targets 0.48.5", ] +[[package]] +name = "getrandom" +version = "0.2.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "94b22e06ecb0110981051723910cbf0b5f5e09a2062dd7663334ee79a9d1286c" +dependencies = [ + "cfg-if", + "libc", + "wasi", +] + [[package]] name = "glob" version = "0.3.1" @@ -617,6 +650,16 @@ version = "0.2.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4ec2a862134d2a7d32d7983ddcdd1c4923530833c9f2ea1a44fc5fa473989058" +[[package]] +name = "libredox" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c0ff37bd590ca25063e35af745c343cb7a0271906fb7b37e4813e8f79f00268d" +dependencies = [ + "bitflags 2.4.2", + "libc", +] + [[package]] name = "libuv-sys2" version = "1.48.0" @@ -813,6 +856,12 @@ dependencies = [ "pkg-config", ] +[[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" @@ -1010,6 +1059,17 @@ dependencies = [ "bitflags 1.3.2", ] +[[package]] +name = "redox_users" +version = "0.4.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bd283d9651eeda4b2a83a43c1c91b266c40fd76ecd39a50a8c630ae69dc72891" +dependencies = [ + "getrandom", + "libredox", + "thiserror", +] + [[package]] name = "regex" version = "1.10.3" @@ -1475,6 +1535,12 @@ dependencies = [ "winapi-util", ] +[[package]] +name = "wasi" +version = "0.11.0+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" + [[package]] name = "wayland-backend" version = "0.3.3" diff --git a/generator/Cargo.toml b/generator/Cargo.toml index d174ec1..3783b74 100644 --- a/generator/Cargo.toml +++ b/generator/Cargo.toml @@ -14,6 +14,7 @@ thiserror = "1.0.58" regex = "1.10.3" two-face = "0.3.0" cached = "0.49.3" +dirs = "5.0.1" [lib] crate-type = ["cdylib"] diff --git a/generator/src/lib.rs b/generator/src/lib.rs index 639f162..197b1ba 100644 --- a/generator/src/lib.rs +++ b/generator/src/lib.rs @@ -16,6 +16,8 @@ use copy_ascii::copy_ascii; use nvim_oxi::{Dictionary, Function, Result}; use save::save_snapshot; +use crate::save::default_save_dir; + #[nvim_oxi::module] fn generator() -> Result { Ok(Dictionary::from_iter([ @@ -24,6 +26,7 @@ fn generator() -> Result { Function::from_fn(copy_into_clipboard), ), ("save_snapshot", Function::from_fn(save_snapshot)), + ("default_save_dir", Function::from_fn(default_save_dir)), ("copy_ascii", Function::from_fn(copy_ascii)), ])) } diff --git a/generator/src/save.rs b/generator/src/save.rs index a455c6b..4602f11 100644 --- a/generator/src/save.rs +++ b/generator/src/save.rs @@ -1,6 +1,20 @@ use crate::{config::TakeSnapshotParams, path::parse_save_path, snapshot::take_snapshot}; use nvim_oxi::{lua::Error::RuntimeError, Error, Result}; +// The function will be called as FFI on Lua side +/// Get the default save directory +#[allow(dead_code)] +pub fn default_save_dir(mut config: TakeSnapshotParams) -> Result<()> { + if let Some(path) = dirs::picture_dir() { + config.save_path = Some(path.to_str().unwrap().to_string()); + Ok(()) + } else { + Err(Error::Lua(RuntimeError( + "Cannot find the picture directory".to_string(), + ))) + } +} + // The function will be called as FFI on Lua side #[allow(dead_code)] pub fn save_snapshot(config: TakeSnapshotParams) -> Result<()> { diff --git a/lua/codesnap/config.lua b/lua/codesnap/config.lua index a91121b..c47c0c0 100644 --- a/lua/codesnap/config.lua +++ b/lua/codesnap/config.lua @@ -16,7 +16,13 @@ end -- If the save_path is already configured, but no explicit filename is specified, -- it will be replaced with auto-generated filename local function parse_save_path(save_path) - if save_path == nil or string_utils.ends_with(save_path, "png") then + if save_path == nil then + local default_path = require("generator").default_save_dir() .. auto_generate_snap_filename() + print("No save_path is specified, using default path: " .. default_path) + return default_path + end + + if string_utils.ends_with(save_path, "png") then return save_path end