diff options
Diffstat (limited to 'scripts/grep-write.clj')
| -rwxr-xr-x | scripts/grep-write.clj | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/scripts/grep-write.clj b/scripts/grep-write.clj new file mode 100755 index 0000000..e730dae --- /dev/null +++ b/scripts/grep-write.clj @@ -0,0 +1,30 @@ +#!/usr/bin/env bb + +(require '[clojure.string :as str]) + +(defn read-file-lines [filepath] + (-> filepath slurp (str/split #"\r?\n" -1))) + +(defn parse-vimgrep [line] + (when-let [[_ filepath linenr col text] (re-matches #"(.*):(\d+):(\d+):(.*)" line)] + [filepath (parse-long linenr) col text])) + +(defn update-line [filepath linenr text] + (let [lines (read-file-lines filepath) + line-changed? #(not= (nth lines (dec linenr)) text)] + (when (and (> (count lines) linenr) (line-changed?)) + (let [new-lines (assoc lines (dec linenr) text)] + (spit filepath (str/join "\n" new-lines)) + true)))) + +(defn apply-vimgrep-change [vimgrep-line] + (when-let [[filepath linenr _ text] (parse-vimgrep vimgrep-line)] + (update-line filepath linenr text))) + +(defn grep-write [] + (->> (line-seq (java.io.BufferedReader. *in*)) + (keep apply-vimgrep-change) + count)) + +(let [change-count (grep-write)] + (println (str "Applied " change-count " changes"))) |
