View Source Owl.ProgressBar (Owl v0.4.0)
A live progress bar widget.
single-bar
Single bar
Owl.ProgressBar.start(id: :users, label: "Creating users", total: 1000)
Enum.each(1..100, fn _ ->
Process.sleep(10)
Owl.ProgressBar.inc(id: :users)
end)
Owl.LiveScreen.await_render()
multiple-bars
Multiple bars
1..10
|> Enum.map(fn index ->
Task.async(fn ->
range = 1..Enum.random(100..500)
label = "Demo Progress ##{index}"
Owl.ProgressBar.start(
id: {:demo, index},
label: label,
total: range.last,
timer: true,
bar_width_ratio: 0.3,
filled_symbol: "#",
partial_symbols: []
)
Enum.each(range, fn _ ->
Process.sleep(Enum.random(10..50))
Owl.ProgressBar.inc(id: {:demo, index})
end)
end)
end)
|> Task.await_many(:infinity)
Owl.LiveScreen.await_render()
Link to this section Summary
Functions
Increases current
value by step
.
Renders a progress bar that can be consumed by Owl.IO.puts/2
.
Starts a progress bar on Owl.LiveScreen
.
Link to this section Types
Link to this section Functions
Increases current
value by step
.
When current
value becomes equal to total
, then progress bar terminates.
options
Options
:id
- an required identifier of the progress bar.:step
- a value by whichcurrent
value should be increased. Defaults to 1.
examples
Examples
Owl.ProgressBar.inc(id: "Creating users")
Owl.ProgressBar.inc(id: "Creating users", step: 10)
@spec render(%{ optional(:current_time) => nil | integer(), optional(:start_time) => nil | integer(), optional(:screen_width) => nil | pos_integer(), optional(:absolute_values) => nil | boolean(), bar_width_ratio: float(), label: String.t(), total: pos_integer(), current: non_neg_integer(), start_symbol: Owl.Data.t(), end_symbol: Owl.Data.t(), filled_symbol: Owl.Data.t(), partial_symbols: [Owl.Data.t()], empty_symbol: Owl.Data.t() }) :: Owl.Data.t()
Renders a progress bar that can be consumed by Owl.IO.puts/2
.
Used as a callback for blocks in Owl.LiveScreen
.
examples
Examples
iex> Owl.ProgressBar.render(%{
...> label: "Demo",
...> total: 200,
...> current: 60,
...> bar_width_ratio: 0.7,
...> start_symbol: "[",
...> end_symbol: "]",
...> filled_symbol: "#",
...> partial_symbols: [],
...> empty_symbol: ".",
...> screen_width: 40
...> }) |> to_string()
"Demo [########....................] 30%"
iex> Owl.ProgressBar.render(%{
...> label: "Demo",
...> total: 200,
...> current: 8,
...> bar_width_ratio: 0.2,
...> start_symbol: "|",
...> absolute_values: true,
...> end_symbol: "|",
...> filled_symbol: "█",
...> partial_symbols: ["▏", "▎", "▍", "▌", "▋", "▊", "▉"],
...> empty_symbol: " ",
...> screen_width: 40,
...> start_time: -576460748012758993,
...> current_time: -576460748012729828
...> }) |> to_string()
"Demo 8/200 00:29.2 |▍ | 4%"
iex> Owl.ProgressBar.render(%{
...> label: "Demo",
...> total: 200,
...> current: 8,
...> bar_width_ratio: 0.7,
...> start_symbol: "[",
...> end_symbol: "]",
...> filled_symbol: Owl.Data.tag("≡", :cyan),
...> partial_symbols: [Owl.Data.tag("-", :green), Owl.Data.tag("=", :blue)],
...> empty_symbol: " ",
...> screen_width: 40
...> })|> Owl.Data.to_ansidata() |> to_string
"Demo [[36m≡[39m[32m-[39m ] 4%[0m"
@spec start( label: String.t(), id: id(), total: pos_integer(), timer: boolean(), absolute_values: boolean(), current: non_neg_integer(), bar_width_ratio: nil | float(), start_symbol: Owl.Data.t(), end_symbol: Owl.Data.t(), filled_symbol: Owl.Data.t(), partial_symbols: [Owl.Data.t()], empty_symbol: Owl.Data.t(), screen_width: pos_integer() ) :: DynamicSupervisor.on_start_child()
Starts a progress bar on Owl.LiveScreen
.
options
Options
:id
- an id of the progress bar. Required.:label
- a label of the progress bar. Required.:total
- a total value. Required.:current
- a current value. Defaults to0
.:bar_width_ratio
- a bar width ratio. Defaults to 0.7.:timer
- set totrue
to launch a timer and display it before the bar in formatMM:SS
. Defaults tofalse
.:absolute_values
- set totrue
to show absolute values before the bar in formatcurrent/total
. Defaults tofalse
.:start_symbol
- a symbol that is rendered at the beginning of the progress bar. Defaults to"["
.:end_symbol
- a symbol that rendered at the end of the progress bar. Defaults to"]"
.:filled_symbol
- a symbol that use used whencurrent
value is big enough to fill the cell. Defaults to"≡"
:partial_symbols
- a list of symbols that are used whencurrent
value is too small to renderfilled_symbol
. Defaults to["-", "="]
.:empty_symbol
- an empty symbol. Defaults to" "
.:screen_width
- a width of output data. Defaults to width of the terminal or 80 symbols, if a terminal is not available.