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.
- data/lib/keyboard_battle/command_dispatcher.rb +26 -24
- data/lib/keyboard_battle/exercise.rb +50 -48
- data/lib/keyboard_battle/keyboard.rb +43 -41
- data/lib/keyboard_battle/report.rb +16 -14
- data/lib/keyboard_battle/version.rb +1 -1
- data/lib/keyboard_battle.rb +3 -0
- metadata +1 -1
@@ -1,27 +1,28 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
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
|
-
|
13
|
+
help
|
10
14
|
end
|
11
|
-
else
|
12
|
-
help
|
13
15
|
end
|
14
|
-
end
|
15
16
|
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
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
|
-
|
24
|
-
|
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
|
-
|
37
|
+
end
|
37
38
|
|
38
|
-
|
39
|
-
|
40
|
-
|
39
|
+
def report(exercise)
|
40
|
+
KeyboardBattle::Report.new(exercise.filename, exercise.results).to_s
|
41
|
+
end
|
41
42
|
|
42
|
-
|
43
|
-
|
43
|
+
def keyboards
|
44
|
+
KeyboardBattle::Keyboard.all
|
45
|
+
end
|
44
46
|
end
|
45
47
|
end
|
@@ -1,64 +1,66 @@
|
|
1
|
-
|
1
|
+
module KeyboardBattle
|
2
|
+
class Exercise
|
2
3
|
|
3
|
-
|
4
|
+
attr_accessor :results, :filename
|
4
5
|
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
6
|
+
def initialize(filename, keyboards)
|
7
|
+
@filename = filename
|
8
|
+
@keyboards = keyboards
|
9
|
+
@results = run
|
10
|
+
end
|
10
11
|
|
11
|
-
|
12
|
+
private
|
12
13
|
|
13
|
-
|
14
|
+
attr_accessor :keyboards
|
14
15
|
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
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
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
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
|
-
|
38
|
-
|
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
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
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
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
54
|
+
def open_and_process(*args)
|
55
|
+
f = File.open(*args)
|
56
|
+
yield f
|
57
|
+
f.close()
|
58
|
+
end
|
58
59
|
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
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
|
-
|
1
|
+
module KeyboardBattle
|
2
|
+
class Keyboard
|
2
3
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
4
|
+
class Qwerty < self
|
5
|
+
def name
|
6
|
+
"qwerty"
|
7
|
+
end
|
7
8
|
|
8
|
-
|
9
|
-
|
10
|
-
|
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
|
-
|
13
|
-
|
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
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
18
|
+
class Dvorak < self
|
19
|
+
def name
|
20
|
+
"dvorak"
|
21
|
+
end
|
21
22
|
|
22
|
-
|
23
|
-
|
24
|
-
|
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
|
-
|
27
|
-
|
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
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
32
|
+
class Colemak < self
|
33
|
+
def name
|
34
|
+
"colemak"
|
35
|
+
end
|
35
36
|
|
36
|
-
|
37
|
-
|
38
|
-
|
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
|
-
|
41
|
-
|
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
|
-
|
46
|
+
attr_accessor :left, :right, :name
|
46
47
|
|
47
|
-
|
48
|
-
|
49
|
-
|
48
|
+
def combined
|
49
|
+
left + right
|
50
|
+
end
|
50
51
|
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
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
|
-
|
1
|
+
module KeyboardBattle
|
2
|
+
class Report
|
2
3
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
4
|
+
def initialize(filename, results)
|
5
|
+
@filename = filename
|
6
|
+
@results = results
|
7
|
+
end
|
7
8
|
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
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
|
data/lib/keyboard_battle.rb
CHANGED