Debian Codegraph @

lib/text/progress.scm

raw
1 ;;;
2 ;;; text.progress - shows progress bar
3 ;;;
4 ;;; Copyright (c) 2007-2024 Shiro Kawai <[email protected]>
5 ;;;
6 ;;; Redistribution and use in source and binary forms, with or without
7 ;;; modification, are permitted provided that the following conditions
8 ;;; are met:
9 ;;;
10 ;;; 1. Redistributions of source code must retain the above copyright
11 ;;; notice, this list of conditions and the following disclaimer.
12 ;;;
13 ;;; 2. Redistributions in binary form must reproduce the above copyright
14 ;;; notice, this list of conditions and the following disclaimer in the
15 ;;; documentation and/or other materials provided with the distribution.
16 ;;;
17 ;;; 3. Neither the name of the authors nor the names of its contributors
18 ;;; may be used to endorse or promote products derived from this
19 ;;; software without specific prior written permission.
20 ;;;
21 ;;; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 ;;; "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 ;;; LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 ;;; A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 ;;; OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 ;;; SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
27 ;;; TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
28 ;;; PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29 ;;; LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30 ;;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31 ;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 ;;;
33
34 (define-module text.progress
35 (use srfi.19)
36 (export make-text-progress-bar))
37 (select-module text.progress)
38
39
40 ;; Progress bar sample:
41 ;;
42 ;; <-- header---> <------- bar -------> <- num -><-time-> <-- info -->
43 ;; foo |############## | 123/211 01:21 ETA sending...
44 ;; ^
45 ;; separator
46
47 ;; The 'ETA' calculation is derived from the scmail's progress bar code
48 ;; written by Satoru Takabayashi.
49
50 (define (make-text-progress-bar :key
51 (header "progress")
52 (header-width 14)
53 (bar-char #\#)
54 (bar-width 40)
55 (num-width 9)
56 (num-format (^[cur max]
57 (format "~d/~d" cur max)))
58 (time-width 7)
59 (info-width 0)
60 (info "")
61 (separator-char #\|)
62 (max-value 100)
63 (port (current-output-port)))
64 (assume (positive? max-value))
65 (let ([current-value 0]
66 [start-time (current-time)]
67 [finish-time #f])
68 (define (show)
69 (format port "~v,,,,va~a~v,,,,va~a~v,,,,v@a~v,,,,v@a~a~v,,,,va~a"
70 header-width header-width header
71 (or separator-char "")
72 bar-width bar-width (make-bar)
73 (or separator-char "")
74 num-width num-width
75 (if (> num-width 0) (num-format current-value max-value) "")
76 time-width time-width
77 (if (> time-width 0) (make-time) "")
78 (if (and (not finish-time) (> time-width 0)) " ETA" " ")
79 info-width info-width
80 (if (> info-width 0) info "")
81 (if finish-time "\n" "\r"))
82 (flush port))
83 (define (make-bar)
84 (make-string (floor->exact (* (/. current-value max-value) bar-width))
85 bar-char))
86 (define (fmt-time seconds)
87 (cond
88 [(not seconds) "--:--"]
89 [(>= seconds 3600)
90 (format "~d:~2,'0d:~2,'0d"
91 (quotient seconds 3600)
92 (quotient (remainder seconds 3600) 60)
93 (remainder seconds 60))]
94 [else
95 (format "~2,'0d:~2,'0d"
96 (quotient seconds 60)
97 (remainder seconds 60))]))
98 (define (time- s t)
99 (time->seconds (time-difference s t)))
100 (define (make-time)
101 (cond
102 [finish-time
103 (fmt-time (round->exact (time- finish-time start-time)))]
104 [(zero? current-value)
105 (fmt-time #f)]
106 [else
107 (let* ([used (time- (current-time) start-time)]
108 [eta (- (/. (* used max-value) current-value) used)])
109 (fmt-time (round->exact eta)))]))
110
111 (^[msg . args]
112 (case msg
113 [(show) (show)]
114 [(set)
115 (when (null? args)
116 (error "text-progress-bar: message 'set requires an argument"))
117 (set! current-value (car args))
118 (show)]
119 [(inc)
120 (when (null? args)
121 (error "text-progress-bar: message 'inc requires an argument"))
122 (set! current-value (min (+ (car args) current-value) max-value))
123 (show)]
124 [(finish)
125 (set! finish-time (current-time))
126 (show)]
127 [(set-header)
128 (when (null? args)
129 (error "text-progress-bar: message 'set-header requires an argument"))
130 (set! header (car args))
131 (show)]
132 [(set-info)
133 (when (null? args)
134 (error "text-progress-bar: message 'set-info requires an argument"))
135 (set! info (car args))
136 (show)]
137 [else
138 (error "text-progress-bar: unrecognized message:" msg)]))
139 ))