Sha256: bee0865656edd4d2393f00be15b4cd44c87b1b32172745729f4a5b39adf3a31d
Contents?: true
Size: 1.4 KB
Versions: 1
Compression:
Stored size: 1.4 KB
Contents
module Typingtutor class Line def initialize(line:, stats:) @original = line @actual = "" @position = 0 @keystrokes = 0 @stats = stats end def play print "\n#{@original.ljust(80)}\r" while(true) do char = STDIN.getch case char.ord when 3 # CTRL-C puts exit(1) 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 results end def print_char if actual_char print HighLine.color(actual_char, ok? ? :correct : :error) else print HighLine.color(expected_char, :plain) end @stats.record_letter(expected_char, ok?) 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 results { chars: @original.length, correct_chars: @original.length.times.select {|i| @original[i] == @actual[i] }.size, keystrokes: @keystrokes, words: @actual.split(' ').size } end end # Line end # Typingtutor
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
typingtutor-1.0.6 | lib/typingtutor/line.rb |