Class: CWG::Tester

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

Direct Known Subclasses

Book, RepeatWord, Reveal, TestLetters, TestWords, Tx

Instance Method Summary (collapse)

Instance Method Details

- (Object) add_space(words)



25
26
27
28
29
# File '/Users/martyn/cw/cw_clone/lib/cw/tester.rb', line 25

def add_space words
  str = ''
  words.to_array.collect { |word| str << word + ' '}
  str
end

- (Object) audio



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

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

- (Object) audio_play



31
32
33
34
35
# File '/Users/martyn/cw/cw_clone/lib/cw/tester.rb', line 31

def audio_play
  audio.convert_words add_space @words
  start_sync()
  audio.play
end

- (Object) audio_stop



132
133
134
# File '/Users/martyn/cw/cw_clone/lib/cw/tester.rb', line 132

def audio_stop
  audio.stop if audio.still_playing?
end

- (Object) check_quit_key_input



278
279
280
281
282
283
284
285
286
# File '/Users/martyn/cw/cw_clone/lib/cw/tester.rb', line 278

def check_quit_key_input
  if key_input.quit_input?
    audio.stop
    Params.exit = true
    quit
    audio_stop
    true
  end
end

- (Boolean) complete_word?

Returns:

  • (Boolean)


172
173
174
# File '/Users/martyn/cw/cw_clone/lib/cw/tester.rb', line 172

def complete_word?
  get_word_last_char == ' '
end

- (Object) current_word



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

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

- (Object) do_events



55
56
57
# File '/Users/martyn/cw/cw_clone/lib/cw/tester.rb', line 55

def do_events
  sleep 0.005
end

- (Boolean) finish?

Returns:

  • (Boolean)


115
116
117
118
119
# File '/Users/martyn/cw/cw_clone/lib/cw/tester.rb', line 115

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

- (Object) get_key_input



15
# File '/Users/martyn/cw/cw_clone/lib/cw/tester.rb', line 15

def get_key_input        ; key_input.read                    ; end

- (Object) get_word_last_char



149
150
151
# File '/Users/martyn/cw/cw_clone/lib/cw/tester.rb', line 149

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

- (Object) init_char_timer



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

def init_char_timer      ; timing.init_char_timer            ; end

- (Boolean) is_relevant_char?

Returns:

  • (Boolean)


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

def is_relevant_char?    ; key_input.is_relevant_char?       ; end

- (Object) key_chr



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

def key_chr              ; key_input.char                    ; end

- (Object) key_input



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

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

- (Object) kill_threads



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

def kill_threads         ; @threads.kill                     ; end

- (Object) monitor_keys



267
268
269
270
271
272
273
274
275
276
# File '/Users/martyn/cw/cw_clone/lib/cw/tester.rb', line 267

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

- (Object) monitor_keys_thread



245
246
247
248
249
# File '/Users/martyn/cw/cw_clone/lib/cw/tester.rb', line 245

def monitor_keys_thread
  monitor_keys
  print "\n\rmonitor keys has quit " if @debug
  Params.exit = true
end

- (Object) move_word_to_process



176
177
178
179
180
# File '/Users/martyn/cw/cw_clone/lib/cw/tester.rb', line 176

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

- (Object) play_words_exit



42
43
44
45
46
47
48
49
50
51
52
53
# File '/Users/martyn/cw/cw_clone/lib/cw/tester.rb', line 42

def play_words_exit
  timing.init_play_words_timeout
  loop do
    break if quit?
    break if timing.play_words_timeout?
    if Params.exit
      audio.stop
      break
    end
    sleep 0.01
  end
end

- (Object) play_words_thread



233
234
235
236
237
# File '/Users/martyn/cw/cw_clone/lib/cw/tester.rb', line 233

def play_words_thread
  play_words_until_quit
  print "\n\rplay has quit " if @debug
  Params.exit = true
end

- (Object) play_words_until_quit



37
38
39
40
# File '/Users/martyn/cw/cw_clone/lib/cw/tester.rb', line 37

def play_words_until_quit
  audio_play
  play_words_exit unless Params.print_letters
end


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

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


109
110
111
112
113
# File '/Users/martyn/cw/cw_clone/lib/cw/tester.rb', line 109

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

Returns:

  • (Boolean)


211
212
213
# File '/Users/martyn/cw/cw_clone/lib/cw/tester.rb', line 211

def print_letters?
  Params.print_letters && ! quit?
end


92
93
94
95
# File '/Users/martyn/cw/cw_clone/lib/cw/tester.rb', line 92

def print_words words
  timing.init_char_timer
  process_words words
end


121
122
123
124
125
126
127
128
129
130
# File '/Users/martyn/cw/cw_clone/lib/cw/tester.rb', line 121

def print_words_exit
  timing.init_print_words_timeout
  loop do
    process_word_maybe
    break if finish?
    sleep 0.01
  end
  @failed = true unless stream.stream_empty?
  print_failed_exit_words unless self.class == RepeatWord
end


239
240
241
242
243
# File '/Users/martyn/cw/cw_clone/lib/cw/tester.rb', line 239

def print_words_thread
  print_words_until_quit
  print "\n\rprint has quit " if @debug
  Params.exit = true
end


102
103
104
105
106
107
# File '/Users/martyn/cw/cw_clone/lib/cw/tester.rb', line 102

def print_words_until_quit
  sync_with_audio_player
  print_words @words
  print_words_exit unless Params.print_letters
  quit
end

- (Object) process_letters(letr)



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File '/Users/martyn/cw/cw_clone/lib/cw/tester.rb', line 59

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

- (Object) process_space_maybe(letr)



202
203
204
205
206
207
208
209
# File '/Users/martyn/cw/cw_clone/lib/cw/tester.rb', line 202

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

- (Object) process_word_maybe



97
98
99
100
# File '/Users/martyn/cw/cw_clone/lib/cw/tester.rb', line 97

def process_word_maybe
  print_marked_maybe
  process_input_word_maybe
end

- (Object) process_words(words)



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File '/Users/martyn/cw/cw_clone/lib/cw/tester.rb', line 76

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

- (Object) push_letter_to_current_word(letr)



145
146
147
# File '/Users/martyn/cw/cw_clone/lib/cw/tester.rb', line 145

def push_letter_to_current_word letr
  current_word.push_letter letr
end

- (Object) quit



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

def quit                 ; @quit = true                      ; end

- (Boolean) quit?

Returns:

  • (Boolean)


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

def quit?                ; @quit                             ; end

- (Boolean) quit_key_input?

Returns:

  • (Boolean)


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

def quit_key_input?      ; key_input.quit_input?             ; end

- (Object) reset_stdin



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

def reset_stdin          ; key_input.reset_stdin             ; end

- (Object) run(words)



259
260
261
262
263
264
265
# File '/Users/martyn/cw/cw_clone/lib/cw/tester.rb', line 259

def run words
  @words = words
  @cw_threads = CWThreads.new(self, thread_processes)
  @cw_threads.run
  reset_stdin
  print.newline
end

- (Object) sleep_char_delay(letr)



191
192
193
# File '/Users/martyn/cw/cw_clone/lib/cw/tester.rb', line 191

def sleep_char_delay letr
  timing.append_char_delay letr, Params.wpm, Params.effective_wpm
end

- (Object) spawn_play(cmd)



13
# File '/Users/martyn/cw/cw_clone/lib/cw/tester.rb', line 13

def spawn_play(cmd)      ; Process.spawn(cmd)                ; end

- (Object) start_sync



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

def start_sync           ; @start_sync = true                ; end

- (Boolean) start_sync?

Returns:

  • (Boolean)


182
183
184
185
186
187
188
189
# File '/Users/martyn/cw/cw_clone/lib/cw/tester.rb', line 182

def start_sync?
  if @start_sync
    @start_sync = nil
    true
  else
    nil
  end
end

- (Object) stream



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

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

- (Object) sync_with_audio_player



140
141
142
143
# File '/Users/martyn/cw/cw_clone/lib/cw/tester.rb', line 140

def sync_with_audio_player
  wait_for_start_sync
  wait_player_startup_delay
end

- (Object) sync_with_play



215
216
217
218
219
220
221
# File '/Users/martyn/cw/cw_clone/lib/cw/tester.rb', line 215

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

- (Object) sync_with_print



223
224
225
226
227
228
229
230
231
# File '/Users/martyn/cw/cw_clone/lib/cw/tester.rb', line 223

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

- (Object) thread_processes



251
252
253
254
255
256
257
# File '/Users/martyn/cw/cw_clone/lib/cw/tester.rb', line 251

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

- (Object) timing



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

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

- (Object) wait_for_no_word_process



162
163
164
165
166
167
168
169
170
# File '/Users/martyn/cw/cw_clone/lib/cw/tester.rb', line 162

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

- (Object) wait_for_start_sync



195
196
197
198
199
200
# File '/Users/martyn/cw/cw_clone/lib/cw/tester.rb', line 195

def wait_for_start_sync
  until start_sync?
    sleep 0.001
    break if quit?
  end
end

- (Object) wait_player_startup_delay



136
137
138
# File '/Users/martyn/cw/cw_clone/lib/cw/tester.rb', line 136

def wait_player_startup_delay
  audio.startup_delay
end

- (Object) word_proc_timeout(arg = :status)



153
154
155
156
157
158
159
160
# File '/Users/martyn/cw/cw_clone/lib/cw/tester.rb', line 153

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