A dynamic progress bar displayable in console.

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

Introduced properties

fun current_percentage: Int

console :: TermProgress :: current_percentage

Get the current percent value.
fun current_value: Int

console :: TermProgress :: current_value

Current value of the progress bar (business value).
protected fun current_value=(current_value: Int)

console :: TermProgress :: current_value=

Current value of the progress bar (business value).
init defaultinit(max_value: Int, current_value: Int)

console :: TermProgress :: defaultinit

fun display(metadata: nullable String)

console :: TermProgress :: display

Display the progress bar.
fun max_columns: Int

console :: TermProgress :: max_columns

Number of columns used to display the progress bar.
fun max_columns=(max_columns: Int)

console :: TermProgress :: max_columns=

Number of columns used to display the progress bar.
fun max_value: Int

console :: TermProgress :: max_value

Max value of the progress bar (business value).
protected fun max_value=(max_value: Int)

console :: TermProgress :: max_value=

Max value of the progress bar (business value).
fun update(new_current: Int, metadata: nullable String)

console :: TermProgress :: update

Update and display the progress bar.

Redefined properties

redef type SELF: TermProgress

console $ TermProgress :: SELF

Type of this instance, automatically specialized in every class

All properties

fun !=(other: nullable Object): Bool

core :: Object :: !=

Have self and other different values?
fun ==(other: nullable Object): Bool

core :: Object :: ==

Have self and other the same value?
type CLASS: Class[SELF]

core :: Object :: CLASS

The type of the class of self.
type SELF: Object

core :: Object :: SELF

Type of this instance, automatically specialized in every class
protected fun class_factory(name: String): CLASS

core :: Object :: class_factory

Implementation used by get_class to create the specific class.
fun class_name: String

core :: Object :: class_name

The class name of the object.
fun current_percentage: Int

console :: TermProgress :: current_percentage

Get the current percent value.
fun current_value: Int

console :: TermProgress :: current_value

Current value of the progress bar (business value).
protected fun current_value=(current_value: Int)

console :: TermProgress :: current_value=

Current value of the progress bar (business value).
init defaultinit(max_value: Int, current_value: Int)

console :: TermProgress :: defaultinit

fun display(metadata: nullable String)

console :: TermProgress :: display

Display the progress bar.
fun get_class: CLASS

core :: Object :: get_class

The meta-object representing the dynamic type of self.
fun hash: Int

core :: Object :: hash

The hash code of the object.
init init

core :: Object :: init

fun inspect: String

core :: Object :: inspect

Developer readable representation of self.
protected fun inspect_head: String

core :: Object :: inspect_head

Return "CLASSNAME:#OBJECTID".
intern fun is_same_instance(other: nullable Object): Bool

core :: Object :: is_same_instance

Return true if self and other are the same instance (i.e. same identity).
fun is_same_serialized(other: nullable Object): Bool

core :: Object :: is_same_serialized

Is self the same as other in a serialization context?
intern fun is_same_type(other: Object): Bool

core :: Object :: is_same_type

Return true if self and other have the same dynamic type.
fun max_columns: Int

console :: TermProgress :: max_columns

Number of columns used to display the progress bar.
fun max_columns=(max_columns: Int)

console :: TermProgress :: max_columns=

Number of columns used to display the progress bar.
fun max_value: Int

console :: TermProgress :: max_value

Max value of the progress bar (business value).
protected fun max_value=(max_value: Int)

console :: TermProgress :: max_value=

Max value of the progress bar (business value).
intern fun object_id: Int

core :: Object :: object_id

An internal hash code for the object based on its identity.
fun output

core :: Object :: output

Display self on stdout (debug only).
intern fun output_class_name

core :: Object :: output_class_name

Display class name on stdout (debug only).
fun serialization_hash: Int

core :: Object :: serialization_hash

Hash value use for serialization
intern fun sys: Sys

core :: Object :: sys

Return the global sys object, the only instance of the Sys class.
abstract fun to_jvalue(env: JniEnv): JValue

core :: Object :: to_jvalue

fun to_s: String

core :: Object :: to_s

User readable representation of self.
fun update(new_current: Int, metadata: nullable String)

console :: TermProgress :: update

Update and display the progress bar.
package_diagram console::TermProgress TermProgress core::Object Object console::TermProgress->core::Object

Parents

interface Object

core :: Object

The root of the class hierarchy.

Class definitions

console $ TermProgress
# 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