Class: CW::Tester

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

Direct Known Subclasses

Book, RepeatWord, Reveal, TestLetters, TestWords, Tx

Instance Method Summary collapse

Instance Method Details

#audio_stopObject



116
117
118
# File 'lib/cw/tester.rb', line 116

def audio_stop
  play.stop if play.still_playing?
end

#complete_word?Boolean

Returns:

  • (Boolean)


152
153
154
# File 'lib/cw/tester.rb', line 152

def complete_word?
  get_word_last_char == ' '
end

#current_wordObject



22
# File 'lib/cw/tester.rb', line 22

def current_word         ; @current_word ||= CurrentWord.new ; end

#cw_threadsObject



229
230
231
# File 'lib/cw/tester.rb', line 229

def cw_threads
  @cw_threads ||= Threads.new(self, thread_processes)
end

#do_eventsObject



25
26
27
# File 'lib/cw/tester.rb', line 25

def do_events
  sleep 0.005
end

#exit!Object



8
# File 'lib/cw/tester.rb', line 8

def exit!                ; Cfg.config.params["exit"] = true  ; end

#exit?Boolean

Returns:

  • (Boolean)


9
# File 'lib/cw/tester.rb', line 9

def exit?                ; Cfg.get_param("exit")             ; end

#failed!Object



98
99
100
# File 'lib/cw/tester.rb', line 98

def failed!
  @failed = true
end

#failed?Boolean

Returns:

  • (Boolean)


94
95
96
# File 'lib/cw/tester.rb', line 94

def failed?
  @failed
end

#finish?Boolean

Returns:

  • (Boolean)


87
88
89
90
91
92
# File 'lib/cw/tester.rb', line 87

def finish?
  return true if stream.stream_empty?
  return true if timing.print_words_timeout?
  return true if quit?
  false
end

#get_key_inputObject

def start_sync ; @start_sync = true ; end



16
# File 'lib/cw/tester.rb', line 16

def get_key_input        ; key_input.read                    ; end

#get_word_last_charObject



129
130
131
# File 'lib/cw/tester.rb', line 129

def get_word_last_char
  @input_word.split(//).last(1).first
end

#init_char_timerObject



23
# File 'lib/cw/tester.rb', line 23

def init_char_timer      ; timing.init_char_timer            ; end

#is_relevant_char?Boolean

Returns:

  • (Boolean)


19
# File 'lib/cw/tester.rb', line 19

def is_relevant_char?    ; key_input.is_relevant_char?       ; end

#key_chrObject



17
# File 'lib/cw/tester.rb', line 17

def key_chr              ; key_input.char                    ; end

#key_inputObject



18
# File 'lib/cw/tester.rb', line 18

def key_input            ; @key_input ||= KeyInput.new       ; end

#kill_threadsObject

def audio ; @audio ||= AudioPlayer.new ; end



14
# File 'lib/cw/tester.rb', line 14

def kill_threads         ; @threads.kill                     ; end

#monitor_keysObject



250
251
252
253
254
255
256
257
258
259
# File 'lib/cw/tester.rb', line 250

def monitor_keys
  loop do
    key_input.read
    break if quit_key_input?
    break if quit?
    break if exit?
    check_sentence_navigation(key_chr) if self.class == Book
    build_word_maybe
  end
end

#monitor_keys_threadObject



222
223
224
225
226
227
# File 'lib/cw/tester.rb', line 222

def monitor_keys_thread
  monitor_keys
  @key_input = nil
  print "\n\rmonitor keys has quit " if @debug
  exit!
end

#move_word_to_processObject



156
157
158
159
160
# File 'lib/cw/tester.rb', line 156

def move_word_to_process
  wait_for_no_word_process
  @process_input_word, @input_word = @input_word, ''
  @word_to_process = true
end

#playObject



11
# File 'lib/cw/tester.rb', line 11

def play                 ; @play ||= Play.new(@words)        ; end

#play_words_threadObject



209
210
211
212
213
214
# File 'lib/cw/tester.rb', line 209

def play_words_thread
#      p @words
  play.play_words_until_quit
  print "\n\rplay has quit " if @debug
  exit!
end


10
# File 'lib/cw/tester.rb', line 10

def print                ; @print ||= Print.new              ; end


80
81
82
83
84
85
# File 'lib/cw/tester.rb', line 80

def print_failed_exit_words
  until stream.stream_empty?
    print.fail stream.pop[:value]
  end
  print.reset
end

Returns:

  • (Boolean)


187
188
189
# File 'lib/cw/tester.rb', line 187

def print_letters?
  Cfg.config["print_letters"] #&& ! quit?
end


62
63
64
65
# File 'lib/cw/tester.rb', line 62

def print_words words
  timing.init_char_timer
  process_words words
end


102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/cw/tester.rb', line 102

def print_words_exit
  return if Cfg.config["print_letters"]
  timing.init_print_words_timeout
  loop do
    process_word_maybe
    if finish?
      break
    end
    sleep 0.01
  end
  failed! unless stream.stream_empty?
  print_failed_exit_words unless self.class == RepeatWord
end


216
217
218
219
220
# File 'lib/cw/tester.rb', line 216

def print_words_thread
  print_words_until_quit
  print "\n\rprint has quit " if @debug
  exit!
end


72
73
74
75
76
77
78
# File 'lib/cw/tester.rb', line 72

def print_words_until_quit
  @failed = false
  sync_with_audio_player
  print_words @words
  print_words_exit
  quit
end

#process_letters(letr) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/cw/tester.rb', line 29

def process_letters letr
  loop do
    do_events
    if self.class == Book
      process_space_maybe(letr) unless @book_details.args[:output] == :letter
      process_word_maybe
      break if change_repeat_or_quit?
      break if timing.char_delay_timeout?
    else
      process_space_maybe(letr) if(self.class == TestWords)
      process_space_maybe(letr) if(self.class == Reveal)
      process_word_maybe
      break if timing.char_delay_timeout?
    end
  end
end

#process_space_maybe(letr) ⇒ Object



178
179
180
181
182
183
184
185
# File 'lib/cw/tester.rb', line 178

def process_space_maybe letr
  if letr == ' '
    stream.push current_word.strip
    current_word.clear
    letr.clear
    print.success ' ' if print_letters?
  end
end

#process_word_maybeObject



67
68
69
70
# File 'lib/cw/tester.rb', line 67

def process_word_maybe
  print_marked_maybe
  process_input_word_maybe
end

#process_words(words) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/cw/tester.rb', line 46

def process_words words
  book_class = (self.class == Book)
  (words.to_s + ' ').each_char do |letr|
    process_letter letr
    if book_class
      stream.add_char(letr) if @book_details.args[:output] == :letter
    else
      stream.add_char(letr) if(self.class == TestLetters)
    end
    process_letters letr
    print.success letr if print_letters?
    break if(book_class && change_repeat_or_quit?)
    break if ((! book_class) && quit?)
  end
end

#push_letter_to_current_word(letr) ⇒ Object



125
126
127
# File 'lib/cw/tester.rb', line 125

def push_letter_to_current_word letr
  current_word.push_letter letr
end

#quitObject



6
# File 'lib/cw/tester.rb', line 6

def quit                 ; Cfg.config.params["quit"] = true  ; end

#quit?Boolean

Returns:

  • (Boolean)


7
# File 'lib/cw/tester.rb', line 7

def quit?                ; Cfg.get_param("quit")             ; end

#quit_key_input?Boolean

Returns:

  • (Boolean)


261
262
263
264
265
266
267
268
269
# File 'lib/cw/tester.rb', line 261

def quit_key_input?
  if key_input.quit_input?
    play.stop
    exit!
    quit
    play.stop
    true
  end
end

#reset_stdinObject



21
# File 'lib/cw/tester.rb', line 21

def reset_stdin          ; key_input.reset_stdin             ; end

#run(words) ⇒ Object



242
243
244
245
246
247
248
# File 'lib/cw/tester.rb', line 242

def run words
  @words = words
  cw_threads.run
  @play = nil
  reset_stdin
  print.newline
end

#sleep_char_delay(letr) ⇒ Object



162
163
164
# File 'lib/cw/tester.rb', line 162

def sleep_char_delay letr
  timing.append_char_delay letr, Cfg.config["wpm"], Cfg.config["effective_wpm"]
end

#streamObject



20
# File 'lib/cw/tester.rb', line 20

def stream               ; @stream ||= Stream.new            ; end

#sync_with_audio_playerObject



120
121
122
123
# File 'lib/cw/tester.rb', line 120

def sync_with_audio_player
  wait_for_start_sync
  play.wait_player_startup_delay
end

#sync_with_playObject



191
192
193
194
195
196
197
# File 'lib/cw/tester.rb', line 191

def sync_with_play
  loop do
    break if sentence_index_current?
    break if quit?
    sleep 0.015
  end
end

#sync_with_printObject



199
200
201
202
203
204
205
206
207
# File 'lib/cw/tester.rb', line 199

def sync_with_print
  loop do
    make_sentence_index_current if ! sentence_index_current?
    break if sentence_index_current?
    break if quit?
    sleep 0.015
    break
  end
end

#thread_processesObject



234
235
236
237
238
239
240
# File 'lib/cw/tester.rb', line 234

def thread_processes
  [
    :monitor_keys_thread,
    :play_words_thread,
    :print_words_thread
  ]
end

#timingObject



12
# File 'lib/cw/tester.rb', line 12

def timing               ; @timing ||= Timing.new            ; end

#wait_for_no_word_processObject



142
143
144
145
146
147
148
149
150
# File 'lib/cw/tester.rb', line 142

def wait_for_no_word_process
  word_proc_timeout(:init)
  while @word_to_process
    sleep 0.01
    if word_proc_timeout
      #        Kernel.exit(1)
    end
  end
end

#wait_for_start_syncObject



166
167
168
169
170
171
172
173
174
175
176
# File 'lib/cw/tester.rb', line 166

def wait_for_start_sync
  timeout = Time.now + 5
  until play.start_sync?
    sleep 0.001
    break if quit?
    if timeout < Time.now
      exit!
      Kernel.exit 1
    end
  end
end

#word_proc_timeout(arg = :status) ⇒ Object



133
134
135
136
137
138
139
140
# File 'lib/cw/tester.rb', line 133

def word_proc_timeout(arg = :status)
  if arg == :init
    @wp_timeout = Time.now + 5
  else
    return true if(Time.now > @wp_timeout)
  end
  return false
end