A peer-to-peer stack for code collaboration
Item newness module (#542)
James Haydon2fad6067e64d
diff --git a/bin/rad-issue b/bin/rad-issueindex 1b3877d..629763c 100755--- a/bin/rad-issue+++ b/bin/rad-issue@@ -43,6 +43,7 @@<state2> ...'.If the option '--fancy' is appended, the list is opened viafzf (note that fzf has to be installed).+ Unread issues are marked with a * before the timestamp.new - Create a new issue in $EDITORshow - Show an issuecomment - Add a comment to an issue@@ -82,7 +83,7 @@:number 10:title 50:author 20- :modified-at 20})+ :modified-at 22})(def wrap-cell(fn [phrase cell-key]@@ -98,35 +99,38 @@(wrap-cell "author" :author)(wrap-cell "updated" :modified-at))))+(def pretty-modified-at+ (fn [i]+ (string-append (lookup :modified-at i)+ (if (lookup :read? i) " " " *"))))+(def pretty-headline(fn [i](match i'{:number n:title tit:state s- :username un- :modified-at m-a}+ :username un}(string/unwords[(pretty-state s)(show n)titun- m-a]))))+ (pretty-modified-at i)]))))(def pretty-row(fn [i](match i- '{:number n+ '{:number n:title tit:state s- :username un- :modified-at m-a}+ :username un}(string-append(wrap-cell (pretty-state s) :state)(wrap-cell (show n) :number)(wrap-cell tit :title)(wrap-cell un :author)- (wrap-cell m-a :modified-at)))))+ (wrap-cell (pretty-modified-at i) :modified-at)))))(def pretty-comment"Markdown view of a comment. Assumes that the comment has been annotated with a@@ -173,6 +177,10 @@(add-username(insert :comments (map add-username (lookup :comments i)) i))))+(def enrich+ (fn [machine i]+ (insert :read? (newness/read-since-modified!? machine i) (add-username i))))+(def list-items"Lists the recent items and uses an editor to add a relevant comment."(fn [machine options]@@ -190,9 +198,9 @@(def items(map mk-key- (map (fn [i] (add-username-issue i))- (filter filt- (reverse (values (list-issues machine)))))))+ (map (fn [i] (enrich machine i))+ (filter filt+ (reverse (values (list-issues machine)))))))(def items-dict (dict-from-seq items))(if (empty-seq? items)@@ -230,7 +238,8 @@(if (integral? n)(do(def issue (verify-issue-number n machine))- (put-str! (pretty-issue (add-username-issue issue))))+ (put-str! (pretty-issue (enrich machine issue)))+ (newness/mark-read! machine machine))(cmd-parse-failure (error/whole-item-number :issue)))(cmd-parse-failure (error/missing-item-number :issue "show")))))@@ -243,7 +252,8 @@(do(verify-issue-number n machine)(simple-edit-issue! machine n {:state :closed})- (put-str! (string-append "Issue #" (show n) " has been closed.")))+ (put-str! (string-append "Issue #" (show n) " has been closed."))+ (newness/mark-read! machine n))(fn [_](put-str! (error/state-change-failure :issue "closed"))(exit! 1)))@@ -258,7 +268,8 @@(do(verify-issue-number n machine)(simple-add-comment! machine n comment (get-git-username!))- (put-str! (string-append "Added comment to issue #" (show n))))+ (put-str! (string-append "Added comment to issue #" (show n)))+ (newness/mark-read! machine n))(cmd-parse-failure (error/whole-item-number :issue)))(cmd-parse-failure (error/missing-item-number :issue "comment on")))))@@ -274,7 +285,8 @@(def i (read (edit-in-editor! template)))(def author {:git-username (get-git-username!)})(match (create-issue! machine (simple-issue (<> i author)))- ['n] (put-str! (string-append "Created issue #" (show n) " in " machine))+ ['n] (do (put-str! (string-append "Created issue #" (show n) " in " machine))+ (newness/mark-read! machine n))_ (put-str! (error/no-number-returned :issue)))))(def cmd-optionsdiff --git a/bin/rad-patch b/bin/rad-patchindex 23f3886..f9de9e0 100755--- a/bin/rad-patch+++ b/bin/rad-patch@@ -43,6 +43,7 @@<state2> ...'.If the option '--fancy' is appended, the list is opened viafzf (note that fzf has to be installed).+ Unread patches are marked with a * before the timestamp.propose - Create a new patch from a single commitshow - Show a patchcomment - Add a comment to a patch@@ -172,8 +173,9 @@:author-email author-email:patch patch})(match (simple-create-patch! machine patch)- ['n] (put-str! (string-append "Proposing patch #" (show n)- " to " machine))+ ['n] (do (put-str! (string-append "Proposing patch #" (show n)+ " to " machine))+ (newness/mark-read! machine n))_ (put-str! (error/no-number-returned :patch)))));; showing patches@@ -187,7 +189,7 @@:number 10:title 50:author 20- :modified-at 20})+ :modified-at 22})(def wrap-cell(fn [phrase cell-key]@@ -203,20 +205,24 @@(wrap-cell "author" :author)(wrap-cell "updated" :modified-at))))+(def pretty-modified-at+ (fn [i]+ (string-append (lookup :modified-at i)+ (if (lookup :read? i) " " " *"))))+(def pretty-headline(fn [i](match i'{:number n:title tit:state s- :author-name an- :modified-at m-a}+ :author-name an}(string/unwords[(pretty-state s)(show n)titan- m-a]))))+ (pretty-modified-at i)]))))(def pretty-row(fn [i]@@ -224,14 +230,13 @@'{:number n:title tit:state s- :author-name an- :modified-at m-a}+ :author-name an}(string-append(wrap-cell (pretty-state s) :state)(wrap-cell (show n) :number)(wrap-cell tit :title)(wrap-cell an :author)- (wrap-cell m-a :modified-at)))))+ (wrap-cell (pretty-modified-at i) :modified-at)))))(def pretty-comment(fn [c]@@ -263,6 +268,10 @@"---Add comment above the line.\n\n"x)))+(def add-read-status!+ (fn [machine i]+ (insert :read? (newness/read-since-modified!? machine i) i)))+(def list-items"Lists all patches and filters them by `options`. If `--new` is passed as anoption only recent items are shown. If `--fancy is passed, the list is opened@@ -282,8 +291,9 @@(def items(map mk-key- (filter filt- (reverse (values (list-patches machine))))))+ (map (fn [i] (add-read-status! machine i))+ (filter filt+ (reverse (values (list-patches machine)))))))(def items-dict (dict-from-seq items))(if (empty-seq? items)@@ -320,7 +330,8 @@(if (integral? n)(do(def patch (verify-patch-number n machine))- (put-str! (pretty-patch patch)))+ (put-str! (pretty-patch (add-read-status! machine patch)))+ (newness/mark-read! machine n))(cmd-parse-failure (error/whole-item-number :patch)))(cmd-parse-failure (error/missing-item-number :patch "show")))))@@ -334,7 +345,8 @@(def edit-state-with-feedback!(fn [message](catch 'daemon-error- (simple-edit-patch! machine n {:state new-state})+ (do (simple-edit-patch! machine n {:state new-state})+ (newness/mark-read! machine n))(fn [x](put-str!(error/state-change-failure :patch (pretty-state new-state)))@@ -359,7 +371,8 @@(do(verify-patch-number n machine)(simple-add-comment! machine n comment (get-git-username!))- (put-str! (string-append "Added comment to patch #" (show n))))+ (put-str! (string-append "Added comment to patch #" (show n)))+ (newness/mark-read! machine n))(cmd-parse-failure (error/whole-item-number :patch)))(cmd-parse-failure (error/missing-item-number :patch "comment on")))))diff --git a/docs/source/reference.rst b/docs/source/reference.rstindex 89bd68b..0064c71 100644--- a/docs/source/reference.rst+++ b/docs/source/reference.rst@@ -663,6 +663,11 @@ Reads a radicle value from a string.Reads many radicle values from a string.+``(<= x y)``+~~~~~~~~~~~~++Test if ``x`` is less than or equal to ``y``.+``prelude/patterns``--------------------@@ -1312,6 +1317,57 @@ Like ``set``, but for refs.Like ``over``, but for refs.+``prelude/io-utils``+--------------------++IO-related utilities++``(fzf-select! xs)``+~~~~~~~~~~~~~~~~~~~~++Select one of many strings with ``fzf``. Requires that ``fzf`` be on the+path. Returns ``[:just x]`` where ``x`` is the selected string, or+``:nothing`` if nothing was selected.++``(edit-in-editor! orig)``+~~~~~~~~~~~~~~~~~~~~~~~~~~++Open ``$EDITOR`` on a file prepopulated with ``orig``. Returns the+contents of the edited file when the editor exits.++``(get-git-config! key)``+~~~~~~~~~~~~~~~~~~~~~~~~~++Get the value associated with a key in git config.++``(set-git-config! key value)``+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~++Set the value associated with a key in git config.++``(get-git-commit-data! format commit)``+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~++Get data from a ``commit`` via ``show`` specified by ``format``++``(get-git-username!)``+~~~~~~~~~~~~~~~~~~~~~~~++Get the user name stored in git config.++``(process-git-with-exit! args msg)``+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~++Processes a git command ``args``. If it fails, the message ``msg`` is+shown and the process exits, otherwise ``:ok`` is passed.++``(base-path!)``+~~~~~~~~~~~~~~~~++Returns the base path for storage of radicle related config files. By+default this is ``$HOME/.config/radicle``. This can be adjusted by+setting ``$XDG_CONFIG_HOME``.+``prelude/key-management``--------------------------diff --git a/rad/prelude.rad b/rad/prelude.radindex 81f942e..24b347a 100644--- a/rad/prelude.rad+++ b/rad/prelude.rad@@ -19,6 +19,7 @@(file-module! "prelude/set.rad")(file-module! "prelude/ref.rad")(file-module! "prelude/lens.rad")+(file-module! "prelude/io-utils.rad")(file-module! "prelude/key-management.rad")(file-module! "prelude/machine.rad")(file-module! "prelude/machine-remote.rad")@@ -26,11 +27,11 @@(file-module! "prelude/validation.rad")(file-module! "prelude/nonces.rad")(file-module! "prelude/util.rad")-(file-module! "prelude/io-utils.rad")(file-module! "prelude/time.rad")(file-module! "prelude/auth.rad")(file-module! "prelude/test.rad")(file-module! "prelude/cmd-parsing.rad")+(file-module! "prelude/track-newness.rad")(import prelude/basic :unqualified)(import prelude/bool :unqualified)@@ -41,11 +42,12 @@(import prelude/maybe :unqualified)(import prelude/list :unqualified)(import prelude/ref :unqualified)+(import prelude/io-utils :unqualified)(import prelude/validation :as 'validator)(import prelude/nonces :unqualified)(import prelude/util :unqualified)-(import prelude/io-utils :unqualified)(import prelude/auth :as 'auth)(import prelude/machine :as 'machine)(import prelude/machine-remote :unqualified)(import prelude/cmd-parsing :unqualified)+(import prelude/track-newness :as 'newness)diff --git a/rad/prelude/basic.rad b/rad/prelude/basic.radindex 4a79f15..504d74b 100644--- a/rad/prelude/basic.rad+++ b/rad/prelude/basic.rad@@ -1,6 +1,6 @@{:module 'prelude/basic:doc "Basic function used for checking equality, determining the type of a value, etc."- :exports '[or some empty-seq? length maybe->>= maybe-foldlM elem? head tail read read-many]}+ :exports '[or some empty-seq? length maybe->>= maybe-foldlM elem? head tail read read-many <=]};; or@@ -102,3 +102,7 @@(:test "read-many"[ (read-many "([:one] {:two 3}) (four \"five\" 6)") ==> [([:one] {:two 3}) (four "five" 6)]])++(def <=+ "Test if `x` is less than or equal to `y`."+ (fn [x y] (or (eq? x y) (< x y))))diff --git a/rad/prelude/io-utils.rad b/rad/prelude/io-utils.radindex 4f84e23..55cf653 100644--- a/rad/prelude/io-utils.rad+++ b/rad/prelude/io-utils.rad@@ -2,6 +2,7 @@:doc "IO-related utilities":exports '[fzf-select! edit-in-editor! get-git-config! set-git-config!get-git-commit-data! get-git-username! process-git-with-exit!+ base-path!]}(import prelude/io '[shell-with-stdout! write-file! process-with-stdout!] :unqualified)@@ -82,3 +83,16 @@_ (do(put-str! msg)(exit! 1)))))++(def base-path!+ "Returns the base path for storage of radicle related config files. By default+ this is `$HOME/.config/radicle`. This can be adjusted by setting+ `$XDG_CONFIG_HOME`."+ (fn []+ (def xgd-config-home (unlines (shell-with-stdout! "echo $XDG_CONFIG_HOME" "")))+ (def home (unlines (shell-with-stdout! "echo $HOME" "")))+ (def config-path+ (if (eq? xgd-config-home "")+ (string-append home "/.config")+ xgd-config-home))+ (string-append config-path "/radicle")))diff --git a/rad/prelude/key-management.rad b/rad/prelude/key-management.radindex 439f910..7431592 100644--- a/rad/prelude/key-management.rad+++ b/rad/prelude/key-management.rad@@ -5,6 +5,7 @@:exports '[read-keys! get-keys! create-keys! set-fake-keys! use-fake-keys!]}(import prelude/io :as 'io)+(import prelude/io-utils :as 'io)(import prelude/strings :as 'string)(import prelude/lens :unqualified)@@ -25,19 +26,9 @@This is intended for testing."(fn [] (set-fake-keys! (gen-key-pair! (default-ecc-curve)))))-(def base-path- (fn []- (def xgd-config-home (string/unlines (io/shell-with-stdout! "echo $XDG_CONFIG_HOME" "")))- (def home (string/unlines (io/shell-with-stdout! "echo $HOME" "")))- (def config-path- (if (eq? xgd-config-home "")- (string-append home "/.config")- xgd-config-home))- (string-append config-path "/radicle")))-(def key-path(fn []- (string-append (base-path) "/my-keys.rad")))+ (string-append (io/base-path!) "/my-keys.rad")))(def read-keys!"Reads the keys stored in `my-keys.rad` or returns `:nothing` if the file@@ -62,7 +53,7 @@"Creates a new key pair and stores it in `my-keys.rad`. Returns thefull absolute path of the created file."(fn []- (io/shell! (string-append "mkdir -p " (base-path)) "")+ (io/shell! (string-append "mkdir -p " (io/base-path!)) "")(def kp (gen-key-pair! (default-ecc-curve)))(io/write-file! (key-path) (show kp))(key-path)))diff --git a/rad/prelude/track-newness.rad b/rad/prelude/track-newness.radnew file mode 100644index 0000000..c0dddce--- /dev/null+++ b/rad/prelude/track-newness.rad@@ -0,0 +1,40 @@+{:module 'prelude/track-newness+ :doc "Provides functions for marking items as `seen` or `read`. Per default,+ this is stored in a file in `$HOME/.config/radicle/item-newness.rad` this can+ be adjusted by setting `$XDG_CONFIG_HOME`."+ :exports '[init-newness-file! mark-read! get-item-newness! read-since-modified!?]}++(import prelude/basic :unqualified)+(import prelude/io :as 'io)+(import prelude/io-utils :as 'io)+(import prelude/patterns :unqualified)++(def path!+ (fn [id]+ (string-append (io/base-path!) "/" id "/item-newness.rad")))++(def init-newness-file!+ (fn [id]+ (io/shell! (string-append "mkdir -p " (io/base-path!) "/" id) "")+ (io/init-file-dict! (path! id))))++(def mark-read!+ (fn [machine-id item-id]+ (init-newness-file! machine-id)+ (io/write-file-key! (path! machine-id) item-id (now!))))++(def get-item-newness!+ (fn [machine-id item-id]+ (init-newness-file! machine-id)+ (match (io/read-file-key! (path! machine-id) item-id)+ :nothing :unread+ (/just 'x) [:read x])))++(def read-since-modified!?+ "Returns true if an item has not been read, or if it has been modified since it+ was last read. Assumes the item has a `:number` field for a unique ID, and a+ `:modified-at` field containing the datetime at which it was last modified."+ (fn [machine-id item]+ (match (get-item-newness! machine-id (lookup :number item))+ :unread #f+ [:read 't] (<= (unix-epoch (lookup :modified-at item)) (unix-epoch t)))))diff --git a/reference-doc.yaml b/reference-doc.yamlindex 36df392..0d6a73b 100644--- a/reference-doc.yaml+++ b/reference-doc.yaml@@ -145,6 +145,7 @@ modules:- prelude/set- prelude/ref- prelude/lens+- prelude/io-utils- prelude/key-management- prelude/machine- prelude/state-machine