module MessageProgress
Shared line-progress percentages for multi-line IRC replies (e.g. !sing lyrics). Public: ReplyContext, line_progress_percent, should_prefix?, format_digit_label, prefix_piece, prefix_lines, prefix_text, format_progress_label Depends: IrcFormat, ReplyDelivery Tests: test/lib/message_progress_test.rb
Constants
- IRC_RESET
- ReplyContext
- STYLE_GUTTER
Public Instance Methods
Source
# File lib/message_progress.rb, line 51 def default_context(piece_index: 0, total_pieces: 1) delivery = ReplyDelivery.current || {} ReplyContext.new( lag_ms: delivery[:lag_ms].to_i, ping_ms: delivery[:ping_ms], piece_index: piece_index, total_pieces: total_pieces, queued: delivery[:queued] ) end
Source
# File lib/message_progress.rb, line 73 def format_digit_label(percent, context: nil) ctx = context || default_context text = label_plain_text(percent) core = text.chomp(" ") colored = "#{STYLE_GUTTER}#{core}#{IRC_RESET} " theme_gutter(colored, ctx) end
Source
# File lib/message_progress.rb, line 81 def format_progress_label(percent, context: nil) format_digit_label(percent, context: context) end
Source
# File lib/message_progress.rb, line 40 def label_plain_text(percent) case percent when 100 "00 " when 1..9 "#{percent}% " else format("%02d ", percent) end end
Source
# File lib/message_progress.rb, line 20 def line_progress_percent(line_number, total_lines) return 100 if total_lines.to_i <= 0 percent = ((line_number.to_f / total_lines) * 100).round [[percent, 1].max, 100].min end
Source
# File lib/message_progress.rb, line 90 def prefix_lines(lines, context: nil) cleaned = Array(lines).map(&:to_s).map(&:strip).reject(&:empty?) return "" if cleaned.empty? total = cleaned.length cleaned.each_with_index.map do |line, index| percent = line_progress_percent(index + 1, total) piece_context = (context || default_context).then do |base| ReplyContext.new( lag_ms: base.lag_ms, ping_ms: base.ping_ms, piece_index: index, total_pieces: total, queued: base.queued ) end prefix_piece(line.rstrip, percent: percent, context: piece_context) end.join("\n") end
Source
# File lib/message_progress.rb, line 85 def prefix_piece(piece, percent:, context: nil) ctx = context || default_context "#{format_digit_label(percent, context: ctx)}#{piece}" end
Source
# File lib/message_progress.rb, line 110 def prefix_text(text, context: nil) prefix_lines(text.to_s.split("\n"), context: context) end
Source
# File lib/message_progress.rb, line 31 def resolve_with_progress(with_progress, logical_lines:, pieces:) case with_progress when :auto should_prefix?(logical_lines: logical_lines, pieces: pieces) else !!with_progress end end
Source
# File lib/message_progress.rb, line 27 def should_prefix?(logical_lines:, pieces:) logical_lines.to_i >= 2 || pieces.to_i >= 2 end
Source
# File lib/message_progress.rb, line 62 def theme_gutter(gutter, context) network = context&.network channel = context&.channel return gutter if network.to_s.empty? || channel.to_s.empty? require_relative "theme_output" ThemeOutput.apply(network: network, channel: channel, plugin: "progress", text: gutter) rescue StandardError gutter end