Class: Marvi::Diagram::StateRenderer
- Inherits:
-
Object
- Object
- Marvi::Diagram::StateRenderer
- Defined in:
- lib/marvi/diagram.rb
Instance Method Summary collapse
-
#initialize(lines, max_width) ⇒ StateRenderer
constructor
A new instance of StateRenderer.
- #render ⇒ Object
Constructor Details
#initialize(lines, max_width) ⇒ StateRenderer
Returns a new instance of StateRenderer.
600 601 602 603 |
# File 'lib/marvi/diagram.rb', line 600 def initialize(lines, max_width) @lines = lines @max_width = max_width end |
Instance Method Details
#render ⇒ Object
605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 |
# File 'lib/marvi/diagram.rb', line 605 def render transitions = [] @lines.each do |line| stripped = line.strip next if stripped.empty? || stripped.start_with?("%%") next if stripped.start_with?("direction") body, _, label = stripped.partition(":") m = body.strip.match(/\A(\[\*\]|\w+)\s*-{2,3}>\s*(\[\*\]|\w+)\z/) return nil unless m transitions << [state_name(m[1], :source), state_name(m[2], :target), label.strip] end return nil if transitions.empty? edge_width = transitions.map { |from, to, _| Unicode::DisplayWidth.of("#{from} #{ARROW} #{to}") }.max spans = transitions.map do |from, to, label| text = "#{from} #{ARROW} #{to}" text = "#{pad_right(text, edge_width)} : #{label}" unless label.empty? [Span.new(text: text, color: COLOR)] end return nil if spans.any? { |s| Unicode::DisplayWidth.of(s.first.text) > @max_width } spans end |