keyboard_battle 0.0.2 → 0.0.3

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.
@@ -1,27 +1,28 @@
1
- class KeyboardBattle::CommandDispatcher
2
-
3
- def initialize(args)
4
- if opt = args.first
5
- case opt
6
- when '--bundled'
7
- self.run(Dir.glob('texts/*.txt'))
1
+ module KeyboardBattle
2
+ class CommandDispatcher
3
+
4
+ def initialize(args)
5
+ if opt = args.first
6
+ case opt
7
+ when '--bundled'
8
+ self.run(Dir.glob('texts/*.txt'))
9
+ else
10
+ self.run(args)
11
+ end
8
12
  else
9
- self.run(args)
13
+ help
10
14
  end
11
- else
12
- help
13
15
  end
14
- end
15
16
 
16
- def run(filenames)
17
- filenames.each do |filename|
18
- exercise = KeyboardBattle::Exercise.new(filename, keyboards)
19
- print report(exercise)
17
+ def run(filenames)
18
+ filenames.each do |filename|
19
+ exercise = KeyboardBattle::Exercise.new(filename, keyboards)
20
+ print report(exercise)
21
+ end
20
22
  end
21
- end
22
23
 
23
- def help
24
- print <<HELP
24
+ def help
25
+ print <<HELP
25
26
 
26
27
  What text(s) would you like to battle upon?
27
28
  You can specify file(s) to exercise, e.g.:
@@ -33,13 +34,14 @@ Or, use --bundled to try it out with a few bundled texts:
33
34
  $ keyboard_battle --bundled
34
35
 
35
36
  HELP
36
- end
37
+ end
37
38
 
38
- def report(exercise)
39
- KeyboardBattle::Report.new(exercise.filename, exercise.results).to_s
40
- end
39
+ def report(exercise)
40
+ KeyboardBattle::Report.new(exercise.filename, exercise.results).to_s
41
+ end
41
42
 
42
- def keyboards
43
- KeyboardBattle::Keyboard.all
43
+ def keyboards
44
+ KeyboardBattle::Keyboard.all
45
+ end
44
46
  end
45
47
  end
@@ -1,64 +1,66 @@
1
- class KeyboardBattle::Exercise
1
+ module KeyboardBattle
2
+ class Exercise
2
3
 
3
- attr_accessor :results, :filename
4
+ attr_accessor :results, :filename
4
5
 
5
- def initialize(filename, keyboards)
6
- @filename = filename
7
- @keyboards = keyboards
8
- @results = run
9
- end
6
+ def initialize(filename, keyboards)
7
+ @filename = filename
8
+ @keyboards = keyboards
9
+ @results = run
10
+ end
10
11
 
11
- private
12
+ private
12
13
 
13
- attr_accessor :keyboards
14
+ attr_accessor :keyboards
14
15
 
15
- # tests a passage of text for reach effort expended (zero for home row,
16
- # increasing for reach), and hand alternation effort. In both values,
17
- # lower is better.
18
- def run
19
- results = { }
20
- keyboards.each do |keyboard|
21
- # set up container vars
22
- prev_hand = nil
23
- alternation_effort = 0
24
- reach_effort = 0
25
- open_and_process(filename,'r') do |file|
26
- while line = file.gets
27
- line.each_char do |char|
28
- if keyboard.combined.include?(char)
16
+ # tests a passage of text for reach effort expended (zero for home row,
17
+ # increasing for reach), and hand alternation effort. In both values,
18
+ # lower is better.
19
+ def run
20
+ results = { }
21
+ keyboards.each do |keyboard|
22
+ # set up container vars
23
+ prev_hand = nil
24
+ alternation_effort = 0
25
+ reach_effort = 0
26
+ open_and_process(filename,'r') do |file|
27
+ while line = file.gets
28
+ line.each_char do |char|
29
+ if keyboard.combined.include?(char)
29
30
 
30
- # measure alternation efficiency
31
- hand = keyboard.left.include?(char) ? 'l' : 'r'
32
- if prev_hand
33
- alternation_effort += (hand == prev_hand) ? 1 : 0
34
- end
35
- prev_hand = hand
31
+ # measure alternation efficiency
32
+ hand = keyboard.left.include?(char) ? 'l' : 'r'
33
+ if prev_hand
34
+ alternation_effort += (hand == prev_hand) ? 1 : 0
35
+ end
36
+ prev_hand = hand
36
37
 
37
- # measure reach efficiency
38
- reach_effort += EFFORT[keyboard.combined.find_index(char)]
38
+ # measure reach efficiency
39
+ reach_effort += EFFORT[keyboard.combined.find_index(char)]
40
+ end
39
41
  end
40
42
  end
41
43
  end
42
- end
43
44
 
44
- results[keyboard.name.to_sym] = {
45
- :alternation_effort => alternation_effort,
46
- :reach_effort => reach_effort,
47
- :raw_score => (alternation_effort + reach_effort)
48
- }
45
+ results[keyboard.name.to_sym] = {
46
+ :alternation_effort => alternation_effort,
47
+ :reach_effort => reach_effort,
48
+ :raw_score => (alternation_effort + reach_effort)
49
+ }
50
+ end
51
+ results
49
52
  end
50
- results
51
- end
52
53
 
53
- def open_and_process(*args)
54
- f = File.open(*args)
55
- yield f
56
- f.close()
57
- end
54
+ def open_and_process(*args)
55
+ f = File.open(*args)
56
+ yield f
57
+ f.close()
58
+ end
58
59
 
59
- EFFORT = ( # left hand + right hand effort values
60
- %w(3 2 2 2 2 2 2 1 1 1 1 1 0 0 0 0 1 1 1 1 1 2 3 2 2 2 2 2 2 1 1 1 1 1 0 0 0 0 1 1 1 1 1 2) +
61
- %w(2 2 2 2 2 2 1 1 1 1 1 1 2 3 1 0 0 0 0 1 1 1 1 1 1 2 2 2 2 2 2 1 1 1 1 1 1 2 3 1 0 0 0 0 1 1 1 1 1 1)
62
- ).collect { |el| el.to_i }
60
+ EFFORT = ( # left hand + right hand effort values
61
+ %w(3 2 2 2 2 2 2 1 1 1 1 1 0 0 0 0 1 1 1 1 1 2 3 2 2 2 2 2 2 1 1 1 1 1 0 0 0 0 1 1 1 1 1 2) +
62
+ %w(2 2 2 2 2 2 1 1 1 1 1 1 2 3 1 0 0 0 0 1 1 1 1 1 1 2 2 2 2 2 2 1 1 1 1 1 1 2 3 1 0 0 0 0 1 1 1 1 1 1)
63
+ ).collect { |el| el.to_i }
63
64
 
65
+ end
64
66
  end
@@ -1,58 +1,60 @@
1
- class KeyboardBattle::Keyboard
1
+ module KeyboardBattle
2
+ class Keyboard
2
3
 
3
- class Qwerty < self
4
- def name
5
- "qwerty"
6
- end
4
+ class Qwerty < self
5
+ def name
6
+ "qwerty"
7
+ end
7
8
 
8
- def left
9
- %w(` 1 2 3 4 5 6 q w e r t a s d f g z x c v b ~ ! @ # $ % ^ Q W E R T A S D F G Z X C V B)
10
- end
9
+ def left
10
+ %w(` 1 2 3 4 5 6 q w e r t a s d f g z x c v b ~ ! @ # $ % ^ Q W E R T A S D F G Z X C V B)
11
+ end
11
12
 
12
- def right
13
- %w(7 8 9 0 - = y u i o p [ ] \\ h j k l ; ' n m , . / & * ( ) _ + Y U I O P { } | H J K L : " N M < > ?)
13
+ def right
14
+ %w(7 8 9 0 - = y u i o p [ ] \\ h j k l ; ' n m , . / & * ( ) _ + Y U I O P { } | H J K L : " N M < > ?)
15
+ end
14
16
  end
15
- end
16
17
 
17
- class Dvorak < self
18
- def name
19
- "dvorak"
20
- end
18
+ class Dvorak < self
19
+ def name
20
+ "dvorak"
21
+ end
21
22
 
22
- def left
23
- %w(` 1 2 3 4 5 6 ' , . p y a o e u i ; q j k x ~ ! @ # $ % ^ " < > P Y A O E U I : Q J K X)
24
- end
23
+ def left
24
+ %w(` 1 2 3 4 5 6 ' , . p y a o e u i ; q j k x ~ ! @ # $ % ^ " < > P Y A O E U I : Q J K X)
25
+ end
25
26
 
26
- def right
27
- %w(7 8 9 0 [ ] f g c r l / = \\ d h t n s - b m w v z & * ( ) { } F G C R L ? + | D H T N S _ B M W V Z)
27
+ def right
28
+ %w(7 8 9 0 [ ] f g c r l / = \\ d h t n s - b m w v z & * ( ) { } F G C R L ? + | D H T N S _ B M W V Z)
29
+ end
28
30
  end
29
- end
30
31
 
31
- class Colemak < self
32
- def name
33
- "colemak"
34
- end
32
+ class Colemak < self
33
+ def name
34
+ "colemak"
35
+ end
35
36
 
36
- def left
37
- %w(` 1 2 3 4 5 6 q w f p g a r s t d z x c v b ~ ! @ # $ % ^ Q W F P G A R S T D Z X C V B)
38
- end
37
+ def left
38
+ %w(` 1 2 3 4 5 6 q w f p g a r s t d z x c v b ~ ! @ # $ % ^ Q W F P G A R S T D Z X C V B)
39
+ end
39
40
 
40
- def right
41
- %w(7 8 9 0 - = j l u y : [ ] \\ h n e i o ' k m , . / & * ( ) _ + J L U Y ; { } | H N E I O " K M < > ?)
41
+ def right
42
+ %w(7 8 9 0 - = j l u y : [ ] \\ h n e i o ' k m , . / & * ( ) _ + J L U Y ; { } | H N E I O " K M < > ?)
43
+ end
42
44
  end
43
- end
44
45
 
45
- attr_accessor :left, :right, :name
46
+ attr_accessor :left, :right, :name
46
47
 
47
- def combined
48
- left + right
49
- end
48
+ def combined
49
+ left + right
50
+ end
50
51
 
51
- def self.all
52
- [
53
- KeyboardBattle::Keyboard::Qwerty.new,
54
- KeyboardBattle::Keyboard::Dvorak.new,
55
- KeyboardBattle::Keyboard::Colemak.new
56
- ]
52
+ def self.all
53
+ [
54
+ KeyboardBattle::Keyboard::Qwerty.new,
55
+ KeyboardBattle::Keyboard::Dvorak.new,
56
+ KeyboardBattle::Keyboard::Colemak.new
57
+ ]
58
+ end
57
59
  end
58
60
  end
@@ -1,20 +1,22 @@
1
- class KeyboardBattle::Report
1
+ module KeyboardBattle
2
+ class Report
2
3
 
3
- def initialize(filename, results)
4
- @filename = filename
5
- @results = results
6
- end
4
+ def initialize(filename, results)
5
+ @filename = filename
6
+ @results = results
7
+ end
7
8
 
8
- def to_s
9
- out = "\n"
10
- out << "#{@filename}:\n"
11
- @results.each do |keyboard, performance|
12
- out << " #{keyboard}:\n"
13
- performance.each do |metric, result|
14
- out << " #{metric}: #{result}\n"
9
+ def to_s
10
+ out = "\n"
11
+ out << "#{@filename}:\n"
12
+ @results.each do |keyboard, performance|
13
+ out << " #{keyboard}:\n"
14
+ performance.each do |metric, result|
15
+ out << " #{metric}: #{result}\n"
16
+ end
15
17
  end
18
+ out << "\n"
16
19
  end
17
- out << "\n"
18
- end
19
20
 
21
+ end
20
22
  end
@@ -1,3 +1,3 @@
1
1
  module KeyboardBattle
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
@@ -2,3 +2,6 @@ require "keyboard_battle/command_dispatcher"
2
2
  require "keyboard_battle/exercise"
3
3
  require "keyboard_battle/keyboard"
4
4
  require "keyboard_battle/report"
5
+
6
+ module KeyboardBattle
7
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: keyboard_battle
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: