bin/rurema in myrurema-0.0.2 vs bin/rurema in myrurema-0.0.3

- old
+ new

@@ -1,15 +1,17 @@ #!/usr/bin/env ruby require 'optparse' require 'pathname' +require 'shellwords' class Pathname; alias / +; end class Options def initialize(argv) @command = nil @open_browser = false + @port = nil @dry_run = false @ruremadir = Pathname("~/.rurema").expand_path @rubyver = RUBY_VERSION @query = "" @@ -25,10 +27,14 @@ o.on("--server", "start web server"){ @command = :server } + o.on("--port=N", + "port number of the web browser (only meaningful with --server)"){|n| + @port = n.to_i + } o.on("--browser", "open web browser (only meaningful with --server)"){ @open_browser = true } o.on("--dry-run", @@ -57,11 +63,11 @@ @query, @num = @optionparser.parse(argv) @query = "" if @query.nil? @num = @num.to_i if @num end attr_reader :dry_run, :ruremadir, :rubyver, :open_browser - attr_reader :command, :query, :num + attr_reader :command, :query, :num, :port def usage puts @optionparser end end @@ -100,11 +106,11 @@ def search(query, ver) should_have_db(ver) sh "#{bitclust_path/'bin/refe.rb'}" + - " #{query} -d #{db_path(ver)}", :silent => true + " #{Shellwords.escape query} -d #{db_path(ver)}", :silent => true end def search_num(query, num, ver) should_have_db(ver) @@ -118,24 +124,30 @@ error "less than #{num} entries found" end end def server + port = @opt.port || default_port(@opt.rubyver) th = Thread.new{ sh "#{bitclust_path/'standalone.rb'}" + - " --baseurl=http://localhost:9292" + - " --port=9292" + + " --baseurl=http://localhost:#{port}" + + " --port=#{port}" + " --database=#{db_path(@opt.rubyver)}" + " --debug" } if @opt.open_browser + sleep 1 # wait for the server to start cmd = (/mswin/ =~ RUBY_PLATFORM) ? "start" : "open" - sh "#{cmd} http://localhost:9292/view/" + sh "#{cmd} http://localhost:#{port}/view/" end th.join end private + + def default_port(ver) + "10" + ver.scan(/\d/).join + end def init_db(ver) sh "#{bitclust_path/'bin/bitclust.rb'}" + " -d #{db_path(ver)} init version=#{ver} encoding=euc-jp"