Class: CWG::Book

Inherits:
Tester show all
Includes:
FileDetails
Defined in:
/Users/martyn/cw/cw_clone/lib/cw/book.rb

Constant Summary

Constant Summary

Constants included from FileDetails

FileDetails::AUDIO, FileDetails::HERE, FileDetails::TEXT

Instance Method Summary (collapse)

Methods included from FileDetails

#init_filenames

Methods inherited from Tester

#audio, #audio_play, #check_quit_key_input, #current_word, #do_events, #finish?, #get_key_input, #get_word_last_char, #init_char_timer, #is_relevant_char?, #key_chr, #key_input, #kill_threads, #monitor_keys, #monitor_keys_thread, #move_word_to_process, #play_words_exit, #play_words_thread, #play_words_until_quit, #print, #print_failed_exit_words, #print_letters?, #print_words, #print_words_exit, #print_words_thread, #print_words_until_quit, #process_letters, #process_space_maybe, #process_word_maybe, #process_words, #push_letter_to_current_word, #quit, #quit?, #quit_key_input?, #reset_stdin, #run, #sleep_char_delay, #spawn_play, #start_sync, #start_sync?, #stream, #sync_with_audio_player, #sync_with_play, #sync_with_print, #timing, #wait_for_no_word_process, #wait_for_start_sync, #wait_player_startup_delay, #word_proc_timeout

Constructor Details

- (Book) initialize(book_details)

Returns a new instance of Book



9
10
11
12
13
14
15
# File '/Users/martyn/cw/cw_clone/lib/cw/book.rb', line 9

def initialize book_details

  init_filenames
  @book_details = book_details
  read_book book_location
  find_sentences
end

Instance Method Details

- (Object) add_space(sentence)



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

def add_space sentence
  sentence + ' '
end

- (Object) audio_play_repeat_tone_maybe



72
73
74
# File '/Users/martyn/cw/cw_clone/lib/cw/book.rb', line 72

def audio_play_repeat_tone_maybe
  play_repeat_tone if repeat_sentence?
end

- (Object) audio_play_tone(t)



29
# File '/Users/martyn/cw/cw_clone/lib/cw/book.rb', line 29

def audio_play_tone t    ; audio.play_tone(t)                ; end

- (Object) audio_stop



32
# File '/Users/martyn/cw/cw_clone/lib/cw/book.rb', line 32

def audio_stop           ; audio.stop if audio.still_playing?; end

- (Object) await_next_sentence_or_quit



127
128
129
130
131
# File '/Users/martyn/cw/cw_clone/lib/cw/book.rb', line 127

def await_next_sentence_or_quit
  loop do
    break if next_sentence_or_quit?
  end
end

- (Object) book_location



33
# File '/Users/martyn/cw/cw_clone/lib/cw/book.rb', line 33

def book_location        ; @book_details.book_location       ; end

- (Object) build_word_maybe



87
88
89
90
91
92
93
94
95
# File '/Users/martyn/cw/cw_clone/lib/cw/book.rb', line 87

def build_word_maybe
  @input_word ||= ''
  @input_word << key_chr if is_relevant_char?
  if @book_details.args[:output] == :letter
    move_word_to_process if is_relevant_char? #todo
  else
    move_word_to_process if complete_word?
  end
end

- (Object) change_and_kill_audio



111
112
113
114
# File '/Users/martyn/cw/cw_clone/lib/cw/book.rb', line 111

def change_and_kill_audio
  change_sentence
  audio_stop
end

- (Boolean) change_or_repeat_sentence?

Returns:

  • (Boolean)


42
43
44
# File '/Users/martyn/cw/cw_clone/lib/cw/book.rb', line 42

def change_or_repeat_sentence?
  sentence.change_or_repeat?
end

- (Boolean) change_repeat_or_quit?

Returns:

  • (Boolean)


46
47
48
49
50
51
52
53
# File '/Users/martyn/cw/cw_clone/lib/cw/book.rb', line 46

def change_repeat_or_quit?
  if(change_or_repeat_sentence? || quit?)
    sentence.index += 1
    write_book_progress
    return true
  end
  false
end

- (Object) change_sentence



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

def change_sentence      ; sentence.change                   ; end

- (Boolean) change_sentence?

Returns:

  • (Boolean)


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

def change_sentence?     ; sentence.change?                  ; end

- (Object) check_sentence_count



178
179
180
181
182
183
184
185
# File '/Users/martyn/cw/cw_clone/lib/cw/book.rb', line 178

def check_sentence_count
  if @book_details.session_finished?
    audio_stop
    quit
    #      reset_stdin
    #     kill_threads
  end
end

- (Object) check_sentence_navigation(chr)



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

def check_sentence_navigation chr
  sentence.check_sentence_navigation chr
end

- (Object) compile_and_play



105
106
107
108
109
# File '/Users/martyn/cw/cw_clone/lib/cw/book.rb', line 105

def compile_and_play
  compile_sentence
  start_sync()
  audio.play
end

- (Object) compile_sentence



101
102
103
# File '/Users/martyn/cw/cw_clone/lib/cw/book.rb', line 101

def compile_sentence
  audio.convert_words(add_space(current_sentence))
end

- (Boolean) complete_word?

Returns:

  • (Boolean)


31
# File '/Users/martyn/cw/cw_clone/lib/cw/book.rb', line 31

def complete_word?       ; get_word_last_char == ' '         ; end

- (Object) convert



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

def convert
  book = @sentence.all.join
  audio.convert_book(book)
end

- (Object) current_sentence



25
# File '/Users/martyn/cw/cw_clone/lib/cw/book.rb', line 25

def current_sentence     ; sentence.current                  ; end

- (Object) current_sentence_ary



26
# File '/Users/martyn/cw/cw_clone/lib/cw/book.rb', line 26

def current_sentence_ary ; sentence.current_to_array         ; end

- (Object) find_sentences



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

def find_sentences       ; sentence.find_all                 ; end

- (Object) get_book_progress



63
64
65
66
# File '/Users/martyn/cw/cw_clone/lib/cw/book.rb', line 63

def get_book_progress
  sentence.read_progress progress_file
  @current_sentence_index = sentence_index
end

- (Object) make_sentence_index_current



155
156
157
# File '/Users/martyn/cw/cw_clone/lib/cw/book.rb', line 155

def make_sentence_index_current
  @current_sentence_index = sentence_index
end

- (Object) next_sentence



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

def next_sentence        ; sentence.next                     ; end

- (Boolean) next_sentence_or_quit?

Returns:

  • (Boolean)


116
117
118
119
120
121
122
123
124
125
# File '/Users/martyn/cw/cw_clone/lib/cw/book.rb', line 116

def next_sentence_or_quit?
  playing = audio.still_playing?
  sleep 0.01 if playing
  next_sentence unless playing
  if change_repeat_or_quit?
    change_and_kill_audio
    #todo      prn.newline unless quit?
    return true
  end
end

- (Object) play_r_tone



30
# File '/Users/martyn/cw/cw_clone/lib/cw/book.rb', line 30

def play_r_tone          ; audio_play_tone @r_tone           ; end

- (Object) play_repeat_tone



28
# File '/Users/martyn/cw/cw_clone/lib/cw/book.rb', line 28

def play_repeat_tone     ; audio_play_tone @repeat_tone      ; end

- (Object) play_sentences_thread



198
199
200
201
202
# File '/Users/martyn/cw/cw_clone/lib/cw/book.rb', line 198

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

- (Object) play_sentences_until_quit



163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File '/Users/martyn/cw/cw_clone/lib/cw/book.rb', line 163

def play_sentences_until_quit
  get_book_progress

  loop do
    check_sentence_count
    sync_with_print
    audio_play_repeat_tone_maybe
    reset_sentence_flags
    compile_and_play
    await_next_sentence_or_quit
    break if quit?
  end
  print_words_exit unless Params.print_letters
end


35
# File '/Users/martyn/cw/cw_clone/lib/cw/book.rb', line 35

def print_book_advice    ; print.print_advice('Play Book')   ; end


142
143
144
145
146
147
148
149
# File '/Users/martyn/cw/cw_clone/lib/cw/book.rb', line 142

def print_marked_maybe
  @popped = stream.pop_next_marked
  if @book_details.args[:output] == :letter
    print.char_result(@popped) if(@popped && ! print_letters?) #todo
  else
    print.results(@popped) if(@popped && ! print_letters?)
  end
end


204
205
206
207
208
# File '/Users/martyn/cw/cw_clone/lib/cw/book.rb', line 204

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


187
188
189
190
191
192
193
194
195
196
# File '/Users/martyn/cw/cw_clone/lib/cw/book.rb', line 187

def print_sentences_until_quit
  loop do
    check_sentence_count
    sync_with_play
    break if quit?
    sync_with_audio_player
    print_words_for_current_sentence
    print.reset
  end
end


151
152
153
# File '/Users/martyn/cw/cw_clone/lib/cw/book.rb', line 151

def print_words_for_current_sentence
  print_words current_sentence
end

- (Object) process_input_word_maybe



76
77
78
79
80
81
82
83
84
85
# File '/Users/martyn/cw/cw_clone/lib/cw/book.rb', line 76

def process_input_word_maybe
  if @word_to_process
    if @book_details.args[:output] == :letter
      stream.match_first_active_element @process_input_word # .strip #todo
    else
      stream.match_last_active_element @process_input_word.strip #todo
    end
    @process_input_word = @word_to_process = nil
  end
end

- (Object) process_letter(letr)



137
138
139
140
# File '/Users/martyn/cw/cw_clone/lib/cw/book.rb', line 137

def process_letter letr
  current_word.process_letter letr
  sleep_char_delay letr
end

- (Object) progress_file



59
60
61
# File '/Users/martyn/cw/cw_clone/lib/cw/book.rb', line 59

def progress_file
  File.expand_path(@progress_file, @text_folder)
end

- (Boolean) quit_or_process_input?

Returns:

  • (Boolean)


133
134
135
# File '/Users/martyn/cw/cw_clone/lib/cw/book.rb', line 133

def quit_or_process_input?
  quit? || @word_to_process
end

- (Object) raw_text



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

def raw_text             ; sentence.text                     ; end

- (Object) read_book(book)



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

def read_book(book)      ; sentence.read_book(book)          ; end

- (Boolean) repeat_sentence?

Returns:

  • (Boolean)


24
# File '/Users/martyn/cw/cw_clone/lib/cw/book.rb', line 24

def repeat_sentence?     ; sentence.repeat?                  ; end

- (Object) reset_sentence_flags



34
# File '/Users/martyn/cw/cw_clone/lib/cw/book.rb', line 34

def reset_sentence_flags ; sentence.reset_flags              ; end

- (Object) sentence



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

def sentence             ; @sentence ||= Sentence.new        ; end

- (Object) sentence_index



27
# File '/Users/martyn/cw/cw_clone/lib/cw/book.rb', line 27

def sentence_index       ; sentence.index                    ; end

- (Boolean) sentence_index_current?

Returns:

  • (Boolean)


159
160
161
# File '/Users/martyn/cw/cw_clone/lib/cw/book.rb', line 159

def sentence_index_current?
  @current_sentence_index && (@current_sentence_index == sentence_index)
end

- (Object) thread_processes



210
211
212
213
214
215
# File '/Users/martyn/cw/cw_clone/lib/cw/book.rb', line 210

def thread_processes
  [:monitor_keys_thread,
   :play_sentences_thread,
   :print_sentences_thread
  ]
end

- (Object) write_book_progress



68
69
70
# File '/Users/martyn/cw/cw_clone/lib/cw/book.rb', line 68

def write_book_progress
  sentence.write_progress progress_file
end