Skip to content

Commit

Permalink
Skip selection if only one algorithm is available
Browse files Browse the repository at this point in the history
  • Loading branch information
l3kn committed Mar 16, 2024
1 parent ae4e655 commit ffe43ef
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 73 deletions.
75 changes: 63 additions & 12 deletions org-fc-algo-sm2.el
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

(require 'org-fc-core)
(require 'org-fc-awk)
(require 'org-fc-dashboard)

(defmacro org-fc-property (symbol standard doc &rest args)
(let (defcustom-args property)
Expand Down Expand Up @@ -187,9 +188,6 @@ INTERVAL is by a random factor between `org-fc-algo-sm2-fuzz-min' and
'interval (format "%.2f" next-interval)
'due (org-fc-timestamp-in next-interval))))

;; NOTE: There's some duplication here as the position object already
;; contains the review data. Working on the review data parsed
;; directly from the file seems safer though.
(cl-defmethod org-fc-algo-log-review ((_algo org-fc-algo-sm2) (position org-fc-position) current rating delta)
(let* ((card (oref position card))
(file (oref card file))
Expand All @@ -210,6 +208,9 @@ INTERVAL is by a random factor between `org-fc-algo-sm2-fuzz-min' and
nil
org-fc-review-history-file)))

;; NOTE: There's some duplication here as the position object already
;; contains the review data. Working on the review data parsed
;; directly from the file seems safer though.
(cl-defmethod org-fc-algo-update-review-data ((algo org-fc-algo-sm2) (position org-fc-position) rating delta)
"Update the review data of a POSITION.
Also add a new entry in the review history file. RATING is a
Expand All @@ -229,35 +230,85 @@ rating the card."
(org-fc-algo-next-review-data algo current rating))
(org-fc-review-data-write review-data)))

(cl-defmethod org-fc-algo-review-stats ((_algo org-fc-algo-sm2))
"Statistics for all card reviews.
(cl-defmethod org-fc-algo-review-history ((_algo org-fc-algo-sm2) card-id position-name)
"Review history for a given CARD-ID and POSITION name.
Returns nil if there is no history file."
(when (file-exists-p org-fc-review-history-file)
(let ((output
(shell-command-to-string
(org-fc-awk--command
"awk/review_stats_sm2.awk"
"awk/review_history_sm2.awk"
:input org-fc-review-history-file
:variables `(("min_box" . ,org-fc-stats-review-min-box))))))
:variables `(("filter_card_id" . ,(or card-id "any"))
("filter_position_name" . ,(or position-name "any")))))))
(if (string-prefix-p "(" output)
(read output)
(error "Org-fc shell error: %s" output)))))

(cl-defmethod org-fc-algo-review-history ((_algo org-fc-algo-sm2) card-id position-name)
"Review history for a given CARD-ID and POSITION name.
(defun org-fc-algo-sm2-dashboard-parameters (cards)
(let* ((pos-count 0)
(avg-ease 0.0) (avg-box 0.0) (avg-interval 0.0))
(dolist (card cards)
(unless (or (oref card suspended)
(not (eq (oref card algo) 'sm2)))
(dolist (pos (oref card positions))
(cl-incf pos-count 1)
(cl-incf avg-ease (plist-get (oref pos data) :ease))
(cl-incf avg-box (plist-get (oref pos data) :box))
(cl-incf avg-interval (plist-get (oref pos data) :interval)))))

(insert "\n")
(if (cl-plusp pos-count)
(progn
(insert (format " %6.2f avg. ease\n" (/ avg-ease pos-count)))
(insert (format " %6.2f avg. box\n" (/ avg-box pos-count)))
(insert (format " %6.2f avg. interval (days)\n\n" (/ avg-interval pos-count))))
(insert " No positions yet\n\n"))))

(defun org-fc-algo-sm2-review-stats ()
"Statistics for all card reviews.
Returns nil if there is no history file."
(when (file-exists-p org-fc-review-history-file)
(let ((output
(shell-command-to-string
(org-fc-awk--command
"awk/review_history_sm2.awk"
"awk/review_stats_sm2.awk"
:input org-fc-review-history-file
:variables `(("filter_card_id" . ,(or card-id "any"))
("filter_position_name" . ,(or position-name "any")))))))
:variables `(("min_box" . ,org-fc-stats-review-min-box))))))
(if (string-prefix-p "(" output)
(read output)
(error "Org-fc shell error: %s" output)))))

(defun org-fc-algo-sm2-dashboard-review-history (_cards)
(let ((reviews-stats (org-fc-algo-sm2-review-stats)))
(insert "\n")
(if reviews-stats
(progn
(dolist (scope '((:day . "day")
(:week . "week")
(:month . "month")
(:all . "all")))
(when-let (stat (plist-get reviews-stats (car scope)))
(when (cl-plusp (plist-get stat :total))
(insert " ")
(if (and (display-graphic-p)
(memq 'svg (and (boundp 'image-types) image-types)))
(insert-image (org-fc-dashboard-bar-chart stat))
(insert (org-fc-dashboard-text-bar-chart stat)))
(insert (propertize (format " %s (%d)\n" (cdr scope) (plist-get stat :total)) 'face 'org-level-1)))))
(insert "\n"))
(insert " No reviews yet\n\n"))))

(org-fc-dashboard-add-section
(org-fc-dashboard-section
:title "SM2 Parameters"
:inserter #'org-fc-algo-sm2-dashboard-parameters))

(org-fc-dashboard-add-section
(org-fc-dashboard-section
:title "SM2 Review History (All Cards)"
:start-visible t
:inserter #'org-fc-algo-sm2-dashboard-review-history))

(org-fc-register-algo 'sm2 org-fc-algo-sm2)

Expand Down
13 changes: 7 additions & 6 deletions org-fc-core.el
Original file line number Diff line number Diff line change
Expand Up @@ -421,12 +421,13 @@ Use `org-fc-register-algo' for adding algorithms.")

(defun org-fc-select-algo ()
"Select a spacing algorithm."
(let* ((choice (completing-read
"Algorithm: "
(mapcar (lambda (c) (car c)) org-fc-algos)
nil
:require-match)))
choice))
(let ((choices
(cl-remove-if
(lambda (name) (eq name 'noop))
(mapcar #'car org-fc-algos))))
(if (= (length choices) 1)
(car choices)
(completing-read "Algorithm: " choices nil :require-match))))

;;; Card Initialization

Expand Down
67 changes: 12 additions & 55 deletions org-fc-dashboard.el
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
(require 'svg)

(require 'org-fc-core)
(require 'org-fc-awk)
(require 'org-fc-review)

;;; Customization
Expand Down Expand Up @@ -169,7 +168,7 @@ environment without svg support."
(save-excursion
(goto-char (car (oref section content-bounds)))
(let ((cur (point)))
(funcall (oref section inserter))
(funcall (oref section inserter) org-fc-dashboard-cards)
(oset section content-bounds (cons cur (point)))
(oset section expanded t)
(oset section visible t)
Expand Down Expand Up @@ -210,14 +209,11 @@ environment without svg support."
:inserter #'org-fc-dashboard-insert-overview)
(org-fc-dashboard-section
:title "Cards by Type"
:inserter #'org-fc-dashboard-insert-cards-by-type)
(org-fc-dashboard-section
:title "SM2 Parameters"
:inserter #'org-fc-dashboard-insert-sm2-stats)
(org-fc-dashboard-section
:title "Review History (All Cards)"
:start-visible t
:inserter #'org-fc-dashboard-insert-review-stats)))
:inserter #'org-fc-dashboard-insert-cards-by-type)))

(defun org-fc-dashboard-add-section (section)
(setq org-fc-dashboard-sections
(append org-fc-dashboard-sections (list section))))

;;; Bar Chart Generation

Expand Down Expand Up @@ -268,13 +264,13 @@ environment without svg support."

;;; Section Definitions

(defun org-fc-dashboard-insert-hotkeys ()
(defun org-fc-dashboard-insert-hotkeys (_cards)
(insert "\n")
(insert " [r] review [q] quit\n")
(insert " [n] next section [p] previous section [Tab] open/close section\n\n"))

(defun org-fc-dashboard-insert-overview ()
(let* ((card-count (length org-fc-dashboard-cards)) (suspended 0) (pos-count 0)
(defun org-fc-dashboard-insert-overview (cards)
(let* ((card-count (length cards)) (suspended 0) (pos-count 0)
(created (list :day 0 :week 0 :month 0))
(due (list :now 0 :day 0 :week 0 :month 0))
(now (current-time))
Expand All @@ -284,7 +280,7 @@ environment without svg support."
(plus-day (time-add now (* 24 60 60)))
(plus-week (time-add now (* 7 24 60 60)))
(plus-month (time-add now (* 30 24 60 60))))
(dolist (card org-fc-dashboard-cards)
(dolist (card cards)
(if (oref card suspended)
(cl-incf suspended 1)
(let ((card-created (oref card created)))
Expand Down Expand Up @@ -330,10 +326,10 @@ environment without svg support."
(plist-get due :month))))
(insert "\n")))

(defun org-fc-dashboard-insert-cards-by-type ()
(defun org-fc-dashboard-insert-cards-by-type (cards)
(insert "\n")
(let* ((by-type (make-hash-table)))
(dolist (card org-fc-dashboard-cards)
(dolist (card cards)
(unless (oref card suspended)
(cl-incf (gethash (oref card type) by-type 0) 1)))

Expand All @@ -343,45 +339,6 @@ environment without svg support."
(insert " No cards yet\n"))
(insert "\n")))

(defun org-fc-dashboard-insert-review-stats ()
(let ((reviews-stats (org-fc-algo-review-stats (org-fc-algo-sm2))))
(insert "\n")
(if reviews-stats
(progn
(dolist (scope '((:day . "day")
(:week . "week")
(:month . "month")
(:all . "all")))
(when-let (stat (plist-get reviews-stats (car scope)))
(when (cl-plusp (plist-get stat :total))
(insert " ")
(if (and (display-graphic-p)
(memq 'svg (and (boundp 'image-types) image-types)))
(insert-image (org-fc-dashboard-bar-chart stat))
(insert (org-fc-dashboard-text-bar-chart stat)))
(insert (propertize (format " %s (%d)\n" (cdr scope) (plist-get stat :total)) 'face 'org-level-1)))))
(insert "\n"))
(insert " No reviews yet\n\n"))))

(defun org-fc-dashboard-insert-sm2-stats ()
(let* ((pos-count 0)
(avg-ease 0.0) (avg-box 0.0) (avg-interval 0.0))
(dolist (card org-fc-dashboard-cards)
(unless (oref card suspended)
(dolist (pos (oref card positions))
(cl-incf pos-count 1)
(cl-incf avg-ease (plist-get (oref pos data) :ease))
(cl-incf avg-box (plist-get (oref pos data) :box))
(cl-incf avg-interval (plist-get (oref pos data) :interval)))))

(insert "\n")
(if (cl-plusp pos-count)
(progn
(insert (format " %6.2f avg. ease\n" (/ avg-ease pos-count)))
(insert (format " %6.2f avg. box\n" (/ avg-box pos-count)))
(insert (format " %6.2f avg. interval (days)\n\n" (/ avg-interval pos-count))))
(insert " No positions yet\n\n"))))

;;; Main View

;; Based on `mu4e-main-view-real'
Expand Down

0 comments on commit ffe43ef

Please sign in to comment.