Pickaxe 0.6.0 → 0.6.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -3,11 +3,22 @@ require "bundler/setup"
3
3
  require "active_support/all"
4
4
  require "rbconfig"
5
5
  require "unidecoder"
6
+ begin
7
+ require 'readline'
8
+ rescue LoadError
9
+ class Readline
10
+ def self.completion_proc=(value); end
11
+ def self.readline(prompt)
12
+ print prompt
13
+ $stdin.gets
14
+ end
15
+ end
16
+ end
6
17
 
7
18
  $LOAD_PATH.unshift(File.expand_path(File.dirname(__FILE__)))
8
19
 
9
20
  module Pickaxe
10
- VERSION = "0.6.0"
21
+ VERSION = "0.6.1"
11
22
 
12
23
  class PickaxeError < StandardError; end
13
24
 
@@ -43,6 +43,10 @@ END_OF_BANNER
43
43
  options[:syntax_check] = true
44
44
  end
45
45
 
46
+ opts.on("--no-clear", "Turn off shell clearing") do |v|
47
+ options[:no_clear] = true
48
+ end
49
+
46
50
  opts.on("--no-color", "Turn off colors") do |v|
47
51
  options[:no_colors] = true
48
52
  end
@@ -68,8 +72,11 @@ begin
68
72
  ! Hi there Windows user.
69
73
 
70
74
  You will not be able to see colors, all diacritics
71
- will be transliterated and dynamic console width
72
- is not available. Sorry for the inconvenience.
75
+ will be transliterated, dynamic console width
76
+ is not available, You cannot answer using the [TAB]
77
+ and the console will not be cleared on new question.
78
+
79
+ Sorry for the inconvenience.
73
80
  END_OF_MESSAGE
74
81
 
75
82
  end
@@ -1,6 +1,7 @@
1
1
  module Pickaxe
2
2
  class Main
3
3
  class NoTests < PickaxeError; end
4
+ class TabTermination < PickaxeError; end
4
5
 
5
6
  cattr_accessor :options
6
7
 
@@ -40,18 +41,26 @@ END_OF_TEST
40
41
  puts "! Hit Control-D or Control-C to end test.\n\n".color(:green)
41
42
  end
42
43
 
44
+ Readline.completion_proc = Proc.new do |line|
45
+ @line = line
46
+ raise TabTermination
47
+ end
48
+
43
49
  while @current_index < @questions.length + (Main.options[:full_test] ? 1 : 0) do
44
50
  @question = @questions[@current_index]
45
51
 
46
52
  unless @question.nil?
53
+ Shell.clear
54
+
55
+ puts "#{@last_answer}\n\n"
47
56
  print "#{@current_index+1} / #{@questions.length}\t\t"
48
- puts "From: #{@question.file}\t\tTime spent: #{spent?}"
57
+ puts "From: #{@question.file}\t\tTime spent: #{spent?}\n\n"
49
58
 
50
59
  puts @question.answered(@answers[@question])
51
60
  else
52
61
  puts END_OF_TEST_MESSAGE
53
62
  end
54
-
63
+
55
64
  until (line = prompt?).nil? or command(line)
56
65
  # empty
57
66
  end
@@ -70,13 +79,18 @@ END_OF_TEST
70
79
  # <+ moves back one question
71
80
  # >+ moves forward one question
72
81
  # a [ b ...] answers the question
82
+ # . shows current question again
73
83
  # ? shows help
74
84
  #
75
85
  def command(line)
86
+ @last_answer = nil
87
+
76
88
  case line
77
89
  when /^\s*@\s*(.+)/ then # @ question
78
90
  @current_index = Integer($1) -1
79
91
  true
92
+ when /\./ then
93
+ true
80
94
  when /<+/ then
81
95
  if @current_index > 0
82
96
  @current_index -= 1
@@ -110,18 +124,25 @@ Available commands (whitespace does not matter):
110
124
  < moves back one question
111
125
  > moves forward one question
112
126
  a [ b ...] answers the question
127
+ . shows current question again
113
128
  ? shows help
114
129
 
115
130
  END_OF_HELP
116
131
  false
117
132
  else
118
- @answers[@question] = line.gsub(/\s+/, "").each_char.collect.to_a.uniq
133
+ @answers[@question] = convert_answers(line)
119
134
  unless Main.options[:full_test]
120
- puts @question.check?(@answers[@question])
135
+ @last_answer = @question.check?(@answers[@question])
136
+
137
+ if @current_index == (@questions.length-1)
138
+ puts @last_answer
139
+ end
140
+
121
141
  if Main.options[:repeat_incorrect] and not @question.correct?(@answers[@question])
122
142
  @answers.delete(@question)
123
143
  @questions.insert(@current_index + 1 + rand(@questions.length - @current_index), @question)
124
- @questions.delete_at(@current_index)
144
+ @questions.delete_at(@current_index)
145
+ @question.reset!
125
146
  else
126
147
  @current_index += 1
127
148
  end
@@ -132,6 +153,17 @@ END_OF_HELP
132
153
  end
133
154
  end
134
155
 
156
+ ANSWER_CONVERTION_HASH = ('a'..'z').to_a[0,10].each_with_index.inject({}) do |m, p|
157
+ m[((p.last + 1) % 10).to_s] = p.first
158
+ m
159
+ end
160
+
161
+ def convert_answers(line)
162
+ line.gsub(/\s+/, "").downcase.each_char.collect do |c|
163
+ ANSWER_CONVERTION_HASH[c] || c
164
+ end.to_a.uniq
165
+ end
166
+
135
167
  def statistics!
136
168
  @questions_length ||= @questions.length.to_f
137
169
 
@@ -161,8 +193,16 @@ END_OF_HELP
161
193
  end
162
194
 
163
195
  def prompt?(p = "? ")
164
- print p
165
- $stdin.gets
196
+ unless Pickaxe::WINDOWS_IT_IS
197
+ begin
198
+ Readline.readline(p)
199
+ rescue TabTermination
200
+ puts
201
+ @line + "\n"
202
+ end else
203
+ print p
204
+ $stdin.gets
205
+ end
166
206
  end
167
207
 
168
208
  def spent?
@@ -11,6 +11,13 @@ module Pickaxe
11
11
  (dynamic_width_stty.nonzero? || dynamic_width_tput)
12
12
  end
13
13
  end
14
+
15
+ def self.clear
16
+ unless Pickaxe::WINDOWS_IT_IS or Main.options[:no_clear]
17
+ print "\e[H\e[2J"
18
+ end
19
+ end
20
+
14
21
  private
15
22
  def self.dynamic_width_stty
16
23
  %x{stty size 2>/dev/null}.split[1].to_i
@@ -134,6 +134,10 @@ module Pickaxe
134
134
  end
135
135
  end
136
136
 
137
+ def reset!
138
+ @shuffled_answers = nil
139
+ end
140
+
137
141
  def shuffled_answers
138
142
  if @shuffled_answers.nil?
139
143
  unless Main.options[:sorted_answers]
@@ -184,10 +188,19 @@ module Pickaxe
184
188
  if correct?(given)
185
189
  "Correct!".color(:green)
186
190
  else
187
- hit = "#{(given & correct_answers).join}".color(:green)
188
- missed = "#{(correct_answers - given).join}".color(:yellow)
189
- incorrect = "#{(given - correct_answers).join}".color(:red)
190
- "Incorrect, should be: #{correct_answers.join}! #{hit} #{missed} #{incorrect}"
191
+ missed = (correct_answers - given)
192
+ missed = unless missed.empty?
193
+ "Missed: #{missed.join}".color(:yellow)
194
+ else
195
+ ""
196
+ end
197
+ incorrect = (given - correct_answers)
198
+ incorrect = unless incorrect.empty?
199
+ "Wrong: #{incorrect.join}".color(:red)
200
+ else
201
+ ""
202
+ end
203
+ "Incorrect, should be:".color(:red) + " #{correct_answers.join}! #{[missed, incorrect].join(" ")}"
191
204
  end
192
205
  end
193
206
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: Pickaxe
3
3
  version: !ruby/object:Gem::Version
4
- hash: 7
4
+ hash: 5
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 6
9
- - 0
10
- version: 0.6.0
9
+ - 1
10
+ version: 0.6.1
11
11
  platform: ruby
12
12
  authors:
13
13
  - Dawid Fatyga
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-12-07 00:00:00 +01:00
18
+ date: 2010-12-12 00:00:00 +01:00
19
19
  default_executable: bin/pickaxe
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency