cw 0.2.2 → 0.2.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -75,8 +75,8 @@ class Print
75
75
 
76
76
  newline_maybe value
77
77
 
78
- print Paint["#{value} ", :blue] if success
79
- print Paint["#{value} ", :red ] unless (success || type == :pass_only)
78
+ print Paint["#{value} ", success_colour] if success
79
+ print Paint["#{value} ", fail_colour ] unless (success || type == :pass_only)
80
80
  return true
81
81
  end
82
82
  end
@@ -86,8 +86,8 @@ class Print
86
86
  end
87
87
 
88
88
  def paint_success_failure(popped)
89
- print paint(popped[:value], :blue) if popped[:success]
90
- print paint(popped[:value], :red ) unless popped[:success]
89
+ print paint(popped[:value], success_colour) if popped[:success]
90
+ print paint(popped[:value], fail_colour ) unless popped[:success]
91
91
  end
92
92
 
93
93
  def char_result popped
@@ -100,39 +100,35 @@ class Print
100
100
 
101
101
  def heading
102
102
  "Current Sentence is duration: secs".length.times do
103
- print '*'
103
+ print paint('*', list_colour)
104
104
  puts
105
105
  end
106
106
  end
107
107
 
108
- def print_blue(word)
109
- print paint(word, :blue)
110
- cursor_pos = word.size
111
- (12 - cursor_pos).times{print ' '}
108
+ def fail word
109
+ print paint("#{word}", fail_colour)
112
110
  end
113
111
 
114
- def print_green(word)
115
- print_blue(word)
116
- print paint("#{word} \r\n", :green)
112
+ def list word
113
+ print paint("#{word}", list_colour)
117
114
  end
118
115
 
119
- def print_red word
120
- print paint("#{word }\r\n", :red)
116
+ def success word
117
+ newline_maybe word
118
+ return if(@print_count == 0 && word == ' ')
119
+ print paint("#{word}", success_colour)
121
120
  end
122
121
 
123
- def prn_red word
124
- print paint("#{word}", :red)
122
+ def success_colour
123
+ Params.success_colour ||= :blue
125
124
  end
126
125
 
127
- def print_fail(word, attempt)
128
- print_blue(word)
129
- print paint("#{attempt }\r\n", :red)
126
+ def fail_colour
127
+ Params.fail_colour ||= :red
130
128
  end
131
129
 
132
- def prn(word)
133
- newline_maybe word
134
- return if(@print_count == 0 && word == ' ')
135
- print paint("#{word}", :blue)
130
+ def list_colour
131
+ Params.list_colour ||= :white
136
132
  end
137
133
 
138
134
  def print_advice name
@@ -14,7 +14,7 @@ class RepeatWord < FileDetails
14
14
  def print_failed_exit_words
15
15
  until stream.stream_empty?
16
16
  word = stream.pop[:value]
17
- print.prn_red word + ' ' unless @repeat_word
17
+ print.fail word + ' ' unless @repeat_word
18
18
  end
19
19
  end
20
20
 
@@ -28,7 +28,7 @@ class RepeatWord < FileDetails
28
28
  process_word_maybe
29
29
  break if timing.char_delay_timeout?
30
30
  end
31
- print.prn letr if print_letters?
31
+ print.success letr if print_letters?
32
32
  break if quit?
33
33
  end
34
34
  end
@@ -0,0 +1,47 @@
1
+ # encoding: utf-8
2
+
3
+ class Reveal < FileDetails
4
+
5
+ include Tester
6
+
7
+ def initialize
8
+ super()
9
+ @reveal_buf = ''
10
+ puts 'Reveal mode:'
11
+ end
12
+
13
+ def print_test_advice ; print.print_advice('Test Words') ; end
14
+
15
+ def print_failed_exit_words
16
+ until stream.stream_empty?
17
+ @reveal_buf += stream.pop[:value] + ' '
18
+ end
19
+ print.success @reveal_buf
20
+ end
21
+
22
+ def process_input_word_maybe
23
+ if @word_to_process
24
+ stream.match_last_active_element @process_input_word.strip
25
+ @process_input_word = @word_to_process = nil
26
+ end
27
+ end
28
+
29
+ def build_word_maybe
30
+ @input_word ||= empty_string
31
+ @input_word << key_chr if is_relevant_char?
32
+ move_word_to_process if complete_word?
33
+ end
34
+
35
+ def process_letter letr
36
+ current_word.process_letter letr
37
+ sleep_char_delay letr
38
+ end
39
+
40
+ def print_marked_maybe
41
+ @popped = stream.pop_next_marked
42
+ if @popped
43
+ @reveal_buf += @popped[:value] + ' '
44
+ end
45
+ end
46
+
47
+ end
@@ -8,7 +8,8 @@ class Sentence
8
8
  def all ; @sentences ; end
9
9
  def next ; @next = true ; end
10
10
  def next? ; @next ; end
11
- def forward ; @index += 1 ; end
11
+ #todo def forward ; @index += 1 ; end
12
+ def forward ; @index ; end
12
13
  def previous? ; @previous ; end
13
14
  def repeat? ; @repeat ; end
14
15
  def change? ; next? || previous? ; end
@@ -25,12 +26,31 @@ class Sentence
25
26
  @index = @index <= 1 ? 0 : @index - 1
26
27
  end
27
28
 
29
+ def create_progress_maybe progress_file
30
+ unless File.exists? progress_file
31
+ reset_progress progress_file
32
+ end
33
+ end
34
+
35
+ def reset_progress progress_file
36
+ @index = 0
37
+ write_progress progress_file
38
+ end
39
+
40
+ def check_end_of_book progress_file
41
+ if @index >= @sentences.size
42
+ reset_progress progress_file
43
+ end
44
+ end
45
+
28
46
  def read_progress progress_file
47
+ create_progress_maybe progress_file
29
48
  File.open(progress_file, 'r') {|f| @index = f.readline.to_i}
49
+ check_end_of_book progress_file
30
50
  end
31
51
 
32
52
  def write_progress progress_file
33
- File.open(progress_file, 'w') {|f| f.puts @index.to_s}
53
+ File.open(progress_file, 'w') {|f| f.puts @index.to_s}
34
54
  end
35
55
 
36
56
  def read_book book
@@ -16,7 +16,7 @@ class TestWords < FileDetails
16
16
 
17
17
  def print_failed_exit_words
18
18
  until stream.stream_empty?
19
- print.prn_red stream.pop[:value] + ' '
19
+ print.fail stream.pop[:value] + ' '
20
20
  end
21
21
  end
22
22
 
@@ -65,6 +65,7 @@ module Tester
65
65
  break if timing.char_delay_timeout?
66
66
  else
67
67
  process_space_maybe(letr) if(self.class == TestWords)
68
+ process_space_maybe(letr) if(self.class == Reveal)
68
69
  process_word_maybe
69
70
  break if timing.char_delay_timeout?
70
71
  end
@@ -81,7 +82,7 @@ module Tester
81
82
  stream.add_char(letr) if(self.class == TestLetters)
82
83
  end
83
84
  process_letters letr
84
- print.prn letr if print_letters?
85
+ print.success letr if print_letters?
85
86
  break if(book_class && change_repeat_or_quit?)
86
87
  break if ((! book_class) && quit?)
87
88
  end
@@ -117,7 +118,7 @@ module Tester
117
118
 
118
119
  def print_failed_exit_words
119
120
  until stream.stream_empty?
120
- print.prn_red stream.pop[:value]
121
+ print.fail stream.pop[:value]
121
122
  end
122
123
  end
123
124
 
@@ -152,6 +153,7 @@ module Tester
152
153
  quit
153
154
  global_quit
154
155
  audio_stop
156
+ true
155
157
  end
156
158
  end
157
159
 
@@ -163,9 +165,20 @@ module Tester
163
165
  @input_word.split(//).last(1).first
164
166
  end
165
167
 
168
+ def word_proc_timeout(arg = :status)
169
+ if arg == :init
170
+ @wp_timeout = Time.now + 5
171
+ else
172
+ return true if(Time.now > @wp_timeout)
173
+ end
174
+ return false
175
+ end
176
+
166
177
  def wait_for_no_word_process
178
+ word_proc_timeout(:init)
167
179
  while @word_to_process
168
- sleep 0.1
180
+ sleep 0.01
181
+ exit(1) if word_proc_timeout
169
182
  end
170
183
  end
171
184
 
@@ -204,7 +217,7 @@ module Tester
204
217
  stream.add_word current_word.strip
205
218
  current_word.clear
206
219
  letr.clear
207
- print.prn space if print_letters?
220
+ print.success space if print_letters?
208
221
  end
209
222
  end
210
223
 
@@ -15,7 +15,7 @@ class Timing
15
15
  end
16
16
 
17
17
  def dot wpm
18
- 1.2 / wpm
18
+ 1.2 / wpm.to_f
19
19
  end
20
20
 
21
21
  def dot_ms
@@ -8,9 +8,9 @@ class ToneGenerator
8
8
 
9
9
  def initialize
10
10
  @max_amplitude = 0.5
11
- @wpm = Params.wpm
11
+ @wpm = Params.wpm.to_f
12
12
  @frequency = Params.frequency
13
- @effective_wpm = Params.effective_wpm ? Params.effective_wpm : @wpm
13
+ @effective_wpm = Params.effective_wpm ? Params.effective_wpm.to_f : @wpm
14
14
  @sample_rate = 2400
15
15
  @print = Print.new
16
16
  end
@@ -13,7 +13,6 @@ class Words
13
13
  end
14
14
 
15
15
  def double_words
16
- puts 'here'
17
16
  temp = []
18
17
  @words.each do |wrd|
19
18
  2.times { temp.push wrd }
@@ -73,6 +72,26 @@ class Words
73
72
  @words.select{|wrd| wrd.include?(letr)}
74
73
  end
75
74
 
75
+ def containing(* letters)
76
+ lets = letters.flatten.join('').split("")
77
+ found, temp, @words = false, @words, []
78
+ temp.each do |wrd|
79
+ wrd_lets = wrd.split("")
80
+ wrd_lets.each do |wrd_let|
81
+ if lets.include?(wrd_let)
82
+ found = true
83
+ else
84
+ found = false
85
+ break
86
+ end
87
+ end
88
+ if found
89
+ @words.push wrd
90
+ end
91
+ end
92
+ @words
93
+ end
94
+
76
95
  def including(* letters)
77
96
  @words = letters.flatten.collect{|letr| including_letter(letr)}.flatten
78
97
  end
@@ -80,15 +80,6 @@ class TestCW < MiniTest::Test
80
80
  assert (Time.now - time) < 1
81
81
  end
82
82
 
83
- def test_default_mp3_filename_is_mp3_output
84
- temp = nil
85
- cw = CW.new {
86
- no_run
87
- temp = Params.audio_filename
88
- }
89
- assert_equal "audio_output.wav", temp
90
- end
91
-
92
83
  def test_reload_reloads_dictionary
93
84
  cw = CW.new {
94
85
  pause
@@ -161,7 +152,7 @@ Ending: x
161
152
  end
162
153
 
163
154
  def test_wpm_defaults_to_25_if_unset
164
- assert_equal 25, @cw.wpm
155
+ assert_equal '25', @cw.wpm.to_s
165
156
  end
166
157
 
167
158
  def test_effective_wpm_defaults_to_nil
@@ -367,6 +358,12 @@ Ending: x
367
358
  assert_equal 456, @cw.frequency
368
359
  end
369
360
 
361
+ def test_play_command_returns_play_command
362
+ assert_equal 567, @cw.play_command(567)
363
+ @cw.play_command 456
364
+ assert_equal 456, @cw.play_command
365
+ end
366
+
370
367
  #todo
371
368
  # def test_play_builds_correct_command_for_default
372
369
  # @cw.words = "some words"
@@ -460,7 +457,7 @@ Ending: x
460
457
  end
461
458
 
462
459
  def test_set_default_filename
463
- assert @cw.cl.cl_audio_filename.include?('audio_output.wav'), 'default audio output filename invalid'
460
+ assert @cw.cl.cl_audio_filename.include?('audio_output'), 'default audio output filename invalid'
464
461
  end
465
462
 
466
463
  def test_set_audio_filename_to_given_name
@@ -0,0 +1,62 @@
1
+ $VERBOSE = nil #FIXME
2
+
3
+ require 'minitest/autorun'
4
+ require 'minitest/pride'
5
+ require_relative '../lib/cw'
6
+
7
+ class TestTester < MiniTest::Test
8
+
9
+ # include Tester
10
+
11
+ ROOT = File.expand_path File.dirname(__FILE__) + '/../'
12
+
13
+ def setup
14
+ @object = Object.new
15
+ @object.extend(Tester)
16
+ #''end @cw = CW.new
17
+ # @cw.pause
18
+ end
19
+
20
+ def teardown
21
+ @cw = nil
22
+ end
23
+
24
+ def test_whatever
25
+ assert true
26
+ end
27
+ #
28
+ def test_quit?
29
+ @object.instance_eval('@quit = :quit')
30
+ assert_equal :quit, @object.quit?
31
+ end
32
+
33
+ def test_quit
34
+ assert_nil @object.quit?
35
+ @object.quit
36
+ assert @object.quit?
37
+ end
38
+
39
+ def test_global_quit?
40
+ @object.instance_eval('@global_quit = :quit')
41
+ assert_equal :quit, @object.global_quit?
42
+ end
43
+
44
+ def test_global_quit
45
+ assert_nil @object.global_quit?
46
+ @object.global_quit
47
+ assert @object.global_quit?
48
+ end
49
+
50
+ def test_print_instantiates_Print_object
51
+ assert_equal Print, @object.print.class
52
+ end
53
+
54
+ def test_timing_instantiates_Timing_object
55
+ assert_equal Timing, @object.timing.class
56
+ end
57
+
58
+ def test_audio_instantiates_AudioPlayer_object
59
+ assert_equal AudioPlayer, @object.audio.class
60
+ end
61
+
62
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cw
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.2.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Martyn Jago
@@ -108,38 +108,53 @@ dependencies:
108
108
  - - ">="
109
109
  - !ruby/object:Gem::Version
110
110
  version: 0.7.0
111
+ - !ruby/object:Gem::Dependency
112
+ name: version
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: 1.0.0
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: 1.0.0
111
125
  - !ruby/object:Gem::Dependency
112
126
  name: minitest
113
127
  requirement: !ruby/object:Gem::Requirement
114
128
  requirements:
115
129
  - - ">="
116
130
  - !ruby/object:Gem::Version
117
- version: '0'
131
+ version: 5.8.4
118
132
  type: :development
119
133
  prerelease: false
120
134
  version_requirements: !ruby/object:Gem::Requirement
121
135
  requirements:
122
136
  - - ">="
123
137
  - !ruby/object:Gem::Version
124
- version: '0'
138
+ version: 5.8.4
125
139
  - !ruby/object:Gem::Dependency
126
140
  name: simplecov
127
141
  requirement: !ruby/object:Gem::Requirement
128
142
  requirements:
129
143
  - - ">="
130
144
  - !ruby/object:Gem::Version
131
- version: '0'
145
+ version: 0.12.0
132
146
  type: :development
133
147
  prerelease: false
134
148
  version_requirements: !ruby/object:Gem::Requirement
135
149
  requirements:
136
150
  - - ">="
137
151
  - !ruby/object:Gem::Version
138
- version: '0'
152
+ version: 0.12.0
139
153
  description: A ruby library to help learn and practice morse code
140
154
  email:
141
155
  - martyn.jago@btinternet.com
142
- executables: []
156
+ executables:
157
+ - cw
143
158
  extensions: []
144
159
  extra_rdoc_files: []
145
160
  files:
@@ -154,17 +169,15 @@ files:
154
169
  - LICENSE
155
170
  - README.md
156
171
  - Rakefile
172
+ - VERSION
157
173
  - audio/.gitkeep
158
- - classes.svg
159
- - classes.svg.dot
174
+ - bin/cw
160
175
  - cw.gemspec
161
- - daily.rb
162
176
  - data/text/abbreviations.txt
163
- - data/text/adv_of_sh_holmes.txt
177
+ - data/text/book.txt
164
178
  - data/text/common_words.txt
165
179
  - data/text/cw_conversation.txt
166
180
  - data/text/most_common_words.txt
167
- - data/text/progress.txt
168
181
  - data/text/q_codes.txt
169
182
  - example.rb
170
183
  - lib/cw.rb
@@ -173,10 +186,10 @@ files:
173
186
  - lib/cw/book.rb
174
187
  - lib/cw/book_details.rb
175
188
  - lib/cw/cl.rb
189
+ - lib/cw/config_file.rb
176
190
  - lib/cw/current_word.rb
177
191
  - lib/cw/cw_dsl.rb
178
192
  - lib/cw/cw_encoding.rb
179
- - lib/cw/cw_params.rb
180
193
  - lib/cw/cw_stream.rb
181
194
  - lib/cw/cw_threads.rb
182
195
  - lib/cw/element.rb
@@ -184,11 +197,13 @@ files:
184
197
  - lib/cw/key_input.rb
185
198
  - lib/cw/monitor_keys.rb
186
199
  - lib/cw/numbers.rb
200
+ - lib/cw/params.rb
187
201
  - lib/cw/print.rb
188
202
  - lib/cw/process.rb
189
203
  - lib/cw/progress.rb
190
204
  - lib/cw/randomize.rb
191
205
  - lib/cw/repeat_word.rb
206
+ - lib/cw/reveal.rb
192
207
  - lib/cw/rss.rb
193
208
  - lib/cw/sentence.rb
194
209
  - lib/cw/speak.rb
@@ -205,7 +220,8 @@ files:
205
220
  - lib/cw/words.rb
206
221
  - test/test_cw.rb
207
222
  - test/test_stream.rb
208
- homepage: http://github.com/mjago/cw
223
+ - test/test_tester.rb
224
+ homepage: http://martynjago.co.uk/CW/
209
225
  licenses:
210
226
  - MIT
211
227
  metadata: {}
@@ -220,7 +236,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
220
236
  requirements:
221
237
  - - ">="
222
238
  - !ruby/object:Gem::Version
223
- version: 1.9.3
239
+ version: 2.0.0
224
240
  required_rubygems_version: !ruby/object:Gem::Requirement
225
241
  requirements:
226
242
  - - ">="