cheat 1.2 → 1.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/cheat.rb +4 -4
- data/lib/cheat/commands.rb +41 -0
- data/lib/new_cheat.rb +163 -0
- metadata +5 -2
data/lib/cheat.rb
CHANGED
@@ -18,13 +18,13 @@ module Cheat
|
|
18
18
|
uri = "http://#{cheat_uri}/y/"
|
19
19
|
|
20
20
|
if %w[sheets all recent].include? @sheet
|
21
|
-
uri = uri.sub('/y/', sheet == 'recent' ? '/yr/' : '/ya/')
|
21
|
+
uri = uri.sub('/y/', @sheet == 'recent' ? '/yr/' : '/ya/')
|
22
22
|
return open(uri) { |body| show(body.read) }
|
23
23
|
end
|
24
24
|
|
25
25
|
return show(File.read(cache_file)) if File.exists?(cache_file) rescue clear_cache if cache_file
|
26
26
|
|
27
|
-
fetch_sheet(uri
|
27
|
+
fetch_sheet(uri + @sheet) if @sheet
|
28
28
|
end
|
29
29
|
|
30
30
|
def fetch_sheet(uri, try_to_cache = true)
|
@@ -62,7 +62,7 @@ module Cheat
|
|
62
62
|
|
63
63
|
# $ cheat greader --versions
|
64
64
|
def show_versions(sheet)
|
65
|
-
fetch_sheet("http://#{cheat_uri}/h/#{sheet}/")
|
65
|
+
fetch_sheet("http://#{cheat_uri}/h/#{sheet}/", false)
|
66
66
|
end
|
67
67
|
|
68
68
|
# $ cheat greader --diff 1[:3]
|
@@ -94,7 +94,7 @@ module Cheat
|
|
94
94
|
puts sheet.first + ':'
|
95
95
|
puts ' ' + sheet.last.gsub("\r",'').gsub("\n", "\n ").wrap
|
96
96
|
rescue
|
97
|
-
puts "That didn't work.
|
97
|
+
puts "That didn't work. Maybe try `$ cheat cheat' for help?"
|
98
98
|
end
|
99
99
|
|
100
100
|
def edit(sheet_yaml)
|
@@ -0,0 +1,41 @@
|
|
1
|
+
module Cheat
|
2
|
+
class Commands
|
3
|
+
def initialize(args)
|
4
|
+
@sheet = args.shift unless args.first =~ /^--/
|
5
|
+
parse_args(args)
|
6
|
+
end
|
7
|
+
|
8
|
+
private
|
9
|
+
def parse_args(args)
|
10
|
+
args.each_with_index do |arg, i|
|
11
|
+
if arg =~ /^--(.+)$/ && respond_to?(meth = $1)
|
12
|
+
if method(meth).arity.zero? || args[i+1] =~ /^--/
|
13
|
+
send(meth)
|
14
|
+
else
|
15
|
+
send(meth, args[i+1])
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
public
|
22
|
+
def help
|
23
|
+
puts self.class.public_instance_methods(false)
|
24
|
+
end
|
25
|
+
|
26
|
+
def diff
|
27
|
+
end
|
28
|
+
|
29
|
+
def add
|
30
|
+
end
|
31
|
+
|
32
|
+
def new
|
33
|
+
end
|
34
|
+
|
35
|
+
def clear_cache
|
36
|
+
end
|
37
|
+
|
38
|
+
def edit
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
data/lib/new_cheat.rb
ADDED
@@ -0,0 +1,163 @@
|
|
1
|
+
$:.unshift File.dirname(__FILE__)
|
2
|
+
%w[rubygems tempfile fileutils net/http yaml open-uri wrap].each { |f| require f }
|
3
|
+
%w[cheat/commands].each { |f| require f }
|
4
|
+
|
5
|
+
module Cheat
|
6
|
+
extend self
|
7
|
+
|
8
|
+
HOST = ARGV.include?('debug') ? 'localhost' : 'cheat.errtheblog.com'
|
9
|
+
PORT = ARGV.include?('debug') ? 3001 : 80
|
10
|
+
SUFFIX = ''
|
11
|
+
|
12
|
+
def sheets(args)
|
13
|
+
Commands.new(args)
|
14
|
+
|
15
|
+
return
|
16
|
+
return unless parse_args(args)
|
17
|
+
|
18
|
+
FileUtils.mkdir(cache_dir) unless File.exists?(cache_dir) if cache_dir
|
19
|
+
|
20
|
+
uri = "http://#{cheat_uri}/y/"
|
21
|
+
|
22
|
+
if %w[sheets all recent].include? @sheet
|
23
|
+
uri = uri.sub('/y/', sheet == 'recent' ? '/yr/' : '/ya/')
|
24
|
+
return open(uri) { |body| show(body.read) }
|
25
|
+
end
|
26
|
+
|
27
|
+
return show(File.read(cache_file)) if File.exists?(cache_file) rescue clear_cache if cache_file
|
28
|
+
|
29
|
+
fetch_sheet(uri, @sheet) if @sheet
|
30
|
+
end
|
31
|
+
|
32
|
+
def fetch_sheet(uri, try_to_cache = true)
|
33
|
+
open(uri, headers) do |body|
|
34
|
+
sheet = body.read
|
35
|
+
File.open(cache_file, 'w') { |f| f.write(sheet) } if try_to_cache && cache_file && !@edit
|
36
|
+
@edit ? edit(sheet) : show(sheet)
|
37
|
+
end
|
38
|
+
rescue OpenURI::HTTPError => e
|
39
|
+
puts "Whoa, some kind of Internets error!", "=> #{e} from #{uri}"
|
40
|
+
end
|
41
|
+
|
42
|
+
def parse_args(args)
|
43
|
+
puts "Looking for help? Try http://cheat.errtheblog.com or `$ cheat cheat'" and return if args.empty?
|
44
|
+
|
45
|
+
if args.delete('--clear-cache') || args.delete('--new')
|
46
|
+
clear_cache
|
47
|
+
return if args.empty?
|
48
|
+
end
|
49
|
+
|
50
|
+
if i = args.index('--diff')
|
51
|
+
diff_sheets(args.first, args[i+1])
|
52
|
+
return
|
53
|
+
end
|
54
|
+
|
55
|
+
add(args.shift) and return if args.delete('--add')
|
56
|
+
clear_cache if @edit = args.delete('--edit')
|
57
|
+
|
58
|
+
@sheet = args.shift
|
59
|
+
|
60
|
+
true
|
61
|
+
end
|
62
|
+
|
63
|
+
# $ cheat greader --diff 1[:3]
|
64
|
+
def diff_sheets(sheet, version)
|
65
|
+
if version =~ /^(\d+)(:(\d+))?$/
|
66
|
+
old_version, new_version = $1, $3
|
67
|
+
else return
|
68
|
+
end
|
69
|
+
|
70
|
+
uri = "http://#{cheat_uri}/d/#{sheet}/#{old_version}"
|
71
|
+
uri += "/#{new_version}" if new_version
|
72
|
+
|
73
|
+
fetch_sheet(uri, false)
|
74
|
+
end
|
75
|
+
|
76
|
+
def cache_file
|
77
|
+
"#{cache_dir}/#{@sheet}.yml" if cache_dir
|
78
|
+
end
|
79
|
+
|
80
|
+
def headers
|
81
|
+
{ 'User-Agent' => 'cheat!', 'Accept' => 'text/yaml' }
|
82
|
+
end
|
83
|
+
|
84
|
+
def cheat_uri
|
85
|
+
"#{HOST}:#{PORT}#{SUFFIX}"
|
86
|
+
end
|
87
|
+
|
88
|
+
def show(sheet_yaml)
|
89
|
+
sheet = YAML.load(sheet_yaml).to_a.first
|
90
|
+
sheet[-1] = sheet.last.join("\n") if sheet[-1].is_a?(Array)
|
91
|
+
puts sheet.first + ':'
|
92
|
+
puts ' ' + sheet.last.gsub("\r",'').gsub("\n", "\n ").wrap
|
93
|
+
end
|
94
|
+
|
95
|
+
def edit(sheet_yaml)
|
96
|
+
sheet = YAML.load(sheet_yaml).to_a.first
|
97
|
+
sheet[-1] = sheet.last.gsub("\r", '')
|
98
|
+
body, title = write_to_tempfile(*sheet), sheet.first
|
99
|
+
return if body.strip == sheet.last.strip
|
100
|
+
res = post_sheet(title, body)
|
101
|
+
check_errors(res, title, body)
|
102
|
+
end
|
103
|
+
|
104
|
+
def add(title)
|
105
|
+
body = write_to_tempfile(title)
|
106
|
+
res = post_sheet(title, body, true)
|
107
|
+
check_errors(res, title, body)
|
108
|
+
end
|
109
|
+
|
110
|
+
def post_sheet(title, body, new = false)
|
111
|
+
uri = "http://#{cheat_uri}/w/"
|
112
|
+
uri += title unless new
|
113
|
+
Net::HTTP.post_form(URI.parse(uri), "sheet_title" => title, "sheet_body" => body.strip, "from_gem" => true)
|
114
|
+
end
|
115
|
+
|
116
|
+
def write_to_tempfile(title, body = nil)
|
117
|
+
# god dammit i hate tempfile, this is so messy but i think it's
|
118
|
+
# the only way.
|
119
|
+
tempfile = Tempfile.new(title + '.cheat')
|
120
|
+
tempfile.write(body) if body
|
121
|
+
tempfile.close
|
122
|
+
system "#{editor} #{tempfile.path}"
|
123
|
+
tempfile.open
|
124
|
+
body = tempfile.read
|
125
|
+
tempfile.close
|
126
|
+
body
|
127
|
+
end
|
128
|
+
|
129
|
+
def check_errors(result, title, text)
|
130
|
+
if result.body =~ /<p class="error">(.+?)<\/p>/m
|
131
|
+
puts $1.gsub(/\n/, '').gsub(/<.+?>/, '').squeeze(' ').wrap(80)
|
132
|
+
puts
|
133
|
+
puts "Here's what you wrote, so it isn't lost in the void:"
|
134
|
+
puts text
|
135
|
+
else
|
136
|
+
puts "Success! Try it!", "$ cheat #{title} --new"
|
137
|
+
end
|
138
|
+
end
|
139
|
+
|
140
|
+
def editor
|
141
|
+
ENV['VISUAL'] || ENV['EDITOR'] || "vim"
|
142
|
+
end
|
143
|
+
|
144
|
+
def cache_dir
|
145
|
+
PLATFORM =~ /win32/ ? win32_cache_dir : File.join(File.expand_path("~"), ".cheat")
|
146
|
+
end
|
147
|
+
|
148
|
+
def win32_cache_dir
|
149
|
+
unless File.exists?(home = ENV['HOMEDRIVE'] + ENV['HOMEPATH'])
|
150
|
+
puts "No HOMEDRIVE or HOMEPATH environment variable. Set one to save a" +
|
151
|
+
"local cache of cheat sheets."
|
152
|
+
return false
|
153
|
+
else
|
154
|
+
return File.join(home, 'Cheat')
|
155
|
+
end
|
156
|
+
end
|
157
|
+
|
158
|
+
def clear_cache
|
159
|
+
FileUtils.rm_rf(cache_dir) if cache_dir
|
160
|
+
end
|
161
|
+
end
|
162
|
+
|
163
|
+
Cheat.sheets(ARGV) if __FILE__ == $0
|
metadata
CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.9.0
|
|
3
3
|
specification_version: 1
|
4
4
|
name: cheat
|
5
5
|
version: !ruby/object:Gem::Version
|
6
|
-
version:
|
7
|
-
date: 2006-11-
|
6
|
+
version: 1.2.1
|
7
|
+
date: 2006-11-10 00:00:00 -08:00
|
8
8
|
summary: Cheat is a simple command line utility reference program.
|
9
9
|
require_paths:
|
10
10
|
- lib
|
@@ -32,11 +32,14 @@ files:
|
|
32
32
|
- README
|
33
33
|
- LICENSE
|
34
34
|
- bin/cheat
|
35
|
+
- lib/cheat
|
35
36
|
- lib/cheat.rb
|
36
37
|
- lib/diffr.rb
|
38
|
+
- lib/new_cheat.rb
|
37
39
|
- lib/responder.rb
|
38
40
|
- lib/site.rb
|
39
41
|
- lib/wrap.rb
|
42
|
+
- lib/cheat/commands.rb
|
40
43
|
- test/fixtures
|
41
44
|
- test/test_cheat.rb
|
42
45
|
test_files: []
|