Skip to content

A peer-to-peer stack for code collaboration

Extract common list code from rad issue and patch (#592)

Merle Breitkreuz6947388429be
diff --git a/bin/rad-issue b/bin/rad-issue
index 1791ed1..dfc75d9 100755
--- a/bin/rad-issue
+++ b/bin/rad-issue
@@ -65,8 +65,6 @@
(fn [error]
(parse-failure error help)))
-(def comment-divider "---")
-
(def verify-issue-number
(fn [issue-no machine]
(match (lookup-default issue-no #f (list-issues machine))
@@ -75,186 +73,11 @@
(exit! 1))
'the-issue the-issue)))
-
-(def add-comment-md
- (fn [x]
- (string-append
- "\n" comment-divider "\n"
- "---Add comment above the line.\n\n"
- x)))
-
-(def pretty-state
- (fn [state]
- (drop 1 (show state))))
-
-(def cells
- {:state 10
- :number 10
- :title 50
- :author 20
- :modified-at 22})
-
-(def wrap-cell
- (fn [phrase cell-key]
- (def c-length (lookup cell-key cells))
- (string-append (take c-length (string/pad-right-to c-length phrase)) " ")))
-
-(def pretty-table-header
- (fn []
- (string-append
- (wrap-cell "state" :state)
- (wrap-cell "#" :number)
- (wrap-cell "title" :title)
- (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}
- (string/unwords
- [(pretty-state s)
- (show n)
- tit
- un
- (pretty-modified-at i)]))))
-
-(def pretty-row
- (fn [i]
- (match i
- '{:number n
- :title tit
- :state s
- :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 (pretty-modified-at i) :modified-at)))))
-
-(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
- :username un
- :created-at c-a}
- (string-replace
- "\r"
- ""
- (string-append
- (pretty-headline i) "\n\n"
- "**State:** " (pretty-state state) "\n"
- "**Labels:** " (show ls) "\n\n"
- b "\n\n"
- "Comments\n"
- "--------\n\n"
- (string/unlines (map pretty-comment cs)))))))
-
-(def add-username
- (fn [d]
- (def n (lookup-default :github-username (lookup-default :git-username "???" d) d))
- (insert :username n d)))
-
-(def add-username-issue
- (fn [i]
- (add-username
- (insert :comments (map add-username (lookup :comments i)) i))))
-
-(def enrich-issue
- "Adds if the issue has been read by the user since it was last modified, and
- adds a author username."
- (fn [machine i]
- (insert :read? (newness/read-since-modified!? machine i) (add-username-issue i))))
-
-(def list-items
- "Lists the recent items and uses an editor to add a relevant comment."
- (fn [machine options]
-
- (def mk-key
- (fn [i]
- (if (lookup :fancy options)
- [(pretty-headline i) i]
- [(pretty-row i) i])))
-
- (def issues (values (list-issues machine)))
-
- (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
- (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)
- (put-str! "No issues!")
- (if (lookup :fancy options)
- (match (fzf-select-with-preview!
- (map first items)
- (fn [s] (pretty-issue (lookup s items-dict))))
- (/just 's)
- (do (def selected (lookup s items-dict))
- (def issue-number (match selected {:number 'n} n))
- (def pretty-item
- (add-comment-md
- (pretty-issue
- (add-username-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! machine issue-number added-comment (get-git-username!))
- (string-append "Sent comment: " added-comment)))))
- _ (put-str! "No selection made."))
- (do
- (put-str! (pretty-table-header))
- (map (fn [d] (put-str! (first d))) items)
- (put-str! (items/summary "issues" issues)))))))
-
(def show-issue!
"Shows a single ISSUE `n`"
(fn [n machine]
(def issue (verify-issue-number n machine))
- (put-str! (pretty-issue (enrich-issue machine issue)))
+ (put-str! (items/pretty-item-view (items/enrich-item machine issue)))
(newness/mark-read! machine n)))
(def close-issue
@@ -277,6 +100,17 @@
(put-str! (string-append "Added comment to issue #" (show n)))
(newness/mark-read! machine n)))
+(def add-comment-from-editor!
+ "Add a new `comment` to ISSUE opened via the list command with option `--fancy`."
+ (fn [machine n added-comment]
+ (simple-add-comment! machine n added-comment (get-git-username!))
+ (string-append "Sent comment: " added-comment)))
+
+(def list
+ (fn [machine options]
+ (def issues (values (list-issues machine)))
+ (items/list-items machine options issues :issue add-comment-from-editor!)))
+
(def new-issue
(fn [machine]
(def template
@@ -318,7 +152,7 @@
(machine/catch-daemon!
(fn []
(match args
- (/list-cmd 'options) (list-items (machine-name) options)
+ (/list-cmd 'options) (list (machine-name) options)
(/cmd-0 "new" help) (new-issue (machine-name))
(/cmd-1 "close" 'n help) (whole-issue-num "close" n (fn [n] (close-issue n (machine-name))))
(/cmd-1 "show" 'n help) (whole-issue-num "show" n (fn [n] (show-issue! n (machine-name))))
diff --git a/bin/rad-patch b/bin/rad-patch
index bc11641..47fad4d 100755
--- a/bin/rad-patch
+++ b/bin/rad-patch
@@ -189,162 +189,11 @@
;; showing patches
-(def pretty-state
- (fn [state]
- (drop 1 (show state))))
-
-(def cells
- {:state 10
- :number 10
- :title 50
- :author 20
- :modified-at 22})
-
-(def wrap-cell
- (fn [phrase cell-key]
- (def c-length (lookup cell-key cells))
- (string-append (take c-length (string/pad-right-to c-length phrase)) " ")))
-
-(def pretty-table-header
- (fn []
- (string-append
- (wrap-cell "state" :state)
- (wrap-cell "#" :number)
- (wrap-cell "commit" :title)
- (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}
- (string/unwords
- [(pretty-state s)
- (show n)
- tit
- an
- (pretty-modified-at i)]))))
-
-(def pretty-row
- (fn [i]
- (match i
- '{:number n
- :title tit
- :state s
- :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 (pretty-modified-at i) :modified-at)))))
-
-(def pretty-comment
- (fn [c]
- (match c
- '{:created-at c-a
- :body b
- :author-name a-n}
- (string-append "### " a-n " [" c-a "]\n\n" b "\n"))))
-
-(def pretty-patch
- (fn [i]
- (match i
- '{:patch p
- :created-at c-a
- :comments cs}
- (string-append
- (pretty-headline i) "\n\n"
- p "\n\n"
- "Comments\n"
- "--------\n\n"
- (string/unlines (map pretty-comment cs))))))
-
-(def comment-divider "----------------------------------------")
-
-(def add-comment-md
- (fn [x]
- (string-append
- "\n" comment-divider "\n"
- "---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 an
- option only recent items are shown. If `--fancy is passed, the list is opened
- via fzf."
- (fn [machine options]
-
- (def mk-key
- (fn [patch]
- (if (lookup :fancy options)
- [(pretty-headline patch) patch]
- [(pretty-row patch) patch])))
-
- (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
- (filter (and-predicate state-filt newness-filt)
- (map (fn [i] (add-read-status! machine i))
- (reverse patches)))))
-
- (def items-dict (dict-from-seq items))
- (if (empty-seq? items)
- (put-str! "No patches!")
- (if (lookup :fancy options)
- (match (fzf-select-with-preview!
- (map first items)
- (fn [s] (pretty-patch (lookup s items-dict))))
- (/just 's)
- (do (def selected (lookup s items-dict))
- (def patch-number (match selected {:number 'n} n))
- (def pretty-item
- (add-comment-md
- (pretty-patch 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! machine patch-number added-comment (get-git-username!))
- (string-append "Sent comment: " added-comment)))))
- _ (put-str! "No selection made."))
- (do
- (put-str! (pretty-table-header))
- (map (fn [d] (put-str! (first d))) items)
- (put-str! (items/summary "patches" patches)))))))
-
(def show-patch!
"Shows a single PATCH `n`"
(fn [machine n]
(def patch (verify-patch-number n machine))
- (put-str! (pretty-patch (add-read-status! machine patch)))
+ (put-str! (items/pretty-item-view (items/enrich-item machine patch)))
(newness/mark-read! machine n)))
(def edit-patch-state!
@@ -358,17 +207,17 @@
(newness/mark-read! machine n))
(fn [x]
(put-str!
- (error/state-change-failure :patch (pretty-state new-state)))
+ (error/state-change-failure :patch (items/pretty-state new-state)))
(exit! 1)))
(put-str! message)))
(verify-patch-number n machine)
(if (eq? new-state :accepted)
(match (push-patch! machine n)
:ok (edit-state-with-feedback!
- (string-append "Patch #" (show n) " has been " (pretty-state new-state) " and merged."))
+ (string-append "Patch #" (show n) " has been " (items/pretty-state new-state) " and merged."))
_ (exit! 1))
(edit-state-with-feedback!
- (string-append "Patch #" (show n) " has been " (pretty-state new-state))))))
+ (string-append "Patch #" (show n) " has been " (items/pretty-state new-state))))))
(def create-comment!
"Add a new `comment` to PATCH."
@@ -378,6 +227,17 @@
(put-str! (string-append "Added comment to patch #" (show n)))
(newness/mark-read! machine n)))
+(def add-comment-from-editor!
+ "Add a new `comment` to PATCH opened via the list command with option `--fancy`."
+ (fn [machine n added-comment]
+ (simple-add-comment! machine n added-comment (get-git-username!))
+ (string-append "Sent comment: " added-comment)))
+
+(def list
+ (fn [machine options]
+ (def patches (values (list-patches machine)))
+ (items/list-items machine options patches :patch add-comment-from-editor!)))
+
(def mark-all-read!
(fn [machine]
(map (fn [i] (newness/mark-read! machine (lookup :number i)))
@@ -403,7 +263,7 @@
(machine/catch-daemon!
(fn []
(match args
- (/list-cmd 'options) (list-items (machine-name) options)
+ (/list-cmd 'options) (list (machine-name) options)
(/cmd-1 "propose" 'commit help) (new-patch! (machine-name) commit)
(/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)))
diff --git a/rad/monadic/items.rad b/rad/monadic/items.rad
index 8dfbcce..408e07e 100644
--- a/rad/monadic/items.rad
+++ b/rad/monadic/items.rad
@@ -1,6 +1,6 @@
{:module 'monadic/items
:doc "Functions for interacting with dicts of items in machines"
- :exports '[summary]}
+ :exports '[summary pretty-item-view list-items pretty-state enrich-item]}
(def summary
"Given a list of `items`, each a dict with a `:state` key, displays a summary of
@@ -20,3 +20,201 @@
(drop 1 (show (nth 0 kv)))))
(seq (group-by (fn [i] (lookup :state i))
items)))))))
+
+(def pretty-state
+ (fn [state]
+ (drop 1 (show state))))
+
+(def cells
+ {:state 10
+ :number 10
+ :title 50
+ :author 20
+ :modified-at 22})
+
+(def wrap-cell
+ (fn [phrase cell-key]
+ (def c-length (lookup cell-key cells))
+ (string-append (take c-length (string/pad-right-to c-length phrase)) " ")))
+
+(def pretty-table-header
+ (fn []
+ (string-append
+ (wrap-cell "state" :state)
+ (wrap-cell "#" :number)
+ (wrap-cell "commit" :title)
+ (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}
+ (string/unwords
+ [(pretty-state s)
+ (show n)
+ tit
+ un
+ (pretty-modified-at i)]))))
+
+(def pretty-row
+ (fn [i]
+ (match i
+ '{:number n
+ :title tit
+ :state s
+ :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 (pretty-modified-at i) :modified-at)))))
+
+(def pretty-comment
+ (fn [c]
+ (match c
+ '{:created-at c-a
+ :body b
+ :username un}
+ (string-append "### " un " [" c-a "]\n\n" b "\n"))))
+
+(def pretty-item-view
+ "Markdown view of an item."
+ (fn [i]
+ (def pretty-comments
+ (fn [comments]
+ (string/unlines (map pretty-comment comments))))
+ (match i
+ '{:number n
+ :title tit
+ :body b
+ :state state
+ :labels ls
+ :comments cs
+ :username un
+ :created-at c-a}
+ (string-replace
+ "\r"
+ ""
+ (string-append
+ (pretty-headline i) "\n\n"
+ "**State:** " (pretty-state state) "\n"
+ "**Labels:** " (show ls) "\n\n"
+ b "\n\n"
+ "Comments\n"
+ "--------\n\n"
+ (pretty-comments cs)))
+ '{:patch p
+ :created-at c-a
+ :comments cs}
+ (string-append
+ (pretty-headline i) "\n\n"
+ p "\n\n"
+ "Comments\n"
+ "--------\n\n"
+ (pretty-comments cs)))))
+
+(def items-name
+ (fn [item-flag]
+ (match item-flag
+ :issue "issues"
+ :patch "patches")))
+
+(def add-username
+ (fn [d]
+ (def a-n (lookup-default :author-name "???" d))
+ (def n (lookup-default :github-username (lookup-default :git-username a-n d) d))
+ (insert :username n d)))
+
+(def add-username-item
+ (fn [i]
+ (add-username
+ (insert :comments (map add-username (lookup :comments i)) i))))
+
+(def enrich-item
+ "Adds if the item has been read by the user since it was last modified, and
+ adds a author username."
+ (fn [machine i]
+ (insert :read? (newness/read-since-modified!? machine i) (add-username-item i))))
+
+(def list-items
+ "Lists the recent items and uses an editor to add a relevant comment."
+ (fn [machine options items item-flag add-comment!]
+
+ (def mk-key
+ (fn [i]
+ (if (lookup :fancy options)
+ [(pretty-headline i) i]
+ [(pretty-row i) i])))
+
+ (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 comment-divider "---")
+
+ (def add-comment-md
+ (fn [x]
+ (string-append
+ "\n" comment-divider "\n"
+ "---Add comment above the line.\n\n"
+ x)))
+
+ (def filtered-items
+ (map mk-key
+ (filter (and-predicate state-filt newness-filt)
+ (map (fn [i] (enrich-item machine i))
+ (reverse items)))))
+
+ (def no-filtered-items
+ (fn []
+ (if (empty-seq? items)
+ (put-str! (string-append "No " (items-name item-flag) "!"))
+ (do
+ (if (lookup :unread options)
+ (put-str! (string-append "No unread " (items-name item-flag) "!"))
+ (put-str! (string-append "No items of state " (string/intercalate ", " (lookup :state options)) "!")))
+ (put-str! (summary (items-name item-flag) items))))))
+
+ (def items-dict (dict-from-seq filtered-items))
+ (if (empty-seq? filtered-items)
+ (no-filtered-items)
+ (if (lookup :fancy options)
+ (match (fzf-select-with-preview!
+ (map first filtered-items)
+ (fn [s] (pretty-item-view (lookup s items-dict))))
+ (/just 's)
+ (do (def selected (lookup s items-dict))
+ (def item-number (match selected {:number 'n} n))
+ (def pretty-item
+ (add-comment-md
+ (pretty-item-view 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."
+ (add-comment! machine item-number added-comment))))
+ _ (put-str! "No selection made."))
+ (do
+ (put-str! (pretty-table-header))
+ (map (fn [d] (put-str! (first d))) filtered-items)
+ (put-str! (summary (items-name item-flag) items)))))))