]> gitweb.factorcode.org Git - factor.git/blobdiff - extra/progress-bars/progress-bars.factor
Progress bars for long-running operations that yield.
[factor.git] / extra / progress-bars / progress-bars.factor
diff --git a/extra/progress-bars/progress-bars.factor b/extra/progress-bars/progress-bars.factor
new file mode 100644 (file)
index 0000000..f1796b4
--- /dev/null
@@ -0,0 +1,24 @@
+! Copyright (C) 2011 Doug Coleman.
+! See http://factorcode.org/license.txt for BSD license.
+USING: combinators.short-circuit kernel math math.order
+sequences ;
+IN: progress-bars
+
+ERROR: invalid-percent x ;
+
+: check-percent ( x -- x )
+    dup 0 1 between? [ invalid-percent ] unless ;
+
+ERROR: invalid-length x ;
+
+: check-length ( x -- x )
+    dup { [ 0 > ] [ integer? ] } 1&& [ invalid-length ] unless ;
+
+: (make-progress-bar) ( percent len completed-ch pending-ch -- string )
+    [ [ * >integer ] keep over - ] 2dip
+    [ <repetition> ] bi-curry@ bi* "" append-as ;
+
+: make-progress-bar ( percent length -- string )
+    [ check-percent ] [ check-length ] bi*
+    CHAR: = CHAR: - (make-progress-bar) ;
+