module Riffer::Rig::Terminal::Markdown::Flow
Constants
- UNSTYLED
- VISIBLE
Public Instance Methods
(Array[Riffer::Rig::Terminal::Markdown::Span] spans, theme: Riffer::Rig::Terminal::Theme, width: Integer?) → Array[String]
Source
# File lib/riffer/rig/terminal/markdown/flow.rb, line 13 def lines(spans, theme:, width:) wrapper = Riffer::Rig::Terminal::Wrapper.new(width: width, indent: 0) wrapped = (wrapper << "#{spans.map(&:text).join}\n").lines(chomp: true) # The Wrapper moves, drops and rewrites whitespace but never a visible # character, so the nth visible character of its output is the nth one the # spans supplied. owners = spans.flat_map { |span| [span] * span.text.scan(VISIBLE).size } sizes = wrapped.map { |line| line.scan(VISIBLE).size } starts = sizes.each_with_object([0]) { |size, sums| sums << (sums.fetch(-1) + size) } wrapped.each_with_index.map do |line, index| paint(line, owners.slice(starts.fetch(index), sizes.fetch(index)) || [], theme) end end
@rbs spans: Array @rbs theme: Riffer::Rig::Terminal::Theme @rbs width: Integer? @rbs return: Array