Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Show difference after failed attempt in put with vimdiff and make it …

…optional
  • Loading branch information...
commit 1141f8643513b3faf428d3068e2944cc63f9d7a4 1 parent c053d18
@fgalassi fgalassi authored
Showing with 47 additions and 23 deletions.
  1. +20 −12 lib/vimgolf/cli.rb
  2. +27 −11 lib/vimgolf/ui.rb
View
32 lib/vimgolf/cli.rb
@@ -1,10 +1,11 @@
module VimGolf
- GOLFHOST = ENV['GOLFHOST'] || "http://vimgolf.com"
- GOLFDEBUG = ENV['GOLFDEBUG'].to_sym rescue false
- GOLFDIFF = ENV['GOLFDIFF'] || '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 -M'
+ GOLFVIM = ENV['GOLFVIM'] || 'vim'
+ PROXY = ENV['http_proxy'] || ''
class Error
end
@@ -75,16 +76,23 @@ 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 [q]uit ?",
+ :type => :warn,
+ :choices => [:diff, :quit]
+ when :diff
+ system("#{GOLFSHOWDIFF} #{diff_files}")
+ 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}"
View
38 lib/vimgolf/ui.rb
@@ -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
Please sign in to comment.
Something went wrong with that request. Please try again.