readme: add information section
[nit.git] / lib / console.nit
index e922a84..b8fe277 100644 (file)
@@ -18,7 +18,7 @@ module console
 # A ANSI/VT100 escape sequence.
 abstract class TermEscape
        # The US-ASCII ESC character.
-       protected fun esc: Char do return 27.ascii
+       protected fun esc: Char do return 27.code_point
 
        # The Control Sequence Introducer (CSI).
        protected fun csi: String do return "{esc}["
@@ -44,8 +44,6 @@ end
 class TermMoveUp
        super TermDirectionalMove
 
-       init do end
-
        # Move by the specified number of cells.
        init by(magnitude: Int) do self.magnitude = magnitude
 
@@ -56,8 +54,6 @@ end
 class TermMoveDown
        super TermDirectionalMove
 
-       init do end
-
        # Move by the specified number of cells.
        init by(magnitude: Int) do self.magnitude = magnitude
 
@@ -68,8 +64,6 @@ end
 class TermMoveFoward
        super TermDirectionalMove
 
-       init do end
-
        # Move by the specified number of cells.
        init by(magnitude: Int) do self.magnitude = magnitude
 
@@ -80,8 +74,6 @@ end
 class TermMoveBackward
        super TermDirectionalMove
 
-       init do end
-
        # Move by the specified number of cells.
        init by(magnitude: Int) do self.magnitude = magnitude
 
@@ -102,8 +94,6 @@ class TermMove
        # 1 is the left.
        var column: Int = 1
 
-       init do end
-
        # Move at the specified position.
        #
        # (1, 1) is the top-left corner of the display.
@@ -347,3 +337,75 @@ redef class String
        # WARNING: SEE: `TermCharFormat`
        fun underline: String do return apply_format(normal.underline)
 end
+
+# A dynamic progressbar displayable in console.
+#
+# Example:
+# ~~~nitish
+# var max = 10
+# var current = 0
+# var pb = new TermProgress(max, current)
+#
+# pb.display
+# for i in [current + 1 .. max] do
+#      nanosleep(1, 0)
+#      pb.update(i)
+# end
+#
+# print "\ndone"
+# ~~~
+#
+# Progressbar can accept metadata to display a small amount of data.
+#
+# Example with metadata:
+# ~~~nitish
+# var pb = new TermProgress(10, 0)
+# for i in [0..10] do
+#      pb.update(i, "Step {i}")
+# end
+# ~~~
+class TermProgress
+
+       # Max value of the progress bar (business value).
+       var max_value: Int
+
+       # Current value of the progress bar (business value).
+       var current_value: Int
+
+       # Number of columns used to display the progress bar.
+       var max_columns = 70 is writable
+
+       # Get the current percent value.
+       fun current_percentage: Int do
+               return current_value * 100 / max_value
+       end
+
+       # Display the progress bar.
+       #
+       # `metadata`  can be used to pass a small amount of data to display after
+       # the progressbar.
+       fun display(metadata: nullable String) do
+               var percent = current_percentage
+               var p = current_value * max_columns / max_value
+               printn "\r{percent}% ["
+               for i in [1..max_columns] do
+                       if i < p then
+                               printn "="
+                       else if i == p then
+                               printn ">"
+                       else
+                               printn " "
+                       end
+               end
+               printn "]"
+               if metadata != null then printn " ({metadata})"
+       end
+
+       # Update and display the progresssbar.
+       #
+       # See `display`.
+       fun update(new_current: Int, metadata: nullable String) do
+               current_value = new_current
+               display(metadata)
+       end
+end