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,11 +1,14 @@
1
1
  # encoding: utf-8
2
2
 
3
3
  module Process
4
+
4
5
  def exist?(pid)
5
6
  Process.kill(0, pid)
6
7
  true
7
8
  rescue Errno::ESRCH
8
9
  false
9
10
  end
11
+
10
12
  module_function :exist?
13
+
11
14
  end
@@ -1,27 +1,30 @@
1
1
  # encoding: utf-8
2
2
 
3
3
  require 'ruby-progressbar'
4
- #require 'paint'
5
4
 
6
- class Progress
5
+ module CWG
7
6
 
8
- def initialize(title)
9
- @title = title
10
- end
7
+ class Progress
11
8
 
12
- def init size
13
- @progress =
14
- ProgressBar.
15
- create(total: size,
16
- title: 'Compiling',
17
- progress_mark: '.',
18
- length: 40,
19
- output: Print::ProgressPrint.new,
20
- format: "%t: |%B| %p% ")
21
- end
9
+ def initialize(title)
10
+ @title = title
11
+ end
12
+
13
+ def init size
14
+ @progress =
15
+ ProgressBar.
16
+ create(total: size,
17
+ title: 'Compiling',
18
+ progress_mark: '.',
19
+ length: 40,
20
+ output: Print::ProgressPrint.new,
21
+ format: "%t: |%B| %p% ")
22
+ end
23
+
24
+ def increment
25
+ @progress.increment
26
+ end
22
27
 
23
- def increment
24
- @progress.increment
25
28
  end
26
29
 
27
30
  end
@@ -1,73 +1,77 @@
1
1
  # encoding: utf-8
2
2
 
3
- #class Randomize provides character randomising
3
+ module CWG
4
4
 
5
- class Randomize
5
+ #class Randomize provides character randomising
6
6
 
7
- def initialize(options, chars)
8
- @options = options
9
- @chars = chars
10
- end
7
+ class Randomize
11
8
 
12
- def word_count
13
- @options[:count] ? @options[:count] : 50
14
- end
9
+ def initialize(options, chars)
10
+ @options = options
11
+ @chars = chars
12
+ end
15
13
 
16
- def size
17
- @options[:size] ? @options[:size] : 4
18
- end
14
+ def word_count
15
+ @options[:count] ? @options[:count] : 50
16
+ end
19
17
 
20
- def lengthen_chars
21
- @chars += @chars while(@chars.length < size)
22
- end
18
+ def size
19
+ @options[:size] ? @options[:size] : 4
20
+ end
23
21
 
24
- def shuffle_chars
25
- @chars.shuffle!
26
- end
22
+ def lengthen_chars
23
+ @chars += @chars while(@chars.length < size)
24
+ end
27
25
 
28
- def take_chars size
29
- @chars.take size
30
- end
26
+ def shuffle_chars
27
+ @chars.shuffle!
28
+ end
31
29
 
32
- def chars_to_alpha
33
- @chrs.collect{|char| char.chr}.join
34
- end
30
+ def take_chars size
31
+ @chars.take size
32
+ end
35
33
 
36
- def has_no_letter?
37
- @alpha[/[a-zA-Z]+/] == @alpha
38
- end
34
+ def chars_to_alpha
35
+ @chrs.collect{|char| char.chr}.join
36
+ end
39
37
 
40
- def has_no_number?
41
- @alpha[/[0-9]+/] == @alpha
42
- end
38
+ def has_no_letter?
39
+ @alpha[/[a-zA-Z]+/] == @alpha
40
+ end
43
41
 
44
- def missing_letters_or_numbers?
45
- if @options && @options[:letters_numbers]
46
- return true if has_no_letter?
47
- return true if has_no_number?
42
+ def has_no_number?
43
+ @alpha[/[0-9]+/] == @alpha
48
44
  end
49
- end
50
45
 
51
- def process_chars
52
- lengthen_chars
53
- shuffle_chars
54
- @chrs = take_chars size
55
- @alpha = chars_to_alpha
56
- end
46
+ def missing_letters_or_numbers?
47
+ if @options && @options[:letters_numbers]
48
+ return true if has_no_letter?
49
+ return true if has_no_number?
50
+ end
51
+ end
57
52
 
58
- def chars_processed?
59
- process_chars
60
- ! missing_letters_or_numbers?
61
- end
53
+ def process_chars
54
+ lengthen_chars
55
+ shuffle_chars
56
+ @chrs = take_chars size
57
+ @alpha = chars_to_alpha
58
+ end
62
59
 
63
- def generate
64
- @words, count = [], word_count
65
- while count > 0
66
- next unless chars_processed?
67
- @words.push @alpha
68
- count -= 1
60
+ def chars_processed?
61
+ process_chars
62
+ ! missing_letters_or_numbers?
69
63
  end
70
- @words
64
+
65
+ def generate
66
+ @words, count = [], word_count
67
+ while count > 0
68
+ next unless chars_processed?
69
+ @words.push @alpha
70
+ count -= 1
71
+ end
72
+ @words
73
+ end
74
+
71
75
  end
72
76
 
73
77
  end
@@ -1,87 +1,80 @@
1
1
  # encoding: utf-8
2
2
 
3
- class RepeatWord < FileDetails
3
+ module CWG
4
4
 
5
- include Tester
5
+ class RepeatWord < Tester
6
6
 
7
- def initialize
8
- super
9
- @repeat_word = true
10
- end
7
+ #overloaded #todo
11
8
 
12
- #overloaded #todo
9
+ # def print_failed_exit_words
10
+ # until stream.stream_empty?
11
+ # word = stream.pop[:value]
12
+ # end
13
+ # puts "empty!"
14
+ # end
13
15
 
14
- def print_failed_exit_words
15
- until stream.stream_empty?
16
- word = stream.pop[:value]
17
- print.fail word + ' ' unless @repeat_word
16
+ def print_words words
17
+ timing.init_char_timer
18
+ (words.to_s + ' ').each_char do |letr|
19
+ process_letter letr
20
+ loop do
21
+ do_events
22
+ process_space_maybe letr
23
+ process_word_maybe
24
+ break if timing.char_delay_timeout?
25
+ end
26
+ print.success letr if print_letters?
27
+ end
18
28
  end
19
- end
20
29
 
21
- def print_words words
22
- timing.init_char_timer
23
- (words.to_s + space).each_char do |letr|
24
- process_letter letr
25
- loop do
26
- do_events
27
- process_space_maybe letr
28
- process_word_maybe
29
- break if timing.char_delay_timeout?
30
+ def process_input_word_maybe
31
+ # puts "process_input_word_maybe"
32
+ if @word_to_process
33
+ stream.match_last_active_element @process_input_word.strip
34
+ @process_input_word = @word_to_process = nil
30
35
  end
31
- print.success letr if print_letters?
32
- break if quit?
33
36
  end
34
- end
35
37
 
36
- def process_input_word_maybe
37
- if @word_to_process
38
- stream.match_last_active_element @process_input_word.strip
39
- @process_input_word = @word_to_process = nil
38
+ def build_word_maybe
39
+ @input_word ||= ''
40
+ @input_word << key_chr if is_relevant_char?
41
+ move_word_to_process if complete_word?
40
42
  end
41
- end
42
-
43
- def build_word_maybe
44
- @input_word ||= empty_string
45
- @input_word << key_chr if is_relevant_char?
46
- move_word_to_process if complete_word?
47
- end
48
43
 
49
- def process_letter letr
50
- current_word.process_letter letr
51
- sleep_char_delay letr
52
- end
53
-
54
- def print_marked_maybe
55
- @popped = stream.pop_next_marked
56
- print.results(@popped, :pass_only) if(@popped && ! print_letters?)
57
- end
44
+ def process_letter letr
45
+ current_word.process_letter letr
46
+ sleep_char_delay letr
47
+ end
58
48
 
59
- #overloaded #todo
49
+ def print_marked_maybe
50
+ @popped = stream.pop_next_marked
51
+ print.results(@popped, :pass_only) if(@popped && ! print_letters?)
52
+ end
60
53
 
61
- def double_words words
62
- temp = []
63
- words.each do |wrd|
64
- 2.times { temp.push wrd }
54
+ def test_env
55
+ if(ENV["CW_ENV"] == "test")
56
+ @words = []
57
+ return true
58
+ end
65
59
  end
66
- temp
67
- end
68
60
 
69
- def run words
70
- temp_words = words.all
71
- temp_words = double_words temp_words if Params.double_words
72
- temp_words.each do |word|
73
- loop do
74
- @input_word, @words = '', Words.new
75
- @quit, @failed = nil, nil
76
- @words.add [word]
77
- @threads = CWThreads.new(self, thread_processes)
78
- @threads.run
79
- break unless @failed
80
- break if global_quit?
61
+ def run words
62
+ temp_words = words.all
63
+ temp_words.each do |word|
64
+ loop do
65
+ break if test_env
66
+ @input_word, @words = '', Words.new
67
+ Cfg.config.params["quit"] = false
68
+ @words.assign word
69
+ @threads = CWThreads.new(self, thread_processes)
70
+ @threads.start_threads
71
+ @threads.wait_for_threads
72
+ @play = nil
73
+ system("stty -raw echo")
74
+ break unless failed?
75
+ end
81
76
  end
82
- break if global_quit?
77
+ # break if Cfg.config["exit"]
83
78
  end
84
- reset_stdin
85
- print.newline
86
79
  end
87
80
  end
@@ -1,47 +1,48 @@
1
1
  # encoding: utf-8
2
2
 
3
- class Reveal < FileDetails
3
+ module CWG
4
4
 
5
- include Tester
5
+ class Reveal < Tester
6
6
 
7
- def initialize
8
- super()
9
- @reveal_buf = ''
10
- puts 'Reveal mode:'
11
- end
7
+ def initialize
8
+ @reveal_buf = ''
9
+ puts 'Reveal mode:'
10
+ end
12
11
 
13
- def print_test_advice ; print.print_advice('Test Words') ; end
12
+ def print_test_advice ; print.print_advice('Test Words') ; end
14
13
 
15
- def print_failed_exit_words
16
- until stream.stream_empty?
17
- @reveal_buf += stream.pop[:value] + ' '
14
+ def print_failed_exit_words
15
+ until stream.stream_empty?
16
+ @reveal_buf += stream.pop[:value] + ' '
17
+ end
18
+ print.success @reveal_buf
18
19
  end
19
- print.success @reveal_buf
20
- end
21
20
 
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
21
+ def process_input_word_maybe
22
+ if @word_to_process
23
+ stream.match_last_active_element @process_input_word.strip
24
+ @process_input_word = @word_to_process = nil
25
+ end
26
26
  end
27
- end
28
27
 
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
28
+ def build_word_maybe
29
+ @input_word ||= ''
30
+ @input_word << key_chr if is_relevant_char?
31
+ move_word_to_process if complete_word?
32
+ end
34
33
 
35
- def process_letter letr
36
- current_word.process_letter letr
37
- sleep_char_delay letr
38
- end
34
+ def process_letter letr
35
+ current_word.process_letter letr
36
+ sleep_char_delay letr
37
+ end
39
38
 
40
- def print_marked_maybe
41
- @popped = stream.pop_next_marked
42
- if @popped
43
- @reveal_buf += @popped[:value] + ' '
39
+ def print_marked_maybe
40
+ @popped = stream.pop_next_marked
41
+ if @popped
42
+ @reveal_buf += @popped[:value] + ' '
43
+ end
44
44
  end
45
+
45
46
  end
46
47
 
47
48
  end
@@ -1,62 +1,66 @@
1
1
  # encoding: utf-8
2
2
 
3
- #class Rss
3
+ module CWG
4
4
 
5
- class Rss
5
+ #class Rss
6
6
 
7
- include TextHelpers
7
+ class Rss
8
8
 
9
- def sources
10
- {
11
- bbc: 'http://feeds.bbci.co.uk/news/rss.xml',
12
- reuters: 'http://feeds.reuters.com/Reuters/worldNews?format=xml',
13
- guardian: 'http://www.theguardian.com/world/rss',
14
- quotation: 'http://feeds.feedburner.com/quotationspage/qotd'
15
- }
16
- end
9
+ include TextHelpers
17
10
 
18
- def source src
19
- sources.has_key?(src) ? sources[src] : sources[:quotation]
20
- end
11
+ def sources
12
+ {
13
+ bbc: 'http://feeds.bbci.co.uk/news/rss.xml',
14
+ reuters: 'http://feeds.reuters.com/Reuters/worldNews?format=xml',
15
+ guardian: 'http://www.theguardian.com/world/rss',
16
+ quotation: 'http://feeds.feedburner.com/quotationspage/qotd'
17
+ }
18
+ end
21
19
 
22
- def read_rss(src, article_count = 3)
23
- require 'feedjira'
24
- require "htmlentities"
25
- require 'sanitize'
26
- coder = HTMLEntities.new
27
- url = source(src)
28
- # return a Hash - each url having a Feedjira::Feed object
29
- feed = Feedjira::Feed.fetch_and_parse url
30
- entry_count = 0
31
- @rss_articles = []
32
- feed.entries.each do |entry|
33
- title = entry.title
34
- unless(title.include?('VIDEO:') ||
35
- title.include?('In pictures:') ||
36
- title.include?('Morning business round-up'))
37
- words = entry.summary
38
- entry_count += 1
20
+ def source src
21
+ sources.has_key?(src) ? sources[src] : sources[:quotation]
22
+ end
23
+
24
+ def read_rss(src, article_count = 3)
25
+ require 'feedjira'
26
+ require "htmlentities"
27
+ require 'sanitize'
28
+ coder = HTMLEntities.new
29
+ url = source(src)
30
+ # return a Hash - each url having a Feedjira::Feed object
31
+ feed = Feedjira::Feed.fetch_and_parse url
32
+ entry_count = 0
33
+ @rss_articles = []
34
+ feed.entries.each do |entry|
35
+ title = entry.title
36
+ unless(title.include?('VIDEO:') ||
37
+ title.include?('In pictures:') ||
38
+ title.include?('Morning business round-up'))
39
+ words = entry.summary
40
+ entry_count += 1
41
+ end
42
+ @rss_articles << (Sanitize.clean coder.decode words).split(',')
43
+ break if entry_count >= article_count
39
44
  end
40
- @rss_articles << (Sanitize.clean coder.decode words).split(',')
41
- break if entry_count >= article_count
42
45
  end
43
- @rss_flag = true
44
- end
45
46
 
46
- def inc_article_index
47
- @article_index += 1
48
- end
47
+ def inc_article_index
48
+ @article_index += 1
49
+ end
49
50
 
50
- def article_index
51
- @article_index || @article_index = 0
52
- end
51
+ def article_index
52
+ @article_index || @article_index = 0
53
+ end
54
+
55
+ def next_article
56
+ temp = @rss_articles[article_index]
57
+ return unless temp
58
+ inc_article_index
59
+ quote = ''
60
+ temp.map { |i| quote += i }
61
+ (quote.split.collect { |article| cw_chars(article.strip.delete("\"").downcase)})
62
+ end
53
63
 
54
- def next_article
55
- temp = @rss_articles[article_index]
56
- return unless temp
57
- inc_article_index
58
- quote = ''
59
- temp.map { |i| quote += i }
60
- (quote.split.collect { |article| cw_chars(article.strip.delete("\"").downcase)})
61
64
  end
65
+
62
66
  end