From 8a99821622c9753d58a7f85cb15cca804f5df9d7 Mon Sep 17 00:00:00 2001 From: eltrac Date: Wed, 15 Jul 2026 11:02:54 +0800 Subject: refactor: partitioning parse-target-string function and lay some ground work for future features --- vimulti.clj | 61 ++++++++++++++++++++++++++++++++++++------------------------- 1 file changed, 36 insertions(+), 25 deletions(-) diff --git a/vimulti.clj b/vimulti.clj index f7a3782..145c69e 100644 --- a/vimulti.clj +++ b/vimulti.clj @@ -5,7 +5,7 @@ '[clojure.edn :as edn] '[clojure.string :as str]) -(def default-config +(def default-config {:editor-command "nvim" :default-target "nvim:" :aliases []}) @@ -37,23 +37,50 @@ (str env-xdg-config-home "/vimulti/" config-filename) config-filename)))) -(def global-config (-> (global-config-path) - init-config - slurp +(def global-config (-> (global-config-path) + init-config + slurp edn/read-string)) ;;; load local config -(def config (cond-> global-config +(def config (cond-> global-config (fs/exists? "vimulti.edn") (deep-merge (-> "vimulti.edn" slurp edn/read-string)))) ;; git helpers (defn- get-current-branch [dir] (-> (shell {:dir dir - :out :string} - "git branch --show-current") + :out :string} + "git branch --show-current") :out str/trim-newline)) +;; parser helpers +(defn- use-alias [target] + (if-let [alias-config (get (:aliases config) (keyword target))] + alias-config + (do (println "No such alias or invalid target.") + (System/exit 1)))) + +(defn- use-branch-or-tag [target branch-style? tag-style?] + (let [separator (if branch-style? #":" (if tag-style? #"@" #" ")) + target-array (str/split target separator) + app-name (if (= (first target-array) "") "nvim" (first target-array)) + repo (fs/path (System/getProperty "user.home") ".config" app-name) + no-mark (= (count target-array) 1)] + (when (> (count target-array) 2) + (println "Invalid target string.") + (System/exit 1)) + (cond-> {:app-name app-name} + branch-style? (assoc :branch (when-not no-mark (last target-array))) + tag-style? (assoc :tag (when-not no-mark (last target-array))) + (fs/exists? repo) (assoc :repo repo)))) + +;; TODO +#_(defn- use-remote [target] + #_(let [app-name (get-name-from-url target)] + (shell {:dir (fs/path (System/getProperty "user.home") ".config" app-name)} + "git clone" target))) + ;; target should be a string like "app-name:branch" or "app-name@tag" ;; returns a map with keys :app-name :branch :tag... (defn- parse-target-string [target-string] @@ -64,24 +91,8 @@ tag-style? (str/includes? target "@") alias-style? (and (not branch-style?) (not tag-style?))] (if alias-style? - ;; if use alias style - (if-let [alias-config (get (:aliases config) target)] - alias-config - (do (println "No such alias or invalid target.") - (System/exit 1))) - ;; if not alias style - (let [separator (if branch-style? #":" (if tag-style? #"@" #" ")) - target-array (str/split target separator) - app-name (if (= (first target-array) "") "nvim" (first target-array)) - repo (fs/path (System/getProperty "user.home") ".config" app-name) - no-mark (= (count target-array) 1)] - (when (> (count target-array) 2) - (println "Invalid target string.") - (System/exit 1)) - (cond-> {:app-name app-name} - branch-style? (assoc :branch (when-not no-mark (last target-array))) - tag-style? (assoc :tag (when-not no-mark (last target-array))) - (fs/exists? repo) (assoc :repo repo)))))) + (use-alias target) + (use-branch-or-tag target branch-style? tag-style?)))) ;; main ;; target should be a string like "app-name:branch" or "app-name@tag" -- cgit v1.2.3