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.
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/autobuild/progress_display.rb', line 4 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
66 67 68 |
# File 'lib/autobuild/progress_display.rb', line 66 def progress_mode @progress_mode end |
#progress_period ⇒ Float
Minimum time between two progress displays
This does not affect normal messages
40 41 42 |
# File 'lib/autobuild/progress_display.rb', line 40 def progress_period @progress_period end |
#silent ⇒ Object
74 75 76 77 78 79 80 |
# File 'lib/autobuild/progress_display.rb', line 74 def silent silent = @silent @silent = true yield ensure @silent = silent end |
Instance Method Details
#display_progress(consider_period: true) ⇒ Object
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 |
# File 'lib/autobuild/progress_display.rb', line 188 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
209 210 211 212 213 214 215 216 217 218 219 220 |
# File 'lib/autobuild/progress_display.rb', line 209 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
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 |
# File 'lib/autobuild/progress_display.rb', line 265 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 + 1 > 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
222 223 224 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 |
# File 'lib/autobuild/progress_display.rb', line 222 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
92 93 94 95 96 97 98 99 |
# File 'lib/autobuild/progress_display.rb', line 92 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
126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/autobuild/progress_display.rb', line 126 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
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/autobuild/progress_display.rb', line 141 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
83 84 85 |
# File 'lib/autobuild/progress_display.rb', line 83 def progress_enabled=(flag) self.progress_mode = flag ? :single_line : :off end |
#progress_enabled? ⇒ Boolean
Whether progress messages will be displayed at all
88 89 90 |
# File 'lib/autobuild/progress_display.rb', line 88 def progress_enabled? !@silent && (@progress_mode != :off) end |
#progress_start(key, *args, done_message: nil) ⇒ Object
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/autobuild/progress_display.rb', line 101 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
162 163 164 165 166 167 168 169 170 |
# File 'lib/autobuild/progress_display.rb', line 162 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
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 |
# File 'lib/autobuild/progress_display.rb', line 172 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
68 69 70 |
# File 'lib/autobuild/progress_display.rb', line 68 def silent? @silent end |
#synchronize(&block) ⇒ Object
22 23 24 25 26 |
# File 'lib/autobuild/progress_display.rb', line 22 def synchronize(&block) result = @display_lock.synchronize(&block) refresh_display result end |