Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fixed #8.

  • Loading branch information...
commit 0b5f89f01575f481323936e2d89b6bd15b6fd938 1 parent 70465a1
@omo authored
Showing with 62 additions and 39 deletions.
  1. +61 −33 lib/ankit/challenge.rb
  2. +1 −6 test/command_test.rb
View
94 lib/ankit/challenge.rb
@@ -25,8 +25,6 @@ def self.styled_text(text, type)
HighLine.color(text, HighLine::GREEN_STYLE)
when :wrong
HighLine.color(text, HighLine::RED_STYLE)
- when :wrong
- HighLine.color(text, HighLine::RED_STYLE)
when :fyi
HighLine.color(text, HighLine::DARK)
else
@@ -77,6 +75,21 @@ def hilight_against_original(wrong)
end
end
end
+
+ def mixed_hilight_for_flash(wrong)
+ diff_from_original(wrong) do |ch|
+ case ch.action
+ when "="
+ StylableText.styled_text(ch.old_element, :fyi)
+ when "!", "-"
+ StylableText.styled_text(ch.old_element, :wrong) + StylableText.styled_text(ch.new_element, :correct)
+ when "+"
+ StylableText.styled_text(ch.new_element, :correct)
+ else
+ raise
+ end
+ end
+ end
end
module Challenge
@@ -126,19 +139,17 @@ def initialize(session, slots)
@this_round = latest_round
end
- def current_card
- # XXX: might be better to cache
- Card.parse(open(current_path, "r") { |f| f.read })
- end
-
def round_delta
latest_round - this_round
end
def runtime; @session.runtime; end
def last_slot; @slots[@index-1]; end
+ def last_path; last_slot.path; end
+ def last_card; card_at(last_path); end
def current_slot; @slots[@index]; end
def current_path; current_slot.path; end
+ def current_card; card_at(current_path); end
def size; @slots.size; end
def over?; @slots.size <= @index; end
def npassed; @slots.count { |c| c.rating == :passed }; end
@@ -197,6 +208,13 @@ def styled_indicator
end
def maturities; slots.map(&:maturity); end
+
+
+ private
+ def card_at(path)
+ # XXX: might be better to cache
+ Card.parse(open(path, "r") { |f| f.read })
+ end
end
class State
@@ -234,16 +252,6 @@ def show_breaking_status
show_summary_header
end
- def show_header
- show_summary_header
- if last_answer
- line.say(StylableText.styled_text("last: #{last_answer}", :fyi))
- else
- line.say("\n")
- end
- line.say("\n")
- end
-
def show_and_ask_enter(msg, type)
line.ask(message_for(msg, type) + " ") { |q| q.readline = true }
end
@@ -332,6 +340,7 @@ def pump
class QuestionState < State
include SlashRecognizing
+ attr_reader :flash
def pump
progress.attack
@@ -354,9 +363,26 @@ def pump
end
end
end
+
+ def put_flash(flash)
+ @flash = flash
+ self
+ end
+
+ private
+
+ def show_header
+ show_summary_header
+ if flash
+ line.say(flash)
+ else
+ line.say("\n")
+ end
+ line.say("\n")
+ end
end
- class FailedStateBase < State
+ class FailedState < State
include SlashRecognizing
def pump
@@ -365,33 +391,35 @@ def pump
typed = "\n" if typed.empty?
erase_last
say("#{typed}", :ask)
- say("#{original}", decoration_type)
+ say("#{original}", :fail)
answered = ask("", :hit_return)
pump_slash_or(answered) do
- mark_progress
+ progress.fail
QuestionState.new(progress)
end
end
-
- end
-
- class FailedState < FailedStateBase
- def mark_progress; progress.fail; end
- def decoration_type; :fail; end
- end
-
- class TypoState < FailedStateBase
- def mark_progress; end
- def decoration_type; :typo; end
end
- class PassedState < State
+ class PassedStateBase < State
include SlashRecognizing
def pump
progress.pass
last_maturity = progress.last_slot.event.maturity
- progress.over? ? RefillState.new(progress) : QuestionState.new(progress, last_answer)
+ progress.over? ? RefillState.new(progress) : QuestionState.new(progress).put_flash(flash)
+ end
+ end
+
+ class TypoState < PassedStateBase
+ def flash
+ hilited = progress.last_card.mixed_hilight_for_flash(last_answer)
+ StylableText.styled_text("last: ", :fyi) + hilited
+ end
+ end
+
+ class PassedState < PassedStateBase
+ def flash
+ StylableText.styled_text("last: #{last_answer}", :fyi)
end
end
View
7 test/command_test.rb
@@ -348,13 +348,8 @@ def test_question_to_typo
actual = ChallengeCommand.new(runtime).initial_state
actual_next = enter_text_pump(actual, FIRST_TYPO_ANSWER)
assert_instance_of(Challenge::TypoState, actual_next)
- actual_next = hit_return_pump(actual_next)
+ actual_next = actual_next.pump
assert_instance_of(Challenge::QuestionState, actual_next)
- assert_equal(actual_next.progress.npassed, 0)
- assert_equal(actual_next.progress.nfailed, 0)
- actual_next = enter_text_pump(actual_next, FIRST_CORRECT_ANSWER)
- assert_instance_of(Challenge::PassedState, actual_next)
- actual_next.pump
assert_equal(actual_next.progress.npassed, 1)
assert_equal(actual_next.progress.nfailed, 0)
end
Please sign in to comment.
Something went wrong with that request. Please try again.