Use progress-advance.

This commit is contained in:
Dominik Pantůček 2023-03-20 16:16:40 +01:00
parent ce348ae901
commit 08ad923f6a
3 changed files with 43 additions and 38 deletions

View file

@ -130,7 +130,8 @@ command-line.import.scm: $(COMMAND-LINE-SOURCES)
MEMBERS-BASE-SOURCES=members-base.scm testing.import.scm \
utils.import.scm dictionary.import.scm member-file.import.scm \
primes.import.scm member-record.import.scm ansi.import.scm \
period.import.scm month.import.scm configuration.import.scm
period.import.scm month.import.scm configuration.import.scm \
progress.import.scm
members-base.so: members-base.o
members-base.o: members-base.import.scm

View file

@ -58,8 +58,9 @@
ansi
period
month
configuration)
configuration
progress)
;; Gets all files and symbolic links from given directory. The
;; symbolic links are represented by cons cells with car being the
;; name and cdr the link target.
@ -172,30 +173,28 @@
(define (load-members dn . opts)
(let ((progress? (and (not (null? opts))
(car opts))))
(when progress?
(display "Loading members "))
(let* ((fss (files-dictionary-filter-4digit-symbols
(files+symlinks->files-dictionary
(get-files+symlinks dn))))
(mb0 (dict-map
(lambda (symfn symlinks)
(when progress?
(display "."))
(members-base-load-member dn
symfn
symlinks))
fss))
(mb1 (dict-reduce (make-dict)
(lambda (acc symfn mr)
(dict-set acc (dict-ref mr 'id) mr))
mb0))
(mb (dict-reduce '()
(lambda (acc id mr)
(cons mr acc))
mb1)))
(when progress?
(print " ok."))
mb)))
(with-progress
progress? "Loading-members " " ok."
(let* ((fss (files-dictionary-filter-4digit-symbols
(files+symlinks->files-dictionary
(get-files+symlinks dn))))
(mb0 (dict-map
(lambda (symfn symlinks)
(when progress?
(progress-advance "."))
(members-base-load-member dn
symfn
symlinks))
fss))
(mb1 (dict-reduce (make-dict)
(lambda (acc symfn mr)
(dict-set acc (dict-ref mr 'id) mr))
mb0))
(mb (dict-reduce '()
(lambda (acc id mr)
(cons mr acc))
mb1)))
mb))))
;; Gets member based by generic predicate
(define (find-member-by-predicate mb pred)

View file

@ -23,7 +23,7 @@
;; CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
;;
(declare (unit configuration))
(declare (unit progress))
(module
progress
@ -43,26 +43,31 @@
;; Prints current progress.
(define (print-current-progress . args)
(display (sprintf "\r\x1b[K~A" (*current-progress*))))
(let ((cp (*current-progress*)))
(when cp
(display (sprintf "\r\x1b[K~A" cp)))))
;; Adds something to current progress and refreshes the display.
(define (progress-advance str)
(*current-progress* (string-append (*current-progress*) (sprintf "~A" str)))
(print-current-progress))
(when (*current-progress*)
(*current-progress* (string-append (*current-progress*) (sprintf "~A" str)))
(print-current-progress)))
;; Runs given procedure within progress environment
(define (run-with-progress pre-msg post-msg thunk)
(parameterize ((*current-progress* pre-msg))
(define (run-with-progress echo? pre-msg post-msg thunk)
(parameterize ((*current-progress* (if echo? pre-msg #f)))
(print-current-progress)
(thunk)
(print-current-progress)
(print post-msg)))
(let ((result (thunk)))
(print-current-progress)
(when echo?
(print post-msg))
result)))
;; Friendly syntax wrapper.
(define-syntax with-progress
(syntax-rules ()
((_ pre post body ...)
(run-with-progress pre post (lambda () body ...)))))
((_ echo? pre post body ...)
(run-with-progress echo? pre post (lambda () body ...)))))
;; If the program uses progress module, disable buffering
(set-buffering-mode! (current-output-port) #:none)