Class: Typer::Application

Inherits:
Object
  • Object
show all
Defined in:
lib/typer.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Application

Returns a new instance of Application.



4
5
6
7
# File 'lib/typer.rb', line 4

def initialize(options = {})
  default_files = Dir.glob(File.expand_path(File.join(File.dirname(__FILE__), '..', 'lessons', '*.txt')))
  @lines = LineCollection.new(options[:file] || default_files)
end

Instance Method Details

#clearObject



50
51
52
# File 'lib/typer.rb', line 50

def clear
  puts "\n" * 3
end

#display_line(line) ⇒ Object



65
66
67
68
# File 'lib/typer.rb', line 65

def display_line line
  print line
  $stdout.flush
end

#display_retry(reason) ⇒ Object



94
95
96
97
# File 'lib/typer.rb', line 94

def display_retry(reason)
  puts ">>> #{reason} Try again!\n"
  puts "\n\n"
end

#display_speed(speed) ⇒ Object



99
100
101
# File 'lib/typer.rb', line 99

def display_speed(speed)
  puts "CPM: #{speed}"
end

#display_welcomeObject



54
55
56
57
58
59
60
61
62
63
# File 'lib/typer.rb', line 54

def display_welcome
  puts
  puts "typer - developer and sysadmin typing trainer"
  puts
  puts "The phrases are selected from all *.txt files in lessons directory and are combined so that a line length is between #{@lines.min_length} and #{@lines.max_length}"
  puts "The initial minimum is #{Speed.minimum.long_string} and will increase slowly depending on how fast you type."
  puts "Press ^C to exit."
  puts "Position your fingers over the home row and let the challenge begin!"
  wait_for_enter "start"
end

#exercise_line(line) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/typer.rb', line 22

def exercise_line line
  clear

  loop do
    display_line line
    wait_for_user_to_be_ready

    input_line, time = (read_line_with_time or throw :exit)

    if input_line == line
      clear

      speed = Speed.calculate(input_line, time)

      speed.influence_minimum!
      if speed.good?
        puts "GOOD! #{speed.long_string}  -  Next minimum: #{Speed.minimum}"
        wait_for_enter
        throw :next_line
      else
        display_retry "Too slow! Minimum: #{Speed.minimum}."
      end
    else
      display_retry "Incorrect."
    end
  end
end

#read_line_with_timeObject



83
84
85
86
87
88
89
90
91
92
# File 'lib/typer.rb', line 83

def read_line_with_time
  s = nil
  t0 = Time.now
  s = $stdin.readline.strip
  t1 = Time.now
  t = (t1 - t0).to_f
  s ? [s, t] : nil
rescue
  nil
end

#runObject



9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/typer.rb', line 9

def run
  catch :exit do
    trap("INT") { throw :exit }
    display_welcome
    loop do
      catch :next_line do
        exercise_line @lines.get_random
      end
    end
  end
  puts "\n"
end

#wait_for_enter(action = "continue") ⇒ Object



75
76
77
78
79
80
81
# File 'lib/typer.rb', line 75

def wait_for_enter action = "continue"
  puts
  sleep 1
  print "Press ENTER to #{action}...";
  $stdout.flush
  gets
end

#wait_for_user_to_be_readyObject



70
71
72
73
# File 'lib/typer.rb', line 70

def wait_for_user_to_be_ready
  sleep 1
  puts "\n"
end