summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author魏曹先生 <1992414357@qq.com>2026-02-27 06:18:30 +0800
committer魏曹先生 <1992414357@qq.com>2026-02-27 06:18:30 +0800
commitda1a67d4d32b081fee59f30c43695de21225522a (patch)
tree84cdb00e05f4d85f9e441894f846cfaa1b884bd0
parent49f8161fafb636d9d44d2f0878a7429058fb1db6 (diff)
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<String>` from literals
-rw-r--r--utils/src/env.rs13
-rw-r--r--utils/src/input.rs18
-rw-r--r--utils/src/lazy_macros.rs13
-rw-r--r--utils/src/lib.rs1
4 files changed, 42 insertions, 3 deletions
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<PathBuf> {
+ 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
@@ -63,6 +63,21 @@ pub async fn input_with_editor(
cache_file: impl AsRef<std::path::Path>,
comment_char: impl AsRef<str>,
) -> Result<String, std::io::Error> {
+ 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<str>,
+ cache_file: impl AsRef<std::path::Path>,
+ comment_char: impl AsRef<str>,
+ editor: String,
+) -> Result<String, std::io::Error> {
let cache_path = cache_file.as_ref();
let default_content = default_text.as_ref();
let comment_prefix = comment_char.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<String>` 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;