From a7c081747dc0ec4404f6a17dc3f9141316cdc534 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Som=C4=93=20Cho?= Date: Sun, 7 May 2023 21:06:19 +0200 Subject: [PATCH] add script to resume last modified Taskwarrior task using Taskwarrior hooks --- install.clj | 3 ++- on-modify-log.clj | 8 ++++++ resumetask.clj | 64 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 74 insertions(+), 1 deletion(-) create mode 100755 on-modify-log.clj create mode 100755 resumetask.clj diff --git a/install.clj b/install.clj index 2350e63..fcf77a5 100755 --- a/install.clj +++ b/install.clj @@ -18,7 +18,8 @@ (:out) (split #"\n")) cljfiles (filter #(includes? % ".clj") files) - scripts (filter #(not= % "install.clj") cljfiles) + scripts (filter #(and (not= % "install.clj") + (not= % "on-modify-log.clj")) cljfiles) extensionless (vec (map drop-extension scripts)) home (System/getProperty "user.home") path "/.local/bin/" diff --git a/on-modify-log.clj b/on-modify-log.clj new file mode 100755 index 0000000..982aba8 --- /dev/null +++ b/on-modify-log.clj @@ -0,0 +1,8 @@ +#!/usr/bin/env bb +(require '[cheshire.core :as json]) + +(let [oldtask (json/parse-string (read-line) true) + newtask (json/parse-string (read-line) true) + uuid (:uuid oldtask)] + (spit "../last-modified.data" uuid) + (println (json/generate-string newtask))) diff --git a/resumetask.clj b/resumetask.clj new file mode 100755 index 0000000..e24b425 --- /dev/null +++ b/resumetask.clj @@ -0,0 +1,64 @@ +#!/usr/bin/env bb +(require '[clojure.string :as str] + '[clojure.java.io :as io] + '[clojure.java.shell :as sh]) + +(defn get-taskwarrior-data-location [] + (let [home (System/getProperty "user.home") + taskrc (-> (try (slurp (str home "/.taskrc")) + (catch Exception _ + (slurp (str home "/.config/task/taskrc")))) + (as-> rc (str/split rc #"\n"))) + ;On NixOS or using Homemanager, the taskrc may be + ;in another place. The following lines tries to find + ;that place. + config (-> taskrc + (as-> lines + (filter #(str/includes? % "include") lines)) + (as-> lines + (map #(drop 8 %) lines)) + (as-> lines + (map #(str/join "" %) lines)) + (as-> paths + (map #(slurp %) paths)) + (as-> lines + (map #(str/split % #"\n") lines)) + (flatten) + (concat taskrc)) + datalocation (-> config + (as-> c + (filter #(str/includes? % "data.location") c)) + (first) + (as-> line (drop 14 line)) + (as-> ch (str/join "" ch)) + (str/replace "~" home))] + datalocation)) + +(defn get-task-id [uuid] + (let [stdout (sh/sh "task" uuid) + id-line (-> (:out stdout) + (str/split #"\n") + (nth 3))] + (when-not (str/includes? id-line "ID") + (println "Task with UUID" uuid "does not exist")) + (when (-> (:out stdout) + (str/split #"\n") + (nth 5) + (str/split #" ") + (last) + (= "Completed")) + (println "Task with" uuid "is already completed")) + (-> (str/split id-line #" ") + (as-> c (filter not-empty c)) + (last)))) + +(let [data-path (get-taskwarrior-data-location) + modified-path (str data-path "/last-modified.data")] + (when-not (.exists (io/file modified-path)) + (println "No last modified data. Have you installed and used the hook?") + (System/exit 1)) + (-> (slurp modified-path) + (get-task-id) + (as-> id (sh/sh "task" id "start")) + (:out) + (println)))