cheat 1.3.0 → 1.3.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.
@@ -1,7 +1,9 @@
1
1
  %w( tempfile fileutils net/http yaml open-uri cheat/wrap ).each { |f| require f }
2
+ require 'pager'
2
3
  RUBY_PLATFORM = PLATFORM unless defined? RUBY_PLATFORM # Ruby 1.8 compatibility
3
4
 
4
5
  module Cheat
6
+ include Pager
5
7
  extend self
6
8
 
7
9
  HOST = ARGV.include?('debug') ? 'localhost' : 'cheat.errtheblog.com'
@@ -22,7 +24,7 @@ module Cheat
22
24
  return open(uri, headers) { |body| process(body.read) }
23
25
  end
24
26
 
25
- return process(File.read(cache_file)) if File.exists?(cache_file) rescue clear_cache if cache_file
27
+ return process(File.read(cache_file)) if File.exists?(cache_file) rescue clear_cache if cache_file
26
28
 
27
29
  fetch_sheet(uri + @sheet) if @sheet
28
30
  end
@@ -31,9 +33,9 @@ module Cheat
31
33
  open(uri, headers) do |body|
32
34
  sheet = body.read
33
35
  FileUtils.mkdir_p(cache_dir) unless File.exists?(cache_dir)
34
- File.open(cache_file, 'w') { |f| f.write(sheet) } if try_to_cache && has_content(sheet) && cache_file && !@edit
36
+ File.open(cache_file, 'w') { |f| f.write(sheet) } if try_to_cache && has_content(sheet) && cache_file && !@edit
35
37
  @edit ? edit(sheet) : show(sheet)
36
- end
38
+ end
37
39
  exit
38
40
  rescue OpenURI::HTTPError => e
39
41
  puts "Whoa, some kind of Internets error!", "=> #{e} from #{uri}"
@@ -48,14 +50,14 @@ module Cheat
48
50
  end
49
51
 
50
52
  if i = args.index('--diff')
51
- diff_sheets(args.first, args[i+1])
53
+ diff_sheets(args.first, args[i+1])
52
54
  end
53
55
 
54
56
  show_versions(args.first) if args.delete('--versions')
55
57
 
56
58
  add(args.shift) and return if args.delete('--add')
57
59
  incoming_file = true if @edit = args.delete('--edit')
58
-
60
+
59
61
  @execute = true if args.delete("--execute") || args.delete("-x")
60
62
  @sheet = args.shift
61
63
 
@@ -76,7 +78,7 @@ module Cheat
76
78
  uri = "http://#{cheat_uri}/d/#{sheet}/#{old_version}"
77
79
  uri += "/#{new_version}" if new_version
78
80
 
79
- fetch_sheet(uri, false)
81
+ fetch_sheet(uri, false)
80
82
  end
81
83
 
82
84
  def has_content(sheet)
@@ -91,9 +93,9 @@ module Cheat
91
93
  end
92
94
 
93
95
  def headers
94
- { 'User-Agent' => 'cheat!', 'Accept' => 'text/yaml' }
96
+ { 'User-Agent' => 'cheat!', 'Accept' => 'text/yaml' }
95
97
  end
96
-
98
+
97
99
  def cheat_uri
98
100
  "#{HOST}:#{PORT}#{SUFFIX}"
99
101
  end
@@ -126,7 +128,7 @@ module Cheat
126
128
  def show(sheet_yaml)
127
129
  sheet = YAML.load(sheet_yaml).to_a.first
128
130
  sheet[-1] = sheet.last.join("\n") if sheet[-1].is_a?(Array)
129
- run_pager
131
+ page
130
132
  puts sheet.first + ':'
131
133
  puts ' ' + sheet.last.gsub("\r",'').gsub("\n", "\n ").wrap
132
134
  rescue Errno::EPIPE
@@ -172,8 +174,8 @@ module Cheat
172
174
  def check_errors(result, title, text)
173
175
  if result.body =~ /<p class="error">(.+?)<\/p>/m
174
176
  puts $1.gsub(/\n/, '').gsub(/<.+?>/, '').squeeze(' ').wrap(80)
175
- puts
176
- puts "Here's what you wrote, so it isn't lost in the void:"
177
+ puts
178
+ puts "Here's what you wrote, so it isn't lost in the void:"
177
179
  puts text
178
180
  else
179
181
  puts "Success! Try it!", "$ cheat #{title}"
@@ -181,7 +183,7 @@ module Cheat
181
183
  end
182
184
 
183
185
  def editor
184
- ENV['VISUAL'] || ENV['EDITOR'] || "vim"
186
+ ENV['VISUAL'] || ENV['EDITOR'] || "vim"
185
187
  end
186
188
 
187
189
  def cache_dir
@@ -203,36 +205,9 @@ module Cheat
203
205
  end
204
206
 
205
207
  def clear_cache_file
206
- FileUtils.rm(cache_file) if File.exists?(cache_file)
208
+ FileUtils.rm(cache_file) if File.exists?(cache_file)
207
209
  end
208
210
 
209
- def run_pager
210
- return if RUBY_PLATFORM =~ /win32/
211
- return unless STDOUT.tty?
212
-
213
- read, write = IO.pipe
214
-
215
- unless Kernel.fork # Child process
216
- STDOUT.reopen(write)
217
- STDERR.reopen(write) if STDERR.tty?
218
- read.close
219
- write.close
220
- return
221
- end
222
-
223
- # Parent process, become pager
224
- STDIN.reopen(read)
225
- read.close
226
- write.close
227
-
228
- ENV['LESS'] = 'FSRX' # Don't page if the input is short enough
229
-
230
- # wait until we have input before we start the pager
231
- Kernel.select [STDIN]
232
- pager = ENV['PAGER'] || 'less'
233
- exec pager rescue exec "/bin/sh", "-c", pager
234
- rescue
235
- end
236
211
  end
237
212
 
238
213
  Cheat.sheets(ARGV) if __FILE__ == $0
@@ -1,3 +1,3 @@
1
1
  module Cheat
2
- VERSION = "1.3.0"
2
+ VERSION = "1.3.1"
3
3
  end
@@ -6,9 +6,9 @@ class String
6
6
 
7
7
  lines.collect! do |line|
8
8
 
9
- if magic_lists
9
+ if magic_lists
10
10
  line =~ /^([\s\-\d\.\:]*\s)/
11
- else
11
+ else
12
12
  line =~ /^([\s]*\s)/
13
13
  end
14
14
 
@@ -18,12 +18,12 @@ class String
18
18
  first = true
19
19
 
20
20
  while line.length > 0
21
- first ? (i, first = 0, false) : i = indent
21
+ first ? (i, first = 0, false) : i = indent
22
22
  pos = width - i
23
23
 
24
24
  if line.length > pos and line[0..pos] =~ /^(.+)\s/
25
25
  subline = $1
26
- else
26
+ else
27
27
  subline = line[0..pos]
28
28
  end
29
29
  buffer += " " * i + subline + "\n"
metadata CHANGED
@@ -1,32 +1,40 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: cheat
3
- version: !ruby/object:Gem::Version
4
- prerelease: false
5
- segments:
6
- - 1
7
- - 3
8
- - 0
9
- version: 1.3.0
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.3.1
5
+ prerelease:
10
6
  platform: ruby
11
- authors:
7
+ authors:
12
8
  - Chris Wanstrath
13
9
  autorequire:
14
10
  bindir: bin
15
11
  cert_chain: []
16
-
17
- date: 2010-07-24 00:00:00 -07:00
18
- default_executable:
19
- dependencies: []
20
-
21
- description: " cheat prints cheat sheets from cheat.errtheblog.com, a wiki-like\n repository of programming knowledge.\n"
12
+ date: 2012-12-06 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: pager
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '1.0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '1.0'
30
+ description: ! " cheat prints cheat sheets from cheat.errtheblog.com, a wiki-like\n
31
+ \ repository of programming knowledge.\n"
22
32
  email: chris@ozmm.org
23
- executables:
33
+ executables:
24
34
  - cheat
25
35
  extensions: []
26
-
27
36
  extra_rdoc_files: []
28
-
29
- files:
37
+ files:
30
38
  - README
31
39
  - LICENSE
32
40
  - lib/ambition/init.rb
@@ -59,35 +67,28 @@ files:
59
67
  - lib/cheat/wrap.rb
60
68
  - lib/cheat.rb
61
69
  - bin/cheat
62
- has_rdoc: true
63
70
  homepage: http://cheat.errtheblog.com
64
71
  licenses: []
65
-
66
72
  post_install_message:
67
73
  rdoc_options: []
68
-
69
- require_paths:
74
+ require_paths:
70
75
  - lib
71
- required_ruby_version: !ruby/object:Gem::Requirement
72
- requirements:
73
- - - ">="
74
- - !ruby/object:Gem::Version
75
- segments:
76
- - 0
77
- version: "0"
78
- required_rubygems_version: !ruby/object:Gem::Requirement
79
- requirements:
80
- - - ">="
81
- - !ruby/object:Gem::Version
82
- segments:
83
- - 0
84
- version: "0"
76
+ required_ruby_version: !ruby/object:Gem::Requirement
77
+ none: false
78
+ requirements:
79
+ - - ! '>='
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ required_rubygems_version: !ruby/object:Gem::Requirement
83
+ none: false
84
+ requirements:
85
+ - - ! '>='
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
85
88
  requirements: []
86
-
87
89
  rubyforge_project:
88
- rubygems_version: 1.3.6
90
+ rubygems_version: 1.8.23
89
91
  signing_key:
90
92
  specification_version: 3
91
93
  summary: cheat prints cheat sheets from cheat.errtheblog.com
92
94
  test_files: []
93
-