Sha256: fe886ce6e3ec6291da314ba4973a1f5c9dab3043565f66056223879f7adcc0c6

Contents?: true

Size: 1.31 KB

Versions: 3

Compression:

Stored size: 1.31 KB

Contents

# frozen_string_literal: true

module Basic101

  class Output

    def initialize(file = $stdout)
      @file = file
      @chars_on_line = 0
      @transcript = NullTranscript.new
    end

    def transcript=(transcript)
      @transcript = transcript
    end

    def puts(s = '')
      print s.to_s + "\n"
    end

    def print(s)
      write_string s, true
    end

    def echo(s)
      write_string s, false
    end

    def tab_to(column)
      column = [0, column].max
      spaces_needed = -> {column - @chars_on_line}
      return if spaces_needed.call < 0
      print ' ' * spaces_needed.call
    end

    def isatty
      @file.isatty
    end

    private

    COLUMNS_PER_TAB = 14

    def write_string(s, transcribe)
      s.chars.each do |c|
        case c
        when "\n"
          write_char c, transcribe
          @chars_on_line = 0
        when "\t"
          write_char ' ', transcribe
          while @chars_on_line % COLUMNS_PER_TAB > 0
            write_char ' ', transcribe
          end
        else
          write_char c, transcribe
        end
      end
      flush
    end

    def write_char(c, transcribe)
      @transcript.save_output(c) if transcribe
      @file.print c
      @chars_on_line += 1 if c =~ /[[:print:]]/
    end

    def flush
      return if isatty
      @file.flush
    end

  end

end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
basic101-1.0.2 lib/basic101/output.rb
basic101-1.0.1 lib/basic101/output.rb
basic101-1.0.0 lib/basic101/output.rb