cw 0.2.4 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (68) hide show
  1. checksums.yaml +4 -4
  2. data/.cw_config +18 -0
  3. data/.gitignore +10 -1
  4. data/LICENSE +2 -1
  5. data/README.md +212 -96
  6. data/Rakefile +15 -2
  7. data/VERSION +1 -1
  8. data/Vagrantfile +45 -0
  9. data/cw.gemspec +6 -1
  10. data/data/text/book_to_read.txt +4 -0
  11. data/data/text/english.txt +10000 -0
  12. data/example.rb +172 -106
  13. data/lib/cw.rb +10 -126
  14. data/lib/cw/alphabet.rb +53 -46
  15. data/lib/cw/audio_player.rb +83 -72
  16. data/lib/cw/book.rb +160 -185
  17. data/lib/cw/book_details.rb +38 -49
  18. data/lib/cw/cl.rb +101 -95
  19. data/lib/cw/common_words.rb +76 -0
  20. data/lib/cw/config.rb +50 -0
  21. data/lib/cw/current_word.rb +23 -24
  22. data/lib/cw/cw_dsl.rb +264 -131
  23. data/lib/cw/cw_encoding.rb +63 -69
  24. data/lib/cw/cw_stream.rb +86 -82
  25. data/lib/cw/cw_threads.rb +132 -22
  26. data/lib/cw/element.rb +60 -54
  27. data/lib/cw/file_details.rb +26 -11
  28. data/lib/cw/key_input.rb +53 -35
  29. data/lib/cw/numbers.rb +26 -19
  30. data/lib/cw/os.rb +13 -0
  31. data/lib/cw/play.rb +92 -0
  32. data/lib/cw/print.rb +102 -100
  33. data/lib/cw/process.rb +3 -0
  34. data/lib/cw/progress.rb +20 -17
  35. data/lib/cw/randomize.rb +56 -52
  36. data/lib/cw/repeat_word.rb +59 -66
  37. data/lib/cw/reveal.rb +32 -31
  38. data/lib/cw/rss.rb +52 -48
  39. data/lib/cw/sentence.rb +83 -76
  40. data/lib/cw/speak.rb +8 -4
  41. data/lib/cw/spoken.rb +8 -4
  42. data/lib/cw/str.rb +62 -30
  43. data/lib/cw/test_letters.rb +20 -28
  44. data/lib/cw/test_words.rb +25 -31
  45. data/lib/cw/tester.rb +219 -226
  46. data/lib/cw/text_helpers.rb +19 -15
  47. data/lib/cw/timing.rb +63 -67
  48. data/lib/cw/tone_generator.rb +176 -153
  49. data/lib/cw/tone_helpers.rb +15 -23
  50. data/lib/cw/voice.rb +12 -8
  51. data/lib/cw/words.rb +136 -106
  52. data/run_script_tests.rb +165 -0
  53. data/test/my_words.txt +1 -0
  54. data/test/test_common_words.rb +71 -0
  55. data/test/test_config.rb +98 -0
  56. data/test/test_current_word.rb +62 -0
  57. data/test/test_cw.rb +87 -120
  58. data/test/test_cw_threads.rb +123 -0
  59. data/test/test_filtering.rb +439 -0
  60. data/test/test_params.rb +28 -0
  61. data/test/test_play.rb +51 -0
  62. data/test/test_stream.rb +83 -83
  63. data/test/test_tester.rb +9 -27
  64. data/test/test_timing.rb +212 -0
  65. metadata +94 -12
  66. data/lib/cw/config_file.rb +0 -69
  67. data/lib/cw/monitor_keys.rb +0 -37
  68. data/lib/cw/params.rb +0 -104
@@ -1,69 +1,58 @@
1
1
  # encoding: utf-8
2
2
 
3
- class BookDetails
3
+ module CWG
4
4
 
5
- attr_reader :args
5
+ class BookDetails
6
6
 
7
- HERE = File.dirname(__FILE__) + '/'
8
- GEM_BOOK_DIRECTORY = HERE + '../../data/text/'
9
- GEM_BOOK_NAME = 'book.txt'
10
- DEFAULT_BOOK_DIRECTORY = 'books'
7
+ attr_reader :args
11
8
 
12
- def initialize
13
- book_directory
14
- book_name
15
- end
9
+ include FileDetails
16
10
 
17
- def is_default_book_dir?
18
- if File.exist? DEFAULT_BOOK_DIRECTORY
19
- if File.directory? DEFAULT_BOOK_DIRECTORY
20
- return true
21
- end
11
+ def initialize
12
+ book_directory
13
+ book_name
22
14
  end
23
- false
24
- end
25
15
 
26
- def book_name
27
- Params.book_name ||= GEM_BOOK_NAME
28
- end
16
+ def book_name
17
+ Cfg.config["book_name"]
18
+ end
29
19
 
30
- def book_directory
31
- book_dir = GEM_BOOK_DIRECTORY
32
- if is_default_book_dir?
33
- book_dir = DEFAULT_BOOK_DIRECTORY
20
+ def book_directory
21
+ File.join ROOT, Cfg.config["book_dir"]
34
22
  end
35
- Params.book_dir ||= book_dir
36
- end
37
23
 
38
- def book_location
39
- File.expand_path(Params.book_name, Params.book_dir)
40
- end
24
+ def book_location
25
+ File.expand_path(book_name, book_directory)
26
+ end
41
27
 
42
- def arguments args
43
- @args = args
44
- @args[:output] = :letter unless @args[:output]
45
- if @args[:duration]
46
- @timeout = Time.now + @args[:duration] * 60.0
28
+ def arguments args
29
+ @args = args
30
+ @args[:output] = :letter unless @args[:output]
31
+ if @args[:duration]
32
+ @timeout = Time.now + @args[:duration] * 60.0
33
+ end
47
34
  end
48
- end
49
35
 
50
- def session_finished?
51
- sentences_complete? || book_timeout?
52
- end
36
+ def session_finished?
37
+ sentences_complete? || book_timeout?
38
+ end
53
39
 
54
- def sentences_complete?
55
- if @args.has_key?(:sentences) && @args[:sentences].is_a?(Fixnum)
56
- if @sentence_count_source
57
- @sentence_count_source = nil
58
- else
59
- @args[:sentences] -= 1
60
- @sentence_count_source = true
40
+ def sentences_complete?
41
+ if @args.has_key?(:sentences) && @args[:sentences].is_a?(Fixnum)
42
+ if @sentence_count_source
43
+ @sentence_count_source = nil
44
+ else
45
+ @args[:sentences] -= 1
46
+ @sentence_count_source = true
47
+ end
48
+ true if(@args[:sentences] < 0)
61
49
  end
62
- true if(@args[:sentences] < 0)
63
50
  end
64
- end
65
51
 
66
- def book_timeout?
67
- @timeout && (Time.now > @timeout)
52
+ def book_timeout?
53
+ @timeout && (Time.now > @timeout)
54
+ end
55
+
68
56
  end
57
+
69
58
  end
@@ -1,121 +1,127 @@
1
1
  # encoding: utf-8
2
2
 
3
- #class Cl performs command-line processing
3
+ module CWG
4
4
 
5
- class Cl
5
+ #class Cl performs command-line processing
6
6
 
7
- def initialize
8
- @tone = {
9
- sinewave: 0,
10
- sawtooth: 1,
11
- squarewave: 2
12
- }
13
- end
7
+ class Cl
14
8
 
15
- def cl_wpm
16
- wpm = Params.wpm
17
- wpm ? "-w #{wpm} " : ''
18
- end
9
+ def initialize
10
+ @tone = {
11
+ sinewave: 0,
12
+ sawtooth: 1,
13
+ squarewave: 2
14
+ }
15
+ end
19
16
 
20
- def cl_effective_wpm
21
- ewpm = Params.effective_wpm
22
- ewpm ? "-e #{ewpm} " : ''
23
- end
17
+ def cl_wpm
18
+ wpm = Cfg.config["wpm"]
19
+ wpm ? "-w #{wpm} " : ''
20
+ end
24
21
 
25
- def cl_word_spacing
26
- ws = Params.word_spacing
27
- ws ? "-W #{ws} " : ''
28
- end
22
+ def cl_effective_wpm
23
+ ewpm = Cfg.config["effective_wpm"]
24
+ ewpm ? "-e #{ewpm} " : ''
25
+ end
29
26
 
30
- def cl_frequency
31
- freq = Params.frequency
32
- freq ? "-f #{freq} " : ''
33
- end
27
+ def cl_word_spacing
28
+ ws = Cfg.config["word_spacing"]
29
+ ws ? "-W #{ws} " : ''
30
+ end
34
31
 
35
- def cl_squarewave
36
- sqr = Params.tone == :squarewave
37
- sqr ? "-T #{@tone[:squarewave]} " : ''
38
- end
32
+ def cl_frequency
33
+ freq = Cfg.config["frequency"]
34
+ freq ? "-f #{freq} " : ''
35
+ end
39
36
 
40
- def cl_sawtooth
41
- st = Params.tone == :sawtooth
42
- st ? "-T #{@tone[:sawtooth]} " : ''
43
- end
37
+ def cl_squarewave
38
+ "Cfg.config[\"tone\"] = #{Cfg.config["tone"]}"
39
+ return '' unless Cfg.config["tone"].to_s == "squarewave"
40
+ "-T #{@tone[:squarewave]} "
41
+ end
44
42
 
45
- def cl_sinewave
46
- sin = Params.tone == :sinewave
47
- sin ? "-T #{@tone[:sinewave]} " : ''
48
- end
43
+ def cl_sawtooth
44
+ "Cfg.config[\"tone\"] = #{Cfg.config["tone"]}"
45
+ return '' unless Cfg.config["tone"].to_s == "sawtooth"
46
+ "-T #{@tone[:sawtooth]} "
47
+ end
49
48
 
50
- def cl_author
51
- author = Params.author
52
- author ? "-a \"#{author}\" " : ''
53
- end
49
+ def cl_sinewave
50
+ "Cfg.config[\"tone\"] = #{Cfg.config["tone"]}"
51
+ return '' unless Cfg.config["tone"].to_s == "sinewave"
52
+ "-T #{@tone[:sinewave]} "
53
+ end
54
54
 
55
- def cl_title
56
- title = Params.title
57
- title ? "-t \"#{title}\" " : ''
58
- end
55
+ def cl_author
56
+ author = Cfg.config["author"]
57
+ author ? "-a \"#{author}\" " : ''
58
+ end
59
59
 
60
- def cl_noise
61
- noise = Params.noise
62
- noise ? "-N 5 -B 1000 " : ''
63
- end
60
+ def cl_title
61
+ title = Cfg.config["title"]
62
+ title ? "-t \"#{title}\" " : ''
63
+ end
64
64
 
65
- def cl_audio_filename
66
- "-o \"#{File.expand_path(Params.audio_filename, Params.audio_dir)}\" "
67
- end
65
+ def cl_noise
66
+ Cfg.config["noise"] ?
67
+ "-N 5 -B 1000 " : ''
68
+ end
68
69
 
69
- def coarse_quality(quality)
70
- {
71
- :high => "-q 1 ",
72
- :medium => "-q 5 ",
73
- :low => "-q 9 "
74
- }[quality]
75
- end
70
+ def cl_audio_filename
71
+ "-o \"#{File.expand_path(Cfg.config["audio_filename"], Cfg.config["audio_dir"])}\" "
72
+ end
76
73
 
77
- def cl_quality
78
- quality = Params.quality
79
- if quality && quality.class == Fixnum
80
- "-q #{quality} "
81
- else
82
- coarse_quality quality
74
+ def coarse_quality(quality)
75
+ {
76
+ :high => "-q 1 ",
77
+ :medium => "-q 5 ",
78
+ :low => "-q 9 "
79
+ }[quality]
83
80
  end
84
- end
85
81
 
86
- def cl_command_line
87
- cl = Params.command_line
88
- cl ? "#{cl}" : ''
89
- end
82
+ def cl_quality
83
+ quality = Cfg.config["quality"]
84
+ if quality && quality.class == Fixnum
85
+ "-q #{quality} "
86
+ else
87
+ coarse_quality quality
88
+ end
89
+ end
90
90
 
91
- def build_command_line
92
- [ cl_wpm,
93
- cl_effective_wpm,
94
- cl_word_spacing,
95
- cl_frequency,
96
- cl_squarewave,
97
- cl_sawtooth,
98
- cl_sinewave,
99
- cl_author,
100
- cl_title,
101
- cl_noise,
102
- cl_audio_filename,
103
- cl_quality,
104
- cl_command_line
105
- ].collect{|param| param}.join
106
- end
91
+ def cl_command_line
92
+ cl = Cfg.config["command_line"]
93
+ cl ? "#{cl}" : ''
94
+ end
107
95
 
108
- def ebook2cw_path
109
- Params.ebook2cw_path ||= 'ebook2cw'
110
- Params.ebook2cw_path
111
- end
96
+ def build_command_line
97
+ [ cl_wpm,
98
+ cl_effective_wpm,
99
+ cl_word_spacing,
100
+ cl_frequency,
101
+ cl_squarewave,
102
+ cl_sawtooth,
103
+ cl_sinewave,
104
+ cl_author,
105
+ cl_title,
106
+ cl_noise,
107
+ cl_audio_filename,
108
+ cl_quality,
109
+ cl_command_line
110
+ ].collect{|param| param}.join
111
+ end
112
112
 
113
- def cl_echo words
114
- "echo #{words} | #{ebook2cw_path} #{build_command_line}"
115
- end
113
+ def ebook2cw_path
114
+ Cfg.config["ebook2cw_path"] || 'ebook2cw'
115
+ end
116
+
117
+ def cl_echo words
118
+ "echo #{words} | #{ebook2cw_path} #{build_command_line}"
119
+ end
120
+
121
+ def cl_full input
122
+ "#{ebook2cw_path} #{build_command_line} #{input}"
123
+ end
116
124
 
117
- def cl_full input
118
- "#{ebook2cw_path} #{build_command_line} #{input}"
119
125
  end
120
126
 
121
127
  end
@@ -0,0 +1,76 @@
1
+ module CWG
2
+
3
+ class CommonWords
4
+
5
+ include FileDetails
6
+
7
+ def initialize
8
+ @words = []
9
+ end
10
+
11
+ def all
12
+ File.foreach(ENGLISH_DICT).collect do |line|
13
+ line.chomp
14
+ end
15
+ end
16
+
17
+ def low last
18
+ results = []
19
+ count = 0
20
+ File.foreach(ENGLISH_DICT).collect do |line|
21
+ if count <= last
22
+ results << line.chomp
23
+ end
24
+ count += 1
25
+ break if count > last
26
+ end
27
+ results
28
+ end
29
+
30
+ def mid first, last
31
+ results = []
32
+ count = 0
33
+ File.foreach(ENGLISH_DICT).collect do |line|
34
+ if (count >= first) && (count <= last)
35
+ results << line.chomp
36
+ end
37
+ count += 1
38
+ break if count > last
39
+ end
40
+ results
41
+ end
42
+
43
+ def parse_quantity(quantity = :default)
44
+ if quantity == :default
45
+ return [0, 999]
46
+ elsif quantity.class == Fixnum
47
+ [0, quantity - 1]
48
+ (0...quantity).collect {|q| q}
49
+ elsif quantity.class == Range
50
+ ary = quantity.to_a
51
+ return ary[0] - 1, ary[-1] -1
52
+ # ary.pop
53
+ # ary.unshift ary[0] - 1
54
+ # ary
55
+ end
56
+ end
57
+
58
+ def read(quantity = :default)
59
+ if quantity == :all
60
+ @words = all
61
+ else
62
+ qty = parse_quantity(quantity)
63
+ if qty[0] == 0
64
+ @words = low qty[-1]
65
+ else
66
+ @words = mid qty[0], qty[1]
67
+ end
68
+ end
69
+ @words
70
+ end
71
+
72
+ def to_a
73
+ @words
74
+ end
75
+ end
76
+ end
@@ -0,0 +1,50 @@
1
+ require 'parseconfig.rb'
2
+
3
+ module CWG
4
+
5
+ module Cfg
6
+
7
+ include CWG::FileDetails
8
+
9
+ CONFIG_METHODS = [
10
+ :name,:wpm,:effective_wpm,:frequency,:audio_filename,:audio_dir,
11
+ :book_name,:book_dir,:play_command,:size,:run_default,:word_spacing,
12
+ :command_line,:author,:title,:quality,:ebook2cw_path,:noise,:tone,
13
+ :word_count,:volume,:list_colour,:success_colour,:fail_colour,
14
+ :no_run,:run,:print_letters,:no_print,:use_ebook2cw,
15
+ :dictionary,:containing,:begin,:end,:including,:word_filename,:max,:min,
16
+ :exit, :quit
17
+ ]
18
+
19
+ def self.config
20
+ unless @config
21
+ @config = ParseConfig.new(CONFIG_FILENAME)
22
+ CONFIG_METHODS.each do |method|
23
+ unless @config[method.to_s]
24
+ @config.add method.to_s, nil
25
+ end
26
+ end
27
+ end
28
+ @config.params["wpm"] = 50 if(ENV["CW_ENV"] == "test")
29
+ @config.params["effective_wpm"] = 50 if(ENV["CW_ENV"] == "test")
30
+ @config
31
+ end
32
+
33
+ def self.reset
34
+ @config = nil
35
+ end
36
+
37
+ def self.reset_param param
38
+ @config.params[param] = false
39
+ end
40
+
41
+ def self.reset_if_nil param
42
+ self.reset_param param if @config[param].nil?
43
+ end
44
+
45
+ def self.get_param param
46
+ self.reset_if_nil param
47
+ @config[param]
48
+ end
49
+ end
50
+ end