Skip to content

A peer-to-peer stack for code collaboration

Recent activity (#349)

James Haydon7df6701dbe7a
diff --git a/bin/rad-issues b/bin/rad-issues
index f8d26c0..5f43f9e 100755
--- a/bin/rad-issues
+++ b/bin/rad-issues
@@ -13,12 +13,14 @@
"rad-issues - Radicle issue CLI
Usage:
- rad-issues [list|new|create-chain] <chain-name>
+ rad-issues [list|list-new|new|create-chain] <chain-name>
rad-issues make-key <nick>
rad-issues names
rad-issues help
list - Select from all issues to view or comment on
+ list-new - Select from all issues that have new activity
+ done - Mark all issues as 'seen'
new - Create a new issue in $EDITOR
create-chain - Create a new issues chain
make-key - Create a key-pair and register the name
@@ -38,56 +40,118 @@
(fn [chain]
(ref (chain/load-chain! (make-name chain)))))
-(def list-issues
- (fn [chain]
+(def mark-last-seen
+ (fn []
+ (write-file-key! ".radicle-issues" :last-mark-done (now!))))
+
+(def comment-divider "---")
+
+(def add-comment-md
+ (fn [x]
+ (string-append
+ "\n" comment-divider "\n"
+ "---Add comment above the line.\n\n"
+ x)))
+
+(def pretty-comment
+ "Markdown view of a comment. Assumes that the comment has been annotated with a
+ username."
+ (fn [c]
+ (match c
+ '{:username u-n
+ :created-at c-a
+ :body b}
+ (string-append "### " u-n " [" c-a "]\n\n" b "\n"))))
+
+(def pretty-issue
+ "Markdown view of an issue. Assumes the issue and comments have been annotated
+ with usernames."
+ (fn [i]
+ (match i
+ '{:number n
+ :title tit
+ :body b
+ :state state
+ :labels ls
+ :comments cs}
+ (string-replace
+ "\r"
+ ""
+ (string-append
+ "# Issue " (show n) ": " tit "\n\n"
+ "**State:** " (if (eq? state :open) "open" "closed") "\n"
+ "**Labels:** " (show ls) "\n\n"
+ b "\n\n"
+ "Comments\n"
+ "--------\n\n"
+ (string/unlines (map pretty-comment cs)))))))
+
+(def list-items
+ "Lists the recent items and uses an editor to add a relevant comment."
+ (fn [chain last-seen]
+
(def chain-ref (make-chain-ref chain))
(load! "rad/monadic/names.rad")
(def names (ref (chain/load-chain! name-base)))
- (def mk-key (fn [v]
- (match v
- ['key 'val] [(string-append (show key) " - " (lookup :title val)) val])))
- (def iss (dict-from-seq (map mk-key (seq (list-issues chain-ref)))))
- (def selected (fzf-select-by-key! iss))
- (def comment-divider "---")
- (def username
+ (def add-username
(fn [d]
- (catch
- 'any
- (lookup :github-username d)
- (fn [x]
- (match (lookup-key names (lookup :author d))
- :nothing "???"
- 'name name)))))
- (def issue-number (fn [d] (lookup :number d)))
- (def body (fn [d] (lookup :body d)))
- (def pretty-comment (fn [c]
- (string-append
- "### " (username c) " [" (lookup :created-at c) "]\n\n"
- (body c) "\n")))
- (def pretty-issue (fn [i]
- (string-replace "\r" "" (string-append
- "\n" comment-divider "\n"
- "---Add comment above the line.\n\n"
- "# Issue " (show (issue-number i)) ": " (lookup :title i) "\n\n"
- "**State:** " (if (eq? (lookup :state i) :open) "open" "closed") "\n"
- "**Labels:** " (show (lookup :labels i)) "\n\n"
- (body i) "\n\n"
- "Comments\n"
- "--------\n\n"
- (string/unlines (map pretty-comment (lookup :comments i)))
- ))))
- (def edited-issue (edit-in-editor! (pretty-issue selected)))
- (def extract-comment
- (fn [xs]
- (string/unlines (take-while (fn [x] (not (eq? x comment-divider))) (string/lines xs)))))
- (def send-comment
- (fn [xs]
- (def comment (extract-comment edited-issue))
- (if (eq? comment "")
- "Issue was not updated."
- (do (simple-add-comment! chain-ref (issue-number selected) comment)
- (string-append "Sent comment: " comment)))))
- (put-str! (send-comment edited-issue))))
+ (def n (catch
+ 'any
+ (lookup :github-username d)
+ (fn [x]
+ (match (lookup-key names (lookup :author d))
+ :nothing "???"
+ 'name name))))
+ (insert :username n d)))
+
+ (def add-username-issue
+ (fn [i]
+ (add-username
+ (insert :comments (map add-username (lookup :comments i)) i))))
+
+ (def mk-key
+ (fn [v]
+ (match v
+ (/as 'a {:activity :new-issue
+ :issue {:number 'n
+ :title 'tit}})
+ [(string-append "[new] Issue " (show n) " - " tit) a]
+ (/as 'a {:activity :new-comments
+ :issue {:number 'n
+ :title 'tit}})
+ [(string-append "[new comments] Issue " (show n) " - " tit) a]
+ (/as 'i {:issue {:number 'n :title 'tit}})
+ [(string-append "Issue " (show n) " - " tit) i])))
+
+ (def items
+ (map mk-key
+ (match last-seen
+ (/just 't) (recent-activity chain-ref t)
+ _ (map (fn [i] {:issue i})
+ (values (list-issues chain-ref))))))
+
+ (if (empty-seq? items)
+ (put-str! "No recent items!")
+ (match (fzf-select! (map first items))
+ (/just 's)
+ (do (def selected (lookup s (dict-from-seq items)))
+ (def issue-number (match selected {:issue {:number 'n}} n))
+ (def pretty-item
+ (add-comment-md
+ (pretty-issue
+ (add-username-issue
+ (lookup :issue selected)))))
+ (def edited-item (edit-in-editor! pretty-item))
+ (def added-comment
+ (string/unlines
+ (take-while (fn [x] (not (eq? x comment-divider)))
+ (string/lines edited-item))))
+ (put-str!
+ (if (eq? added-comment "")
+ "Item was not updated."
+ (do (simple-add-comment! chain-ref issue-number added-comment)
+ (string-append "Sent comment: " added-comment)))))
+ _ (put-str! "No selection made.")))))
(def new-issue
(fn [chain]
@@ -99,10 +163,9 @@
:labels []
}
")
- (def iss (read (edit-in-editor! template)))
+ (def i (read (edit-in-editor! template)))
(create-issue!
- chain-ref
- (<> iss {:state :open :comments [] :created-at (now!)}))))
+ chain-ref (simple-issue i))))
(def create-chain
(fn [chain]
@@ -120,13 +183,17 @@
(load! "rad/monadic/names.rad")
(print! (list-names name-base))))
+(init-file-dict! ".radicle-issues")
+
(def args (get-args!))
(match args
- ["list" 'chain] (list-issues chain)
+ ["list-new" 'chain] (list-items chain (read-file-key! ".radicle-issues" :last-mark-done))
+ ["list" 'chain] (list-items chain :nothing)
["names"] (names)
["make-key" 'name] (make-key name)
["new" 'chain] (new-issue chain)
["create-chain" 'chain] (create-chain chain)
+ ["done"] (mark-last-seen)
["help"] (put-str! help)
_ (do
(put-str! (string-append "Unrecognized command" (show args)))
diff --git a/rad/monadic/issues.rad b/rad/monadic/issues.rad
index 0e7d669..6b3fbf0 100644
--- a/rad/monadic/issues.rad
+++ b/rad/monadic/issues.rad
@@ -36,15 +36,14 @@
(def simple-issue
"Create an open issue with no comments or labels."
- (fn [tit bod]
+ (fn [i]
(def t (now!))
- {:title tit
- :body bod
- :labels []
- :state :open
- :comments []
- :created-at t
- :modified-at t}))
+ (<> {:labels []
+ :state :open
+ :comments []
+ :created-at t
+ :modified-at t}
+ i)))
(def simple-comment
"Create a comment for an issue."
@@ -84,7 +83,7 @@
(def simple-create-issue!
"Create a remote issue with sensible defaults."
(fn [chain tit bod]
- (create-issue! chain (simple-issue tit bod))))
+ (create-issue! chain (simple-issue {:title tit :body bod}))))
(def simple-add-comment!
"Create a remote comment."
@@ -154,30 +153,39 @@
(def is (sort-by (fn [i] (- 0 (lookup :number i))) (values (list-issues chain))))
(take n is)))
-(def get-recent
- "Returns the activity on the chain since a timestamp. This is a dict with two
- keys: `:new-issues` and `:new-comments`. The comments are annotated with issue
- numbers."
+(def recent-activity
+ "Returns the activity on the chain since a timestamp."
(fn [chain from]
+ (def mod-at
+ (fn [x]
+ (unix-epoch
+ (lookup :modified-at x))))
(def u (unix-epoch from))
- (def mod-at (fn [x] (unix-epoch (lookup :modified-at x))))
(def since
+ (fn [xs]
+ (filter (fn [x] (< u (mod-at x))) xs)))
+ (def most-recent-first
(fn [xs]
(sort-by (fn [x] (- 0 (mod-at x)))
- (filter (fn [x] (< u (mod-at x))) xs))))
- (def is (values (list-issues chain)))
+ xs)))
+ (def all-issues (values (list-issues chain)))
+ (def is (map (fn [i]
+ {:activity :new-issue
+ :modified-at (lookup :modified-at i)
+ :issue i})
+ (since all-issues)))
(def cs
- (foldr (fn [i acc]
- (<> (map (fn [c]
- (insert :issue-number
- (lookup :number i)
- c))
- (lookup :comments i))
- acc))
- []
- is))
- {:new-issues (since is)
- :new-comments (since cs)}))
+ (cat-maybes
+ (map (fn [i]
+ (def xs (most-recent-first (since (lookup :comments i))))
+ (if (eq? xs [])
+ :nothing
+ [:just
+ {:activity :new-comments
+ :modified-at (lookup :modified-at (first xs))
+ :issue i}]))
+ all-issues)))
+ (most-recent-first (<> is cs))))
(:test
"The monadic issues chain works."
@@ -197,10 +205,7 @@
(simple-add-comment! chain 0 "new")
(fetch! chain)
(def is (list-issues chain))
- (def new (get-recent chain t))
- (def new-issues (map (fn [i] (lookup :title i)) (lookup :new-issues new)))
- (def new-comments (map (fn [c] (lookup :body c)) (lookup :new-comments new)))
- )]
+ (def new (recent-activity chain t)))]
[ (length (seq is)) ==> 4 ]
[ (view (.. (@ 0) (@ :title)) is) ==> "title0" ]
[ (view (.. (@ 1) (@ :title)) is) ==> "title1" ]
@@ -209,6 +214,7 @@
[ (short (first (values is))) ==> "0: title0" ]
[ (short (first (list-last chain 1))) ==> "3: newer" ]
[ (view (.. (@ 0) (@ :state)) is) ==> :closed ]
- [ new-issues ==> ["newer" "new"] ]
- [ new-comments ==> ["new"]]
+ [ (length new) ==> 3 ]
+ [ (view (.. (@nth 0) (@ :activity)) new) ==> :new-comments ]
+ [ (view (... [(@nth 1) (@ :issue) (@ :title)]) new) ==> "newer" ]
)
diff --git a/rad/prelude.rad b/rad/prelude.rad
index 7515d04..f0e3c68 100644
--- a/rad/prelude.rad
+++ b/rad/prelude.rad
@@ -7,6 +7,7 @@
(file-module! "rad/prelude/dict.rad")
(file-module! "rad/prelude/io.rad")
(file-module! "rad/prelude/bool.rad")
+(file-module! "rad/prelude/maybe.rad")
(file-module! "rad/prelude/exception.rad")
(file-module! "rad/prelude/list.rad")
(file-module! "rad/prelude/set.rad")
@@ -24,6 +25,7 @@
(import prelude/dict :unqualified)
(import prelude/io :unqualified)
(import prelude/bool :unqualified)
+(import prelude/maybe :unqualified)
(import prelude/list :unqualified)
(import prelude/ref :unqualified)
(import prelude/validation :as 'validator)
diff --git a/rad/prelude/chain.rad b/rad/prelude/chain.rad
index 0f55e62..993f4f7 100644
--- a/rad/prelude/chain.rad
+++ b/rad/prelude/chain.rad
@@ -191,6 +191,7 @@
"rad/prelude/strings.rad"
"rad/prelude/dict.rad"
"rad/prelude/bool.rad"
+ "rad/prelude/maybe.rad"
"rad/prelude/list.rad"
"rad/prelude/set.rad"
"rad/prelude/ref.rad"
diff --git a/rad/prelude/dict.rad b/rad/prelude/dict.rad
index 0f9564c..91c05ba 100644
--- a/rad/prelude/dict.rad
+++ b/rad/prelude/dict.rad
@@ -2,6 +2,8 @@
:doc "Functions for manipualting dicts."
:exports '[dict-from-seq keys values rekey modify-map delete-many lookup-default lookup-maybe]}
+(import prelude/patterns :unqualified)
+
(def dict-from-seq
"Creates a dictionary from a list of key-value pairs."
(fn [xs]
diff --git a/rad/prelude/io-utils.rad b/rad/prelude/io-utils.rad
index 444528d..ab0fb16 100644
--- a/rad/prelude/io-utils.rad
+++ b/rad/prelude/io-utils.rad
@@ -1,17 +1,20 @@
{:module 'prelude/io-utils
:doc "IO-related utilities"
- :exports '[fzf-select-by-key! edit-in-editor!]}
+ :exports '[fzf-select! edit-in-editor!]}
(import prelude/io '[shell-with-stdout! write-file!] :unqualified)
(import prelude/strings '[unlines] :unqualified)
-(def fzf-select-by-key!
- "Select from keys in `dict` with `fzf`, returning the corresponding values.
- The keys of the dict must be strings.
- Requires that `fzf` be on the path."
- (fn [dict]
- (def result (process-with-stdout! "fzf" [] (string-append (unlines (keys dict)) "\n")))
- (lookup (string-replace "\n" "" (nth 0 result)) dict)))
+(def fzf-select!
+ "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."
+ (fn [xs]
+ (def result (process-with-stdout! "fzf" [] (string-append (unlines xs) "\n")))
+ (match result
+ [] :nothing
+ ['x] [:just (string-replace "\n" "" x)])))
(def edit-in-editor!
"Open `$EDITOR` on a file prepopulated with `orig`. Returns the contents of the edited file when the editor exits."
diff --git a/rad/prelude/maybe.rad b/rad/prelude/maybe.rad
new file mode 100644
index 0000000..5ebb967
--- /dev/null
+++ b/rad/prelude/maybe.rad
@@ -0,0 +1,14 @@
+{:module 'prelude/maybe
+ :doc "Functions for dealing with optional values."
+ :exports '[cat-maybes]}
+
+(import prelude/patterns :unqualified)
+
+(def cat-maybes
+ (fn [xs]
+ (foldr (fn [x_ acc]
+ (match x_
+ (/just 'x) (cons x acc)
+ _ acc))
+ []
+ xs)))