From da1a67d4d32b081fee59f30c43695de21225522a Mon Sep 17 00:00:00 2001 From: 魏曹先生 <1992414357@qq.com> Date: Fri, 27 Feb 2026 06:18:30 +0800 Subject: Add utility functions and macros - Add `current_tempfile_path` function to get temporary file paths - Add `input_with_editor_custom` function that accepts custom editor - Add `string_vec!` macro for creating `Vec` from literals --- utils/src/env.rs | 13 +++++++++++++ utils/src/input.rs | 18 +++++++++++++++--- utils/src/lazy_macros.rs | 13 +++++++++++++ utils/src/lib.rs | 1 + 4 files changed, 42 insertions(+), 3 deletions(-) create mode 100644 utils/src/lazy_macros.rs diff --git a/utils/src/env.rs b/utils/src/env.rs index 81dfbd7..1834cd3 100644 --- a/utils/src/env.rs +++ b/utils/src/env.rs @@ -1,3 +1,5 @@ +use std::path::PathBuf; + /// Returns the current locale string based on environment variables. /// /// The function checks for locale settings in the following order: @@ -92,3 +94,14 @@ pub async fn get_default_editor() -> String { "jvii".to_string() } + +/// Get temporary file path +pub fn current_tempfile_path(name: &str) -> Option { + dirs::config_local_dir().map(|path| { + if cfg!(target_os = "linux") { + path.join("jvcs").join(".temp").join(name) + } else { + path.join("JustEnoughVCS").join(".temp").join(name) + } + }) +} diff --git a/utils/src/input.rs b/utils/src/input.rs index 80ea569..bc67d90 100644 --- a/utils/src/input.rs +++ b/utils/src/input.rs @@ -62,6 +62,21 @@ pub async fn input_with_editor( default_text: impl AsRef, cache_file: impl AsRef, comment_char: impl AsRef, +) -> Result { + input_with_editor_cutsom( + default_text, + cache_file, + comment_char, + get_default_editor().await, + ) + .await +} + +pub async fn input_with_editor_cutsom( + default_text: impl AsRef, + cache_file: impl AsRef, + comment_char: impl AsRef, + editor: String, ) -> Result { let cache_path = cache_file.as_ref(); let default_content = default_text.as_ref(); @@ -70,9 +85,6 @@ pub async fn input_with_editor( // Write default text to cache file fs::write(cache_path, default_content).await?; - // Get editor from environment variable - let editor = get_default_editor().await; - // Open editor with cache file let status = Command::new(editor).arg(cache_path).status().await?; diff --git a/utils/src/lazy_macros.rs b/utils/src/lazy_macros.rs new file mode 100644 index 0000000..1019d0f --- /dev/null +++ b/utils/src/lazy_macros.rs @@ -0,0 +1,13 @@ +/// A macro for creating a `Vec` from string literals. +/// +/// # Examples +/// ``` +/// let v = string_vec!["hello", "world"]; +/// assert_eq!(v, vec!["hello".to_string(), "world".to_string()]); +/// ``` +#[macro_export] +macro_rules! string_vec { + ($($elem:expr),* $(,)?) => { + vec![$($elem.to_string()),*] + }; +} diff --git a/utils/src/lib.rs b/utils/src/lib.rs index 682c679..ef56189 100644 --- a/utils/src/lib.rs +++ b/utils/src/lib.rs @@ -3,6 +3,7 @@ pub mod env; pub mod fs; pub mod globber; pub mod input; +pub mod lazy_macros; pub mod levenshtein_distance; pub mod logger; pub mod push_version; -- cgit