Sha256: 3bf5d3468e0037a0ba85000c70e58c7e0c0f77e4078a186c9d1b3356b54b5c6d

Contents?: true

Size: 884 Bytes

Versions: 2

Compression:

Stored size: 884 Bytes

Contents

module CliSpinnable
  class Line
    class Error < CliSpinnableError; end

    CARRIAGE_RETURN = "\r".freeze
    NEWLINE = "\n".freeze

    attr_accessor :newline

    def initialize(str = nil, sign = nil, newline = nil)
      self.str = str
      @sign = Sign.new(sign)
      self.newline = newline
    end

    def sign=(sym)
      sign.sign = sym
    end

    def <<(arg)
      str << ensure_single_line(String(arg))
    end

    def str=(arg)
      @str = ensure_single_line(String(arg))
    end

    def to_s_resetting_newline
      [CARRIAGE_RETURN, sign, str, newline_with_reset].join
    end

    private

    attr_reader :str, :sign

    def ensure_single_line(str)
      raise Error, 'Multiline strings not allowed' if str.include?(NEWLINE)
      str
    end

    def newline_with_reset
      return unless newline
      self.newline = false
      NEWLINE
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
cli_spinnable-0.2 lib/cli_spinnable/line.rb
cli_spinnable-0.1 lib/cli_spinnable/line.rb