cmdline-fu 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
data/Manifest CHANGED
@@ -4,3 +4,4 @@ README.md
4
4
  Rakefile
5
5
  VERSION
6
6
  bin/cmdline-fu
7
+ example/dot.cmdline-fu.conf
data/README.md CHANGED
@@ -6,22 +6,58 @@ commandlinefu is community based repository for usefull commandline such as one-
6
6
  This script scrape available TAG from site's top page with `hpricot` for supporting query based on TAG.
7
7
  TAGS file are stored to `~/.cmdline-fu.tags`.
8
8
 
9
- `o` option depends on `open` command of OSX(means supported only on MacOS).
9
+ Install
10
+ ----------------------------------
11
+ gem install cmdline-fu
10
12
 
11
- ## Install:
12
13
 
13
- gem install cmdline-fu
14
+ Sample Configuration("~/.cmdline-fu.conf")
15
+ ----------------------------------
16
+ {
17
+ #--------------------------------------------------------
18
+ # Proxy setting. you can overwrite this
19
+ # with HTTP_PROXY environment variable
20
+ #--------------------------------------------------------
21
+ # :http_proxy_ => 'http://192.168.1.1:3128',
14
22
 
15
- ## Usage
23
+ #--------------------------------------------------------
24
+ # Disable colorized output
25
+ #--------------------------------------------------------
26
+ #:colorize => false,
16
27
 
17
- cmdline-fu COMMAND [PAGE] [o]
28
+ #--------------------------------------------------------
29
+ # Supported colors are...
30
+ # black red green yellow blue magenta cyan white
31
+ #--------------------------------------------------------
32
+ :color_match => 'yellow',
33
+ :color_desc => 'cyan',
34
+ :color_footer => 'green',
35
+
36
+ #--------------------------------------------------------
37
+ # Web Browser command used such as 'cmdline-fu browse o'.
38
+ #--------------------------------------------------------
39
+ # for linux
40
+ :browse_cmd => 'firefox',
41
+ # :browse_cmd => 'google-chrome',
42
+
43
+ # Mac OSX's default
44
+ # :browse_cmd => 'open',
45
+ }
46
+
47
+ Usage
48
+ ----------------------------------
49
+
50
+ cmdline-fu COMMAND [PAGE] [o] [-n]
18
51
 
19
52
  COMMAND: list_tag [MATCHER], browse, using WORD, by USER, tagged TAG, matching WORD
20
53
  PAGE: 1-999 (defaut: 1)
21
- o: 'o'pen in browser
22
-
23
- ## Example
54
+ o: open in browser
55
+ -n: not colorize
24
56
 
57
+ Example
58
+ ----------------------------------
59
+
60
+ cmdline-fu renew_tag
25
61
  cmdline-fu list_tag
26
62
  cmdline-fu list_tag vm
27
63
  cmdline-fu browse
@@ -31,9 +67,11 @@ TAGS file are stored to `~/.cmdline-fu.tags`.
31
67
  cmdline-fu tagged install
32
68
  cmdline-fu matching find
33
69
 
34
- ## Abbreviation
70
+ Abbreviation
71
+ ----------------------------------
35
72
  Unique abbreviation for command is supported.
36
73
 
74
+ cmdline-fu r
37
75
  cmdline-fu l
38
76
  cmdline-fu l vm
39
77
  cmdline-fu br
data/Rakefile CHANGED
@@ -10,5 +10,5 @@ Echoe.new "cmdline-fu", File.read("./VERSION").chomp do |p|
10
10
  p.rubyforge_name = nil
11
11
  p.url = "http://github.com/t9md/cmdline-fu"
12
12
  p.runtime_dependencies << 'colored'
13
- p.runtime_dependencies << 'hpricot >=0.8.2'
13
+ # p.runtime_dependencies << 'hpricot >=0.8.2'
14
14
  end
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.2
1
+ 0.1.3
data/bin/cmdline-fu CHANGED
@@ -1,15 +1,10 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
3
  require "rubygems"
4
- require 'net/http'
4
+ require "net/http"
5
5
  require "ostruct"
6
6
  require "abbrev"
7
7
 
8
- begin
9
- require "colored"
10
- rescue LoadError
11
- end
12
-
13
8
  # Example
14
9
  # browse/sort-by-votes - All commands sorted by votes
15
10
  # tagged/163/grep - Commands tagged with 'grep', sorted by date (the default sort order)
@@ -18,100 +13,153 @@ end
18
13
  # api_comand_set = [ :browse, :tagged, :matching ]
19
14
  # api_format = [ :plaintext, :json, :rss ]
20
15
  # api_url = "http://www.commandlinefu.com/commands/<command-set>/<format>/"
16
+
17
+ def try_require(lib, msg=nil)
18
+ begin
19
+ require lib
20
+ rescue LoadError => e
21
+ warn "can't load #{lib}: #{e}"
22
+ warn "\n#{msg}\n"
23
+ end
24
+ end
25
+
26
+ try_require "colored"
21
27
 
22
28
  class CommandLineFu
23
29
  API_URL = 'http://www.commandlinefu.com/commands'
24
30
 
25
- # attr_reader :api_url
31
+ DEFAULT_OPT = {
32
+ :page => 1,
33
+ :command => "browse",
34
+ :format => "plaintext",
35
+ :sort_order => "sort-by-votes",
36
+ :tag_file => "~/.cmdline-fu.tags",
37
+ :tag_expire_days => 3,
38
+ :open_browser => false,
39
+ :colorize => true,
40
+ # 'black' => 30,
41
+ # 'red' => 31,
42
+ # 'green' => 32,
43
+ # 'yellow' => 33,
44
+ # 'blue' => 34,
45
+ # 'magenta' => 35,
46
+ # 'cyan' => 36,
47
+ # 'white' => 37
48
+ :color_match => 'yellow',
49
+ :color_desc => 'cyan',
50
+ :color_footer => 'green'
51
+ }
52
+
53
+ attr_reader :opt
54
+ def initialize(user_opt)
55
+ @opt = OpenStruct.new(DEFAULT_OPT.update(user_config).update(user_opt))
56
+ # p @opt
57
+ # exit
58
+ end
26
59
 
27
- def initialize user_opt
28
- @opt = OpenStruct.new($default_opt.update(user_opt))
29
- @command = @opt.command
30
- @format = @opt.format
31
- @sort_order = @opt.sort_order
32
- unless @command == "list_tag"
33
- build_url
60
+ def user_config
61
+ user_config = File.expand_path "~/.cmdline-fu.conf"
62
+ if File.exist? user_config
63
+ return(eval File.read(user_config))
64
+ else
65
+ return {}
34
66
  end
35
67
  end
36
68
 
37
69
  def result
38
- return list_tag if @command == "list_tag"
39
-
40
-
41
- page_idx = (@opt.page - 1) * 25
42
- url = @api_url + "/#{page_idx}"
70
+ if %w(list_tag renew_tag version).include? opt.command
71
+ return send(opt.command)
72
+ end
73
+ page_idx = (opt.page - 1) * 25
43
74
 
44
- if @opt.open_browser
45
- system("open '#{url}'")
75
+ url = "#{api_url}/#{page_idx}"
76
+ if opt.open_browser
77
+ system("#{browse_cmd} '#{url}'")
46
78
  return
47
79
  end
80
+
48
81
  result = open_url(url)
49
- return "#{@opt.search} NOT FOUND" if result.code != "200"
82
+ return "#{opt.search} NOT FOUND" if result.code != "200"
50
83
 
51
84
  # each entry is separated by blank line , and we need to -1 for page headers.
52
85
  b = result.body.split("\n\n")
53
86
  header = b.shift
54
87
  num_of_entries = b.size
55
88
  body = b.join("\n\n")
89
+
56
90
  unless num_of_entries.zero?
57
- body = (defined?(Colored) ? colorize(body) : body)
58
- body << "\n\n## Page(#{@opt.page}):#{page_idx}-#{page_idx+num_of_entries} #{url}".green
59
- body << "\n##{header}".green
91
+ footer = "\n\n## Page(#{opt.page}):#{page_idx}-#{page_idx+num_of_entries} #{url}\n#{header}"
92
+ p opt.colorize
93
+ if opt.colorize
94
+ body = colorize(body)
95
+ footer = footer.send(opt.color_footer)
96
+ end
97
+ body + footer
60
98
  end
61
- body
62
99
  end
63
100
 
64
101
  def open_url(url)
65
- uri = URI.parse(url)
66
- http = Net::HTTP.new(uri.host, uri.port)
102
+ Net::HTTP.version_1_2
103
+ uri = URI.parse(url)
104
+ proxy = URI.parse(ENV['HTTP_PROXY'] || opt.http_proxy_|| "")
105
+ http = Net::HTTP::Proxy(proxy.host, proxy.port).start(uri.host, uri.port)
67
106
  request = Net::HTTP::Get.new(uri.request_uri)
68
107
  http.request(request)
69
108
  end
70
109
 
71
110
  private
72
- def build_url
73
- command_part = send("setup_#{@command}")
74
- @api_url = "#{API_URL}/%s/%s" % [command_part, @sort_order ]
75
- @api_url += "/#{@format}" if @format
76
- end
77
-
78
- def setup_browse
79
- "#{@command}"
111
+ def browse_cmd
112
+ return opt.browse_cmd if opt.browse_cmd
113
+ case RUBY_PLATFORM.downcase
114
+ when /darwin/ then "open"
115
+ when /linux/ then "firefox"
116
+ when /mswin/ then 'echo "NOT SUPPORTED for browse command"'
117
+ end
80
118
  end
81
119
 
82
- def setup_using
83
- "#{@command}/#{@opt.search}"
120
+ def api_url
121
+ command_part = command_set_for(opt.command)
122
+ url = "#{API_URL}/#{command_part}/#{opt.sort_order}"
123
+ url << "/#{opt.format}" if opt.format
124
+ url
84
125
  end
85
- alias_method :setup_by, :setup_using
86
126
 
87
- def setup_matching
88
- b64text = [@opt.search].pack("m").chomp
89
- search = @opt.search.tr(' ','-')
90
- "#{@command}/#{search}/#{b64text}"
127
+ def command_set_for(command)
128
+ {
129
+ "version" => "version",
130
+ "browse" => "browse",
131
+ "using" => "using/#{opt.search}",
132
+ "by" => "by/#{opt.search}",
133
+ "matching" => lambda {
134
+ b64text = [opt.search].pack("m").chomp
135
+ search = opt.search.tr(' ','-')
136
+ "matching/#{search}/#{b64text}"
137
+ }.call,
138
+ "tagged" => lambda {
139
+ unless tag_id = tags[opt.search]
140
+ raise "Tag not found"
141
+ end
142
+ "tagged/#{tag_id}/#{opt.search}"
143
+ }.call
144
+ }[command]
91
145
  end
92
146
 
93
- def setup_tagged
94
- tag = @opt.search
95
- tag_id = tags[tag]
96
- raise "Tag not found" unless tag_id
97
- "#{@command}/#{tag_id}/#{tag}"
147
+ def version
148
+ return File.read(File.dirname(File.expand_path(__FILE__)) + "/../VERSION" )
98
149
  end
99
150
 
100
151
  def colorize(string)
101
152
  string.split("\n").map do |e|
102
153
  e.chomp!
103
- (e =~ /^#/) ? e.blue : e.gsub(/#{@opt.search}/){|m| Colored.colorize m, :foreground => 'magenta' }
154
+ (e =~ /^#/) ? e.send(opt.color_desc) :
155
+ e.gsub(/#{opt.search}/){|m|
156
+ Colored.colorize m, :foreground => opt.color_match
157
+ }
104
158
  end.join("\n")
105
159
  end
106
160
 
107
- def tag_file_is_too_old?
108
- tag_file = File.expand_path @opt.tag_file
109
- expire_limit = (60 * 60 * 24) * @opt.tag_expire_days
110
- expire_limit < (Time.now - File.stat(tag_file).mtime)
111
- end
112
-
113
161
  def list_tag
114
- candidate = tags.keys.grep(/#{@opt.search}/)
162
+ candidate = tags.keys.grep(/#{opt.search}/)
115
163
  result = if candidate.size >= 1
116
164
  colorize(candidate.join("\n"))
117
165
  elsif candidate.size == 0
@@ -125,10 +173,11 @@ class CommandLineFu
125
173
  end
126
174
 
127
175
  def build_tag_file
128
- tag_file = File.expand_path @opt.tag_file
129
- h = extract_tag
130
- File.open(tag_file, "wb"){ |f| Marshal.dump(h, f) }
176
+ tag_file = File.expand_path opt.tag_file
177
+ File.open(tag_file, "wb"){ |f| Marshal.dump(scrape_tag, f) }
178
+ return
131
179
  end
180
+ alias_method :renew_tag, :build_tag_file
132
181
 
133
182
  # def extract_tag_nokogiri
134
183
  # require 'nokogiri'
@@ -144,8 +193,17 @@ class CommandLineFu
144
193
  # h
145
194
  # end
146
195
 
147
- def extract_tag
148
- require "hpricot"
196
+ def scrape_tag
197
+ ret = try_require "hpricot", <<-EOS
198
+
199
+ if you want to TAG based query, install 'hpricot'
200
+
201
+ gem install hpricot
202
+
203
+ EOS
204
+ unless ret
205
+ exit
206
+ end
149
207
  h = {}
150
208
  url = 'http://www.commandlinefu.com/commands/browse'
151
209
  Hpricot(open_url(url).body).
@@ -160,15 +218,13 @@ class CommandLineFu
160
218
  end
161
219
 
162
220
  def load_tags
163
- tag_file = File.expand_path @opt.tag_file
164
- if not File.exist?(tag_file) or tag_file_is_too_old? or $NOCACHE
165
- build_tag_file
221
+ tag_file = File.expand_path opt.tag_file
222
+ unless File.exist?(tag_file)
223
+ build_tag_file
166
224
  end
167
225
  File.open(tag_file, "rb"){ |f| Marshal.load f }
168
226
  end
169
-
170
227
  end
171
- # $NOCACHE = true
172
228
 
173
229
  # TEST
174
230
  # [
@@ -181,26 +237,10 @@ end
181
237
  # puts CommandLineFu.new(opt).api_url
182
238
  # end
183
239
 
184
- $default_opt = {
185
- :page => 1,
186
- :command => "browse",
187
- :format => "plaintext",
188
- :sort_order => "sort-by-votes",
189
- :tag_file => "~/.cmdline-fu.tags",
190
- :tag_expire_days => 3,
191
- :open_browser => false,
192
- }
193
-
194
- command_table = Abbrev.abbrev(%w(list_tag browse using by tagged matching))
195
-
196
- # user_opt = {}
197
- # ARGV = %w(browse)
198
- # ARGV = %w(using find)
199
- # ARGV = %w(by atoponce)
200
- # ARGV = %w(matching find)
201
-
240
+ command_table = Abbrev.abbrev(%w(list_tag browse using by tagged matching renew_tag version))
202
241
 
203
242
  user_opt = {}
243
+ user_opt[:colorize] = false if ARGV.delete('-n')
204
244
  user_opt[:command] = command_table[ARGV.shift]
205
245
 
206
246
  if ARGV.last == 'o'
@@ -213,21 +253,22 @@ page = ARGV.last.to_i
213
253
  user_opt[:page] = page.zero? ? 1 : page
214
254
  user_opt[:search] = ARGV.shift
215
255
 
216
-
217
256
  PROGRAM_NAME = File.basename $0
218
257
  if user_opt[:command].nil?
219
258
  puts <<-EOS
220
259
 
221
260
  #{"Usage".bold}
222
261
 
223
- #{PROGRAM_NAME} COMMAND [PAGE] [o]
262
+ #{PROGRAM_NAME} COMMAND [PAGE] [o] [-n]
224
263
 
225
264
  COMMAND: list_tag [MATCHER], browse, using WORD, by USER, tagged TAG, matching WORD
226
265
  PAGE: 1-999 (defaut: 1)
227
- o: 'o'pen in browser
266
+ o: open in browser
267
+ -n: not colorize
228
268
 
229
269
  #{"Example".bold}
230
270
 
271
+ #{PROGRAM_NAME} renew_tag
231
272
  #{PROGRAM_NAME} list_tag
232
273
  #{PROGRAM_NAME} list_tag vm
233
274
  #{PROGRAM_NAME} browse
@@ -240,6 +281,7 @@ if user_opt[:command].nil?
240
281
  #{"Abbreviation".bold}
241
282
  Unique abbreviation for command is supported.
242
283
 
284
+ #{PROGRAM_NAME} r
243
285
  #{PROGRAM_NAME} l
244
286
  #{PROGRAM_NAME} l vm
245
287
  #{PROGRAM_NAME} br
data/cmdline-fu.gemspec CHANGED
@@ -2,17 +2,17 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{cmdline-fu}
5
- s.version = "0.1.2"
5
+ s.version = "0.1.3"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = ["t9md"]
9
- s.date = %q{2010-12-29}
9
+ s.date = %q{2011-01-22}
10
10
  s.default_executable = %q{cmdline-fu}
11
11
  s.description = %q{CLI frontend for http://www.commandlinefu.com/ }
12
12
  s.email = %q{taqumd@gmail.com}
13
13
  s.executables = ["cmdline-fu"]
14
14
  s.extra_rdoc_files = ["LICENSE.txt", "README.md", "bin/cmdline-fu"]
15
- s.files = ["LICENSE.txt", "Manifest", "README.md", "Rakefile", "VERSION", "bin/cmdline-fu", "cmdline-fu.gemspec"]
15
+ s.files = ["LICENSE.txt", "Manifest", "README.md", "Rakefile", "VERSION", "bin/cmdline-fu", "example/dot.cmdline-fu.conf", "cmdline-fu.gemspec"]
16
16
  s.homepage = %q{http://github.com/t9md/cmdline-fu}
17
17
  s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Cmdline-fu", "--main", "README.md"]
18
18
  s.require_paths = ["lib"]
@@ -25,13 +25,10 @@ Gem::Specification.new do |s|
25
25
 
26
26
  if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
27
27
  s.add_runtime_dependency(%q<colored>, [">= 0"])
28
- s.add_runtime_dependency(%q<hpricot>, [">= 0.8.2"])
29
28
  else
30
29
  s.add_dependency(%q<colored>, [">= 0"])
31
- s.add_dependency(%q<hpricot>, [">= 0.8.2"])
32
30
  end
33
31
  else
34
32
  s.add_dependency(%q<colored>, [">= 0"])
35
- s.add_dependency(%q<hpricot>, [">= 0.8.2"])
36
33
  end
37
34
  end
@@ -0,0 +1,30 @@
1
+ {
2
+ #--------------------------------------------------------
3
+ # Proxy setting. you can overwrite this
4
+ # with HTTP_PROXY environment variable
5
+ #--------------------------------------------------------
6
+ # :http_proxy_ => 'http://192.168.1.1:3128',
7
+
8
+ #--------------------------------------------------------
9
+ # Disable colorized output
10
+ #--------------------------------------------------------
11
+ #:colorize => false,
12
+
13
+ #--------------------------------------------------------
14
+ # Supported colors are...
15
+ # black red green yellow blue magenta cyan white
16
+ #--------------------------------------------------------
17
+ :color_match => 'yellow',
18
+ :color_desc => 'cyan',
19
+ :color_footer => 'green',
20
+
21
+ #--------------------------------------------------------
22
+ # Web Browser command used such as 'cmdline-fu browse o'.
23
+ #--------------------------------------------------------
24
+ # for linux
25
+ :browse_cmd => 'firefox',
26
+ # :browse_cmd => 'google-chrome',
27
+
28
+ # Mac OSX's default
29
+ # :browse_cmd => 'open',
30
+ }
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cmdline-fu
3
3
  version: !ruby/object:Gem::Version
4
- hash: 31
4
+ hash: 29
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 2
10
- version: 0.1.2
9
+ - 3
10
+ version: 0.1.3
11
11
  platform: ruby
12
12
  authors:
13
13
  - t9md
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-12-29 00:00:00 +09:00
18
+ date: 2011-01-22 00:00:00 +09:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -32,22 +32,6 @@ dependencies:
32
32
  version: "0"
33
33
  type: :runtime
34
34
  version_requirements: *id001
35
- - !ruby/object:Gem::Dependency
36
- name: hpricot
37
- prerelease: false
38
- requirement: &id002 !ruby/object:Gem::Requirement
39
- none: false
40
- requirements:
41
- - - ">="
42
- - !ruby/object:Gem::Version
43
- hash: 59
44
- segments:
45
- - 0
46
- - 8
47
- - 2
48
- version: 0.8.2
49
- type: :runtime
50
- version_requirements: *id002
51
35
  description: "CLI frontend for http://www.commandlinefu.com/ "
52
36
  email: taqumd@gmail.com
53
37
  executables:
@@ -65,6 +49,7 @@ files:
65
49
  - Rakefile
66
50
  - VERSION
67
51
  - bin/cmdline-fu
52
+ - example/dot.cmdline-fu.conf
68
53
  - cmdline-fu.gemspec
69
54
  has_rdoc: true
70
55
  homepage: http://github.com/t9md/cmdline-fu