Class: RubyRich::ProgressBar
- Inherits:
-
Object
- Object
- RubyRich::ProgressBar
- Defined in:
- lib/ruby_rich/progress_bar.rb
Defined Under Namespace
Classes: MultiProgress
Constant Summary collapse
- STYLES =
进度条样式
{ default: { filled: '=', empty: ' ', prefix: '[', suffix: ']' }, blocks: { filled: '█', empty: '░', prefix: '[', suffix: ']' }, arrows: { filled: '>', empty: '-', prefix: '[', suffix: ']' }, dots: { filled: '●', empty: '○', prefix: '(', suffix: ')' }, line: { filled: '━', empty: '─', prefix: '│', suffix: '│' }, gradient: { filled: ['▁', '▂', '▃', '▄', '▅', '▆', '▇', '█'], empty: ' ', prefix: '[', suffix: ']' } }.freeze
Instance Attribute Summary collapse
-
#progress ⇒ Object
readonly
Returns the value of attribute progress.
-
#start_time ⇒ Object
readonly
Returns the value of attribute start_time.
-
#total ⇒ Object
readonly
Returns the value of attribute total.
Class Method Summary collapse
-
.with_progress(total, **options) ⇒ Object
静态方法:创建带回调的进度条.
Instance Method Summary collapse
- #advance(amount = 1) ⇒ Object
- #completed? ⇒ Boolean
- #elapsed_time ⇒ Object
- #eta ⇒ Object
- #finish(message: nil) ⇒ Object
-
#initialize(total, width: 50, style: :default, title: nil, show_percentage: true, show_rate: false, show_eta: false) ⇒ ProgressBar
constructor
A new instance of ProgressBar.
- #percentage ⇒ Object
- #rate ⇒ Object
- #render ⇒ Object
- #set_progress(value) ⇒ Object
- #start ⇒ Object
Constructor Details
#initialize(total, width: 50, style: :default, title: nil, show_percentage: true, show_rate: false, show_eta: false) ⇒ ProgressBar
Returns a new instance of ProgressBar.
16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/ruby_rich/progress_bar.rb', line 16 def initialize(total, width: 50, style: :default, title: nil, show_percentage: true, show_rate: false, show_eta: false) @total = total @progress = 0 @width = width @style = style @title = title @show_percentage = show_percentage @show_rate = show_rate @show_eta = show_eta @start_time = nil @last_update_time = nil @update_history = [] end |
Instance Attribute Details
#progress ⇒ Object (readonly)
Returns the value of attribute progress.
4 5 6 |
# File 'lib/ruby_rich/progress_bar.rb', line 4 def progress @progress end |
#start_time ⇒ Object (readonly)
Returns the value of attribute start_time.
4 5 6 |
# File 'lib/ruby_rich/progress_bar.rb', line 4 def start_time @start_time end |
#total ⇒ Object (readonly)
Returns the value of attribute total.
4 5 6 |
# File 'lib/ruby_rich/progress_bar.rb', line 4 def total @total end |
Class Method Details
.with_progress(total, **options) ⇒ Object
静态方法:创建带回调的进度条
120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/ruby_rich/progress_bar.rb', line 120 def self.with_progress(total, **) = new(total, **) .start begin yield() if block_given? ensure .finish end end |
Instance Method Details
#advance(amount = 1) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/ruby_rich/progress_bar.rb', line 36 def advance(amount = 1) @start_time ||= Time.now @progress += amount @progress = [@progress, @total].min current_time = Time.now @update_history << { time: current_time, progress: @progress } # 保留最近的几个更新用于计算速率 @update_history = @update_history.last(10) @last_update_time = current_time render puts if completed? end |
#completed? ⇒ Boolean
65 66 67 |
# File 'lib/ruby_rich/progress_bar.rb', line 65 def completed? @progress >= @total end |
#elapsed_time ⇒ Object
74 75 76 77 |
# File 'lib/ruby_rich/progress_bar.rb', line 74 def elapsed_time return 0 unless @start_time Time.now - @start_time end |
#eta ⇒ Object
92 93 94 95 96 |
# File 'lib/ruby_rich/progress_bar.rb', line 92 def eta return 0 if rate == 0 || completed? remaining = @total - @progress remaining / rate end |
#finish(message: nil) ⇒ Object
111 112 113 114 115 116 117 |
# File 'lib/ruby_rich/progress_bar.rb', line 111 def finish(message: nil) if puts "\r\e[K#{}" else puts end end |
#percentage ⇒ Object
69 70 71 72 |
# File 'lib/ruby_rich/progress_bar.rb', line 69 def percentage return 0 if @total == 0 (@progress.to_f / @total * 100).round(1) end |
#rate ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/ruby_rich/progress_bar.rb', line 79 def rate return 0 if @update_history.length < 2 first_update = @update_history.first last_update = @update_history.last time_diff = last_update[:time] - first_update[:time] progress_diff = last_update[:progress] - first_update[:progress] return 0 if time_diff == 0 progress_diff / time_diff end |
#render ⇒ Object
98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/ruby_rich/progress_bar.rb', line 98 def render = status_text = render_status output = "" output << "\e[94m#{@title}: \e[0m" if @title output << output << " #{status_text}" unless status_text.empty? print "\r\e[K#{output}" $stdout.flush end |
#set_progress(value) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/ruby_rich/progress_bar.rb', line 52 def set_progress(value) @start_time ||= Time.now @progress = [[value, 0].max, @total].min current_time = Time.now @update_history << { time: current_time, progress: @progress } @update_history = @update_history.last(10) @last_update_time = current_time render puts if completed? end |
#start ⇒ Object
30 31 32 33 34 |
# File 'lib/ruby_rich/progress_bar.rb', line 30 def start @start_time = Time.now @last_update_time = @start_time render end |