module ClockMeaningSpread
ClockMeaningSpread — spread flip definitions across multiline clock ticks. Public: spread, wrap_text Depends: TextUtil, ClockCondense Tests: test/lib/clock_meaning_spread_test.rb
Public Instance Methods
Source
# File lib/clock/meaning_spread.rb, line 57 def pick_multiple(pool, lines:, exclude:, rng:) blocked = Array(exclude).map(&:to_s).reject(&:empty?) choices = pool.reject { |line| blocked.include?(line) } choices = pool if choices.empty? selected = if choices.length <= lines choices else choices.shuffle(random: rng).first(lines) end selected.map { |line| ClockCondense.full_line(line) } end
Source
# File lib/clock/meaning_spread.rb, line 14 def spread(pool, lines:, exclude: [], rng: Random.new, max_width: ClockCondense::MAX_BODY_LINE) pool = Array(pool).map(&:to_s).reject { |line| line.strip.empty? } return [] if pool.empty? budget = [lines.to_i, 1].max if pool.length == 1 return wrap_text(ClockCondense.full_line(pool.first), lines: budget, max_width: max_width) end pick_multiple(pool, lines: budget, exclude: exclude, rng: rng) end
Source
# File lib/clock/meaning_spread.rb, line 27 def wrap_text(text, lines:, max_width: ClockCondense::MAX_BODY_LINE) text = TextUtil.squish(text.to_s) return [] if text.empty? budget = [lines.to_i, 1].max result = [] remaining = text while !remaining.empty? && result.length < budget if remaining.length <= max_width result << remaining remaining = "" else slice = remaining[0, max_width] break_at = slice.rindex(" ") break_at = max_width if break_at.nil? || break_at.zero? result << remaining[0, break_at].rstrip remaining = remaining[break_at..].lstrip end end if !remaining.empty? && !result.empty? result[-1] = "#{result[-1]} #{remaining}".strip elsif !remaining.empty? result << remaining end result end