Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

adds console runner

  • Loading branch information...
commit 60eb564b1df443b8f064f062ce467b8833f94ffb 1 parent 91928fc
@Najaf Najaf authored
View
9 Rakefile
@@ -1,6 +1,10 @@
require "bundler/gem_tasks"
+$:.push File.join(File.dirname(__FILE__))
+require 'lib/dumb-answers'
require "rake/testtask"
+include DumbAnswers
+
Rake::TestTask.new :spec do |t|
t.libs << "lib" << "spec"
t.test_files = FileList["spec/**/*_spec.rb"]
@@ -21,3 +25,8 @@ end
task default: :spec
+
+task :run, :tree do |_, args|
+ Tree.data_directory = File.dirname(__FILE__)
+ ConsoleRunner.run args[:tree].to_s
+end
View
4 dumb-answers.gemspec
@@ -19,4 +19,8 @@ Gem::Specification.new do |gem|
gem.add_development_dependency 'rake'
gem.add_development_dependency 'turn'
gem.add_development_dependency 'minitest-matchers'
+
+ gem.add_development_dependency 'colorize'
+ gem.add_development_dependency 'html2markdown'
+ gem.add_development_dependency 'ruby-terminfo'
end
View
1  lib/dumb-answers.rb
@@ -7,4 +7,5 @@ module DumbAnswers
autoload :State, 'dumb-answers/state'
autoload :Tree, 'dumb-answers/tree'
autoload :ConditionalEnding, 'dumb-answers/conditional_ending'
+ autoload :ConsoleRunner, 'dumb-answers/console_runner'
end
View
48 lib/dumb-answers/console_runner.rb
@@ -0,0 +1,48 @@
+#encoding: UTF-8
+require 'colorize'
+require 'html2markdown'
+require 'terminfo'
+module DumbAnswers
+ module ConsoleRunner
+ class << self
+ def run(name)
+ @state = Tree.begin name
+ show_current
+ end
+
+ def show_current
+ if @state.current.is_a? Question
+ puts ("" + @state.current.text).white.on_blue
+
+ if @state.current.additional_text
+ puts html_to_markdown(@state.current.additional_text)
+ end
+
+ @state.current.answers.each do |key, value|
+ puts " #{key}: #{value}"
+ end
+
+ until @state.current.answers.keys.include? (answer_code = prompt_for_answer); end
+
+ @state.answer! @state.current.id, answer_code
+
+ show_current
+ else
+
+ puts ("ENDING" + ('>' * (TermInfo.screen_size[1] - 6)) ).green
+ puts html_to_markdown(@state.current.text)
+ end
+ end
+
+ def prompt_for_answer
+ print "> "
+ $stdin.gets.to_s.chars.first
+ end
+
+ def html_to_markdown(html)
+ HTMLPage.new(contents: html).markdown
+ end
+
+ end
+ end
+end
View
5 lib/dumb-answers/state.rb
@@ -40,6 +40,11 @@ def to_hash
history: @history
}
end
+
+ def current
+ return @current unless @current.is_a? ConditionalEnding
+ @current.to_ending(@history)
+ end
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.