Permalink
Browse files

Merge branch 'fix_diff' of git://github.com/fgalassi/vimgolf

  • Loading branch information...
2 parents 064c5c0 + 6904133 commit 81f776380a19275307918f75ad258c7d431a9121 @igrigorik committed Feb 27, 2012
Showing with 75 additions and 37 deletions.
  1. +48 −26 lib/vimgolf/cli.rb
  2. +27 −11 lib/vimgolf/ui.rb
View
@@ -1,14 +1,17 @@
module VimGolf
- GOLFHOST = ENV['GOLFHOST'] || "http://vimgolf.com"
- GOLFDEBUG = ENV['GOLFDEBUG'].to_sym rescue false
- GOLFDIFF = ENV['GOFLDIFF'] || 'diff'
- GOLFVIM = ENV['GOLFVIM'] || 'vim'
- PROXY = ENV['http_proxy'] || ''
+ GOLFDEBUG = ENV['GOLFDEBUG'].to_sym rescue false
+ GOLFHOST = ENV['GOLFHOST'] || "http://vimgolf.com"
+ GOLFDIFF = ENV['GOLFDIFF'] || 'diff'
+ GOLFSHOWDIFF = ENV['GOLFSHOWDIFF'] || 'vim -d -n'
+ GOLFVIM = ENV['GOLFVIM'] || 'vim'
+ PROXY = ENV['http_proxy'] || ''
class Error
end
+ class RetryException < Exception; end
+
class UI
def debug(*); end
end
@@ -60,11 +63,11 @@ def setup
DESC
def put(id = nil)
- VimGolf.ui.warn "Launching VimGolf session for challenge: #{id}"
+ VimGolf.ui.warn "Downloading Vimgolf challenge: #{id}"
+ type = download(id)
begin
- type = download(id)
-
+ VimGolf.ui.warn "Launching VimGolf session for challenge: #{id}"
# - n - no swap file, memory only editing
# - +0 - always start on line 0
# - --noplugin - don't load any plugins, lets be fair!
@@ -75,16 +78,28 @@ def put(id = nil)
system(vimcmd)
if $?.exitstatus.zero?
- diff = `#{GOLFDIFF} \"#{input(id, type)}\" \"#{output(id)}\"`
+ diff_files = "\"#{input(id, type)}\" \"#{output(id)}\""
+ diff = `#{GOLFDIFF} #{diff_files}`
score = Keylog.score(IO.read(log(id)))
if diff.size > 0
- VimGolf.ui.warn "Uh oh, looks like your entry does not match the desired output:"
- VimGolf.ui.warn "#"*50
- puts diff
- VimGolf.ui.warn "#"*50
- VimGolf.ui.warn "Please try again! Your score for this (failed) attempt was: #{score}"
- return
+ loop do
+ VimGolf.ui.warn "Uh oh, looks like your entry does not match the desired output."
+ case VimGolf.ui.ask "Would you like to see a [d]iff or [r]etry or [q]uit ?",
+ :type => :warn,
+ :choices => [:diff, :retry, :quit]
+ when :diff
+ VimGolf.ui.warn "Showing vimdiff of your attempt (left) and correct output (right)"
+ system("#{GOLFSHOWDIFF} #{diff_files}")
+ when :retry
+ VimGolf.ui.warn "Your score for this (failed) attempt was: #{score}. Let's try again!!\n#{'#'*50}"
+ save_challenge(id)
+ raise RetryException
+ when :quit
+ VimGolf.ui.warn "Please try again! Your score for this (failed) attempt was: #{score}"
+ return
+ end
+ end
end
VimGolf.ui.info "Success! Your output matches. Your score: #{score}"
@@ -112,10 +127,13 @@ def put(id = nil)
VimGolf.ui.error error
end
- rescue Exception => e
- VimGolf.ui.error "Uh oh, something went wrong! Error: #{e}"
- VimGolf.ui.error "If the error persists, please report it to github.com/igrigorik/vimgolf"
+ rescue RetryException => e
+ retry
end
+
+ rescue Exception => e
+ VimGolf.ui.error "Uh oh, something went wrong! Error: #{e}"
+ VimGolf.ui.error "If the error persists, please report it to github.com/igrigorik/vimgolf"
end
no_tasks do
@@ -128,29 +146,33 @@ def download(id)
proxy = Net::HTTP::Proxy(proxy_url.host, proxy_url.port, proxy_user, proxy_pass)
res = proxy.start(url.host, url.port) { |http| http.request(req) }
- data = YAML.load(res.body)
+ @data = YAML.load(res.body)
- if !data.is_a? Hash
+ if !@data.is_a? Hash
raise
- elsif data['client'] != Vimgolf::VERSION
- VimGolf.ui.error "Client version mismatch. Installed: #{Vimgolf::VERSION}, Required: #{data['client']}."
+ elsif @data['client'] != Vimgolf::VERSION
+ VimGolf.ui.error "Client version mismatch. Installed: #{Vimgolf::VERSION}, Required: #{@data['client']}."
VimGolf.ui.error "\t gem install vimgolf"
raise "Bad Version"
end
- File.open(Config.put_path + "/#{id}.#{data['in']['type']}", "w") {|f| f.puts data['in']['data']}
- File.open(Config.put_path + "/#{id}.output", "w") {|f| f.puts data['out']['data']}
- File.open(Config.put_path + "/#{id}.golfrc", "w") {|f| f.puts data['vimrc']}
+ save_challenge(id)
- data['in']['type']
+ @data['in']['type']
rescue Exception => e
debug(e)
raise "Uh oh, couldn't download or parse challenge, please verify your challenge id & client version."
end
end
+ def save_challenge(id)
+ File.open(Config.put_path + "/#{id}.#{@data['in']['type']}", "w") {|f| f.puts @data['in']['data']}
+ File.open(Config.put_path + "/#{id}.output", "w") {|f| f.puts @data['out']['data']}
+ File.open(Config.put_path + "/#{id}.golfrc", "w") {|f| f.puts @data['vimrc']}
+ end
+
def upload(id)
begin
url = URI.parse("#{GOLFHOST}/entry.yaml")
View
@@ -1,7 +1,14 @@
+require 'highline'
+
module VimGolf
class CLI
class UI < Thor::Base.shell
+ def initialize
+ super
+ @hl = HighLine.new($stdin)
+ end
+
def error(name, message = nil)
begin
orig_out, $stdout = $stdout, $stderr
@@ -43,22 +50,31 @@ def debug(name, message = nil)
end
end
- def ask(message, password = false)
+ def ask(message, options)
begin
- require 'highline'
- @hl ||= HighLine.new($stdin)
- if not $stdin.tty?
- @hl.ask(message)
- elsif password
- @hl.ask(message) {|q| q.echo = "*" }
- else
- @hl.ask(message) {|q| q.readline = true }
- end
+ message = color_string(message, options[:type])
+ details = Proc.new {|q|
+ q.readline = !$stdin.tty?
+ }
+ @hl.ask(message, options[:choices], &details)
rescue EOFError
return ''
end
end
+ def color_string(str, type)
+ @hl.color(
+ str,
+ case type
+ when :info then :green
+ when :warn then :yellow
+ when :error then :red
+ when :debug then :cyan
+ else nil
+ end
+ )
+ end
+
def print_envs(apps, default_env_name = nil, simple = false)
if simple
envs = apps.map{ |a| a.environments }
@@ -113,4 +129,4 @@ def set_color(string, color, bold=false)
end
end
-end
+end

0 comments on commit 81f7763

Please sign in to comment.