-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathprogress.lisp
180 lines (163 loc) · 6.29 KB
/
progress.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
(defpackage #:qlot/progress
(:use #:cl)
(:import-from #:qlot/logger
#:*terminal*
#:*debug*)
(:import-from #:qlot/color
#:color-text)
(:import-from #:bordeaux-threads)
(:import-from #:lparallel
#:pmapc
#:task-handler-bind
#:*kernel*
#:make-kernel)
(:import-from #:lparallel.queue
#:make-queue
#:push-queue
#:pop-queue)
(:export #:make-progress
#:make-line
#:print-progress
#:refresh-progress-line
#:add-line
#:progress
#:run-in-parallel))
(in-package #:qlot/progress)
(defgeneric print-progress (object body stream))
(defstruct (progress-line
(:constructor make-line (header)))
(type :in-progress :type (or null (member :in-progress :done :aborted)))
(header "" :type string))
(defun progress-symbol (type)
(ecase type
(:in-progress
(color-text :yellow
(string (code-char 9679))))
(:done
(color-text :green
(string (code-char 10003))))
(:aborted
(color-text :red
(string (code-char 10799))))
('nil nil)))
(defmethod print-progress ((object progress-line) body stream)
(format stream "~@[~A ~]~A ~A~%"
(progress-symbol (progress-line-type object))
(progress-line-header object)
(color-text :gray body)))
(defstruct (progress-manager (:constructor %make-progress-manager))
(lines nil :type list)
(stream nil :type (or stream null))
(lock (bt2:make-lock :name "progress-manager-lock")
:type bt2:lock))
(defun make-progress (line-or-lines &key (stream *standard-output*))
(let ((lines (if (listp line-or-lines)
line-or-lines
(list line-or-lines))))
(assert (every (lambda (line) (typep line 'progress-line)) lines))
(%make-progress-manager :lines lines
:stream stream)))
(defmacro with-manager-lock ((manager) &body body)
`(bt2:with-lock-held ((progress-manager-lock ,manager))
,@body))
(defmethod print-progress ((manager progress-manager) body stream)
(with-manager-lock (manager)
(loop for line in (progress-manager-lines manager)
do (print-progress line body stream)))
(values))
(defvar *progress-output* nil)
(defun move-up (n)
(when *terminal*
(format *progress-output* "~C[~DA" #\Esc n)))
(defun clear-line ()
(if *terminal*
(format *progress-output* "~C[2K" #\Esc)
(fresh-line *progress-output*)))
(defmacro with-excursion ((stream) &body body)
`(let ((*progress-output* ,stream))
(when *terminal*
(format *progress-output* "~C[s" #\Esc))
(prog1 (progn ,@body)
(when *terminal*
(format *progress-output* "~C[u" #\Esc))
(force-output *progress-output*))))
(defun refresh-progress-line (manager line body)
(check-type manager progress-manager)
(check-type line progress-line)
(check-type body string)
(assert (find line (progress-manager-lines manager) :test 'eq))
(with-manager-lock (manager)
(let* ((lines (progress-manager-lines manager))
(pos (position line lines :test 'eq))
(len (length lines)))
(with-excursion ((progress-manager-stream manager))
(when pos
(move-up (- len pos))
(clear-line))
(print-progress line body (progress-manager-stream manager)))))
(values))
(defun add-line (manager header &key (initial-body ""))
(check-type manager progress-manager)
(let ((new-line (make-line header)))
(with-manager-lock (manager)
(print-progress new-line initial-body (progress-manager-stream manager))
(if (progress-manager-lines manager)
(setf (cdr (last (progress-manager-lines manager)))
(list new-line))
(setf (progress-manager-lines manager)
(list new-line))))
new-line))
(defvar *mailbox*)
(defvar *progress-line*)
(defun progress (type &optional control &rest args)
(when (boundp '*progress-line*)
(unless (keywordp type)
(push control args)
(psetf control type
type nil))
(when type
(setf (progress-line-type *progress-line*) type))
(let ((text (apply #'format nil control args)))
(push-queue (cons *progress-line* text) *mailbox*)
text)))
(defun run-in-parallel (worker-fn jobs &key (concurrency 1) job-header-fn failed-fn)
(declare (ignorable concurrency))
(let* ((manager (make-progress nil))
(*mailbox* (make-queue))
(bt2:*default-special-bindings* (cons `(*mailbox* . ,*mailbox*)
bt2:*default-special-bindings*))
(progress-thread
(bt2:make-thread
(lambda ()
(loop for (line . body) = (pop-queue *mailbox*)
do (refresh-progress-line manager line body)))
:name "qlot progress manager")))
#+(or ecl clasp win32)
(dolist (job jobs)
(let ((*progress-line*
(add-line manager
(funcall (or job-header-fn #'princ-to-string) job))))
(funcall worker-fn job)))
#-(or ecl clasp win32)
(let ((*kernel* (make-kernel concurrency
:bindings bt2:*default-special-bindings*)))
(unwind-protect
(handler-bind (#+sbcl (sb-sys:interactive-interrupt
(lambda (c)
(declare (ignore c))
(lparallel:end-kernel))))
(task-handler-bind ((error #'lparallel:invoke-transfer-error))
(pmapc
(lambda (job)
(let ((*progress-line*
(add-line manager
(funcall (or job-header-fn #'princ-to-string) job))))
(handler-bind ((error
(lambda (e)
(when *debug*
(uiop:print-condition-backtrace e))
(when failed-fn
(funcall failed-fn)))))
(funcall worker-fn job))))
jobs)))
(ignore-errors (bt2:destroy-thread progress-thread))))))