Example:
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"
Progress bar can accept metadata to display a small amount of data.
Example with metadata:
var pb = new TermProgress(10, 0)
for i in [0..10] do
pb.update(i, "Step {i}")
end
console :: TermProgress :: current_percentage
Get the current percent value.console :: TermProgress :: current_value
Current value of the progress bar (business value).console :: TermProgress :: current_value=
Current value of the progress bar (business value).console :: TermProgress :: defaultinit
console :: TermProgress :: max_columns
Number of columns used to display the progress bar.console :: TermProgress :: max_columns=
Number of columns used to display the progress bar.console :: TermProgress :: max_value
Max value of the progress bar (business value).console :: TermProgress :: max_value=
Max value of the progress bar (business value).console $ TermProgress :: SELF
Type of this instance, automatically specialized in every classcore :: Object :: class_factory
Implementation used byget_class
to create the specific class.
console :: TermProgress :: current_percentage
Get the current percent value.console :: TermProgress :: current_value
Current value of the progress bar (business value).console :: TermProgress :: current_value=
Current value of the progress bar (business value).console :: TermProgress :: defaultinit
core :: Object :: defaultinit
core :: Object :: is_same_instance
Return true ifself
and other
are the same instance (i.e. same identity).
core :: Object :: is_same_serialized
Isself
the same as other
in a serialization context?
core :: Object :: is_same_type
Return true ifself
and other
have the same dynamic type.
console :: TermProgress :: max_columns
Number of columns used to display the progress bar.console :: TermProgress :: max_columns=
Number of columns used to display the progress bar.console :: TermProgress :: max_value
Max value of the progress bar (business value).console :: TermProgress :: max_value=
Max value of the progress bar (business value).core :: Object :: output_class_name
Display class name on stdout (debug only).
# A dynamic progress bar 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"
# ~~~
#
# Progress bar 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 progress bar.
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 progress bar.
#
# See `display`.
fun update(new_current: Int, metadata: nullable String) do
current_value = new_current
display(metadata)
end
end
lib/console/console.nit:347,1--417,3