Skip to content

A peer-to-peer stack for code collaboration

Item newness improvements (#577)

James Haydoneb178b050da5
diff --git a/bin/rad-issue b/bin/rad-issue
index 17b08c0..f5c6398 100755
--- a/bin/rad-issue
+++ b/bin/rad-issue
@@ -33,8 +33,9 @@
Usage:
rad issue list " list-opts-help "
rad issue new
- rad issue [show | close] <issue-number>
+ rad issue [show | close | mark-read | mark-unread] <issue-number>
rad issue comment <issue-number> <comment>
+ rad all-read
rad issue help
list - Lists all issues
@@ -46,12 +47,17 @@
<state2> ...'.
If the option '--fancy' is appended, the list is opened via
fzf (note that fzf has to be installed).
+ If the option '--unread' is appended, only unread issues
+ are listed.
Unread issues are marked with a * before the timestamp.
new - Create a new issue in $EDITOR
show - Show an issue
comment - Add a comment to an issue
close - Close an issue
This command is restricted to the maintainer of the project.
+ mark-read - Mark issue as read
+ mark-unread - Mark issue as unread
+ all-read - Mark all issues as read
help - Print this help and exit
"))
@@ -198,16 +204,21 @@
(def issues (values (list-issues machine)))
- (def filt
+ (def state-filt
(match (map (fn [opt] (read (string-append ":" opt))) (lookup :state options))
[] (fn [x] #t)
'args (fn [x] (elem? (lookup :state x) args))))
+ (def newness-filt
+ (if (lookup :unread options)
+ (fn [x] (not (lookup :read? x)))
+ (fn [x] #t)))
+
(def items
(map mk-key
- (map (fn [i] (enrich-issue machine i))
- (filter filt
- (reverse issues)))))
+ (filter (and-predicate state-filt newness-filt)
+ (map (fn [i] (enrich-issue machine i))
+ (reverse issues)))))
(def items-dict (dict-from-seq items))
(if (empty-seq? items)
@@ -286,6 +297,7 @@
[
{ :key :state :type :multi-opt :options ["-s" "--state" "--filter-by-state"] :possible-values valid-states :default ["open"]}
{ :key :fancy :type :flag :options ["--fancy"] :default #f }
+ { :key :unread :type :flag :options ["--unread"] :default #f }
])
(def /list-cmd
@@ -298,6 +310,11 @@
(fn [action num-str f]
(whole-num help :issue action num-str f)))
+(def mark-all-read!
+ (fn [machine]
+ (map (fn [i] (newness/mark-read! machine (lookup :number i)))
+ (values (list-issues machine)))))
+
(machine/catch-daemon!
(fn []
(match args
@@ -307,6 +324,8 @@
(/cmd-1 "show" 'n help) (whole-issue-num "show" n (fn [n] (show-issue! n (machine-name))))
(/cmd-2 "comment" 'n 'comment help) (whole-issue-num "comment on" n (fn [n] (create-comment! (machine-name) n comment)))
(/cmd-1 "mark-read" 'n help) (whole-issue-num "mark as read" n (fn [n] (newness/mark-read! (machine-name) n)))
+ (/cmd-1 "mark-unread" 'n help) (whole-issue-num "mark as unread" n (fn [n] (newness/mark-unread! (machine-name) n)))
+ (/cmd-0 "all-read" help) (mark-all-read! (machine-name))
["help"] (put-str! help)
["-h"] (put-str! help)
["--help"] (put-str! help)
diff --git a/bin/rad-patch b/bin/rad-patch
index 6e4d2f3..8edf112 100755
--- a/bin/rad-patch
+++ b/bin/rad-patch
@@ -33,8 +33,9 @@
Usage:
rad patch list " list-opts-help "
rad patch propose <commit>
- rad patch [show | retract | accept | reject | checkout] <patch-number>
+ rad patch [show | retract | accept | reject | checkout | mark-read | mark-unread] <patch-number>
rad patch comment <patch-number> <comment>
+ rad patch all-read
rad patch help
list - Lists all patches
@@ -46,6 +47,8 @@
<state2> ...'.
If the option '--fancy' is appended, the list is opened via
fzf (note that fzf has to be installed).
+ If the option '--unread' is appended, only unread patches
+ are listed.
Unread patches are marked with a * before the timestamp.
propose - Create a new patch from a single commit
show - Show a patch
@@ -61,6 +64,9 @@
patch to it.
reject - Reject a patch
This command is restricted to the maintainer of the project.
+ mark-read - Mark patch as read
+ mark-unread - Mark patch as unread
+ all-read - Mark all patches as read
help - Print this help and exit
"))
@@ -287,17 +293,22 @@
[(pretty-headline patch) patch]
[(pretty-row patch) patch])))
- (def filt
+ (def state-filt
(match (map (fn [opt] (read (string-append ":" opt))) (lookup :state options))
[] (fn [x] #t)
'args (fn [x] (elem? (lookup :state x) args))))
(def patches (values (list-patches machine)))
+ (def newness-filt
+ (if (lookup :unread options)
+ (fn [x] (not (lookup :read? x)))
+ (fn [x] #t)))
+
(def items
(map mk-key
- (map (fn [i] (add-read-status! machine i))
- (filter filt
+ (filter (and-predicate state-filt newness-filt)
+ (map (fn [i] (add-read-status! machine i))
(reverse patches)))))
(def items-dict (dict-from-seq items))
@@ -367,10 +378,16 @@
(put-str! (string-append "Added comment to patch #" (show n)))
(newness/mark-read! machine n)))
+(def mark-all-read!
+ (fn [machine]
+ (map (fn [i] (newness/mark-read! machine (lookup :number i)))
+ (values (list-patches machine)))))
+
(def cmd-options
[
{ :key :state :type :multi-opt :options status-opts :possible-values valid-states :default ["pending"]}
{ :key :fancy :type :flag :options ["--fancy"] :default #f }
+ { :key :unread :type :flag :options ["--unread"] :default #f }
])
(def /list-cmd
@@ -391,11 +408,13 @@
(/cmd-1 "checkout" 'n help) (whole-patch-num "checkout" n (fn [n] (checkout-patch! (machine-name) n)))
(/cmd-1 "show" 'n help) (whole-patch-num "show" n (fn [n] (show-patch! (machine-name) n)))
(/cmd-1 "mark-read" 'n help) (whole-patch-num "mark as read" n (fn [n] (newness/mark-read! (machine-name) n)))
+ (/cmd-1 "mark-unread" 'n help) (whole-patch-num "mark as unread" n (fn [n] (newness/mark-unread! (machine-name) n)))
(/cmd-1 "retract" 'n help) (whole-patch-num "retract" n (fn [n] (edit-patch-state! (machine-name) n :retracted)))
(/cmd-1 "accept" 'n help) (whole-patch-num "accept" n (fn [n] (edit-patch-state! (machine-name) n :accepted)))
(/cmd-1 "reject" 'n help) (whole-patch-num "reject" n (fn [n] (edit-patch-state! (machine-name) n :rejected)))
(/cmd-2 "comment" 'n 'comment help) (whole-patch-num "comment on" n (fn [n] (create-comment! (machine-name) n comment)))
(/cmd-0 "create-machine" help) (new-patch-machine! (machine-name))
+ (/cmd-0 "all-read" help) (mark-all-read! (machine-name))
["help"] (put-str! help)
["-h"] (put-str! help)
["--help"] (put-str! help)
diff --git a/docs/source/reference.rst b/docs/source/reference.rst
index 0124b18..14b0dc2 100644
--- a/docs/source/reference.rst
+++ b/docs/source/reference.rst
@@ -742,6 +742,11 @@ Returns ``y`` if ``x`` is not ``#f``, otherwise returns ``x``
Checks that all the items of a list are truthy.
+``(and-predicate f g)``
+~~~~~~~~~~~~~~~~~~~~~~~
+
+Pointwise conjunction of predicates.
+
``prelude/seq``
---------------
@@ -1067,6 +1072,14 @@ Like ``lookup`` but returns ``default`` if the key is not in the map.
Like ``lookup`` but returns ``[:just x]`` if the key is not in the map
and ``:nothing`` otherwise.
+``(safe-modify-map k f d)``
+~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+Modifies the association of a value to a key ``k`` in a dict ``d``. The
+function ``f`` will receive ``[:just v]`` if ``(eq? (lookup k d) v)``,
+otherwise it will receive ``:nothing``. It should return
+``[:just new-v]`` to change the value, and ``:nothing`` to remove it.
+
``(group-by f xs)``
~~~~~~~~~~~~~~~~~~~
@@ -1166,6 +1179,12 @@ Read a file key. Assumes that the file contents is a serialised dict.
Write a key to a file. Assumes that the file contents is a serialised
dict.
+``(delete-file-key! file k)``
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+Delete a key from a file. Assumes that the file contents is a serialised
+dict.
+
``(ls!)``
~~~~~~~~~
diff --git a/rad/prelude/bool.rad b/rad/prelude/bool.rad
index 0a20ea0..b3af177 100644
--- a/rad/prelude/bool.rad
+++ b/rad/prelude/bool.rad
@@ -1,6 +1,6 @@
{:module 'prelude/bool
:doc "Functions for dealing with truthiness and #f."
- :exports '[not and all]}
+ :exports '[not and all and-predicate]}
;; boolean functions ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
@@ -37,3 +37,7 @@
[ (all '(#t #t #t #t)) ==> #t ]
[ (all '(#t #t #f #t)) ==> #f ]
)
+
+(def and-predicate
+ "Pointwise conjunction of predicates."
+ (fn [f g] (fn [x] (and (f x) (g x)))))
diff --git a/rad/prelude/cmd-parsing.rad b/rad/prelude/cmd-parsing.rad
index 23ca959..9d81574 100644
--- a/rad/prelude/cmd-parsing.rad
+++ b/rad/prelude/cmd-parsing.rad
@@ -38,16 +38,15 @@
once in a command, a list of valid options is passed with `:possible-values`).
If `opt` is empty, the default for each `cmd-opts` is returned. "
(fn [cmd opts cmd-opts help]
- (def empty-opts
- (dict-from-seq
- (map (fn [opt]
- (match opt
- {:key 'k :type :multi-opt} [k []]
- {:key 'k :default 'd} [k d]))
- cmd-opts)))
- (def default
- (dict-from-seq
- (map (fn [opt] (list (lookup :key opt) (lookup :default opt))) cmd-opts)))
+ (def add-defaults
+ (fn [givens]
+ (dict-from-seq
+ (map (fn [opt]
+ (def k (lookup :key opt))
+ [k (match (lookup-maybe k givens)
+ :nothing (lookup :default opt)
+ [:just 'given] given)])
+ cmd-opts))))
(def-rec parse-cli
(fn [args]
(def parse-arg
@@ -58,7 +57,12 @@
(match args
(/cons 'val 'rest)
(if (elem? val valid-args)
- (modify-map opt-key (fn [x] (cons val x)) (parse-cli rest))
+ (safe-modify-map opt-key
+ (fn [maybe-xs]
+ (match maybe-xs
+ :nothing [:just [val]]
+ [:just 'xs] [:just (cons val xs)]))
+ (parse-cli rest))
(do
(if (starts-with? val "-")
(parse-failure
@@ -79,12 +83,11 @@
(insert (lookup :key option) #t (parse-cli rest))
(parse-arg option rest))))
(match args
- /nil empty-opts
+ /nil {}
(/cons 'opt 'rest) (parse-opt opt rest))))
(fn [v]
(match v
- [cmd] [:just {opts default}]
- (/cons cmd 'os) [:just {opts (parse-cli os)}]
+ (/cons cmd 'os) [:just {opts (add-defaults (parse-cli os))}]
_ :nothing))))
(:test "/cmd-opts"
diff --git a/rad/prelude/dict.rad b/rad/prelude/dict.rad
index 108e51f..05a2cd8 100644
--- a/rad/prelude/dict.rad
+++ b/rad/prelude/dict.rad
@@ -1,6 +1,6 @@
{:module 'prelude/dict
:doc "Functions for manipualting dicts."
- :exports '[dict-from-seq keys values rekey modify-map delete-many lookup-default lookup-maybe group-by]}
+ :exports '[dict-from-seq keys values rekey modify-map delete-many lookup-default lookup-maybe safe-modify-map group-by]}
(import prelude/patterns :unqualified)
@@ -77,6 +77,21 @@
[ (lookup-default :one 2 {:one 1}) ==> 1 ]
[ (lookup-default :two 2 {:one 1}) ==> 2 ])
+(def safe-modify-map
+ "Modifies the association of a value to a key `k` in a dict `d`. The function
+ `f` will receive `[:just v]` if `(eq? (lookup k d) v)`, otherwise it will
+ receive `:nothing`. It should return `[:just new-v]` to change the value, and
+ `:nothing` to remove it."
+ (fn [k f d]
+ (match (f (lookup-maybe k d))
+ :nothing (delete k d)
+ [:just 'v] (insert k v d))))
+
+(:test "safe-modify-map"
+ [ (safe-modify-map :foo (fn [x] (match x :nothing [:just 0] [:just 'y] [:just (+ 1 y)])) {}) ==> {:foo 0} ]
+ [ (safe-modify-map :foo (fn [x] (match x :nothing [:just 0] [:just 'y] [:just (+ 1 y)])) {:foo 42}) ==> {:foo 43} ]
+ [ (safe-modify-map :foo (fn [x] (match x :nothing [:just 0] [:just _] :nothing)) {:foo 42}) ==> {}])
+
(def group-by
"Partitions the values of a sequence `xs` according to the images under `f`. The
partitions are returned in a dict keyed by the return value of `f`."
diff --git a/rad/prelude/io.rad b/rad/prelude/io.rad
index 5489b2f..30fe651 100644
--- a/rad/prelude/io.rad
+++ b/rad/prelude/io.rad
@@ -4,7 +4,7 @@
shell-with-stdout! shell-no-stdin! write-file!
process-with-stdout! process-with-stdout-stderr-exitcode!
process-with-stdout-strict!
- init-file-dict! read-file-key! write-file-key! ls! modify-file!
+ init-file-dict! read-file-key! write-file-key! delete-file-key! ls! modify-file!
install-fake-filesystem! prompt!
]}
@@ -242,6 +242,12 @@ process exited normally and `[:error n]` otherwise. Example: `(process!
(modify-file! file
(fn [s] (insert k v s)))))
+(def delete-file-key!
+ "Delete a key from a file. Assumes that the file contents is a serialised dict."
+ (fn [file k]
+ (modify-file! file
+ (fn [s] (delete k s)))))
+
(def read-file-key!
"Read a file key. Assumes that the file contents is a serialised dict."
(fn [file k]
diff --git a/rad/prelude/track-newness.rad b/rad/prelude/track-newness.rad
index c0dddce..5f5757a 100644
--- a/rad/prelude/track-newness.rad
+++ b/rad/prelude/track-newness.rad
@@ -2,7 +2,7 @@
: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!?]}
+ :exports '[init-newness-file! mark-read! mark-unread! get-item-newness! read-since-modified!? mark-all-read!]}
(import prelude/basic :unqualified)
(import prelude/io :as 'io)
@@ -23,6 +23,11 @@
(init-newness-file! machine-id)
(io/write-file-key! (path! machine-id) item-id (now!))))
+(def mark-unread!
+ (fn [machine-id item-id]
+ (init-newness-file! machine-id)
+ (io/delete-file-key! (path! machine-id) item-id)))
+
(def get-item-newness!
(fn [machine-id item-id]
(init-newness-file! machine-id)
@@ -38,3 +43,8 @@
(match (get-item-newness! machine-id (lookup :number item))
:unread #f
[:read 't] (<= (unix-epoch (lookup :modified-at item)) (unix-epoch t)))))
+
+(def mark-all-read!
+ "Mark a list of items as read. Assumes the items have a `:number` field."
+ (fn [machine-id items]
+ (map (fn [i] (mark-read! machine-id (lookup :number i))) items)))