Sha256: 0806678559da042133d2bc37de3107bb12c768790d3728dac7c89c0c4c6b67da

Contents?: true

Size: 1.31 KB

Versions: 1

Compression:

Stored size: 1.31 KB

Contents

module Typingtutor
  class Line
    def initialize(string)
      @original = string
      @actual   = ""
      @position = 0
      @keystrokes = 0
    end

    def play
      print "\n#{@original.ljust(80)}\r"

      while(true) do
        char = STDIN.getch
        case char.ord
        when 3 then exit(1) # CTRL-C
        when 13 then break # enter
        when 127 # backspace
          @actual.chop!
          @position = [@position - 1, 0].max
          print_backspace
        else
          @actual += char
          print_char
          @position += 1
          @keystrokes +=1
        end
      end
      return stats
    end

    def print_char
      if actual_char
        print HighLine.color(actual_char, ok? ? :correct : :error)
      else
        print HighLine.color(expected_char, :plain)
      end
    end

    def print_backspace
      print "\b#{HighLine.color(expected_char || " ", :plain)}\b"
    end

    def expected_char; @original[@position]; end
    def actual_char;   @actual[@position];   end
    def ok?; expected_char == actual_char; end

    def stats
      { total_chars: @original.length,
        correct_chars: @original.length.times.select {|i| @original[i] == @actual[i] }.size,
        keystrokes: @keystrokes,
        total_words: @original.split(' ').size
      }
    end

  end # Line
end # Typingtutor

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
typingtutor-1.0.3 lib/typingtutor/line.rb