-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.rb
82 lines (74 loc) · 1.89 KB
/
main.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
require 'optparse'
require 'bundler'
Bundler.require
ECMA_CSI = "\e[".freeze
def clear_line
print(ECMA_CSI + '0m' + TTY::Cursor.clear_line)
end
# `--norandom` option
option_param = {}
options = OptionParser.new
options.on('--norandom', 'disable randomly selected question') { option_param[:norandom] = true }
options.parse!(ARGV)
reader = TTY::Reader.new
pastel = Pastel.new
success = 0
miss = 0
all_time = 0
yml_file = ARGV[0] || 'default'
questions = YAML.load_file("questions/#{yml_file}.yml")[:questions]
CENTENCE_NUM = if !option_param[:norandom]
10
else
questions.length
end
puts 'Start Typing...!!'
questions.sample(CENTENCE_NUM).each_with_index do |question, index|
puts ''
puts ''
question = questions[index] if option_param[:norandom]
answer = ''
buffer = ''
first = true
puts question[:kanji]
print question[:kana]
while question[:kana] != answer.to_kana
buffer = reader.read_char
if first
start_at = Time.now
first = false
end
# Backspace
if buffer == "\u007F"
answer = answer.to_kana[0..-2].to_roma
miss += 1
else
answer << buffer
success += 1
end
clear_line
missed = false
question[:kana].each_char.with_index do |char, i|
if i < answer.to_kana.length
if answer.to_kana[i] == char && !missed
print pastel.on_blue(char)
else
print pastel.on_red(char)
missed = true
end
else
print char
end
end
end
all_time += Time.now - start_at
end
puts ''
puts ''
puts '**************************************'
puts " Diff : #{all_time.round(2)}s"
puts " Touch : #{success}"
puts " Miss : #{miss}"
puts " Speed : #{(success / all_time * 60).round}word/minute"
puts " Correct: #{(success / (success + miss).to_f).round(2) * 100}%"
puts '**************************************'