A peer-to-peer stack for code collaboration
{:module 'monadic/items
:doc "Functions for interacting with dicts of items in machines"
:exports
'[summary pretty-item-view list-items pretty-state enrich-item
verify-item-number]}
(import prelude/error-messages :as 'error)
(def summary
"Given a list of `items`, each a dict with a `:state` key, displays a summary of
how many items there are of each state."
(fn [items-name items]
(string-append
"\n"
(show (length items))
" "
items-name
": "
(string/intercalate
", "
(map (fn [kv]
(string-append (show (length (nth 1 kv)))
" "
(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)))))))
(def verify-item-number
"Verifies that the item corresponding to `item-number` is part of the given
list of `items`. Returns an error and exits if the item is not found."
(fn [item-number items item-flag]
(match (lookup-maybe item-number items)
[:just 'issue] issue
:nothing (do
(put-str! (error/item-not-found item-flag (show item-number)))
(exit! 1)))))