typingtutor 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 172e5e72904ce07bf6295b7491915a254519987e
4
+ data.tar.gz: 3753eb0249be11a5daae7cdf66133f4306a14552
5
+ SHA512:
6
+ metadata.gz: 6de199dba40c1dc4a9e165428564a7a4ab65681bd1b7c4f9d9a39761c9cb0a2072ac6d58f64fbca58b5539976d74efc5424483323eb6b1a9b2f23a7ed13150b2
7
+ data.tar.gz: 78d4829b31028c505bd303f3ffa2c4bbc8487c26066ad6e06e4f8fa7d26b63e5f4946eb554929d0a725620ceb073ea3799b9fb2c3564061b75f2fedff1b85a74
data/bin/typingtutor ADDED
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env ruby
2
+ require 'typingtutor'
3
+ Typingtutor::Runner.new.run(ARGV[0])
@@ -0,0 +1,57 @@
1
+ module Typingtutor
2
+ class Line
3
+ def initialize(string)
4
+ @original = string
5
+ @actual = ""
6
+ @position = 0
7
+ @keystrokes = 0
8
+ end
9
+
10
+ def play
11
+ print "\n#{@original.ljust(80)}\r"
12
+
13
+ while(true) do
14
+ char = STDIN.getch
15
+ case char.ord
16
+ when 3 then exit(1) # CTRL-C
17
+ when 13 then break # enter
18
+ when 127 # backspace
19
+ @actual.chop!
20
+ @position = [@position - 1, 0].max
21
+ print_backspace
22
+ else
23
+ @actual += char
24
+ print_char
25
+ @position += 1
26
+ @keystrokes +=1
27
+ end
28
+ end
29
+ return stats
30
+ end
31
+
32
+ def print_char
33
+ if actual_char
34
+ print HighLine.color(actual_char, ok? ? :correct : :error)
35
+ else
36
+ print HighLine.color(expected_char, :plain)
37
+ end
38
+ end
39
+
40
+ def print_backspace
41
+ print "\b#{HighLine.color(expected_char, :plain)}\b"
42
+ end
43
+
44
+ def expected_char; @original[@position]; end
45
+ def actual_char; @actual[@position]; end
46
+ def ok?; expected_char == actual_char; end
47
+
48
+ def stats
49
+ { total_chars: @original.length,
50
+ correct_chars: @original.length.times.select {|i| @original[i] == @actual[i] }.size,
51
+ keystrokes: @keystrokes,
52
+ total_words: @original.split(' ').size
53
+ }
54
+ end
55
+
56
+ end # Line
57
+ end # Typingtutor
@@ -0,0 +1,76 @@
1
+ module Typingtutor
2
+ class Runner
3
+ include HighLine::SystemExtensions
4
+
5
+ def run(exercise_name)
6
+ exercise = exercises[exercise_name]
7
+ if exercise
8
+ setup_color_scheme
9
+ play_intro
10
+ @start = Time.now
11
+ stats = exercise.map { |line| Line.new(line).play }
12
+ print_stats(stats)
13
+ else
14
+ puts "usage: typingtutor <exercise>"
15
+ puts "available:"
16
+ exercises.keys.each {|e| puts "- #{e}"}
17
+ end
18
+ end
19
+
20
+ protected
21
+
22
+ def play_intro
23
+ puts "Typingtutor: Touch Typing Training"; sleep 0.5;
24
+ 3.downto(1) {|n| print "#{n}"; sleep 0.5; print '.'; sleep 0.5}
25
+ print "GO!\n"
26
+ end
27
+
28
+ def setup_color_scheme
29
+ cs = HighLine::ColorScheme.new do |cs|
30
+ cs[:plain] = [:white]
31
+ cs[:correct] = [:green]
32
+ cs[:error] = [:red, :bold]
33
+ end
34
+ HighLine.color_scheme = cs
35
+ end
36
+
37
+ def exercises
38
+ {
39
+ "lorem" => [
40
+ 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.', 'Nam nulla lacus, pharetra nec sollicitudin nec, faucibus non est. Nunc a erat in ante aliquet dapibus.',
41
+ 'Quisque congue libero vel mattis sodales.', 'Sed pellentesque, nunc nec gravida molestie, tellus mauris semper tellus, ac suscipit nisl est sit amet augue.',
42
+ 'Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos.', 'Sed vehicula risus risus, a lobortis velit ultricies ut. Nullam porttitor leo eu leo congue, id porta elit vestibulum.',
43
+ 'Suspendisse facilisis ut diam nec molestie. Donec consequat elementum volutpat.' ],
44
+ "test" => [
45
+ 'paolo is a nice guy', 'but he cannot type fast', 'that is a shame.'
46
+ ],
47
+ "fox" => ["The quick brown fox jumps over the lazy dog"],
48
+ "haikurb" => [
49
+ '"eyes".scan /the_darkness/',
50
+ 'catch( :in_the_wind ) { ?a.round; "breath"',
51
+ 'or "a".slice /of_moon/ }'
52
+ ]
53
+ }
54
+ end
55
+
56
+ def print_stats(stats)
57
+ time = Time.now - @start
58
+ total_chars = stats.map {|s| s[:total_chars] }.inject(:+)
59
+ correct_chars = stats.map {|s| s[:correct_chars] }.inject(:+)
60
+ total_words = stats.map {|s| s[:total_words] }.inject(:+)
61
+ keystrokes = stats.map {|s| s[:keystrokes] }.inject(:+)
62
+
63
+ typing_accuracy = (correct_chars.to_f / keystrokes.to_f * 100).to_i
64
+ word_accuracy = (correct_chars.to_f / total_chars.to_f * 100).to_i # TODO
65
+
66
+ gross_wpm = total_words / (time / 60)
67
+
68
+ puts
69
+ puts "------------------------"
70
+ puts "Time: #{time.round(1)}s (#{total_words} words)"
71
+ puts "Speed: #{gross_wpm.round} wpm"
72
+ puts "Word accuracy: #{word_accuracy}%"
73
+ puts "Typing accuracy: #{typing_accuracy}%"
74
+ end
75
+ end # Runner
76
+ end # Typingtutor
@@ -0,0 +1,4 @@
1
+ require "highline"
2
+ require 'io/console'
3
+ require 'typingtutor/line'
4
+ require 'typingtutor/runner'
metadata ADDED
@@ -0,0 +1,62 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: typingtutor
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Paolo Dona
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-08-31 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: highline
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '='
18
+ - !ruby/object:Gem::Version
19
+ version: 1.7.8
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '='
25
+ - !ruby/object:Gem::Version
26
+ version: 1.7.8
27
+ description:
28
+ email: paolo.dona@gmail.com
29
+ executables:
30
+ - typingtutor
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - bin/typingtutor
35
+ - lib/typingtutor.rb
36
+ - lib/typingtutor/line.rb
37
+ - lib/typingtutor/runner.rb
38
+ homepage: https://github.com/paolodona/typingtutor
39
+ licenses:
40
+ - MIT
41
+ metadata: {}
42
+ post_install_message:
43
+ rdoc_options: []
44
+ require_paths:
45
+ - lib
46
+ required_ruby_version: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: '0'
51
+ required_rubygems_version: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ requirements: []
57
+ rubyforge_project:
58
+ rubygems_version: 2.4.5.1
59
+ signing_key:
60
+ specification_version: 4
61
+ summary: Command line typing tutor
62
+ test_files: []