Class: Autobuild::ProgressDisplay
- Inherits:
-
Object
- Object
- Autobuild::ProgressDisplay
- Defined in:
- lib/autobuild/progress_display.rb
Overview
Management of the progress display
Constant Summary collapse
- PROGRESS_MODES =
Valid progress modes
%I[single_line newline off].freeze
Instance Attribute Summary collapse
-
#progress_mode ⇒ Symbol
Return the current display mode.
-
#progress_period ⇒ Float
Minimum time between two progress displays.
- #silent ⇒ Object
Instance Method Summary collapse
- #display_progress(consider_period: true) ⇒ Object
- #find_common_prefix(msg, other_msg) ⇒ Object
- #format_grouped_messages(raw_messages, indent: " ", width: TTY::Screen.width) ⇒ Object
- #group_messages(messages) ⇒ Object
-
#initialize(io, color: ::Autobuild.method(:color)) ⇒ ProgressDisplay
constructor
A new instance of ProgressDisplay.
- #message(message, *args, io: @io, force: false) ⇒ Object
- #progress(key, *args) ⇒ Object
- #progress_done(key, display_last = true, message: nil) ⇒ Object
-
#progress_enabled=(flag) ⇒ Object
deprecated
Deprecated.
use progress_mode= instead
-
#progress_enabled? ⇒ Boolean
Whether progress messages will be displayed at all.
- #progress_start(key, *args, done_message: nil) ⇒ Object
- #refresh_display ⇒ Object
- #refresh_display_under_lock ⇒ Object
- #silent? ⇒ Boolean
- #synchronize(&block) ⇒ Object
Constructor Details
#initialize(io, color: ::Autobuild.method(:color)) ⇒ ProgressDisplay
Returns a new instance of ProgressDisplay.
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/autobuild/progress_display.rb', line 7 def initialize(io, color: ::Autobuild.method(:color)) @io = io @cursor = TTY::Cursor @last_formatted_progress = [] @progress_messages = Concurrent::Array.new @silent = false @color = color @display_lock = Mutex.new @next_progress_display = Time.at(0) @progress_mode = :single_line @progress_period = 0.1 @message_queue = Queue.new @forced_progress_display = Concurrent::AtomicBoolean.new(false) end |
Instance Attribute Details
#progress_mode ⇒ Symbol
Return the current display mode
69 70 71 |
# File 'lib/autobuild/progress_display.rb', line 69 def progress_mode @progress_mode end |
#progress_period ⇒ Float
Minimum time between two progress displays
This does not affect normal messages
43 44 45 |
# File 'lib/autobuild/progress_display.rb', line 43 def progress_period @progress_period end |
#silent ⇒ Object
77 78 79 80 81 82 83 |
# File 'lib/autobuild/progress_display.rb', line 77 def silent silent = @silent @silent = true yield ensure @silent = silent end |
Instance Method Details
#display_progress(consider_period: true) ⇒ Object
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 |
# File 'lib/autobuild/progress_display.rb', line 191 def display_progress(consider_period: true) return unless progress_enabled? return if consider_period && (@next_progress_display > Time.now) formatted = ( @progress_messages.map(&:last), indent: " " ) if @progress_mode == :newline @io.print formatted.join("\n") @io.print "\n" else @io.print @cursor.clear_screen_down @io.print formatted.join("\n") @io.print @cursor.up(formatted.size - 1) if formatted.size > 1 @io.print @cursor.column(0) end @io.flush @next_progress_display = Time.now + @progress_period end |
#find_common_prefix(msg, other_msg) ⇒ Object
212 213 214 215 216 217 218 219 220 221 222 223 |
# File 'lib/autobuild/progress_display.rb', line 212 def find_common_prefix(msg, other_msg) msg = msg.split(' ') other_msg = other_msg.split(' ') msg.each_with_index do |token, idx| if other_msg[idx] != token prefix = msg[0..(idx - 1)].join(" ") prefix << ' ' unless prefix.empty? return prefix end end msg.join(' ') end |
#format_grouped_messages(raw_messages, indent: " ", width: TTY::Screen.width) ⇒ Object
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 |
# File 'lib/autobuild/progress_display.rb', line 268 def (, indent: " ", width: TTY::Screen.width) groups = () groups.each_with_object([]) do |(prefix, ), lines| if prefix.empty? lines.concat(.map { |m| "#{indent}#{m.strip}" }) next end lines << "#{indent}#{prefix.dup.strip} #{.shift}" until .empty? msg = .shift.strip margin = .empty? ? 1 : 2 if lines.last.size + margin + msg.size > width lines.last << "," lines << +"" lines.last << indent << indent << msg else lines.last << ", " << msg end end lines.last << "," unless .empty? end end |
#group_messages(messages) ⇒ Object
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 |
# File 'lib/autobuild/progress_display.rb', line 225 def () = .sort groups = Array.new groups << ["", (0....size)] .each_with_index do |msg, idx| prefix = nil grouping = false [(idx + 1)..-1].each_with_index do |other_msg, other_idx| other_idx += idx + 1 prefix ||= find_common_prefix(msg, other_msg) break unless other_msg.start_with?(prefix) if grouping break if prefix != groups.last[0] groups.last[1] << other_idx else current_prefix, current_group = groups.last if prefix.size > current_prefix.size # create a new group group_end_index = [idx - 1, current_group.last].min groups.last[1] = (current_group.first..group_end_index) groups << [prefix, [idx, other_idx]] grouping = true else break end end end end if groups.last.last.last < .size groups << ["", (groups.last.last.last + 1)...(.size)] end groups.map do |prefix, indexes| indexes = indexes.to_a next if indexes.empty? range = (prefix.size)..-1 [prefix, indexes.map { |i| [i][range] }] end.compact end |
#message(message, *args, io: @io, force: false) ⇒ Object
95 96 97 98 99 100 101 102 |
# File 'lib/autobuild/progress_display.rb', line 95 def (, *args, io: @io, force: false) return if silent? && !force io = args.pop if args.last.respond_to?(:to_io) @message_queue << [, args, io] refresh_display end |
#progress(key, *args) ⇒ Object
129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/autobuild/progress_display.rb', line 129 def progress(key, *args) found = false @progress_messages.map! do |msg_key, msg| if msg_key == key found = true [msg_key, @color.call(*args)] else [msg_key, msg] end end @progress_messages << [key, @color.call(*args)] unless found refresh_display end |
#progress_done(key, display_last = true, message: nil) ⇒ Object
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
# File 'lib/autobuild/progress_display.rb', line 144 def progress_done(key, display_last = true, message: nil) current_size = @progress_messages.size @progress_messages.delete_if do |msg_key, msg| if msg_key == key = msg if display_last && ! true end end changed = current_size != @progress_messages.size if changed if (" #{}") # NOTE: message updates the display already else refresh_display end true end end |
#progress_enabled=(flag) ⇒ Object
Deprecated.
use progress_mode= instead
86 87 88 |
# File 'lib/autobuild/progress_display.rb', line 86 def progress_enabled=(flag) self.progress_mode = flag ? :single_line : :off end |
#progress_enabled? ⇒ Boolean
Whether progress messages will be displayed at all
91 92 93 |
# File 'lib/autobuild/progress_display.rb', line 91 def progress_enabled? !@silent && (@progress_mode != :off) end |
#progress_start(key, *args, done_message: nil) ⇒ Object
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/autobuild/progress_display.rb', line 104 def progress_start(key, *args, done_message: nil) progress_done(key) = @color.call(*args) @progress_messages << [key, ] if progress_enabled? @forced_progress_display.make_true else " #{}" end refresh_display if block_given? begin result = yield progress_done(key, message: ) result rescue Exception progress_done(key) raise end end end |
#refresh_display ⇒ Object
165 166 167 168 169 170 171 172 173 |
# File 'lib/autobuild/progress_display.rb', line 165 def refresh_display return unless @display_lock.try_lock begin refresh_display_under_lock ensure @display_lock.unlock end end |
#refresh_display_under_lock ⇒ Object
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 |
# File 'lib/autobuild/progress_display.rb', line 175 def refresh_display_under_lock # Display queued messages until @message_queue.empty? , args, io = @message_queue.pop io.print @cursor.clear_screen_down if @progress_mode == :single_line io.puts @color.call(, *args) io.flush if @io != io end # And re-display the progress display_progress(consider_period: @forced_progress_display.false?) @forced_progress_display.make_false @io.flush end |
#silent? ⇒ Boolean
71 72 73 |
# File 'lib/autobuild/progress_display.rb', line 71 def silent? @silent end |
#synchronize(&block) ⇒ Object
25 26 27 28 29 |
# File 'lib/autobuild/progress_display.rb', line 25 def synchronize(&block) result = @display_lock.synchronize(&block) refresh_display result end |