From 62ac9a887b78c3b5f724c8ea66cb3c0377ca2cb3 Mon Sep 17 00:00:00 2001 From: Daniel Borchmann Date: Wed, 28 Nov 2018 16:50:18 +0100 Subject: [PATCH] [Timeline] Extract gap-filling into separate function --- site-lisp/timeline-tools.el | 67 +++++++++++++++++++++---------------- 1 file changed, 38 insertions(+), 29 deletions(-) diff --git a/site-lisp/timeline-tools.el b/site-lisp/timeline-tools.el index 83bbf91..5b1ed2f 100644 --- a/site-lisp/timeline-tools.el +++ b/site-lisp/timeline-tools.el @@ -44,7 +44,7 @@ :group 'applications) (defcustom timeline-tools-filter-functions - (list #'timeline-tools-remove-short-entries + (list #'timeline-tools-fill-gaps #'timeline-tools-cluster-same-entries) "List of functions to apply when formatting timelines. Filter are applied in the order they are given in this list." @@ -332,39 +332,48 @@ function will throw an error." A slot is short if it is not longer than THRESHOLD seconds. Resulting gaps are distributed evenly among adjacent slots. THRESHOLD defaults to the value of -`timeline-tools-short-task-threshold’ if not supplied." +`timeline-tools-short-task-threshold’ if not supplied. + +This function destructively modifies its first argument." (unless (null timeline) - (let ((start (timeline-tools-entry-start-time (-first-item timeline))) - (end (timeline-tools-entry-end-time (-last-item timeline))) - (threshold (or threshold timeline-tools-short-task-threshold)) - new-timeline) + (let ((start (timeline-tools-entry-start-time (-first-item timeline))) + (end (timeline-tools-entry-end-time (-last-item timeline))) + (threshold (or threshold timeline-tools-short-task-threshold)) + new-timeline) - ;; remove all slots that are too short - (setq new-timeline - (cl-remove-if (lambda (entry) - (<= (- (timeline-tools-entry-end-time entry) - (timeline-tools-entry-start-time entry)) - threshold)) - timeline)) + ;; remove all slots that are too short + (setq new-timeline + (cl-remove-if (lambda (entry) + (<= (- (timeline-tools-entry-end-time entry) + (timeline-tools-entry-start-time entry)) + threshold)) + timeline)) - ;; reset start and end times - (setf (timeline-tools-entry-start-time (-first-item new-timeline)) start) - (setf (timeline-tools-entry-end-time (-last-item new-timeline)) end) + ;; reset start and end times + (setf (timeline-tools-entry-start-time (-first-item new-timeline)) start) + (setf (timeline-tools-entry-end-time (-last-item new-timeline)) end) - ;; distribute gaps evenly among adjacent slots - (cl-do - ((sub-timeline new-timeline (cdr sub-timeline))) - ((null (cdr sub-timeline))) - (let* ((entry-1 (-first-item sub-timeline)) - (entry-2 (-second-item sub-timeline)) - (end-1 (timeline-tools-entry-end-time entry-1)) - (start-2 (timeline-tools-entry-start-time entry-2))) - (when (not (= end-1 start-2)) - (let ((middle (/ (+ end-1 start-2) 2))) - (setf (timeline-tools-entry-end-time entry-1) middle) - (setf (timeline-tools-entry-start-time entry-2) middle))))) + ;; distribute gaps evenly among adjacent slots + (timeline-tools-fill-gaps new-timeline)))) - new-timeline))) +(defun timeline-tools-fill-gaps (timeline) + "Fill gaps in TIMELINE evenly. + +This is achieved by extending the start time and the end time of +the surrounding entries equally. + +This function destructively modifies its first argument." + (cl-do + ((sub-timeline timeline (cdr sub-timeline))) + ((null (cdr sub-timeline)) timeline) + (let* ((entry-1 (-first-item sub-timeline)) + (entry-2 (-second-item sub-timeline)) + (end-1 (timeline-tools-entry-end-time entry-1)) + (start-2 (timeline-tools-entry-start-time entry-2))) + (when (not (= end-1 start-2)) + (let ((middle (/ (+ end-1 start-2) 2))) + (setf (timeline-tools-entry-end-time entry-1) middle) + (setf (timeline-tools-entry-start-time entry-2) middle)))))) (defun timeline-tools-transform-timeline (timeline) "Return timeline from files, after application of