Sha256: 3bf6756a34ff4aa55bc2bc80ed989de62c9566d0e9e58ce50a9011f0a73e4acb

Contents?: true

Size: 1.32 KB

Versions: 4

Compression:

Stored size: 1.32 KB

Contents

require_relative '../wrap'
require_relative 'basic_wrapper'
require_relative 'layer'

module LovelyRufus
  module Layers
    class HangoutWrapper < Layer
      def call(wrap)
        @wrap = wrap
        final = hangout_line ? rewrapped : wrap
        next_layer.call(final)
      end

      private_attr_reader :wrap

      private

      module HangoutFinder
        module_function

        def between?(line_a, line_b)
          last_space = line_a.chomp.rindex(/\p{space}/)
          last_space and last_space >= line_b.chomp.size
        end

        def reverse?(line_a, line_b)
          cut = line_a.chomp.rindex(/\p{space}/)
          a_after = line_a[0...cut] + "\n"
          b_after = line_a[(cut + 1)..-1] + line_b
          b_after.chomp.rindex(/\p{space}/) > a_after.size
        end
      end

      def hangout_line
        lines.each_cons(2).with_index do |(a, b), i|
          if HangoutFinder.between?(a, b)
            return a unless i == lines.size - 2 and HangoutFinder.reverse?(a, b)
          end
        end
      end

      def lines
        @lines ||= wrap.lines
      end

      def rewrapped
        hangout_line[-1] = NBSP
        HangoutWrapper.new.call(wrapped)
      end

      def wrapped
        unfolded = Wrap[lines.join, width: wrap.width]
        BasicWrapper.new.call(unfolded)
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
lovely_rufus-1.0.0 lib/lovely_rufus/layers/hangout_wrapper.rb
lovely_rufus-0.3.2 lib/lovely_rufus/layers/hangout_wrapper.rb
lovely_rufus-0.3.1 lib/lovely_rufus/layers/hangout_wrapper.rb
lovely_rufus-0.3.0 lib/lovely_rufus/layers/hangout_wrapper.rb