Class: CLI::UI::Progress
- Inherits:
-
Object
- Object
- CLI::UI::Progress
- Defined in:
- lib/cli/ui/progress.rb
Constant Summary collapse
- FILLED_BAR =
A Cyan filled block
"\e[46m"
- UNFILLED_BAR =
A bright white block
"\e[1;47m"
Class Method Summary collapse
-
.progress(width: Terminal.width) ⇒ Object
Add a progress bar to the terminal output.
Instance Method Summary collapse
-
#initialize(width: Terminal.width) ⇒ Progress
constructor
Initialize a progress bar.
-
#tick(percent: 0.01, set_percent: nil) ⇒ Object
Set the progress of the bar.
-
#to_s ⇒ Object
Format the progress bar to be printed to terminal.
Constructor Details
#initialize(width: Terminal.width) ⇒ Progress
Initialize a progress bar. Typically used in a Progress.progress
block
Options
One of the follow can be used, but not both together
-
:width
- The width of the terminal
49 50 51 52 |
# File 'lib/cli/ui/progress.rb', line 49 def initialize(width: Terminal.width) @percent_done = 0 @max_width = width end |
Class Method Details
.progress(width: Terminal.width) ⇒ Object
Add a progress bar to the terminal output
Example Usage:
Set the percent to X
CLI::UI::Progress.progress do ||
.tick(set_percent: percent)
end
Increase the percent by 1 percent
CLI::UI::Progress.progress do ||
.tick
end
Increase the percent by X
CLI::UI::Progress.progress do ||
.tick(percent: 5)
end
31 32 33 34 35 36 37 38 39 40 |
# File 'lib/cli/ui/progress.rb', line 31 def self.progress(width: Terminal.width) = Progress.new(width: width) print CLI::UI::ANSI.hide_cursor yield() ensure puts .to_s CLI::UI.raw do print(ANSI.show_cursor) end end |
Instance Method Details
#tick(percent: 0.01, set_percent: nil) ⇒ Object
Set the progress of the bar. Typically used in a Progress.progress
block
Options
One of the follow can be used, but not both together
-
:percent
- Increment progress by a specific percent amount -
:set_percent
- Set progress to a specific percent
62 63 64 65 66 67 68 69 70 |
# File 'lib/cli/ui/progress.rb', line 62 def tick(percent: 0.01, set_percent: nil) raise ArgumentError, 'percent and set_percent cannot both be specified' if percent != 0.01 && set_percent @percent_done += percent @percent_done = set_percent if set_percent @percent_done = [@percent_done, 1.0].min # Make sure we can't go above 1.0 print to_s print CLI::UI::ANSI.previous_line + "\n" end |
#to_s ⇒ Object
Format the progress bar to be printed to terminal
74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/cli/ui/progress.rb', line 74 def to_s suffix = " #{(@percent_done * 100).round(2)}%" workable_width = @max_width - Frame.prefix_width - suffix.size filled = [(@percent_done * workable_width.to_f).ceil, 0].max unfilled = [workable_width - filled, 0].max CLI::UI.resolve_text [ FILLED_BAR + ' ' * filled, UNFILLED_BAR + ' ' * unfilled, CLI::UI::Color::RESET.code + suffix ].join end |