Remove redundant conversion of remaining effort durations

When sorting, we need the remaining efforts as numbers, so let's return those
numbers directly instead of first converting them to durations only for them to
be converted back to numbers.
This commit is contained in:
Daniel - 2023-04-02 10:36:13 +02:00
parent 866f652c4b
commit f120e4b01f
Signed by: dbo
GPG Key ID: 784AA8DF0CCDF625
1 changed files with 15 additions and 11 deletions

View File

@ -421,13 +421,17 @@ is clocked in."
60) 60)
0)))) 0))))
(defun db/org-remaining-effort-of-current-item () (defun db/org-remaining-effort-of-current-item (&optional as-number)
"Return remaining effort of Org item at point, as duration. "Return remaining effort of Org item at point.
The remaining effort is computed as the planned effort minus the The remaining effort is computed as the planned effort minus the
already clocked time. If this result is negative, return zero. already clocked time. If this result is negative, return zero.
If no effort is specified, return an empty string." Return the remaining effort as duration string by default. When
optional AS-NUMBER is non-nil, return the effort as number.
If no effort is specified at the item at point, return an empty
string, or nil when AS-NUMBER is non-nil."
(if (derived-mode-p 'org-agenda-mode) (if (derived-mode-p 'org-agenda-mode)
@ -444,10 +448,12 @@ If no effort is specified, return an empty string."
(user-error "Not in Org mode buffer, aborting")) (user-error "Not in Org mode buffer, aborting"))
(if-let ((effort (org-entry-get (point) "Effort"))) (if-let ((effort (org-entry-get (point) "Effort")))
(org-duration-from-minutes (let ((remaining-effort (max 0 (- (org-duration-to-minutes effort)
(max 0 (- (org-duration-to-minutes effort) (db/org-clocked-time-for-current-item)))))
(db/org-clocked-time-for-current-item)))) (if as-number
""))) remaining-effort
(org-duration-from-minutes remaining-effort)))
(if as-number nil ""))))
(defun db/org-cmp-remaining-effort (a b) (defun db/org-cmp-remaining-effort (a b)
"Compare the remaining efforts of Org items A and B. "Compare the remaining efforts of Org items A and B.
@ -462,14 +468,12 @@ accessible via the `org-hd-marker' text property."
(ea (or (and (markerp ma) (ea (or (and (markerp ma)
(marker-buffer ma) (marker-buffer ma)
(org-with-point-at ma (org-with-point-at ma
(org-duration-to-minutes ; this is inefficient (db/org-remaining-effort-of-current-item 'as-number)))
(db/org-remaining-effort-of-current-item))))
def)) def))
(eb (or (and (markerp mb) (eb (or (and (markerp mb)
(marker-buffer mb) (marker-buffer mb)
(org-with-point-at mb (org-with-point-at mb
(org-duration-to-minutes (db/org-remaining-effort-of-current-item 'as-number)))
(db/org-remaining-effort-of-current-item))))
def))) def)))
(cond ((> ea eb) +1) (cond ((> ea eb) +1)
((< ea eb) -1)))) ((< ea eb) -1))))