src/myrurema.rb in myrurema-0.1.0 vs src/myrurema.rb in myrurema-0.2.0
- old
+ new
@@ -9,16 +9,21 @@
def initialize(argv)
@command = nil
@open_browser = false
@port = nil
@dry_run = false
+ @no_ask = false
@ruremadir = Pathname("~/.rurema").expand_path
@rubyver = RUBY_VERSION
@optionparser = OptionParser.new{|o|
+ o.banner = [
+ "Usage: rurema [options] <method name or class name>",
+ ].join("\n")
+
o.on("--init",
- "initialize rurema"){
+ "initialize rurema system"){
@command = :init
}
o.on("--update",
"update documents and database"){
@command = :update
@@ -29,12 +34,16 @@
}
o.on("--preview",
"render a reference as HTML"){
@command = :preview
}
+ o.on("--list",
+ "list all classes"){
+ @command = :list
+ }
- o.on("----"){}
+ o.on("---- (OPTIONS)"){}
o.on("--port=N",
"port number of the web browser (only meaningful with --server)"){|n|
@port = n.to_i
}
@@ -44,20 +53,24 @@
}
o.on("--dry-run",
"show commands only"){
@dry_run = true
}
+ o.on("--no-ask",
+ "do not ask keyboard input"){
+ @no_ask = true
+ }
o.on("--ruremadir=PATH",
"specify rurema directory (default: #{@ruremadir})"){|path|
@ruremadir = Pathname(path)
}
o.on("--rubyver=STR",
"specify Ruby version (default: #{@rubyver})"){|str|
@rubyver = str
}
- o.on("-----"){}
+ o.on("----- (INFO)"){}
o.on("--version",
"show version of myrurema"){
puts MyRurema::VERSION
exit
@@ -68,12 +81,12 @@
exit
}
}
@rest_args = @optionparser.parse(argv)
end
- attr_accessor :dry_run, :ruremadir, :rubyver, :open_browser
- attr_accessor :command, :port, :rest_args
+ attr_accessor :dry_run, :no_ask, :ruremadir, :rubyver,
+ :open_browser, :command, :port, :rest_args
def ruremadir=(dir)
@ruremadir = Pathname(dir)
end
@@ -118,12 +131,37 @@
end
def search(query, ver)
should_have_db(ver)
- sh "#{bitclust_path/'bin/refe.rb'}" +
- " #{Shellwords.escape query} -d #{db_path(ver)}", :silent => true
+ cmd = "#{bitclust_path/'bin/refe.rb'}" +
+ " #{Shellwords.escape query} -d #{db_path(ver)}"
+ sh cmd, :silent => true do |txt|
+ if txt.lines.count < 10 and
+ txt.lines.first(2).join =~ /#{query}.*#{query}/m and
+ !@opt.no_ask
+
+ words = {}
+ k = 0
+ puts txt.lines.map{|line|
+ line.gsub(/(\S+)/){|str|
+ k+=1
+ words[k] = str
+ "(#{k})#{str}"
+ }
+ }
+ print "which one? > "
+ line = $stdin.gets or (puts; exit)
+ n = line.to_i
+
+ puts "searching #{words[n]}"
+ puts
+ search(words[n].sub(/\.#/, "."), ver)
+ else
+ puts txt
+ end
+ end
end
def search_num(query, num, ver)
should_have_db(ver)
@@ -136,10 +174,17 @@
else
error "less than #{num} entries found"
end
end
+ def list
+ should_have_db(@opt.rubyver)
+
+ sh "#{bitclust_path/'bin/refe.rb'}" +
+ " -l -d #{db_path(@opt.rubyver)}", :silent => true
+ end
+
def server
port = @opt.port || default_port(@opt.rubyver)
th = Thread.new{
sh "#{bitclust_path/'standalone.rb'}" +
" --baseurl=http://localhost:#{port}" +
@@ -224,12 +269,18 @@
def db_path(ver)
@opt.ruremadir / "db" / ver
end
- def sh(cmd, opt={})
+ def sh(cmd, opt={}, &block)
puts cmd unless opt[:silent]
- system cmd unless @opt.dry_run
+ return if @opt.dry_run
+
+ if block
+ block.call(`#{cmd}`)
+ else
+ system cmd
+ end
end
def error(msg)
$stderr.puts msg
exit