Skip to content

Commit

Permalink
[Feature]: Use xdg dir as a default if possible
Browse files Browse the repository at this point in the history
  • Loading branch information
Uzaaft committed Jul 24, 2024
1 parent 06d8e34 commit 0b2b3b6
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 1 deletion.
66 changes: 66 additions & 0 deletions generator/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions generator/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
3 changes: 3 additions & 0 deletions generator/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Dictionary> {
Ok(Dictionary::from_iter([
Expand All @@ -24,6 +26,7 @@ fn generator() -> Result<Dictionary> {
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)),
]))
}
14 changes: 14 additions & 0 deletions generator/src/save.rs
Original file line number Diff line number Diff line change
@@ -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<()> {
Expand Down
8 changes: 7 additions & 1 deletion lua/codesnap/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 0b2b3b6

Please sign in to comment.