Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
ongaeshi committed Feb 1, 2012
2 parents a85fd18 + b1daf7a commit 17d8216
Show file tree
Hide file tree
Showing 10 changed files with 134 additions and 15 deletions.
7 changes: 7 additions & 0 deletions HISTORY.ja.rdoc
Original file line number Original file line Diff line number Diff line change
@@ -1,3 +1,10 @@
=== 0.4.0 2012/02/02

* gmilk
* -iオプションの復活
* -gオプションの追加(Goto line mode)
* -cオプションを追加(マッチ数のみを表示)

=== 0.3.0 2012/01/20 === 0.3.0 2012/01/20


* gmilk * gmilk
Expand Down
7 changes: 7 additions & 0 deletions HISTORY.rdoc
Original file line number Original file line Diff line number Diff line change
@@ -1,3 +1,10 @@
=== 0.4.0 2012/02/02

* gmilk
* Revert -i option
* Add -g option (Goto line mode)
* Add -c option (Disp count num)

=== 0.3.0 2012/01/20 === 0.3.0 2012/01/20


* gmilk * gmilk
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Original file line Diff line number Diff line change
@@ -1 +1 @@
0.3.0 0.4.0
2 changes: 1 addition & 1 deletion bin/gmilk
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
require 'rubygems' require 'rubygems'
require 'milkode/grep/cli_grep' require 'milkode/grep/cli_grep'


Version = "0.3.0" Version = "0.4.0"
Milkode::CLI_Grep.execute(STDOUT, ARGV) Milkode::CLI_Grep.execute(STDOUT, ARGV)
2 changes: 1 addition & 1 deletion bin/milk
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
require 'rubygems' require 'rubygems'
require 'milkode/cdstk/cli_cdstk' require 'milkode/cdstk/cli_cdstk'


Version = "0.3.0" Version = "0.4.0"
Milkode::CLI_Cdstk.execute(STDOUT, ARGV) Milkode::CLI_Cdstk.execute(STDOUT, ARGV)
2 changes: 1 addition & 1 deletion lib/milkode/cdweb/app.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
set :haml, :format => :html5 set :haml, :format => :html5


get '/' do get '/' do
@version = "0.3.0" @version = "0.4.0"
@package_num = Database.instance.fileList('').size @package_num = Database.instance.fileList('').size
@file_num = Database.instance.fileNum @file_num = Database.instance.fileNum
haml :index haml :index
Expand Down
29 changes: 28 additions & 1 deletion lib/milkode/common/util.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -103,7 +103,34 @@ def normalize_filename(str)
def downcase?(str) def downcase?(str)
str == str.downcase str == str.downcase
end end


# parse_gotoline(['a', '123', 'b']) #=> [['a', 'b'], 123]]
# parse_gotoline(['a', '123', 'b', 55]) #=> [['a', 'b', '123'], 55]]
# parse_gotoline(['a:5']) #=> [['a'], 55]]
def parse_gotoline(words)
lineno = -1
index = -1

words = words.map{|v|
v.split(':')
}.flatten

words.each_with_index do |v, idx|
n = v.to_i
if (n != 0)
lineno = n
index = idx
end
end

if (lineno == -1)
[words, 1] # 行番号らしきものは見つからなかった
else
words.delete_at(index)
[words, lineno]
end
end

end end
end end


Expand Down
40 changes: 36 additions & 4 deletions lib/milkode/findgrep/findgrep.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ class FindGrep
:isMatchFile, :isMatchFile,
:dispHtml, :dispHtml,
:matchCountLimit, :matchCountLimit,
:keywords) :keywords,
:gotoline)


DEFAULT_OPTION = Option.new([], DEFAULT_OPTION = Option.new([],
[], [],
Expand All @@ -57,7 +58,8 @@ class FindGrep
false, false,
false, false,
-1, -1,
[]) [],
-1)


class MatchCountOverError < RuntimeError ; end class MatchCountOverError < RuntimeError ; end


Expand Down Expand Up @@ -139,7 +141,13 @@ def searchAndPrint(stdout)


def pickupRecords def pickupRecords
raise unless @option.dbFile raise unless @option.dbFile
searchDatabase records = searchDatabase
@result.time_stop
records
end

def time_s
Gren::Util::time_s(@result.time)
end end


def searchFromDB(stdout, dir) def searchFromDB(stdout, dir)
Expand All @@ -151,7 +159,17 @@ def searchFromDB(stdout, dir)


# 検索にヒットしたファイルを実際に検索 # 検索にヒットしたファイルを実際に検索
begin begin
if (@patterns.size > 0) if (@option.gotoline > 0)
records.each do |record|
if FileTest.exist?(record.path)
relative_path = Milkode::Util::relative_path(record.path, Dir.pwd).to_s
line = getTextLineno(relative_path, @option.gotoline)
stdout.puts "#{relative_path}:#{@option.gotoline}:#{line}" if (line)
@result.match_file_count += 1
raise MatchCountOverError if (0 < @option.matchCountLimit && @option.matchCountLimit <= @result.match_file_count)
end
end
elsif (@patterns.size > 0)
records.each do |record| records.each do |record|
if (@option.groongaOnly) if (@option.groongaOnly)
searchGroongaOnly(stdout, record) searchGroongaOnly(stdout, record)
Expand All @@ -172,6 +190,20 @@ def searchFromDB(stdout, dir)
end end
end end


def getTextLineno(path, no)
index = no - 1

open(path, "r") do |file|
lines = file2data(file)

if (index < lines.size)
lines[index]
else
nil
end
end
end

def searchDatabase def searchDatabase
# 全てのパターンを検索 # 全てのパターンを検索
table = @documents.select do |record| table = @documents.select do |record|
Expand Down
51 changes: 45 additions & 6 deletions lib/milkode/grep/cli_grep.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -34,16 +34,27 @@ def self.execute(stdout, arguments=[])
-l, Change state 'line'. (Match line words.) -l, Change state 'line'. (Match line words.)
-k, Change state 'keyword'. (Match file-content or file-path.) -k, Change state 'keyword'. (Match file-content or file-path.)
First state is 'line'. First state is 'line'.
Example: gmilk line1 line2 -k keyword1 keyword2 -l line3 -k keyword3 ... Example:
gmilk line1 line2 -k keyword1 keyword2 -l line3 -k keyword3 ...
Gotoline:
-g, Go to line mode.
Enter a file name and line number. If you omit the line number jumps to the line:1.
Example:
gmilk -g database lib 7
lib/database.rb:7:xxxxxxxxxxxxxxx
database_lib.rb:7:yyyyyyyyyyyyyyy
Normal: Normal:
EOF EOF
opt.on('-a', '--all', 'Search all package.') {|v| my_option[:all] = true } opt.on('-a', '--all', 'Search all package.') {|v| my_option[:all] = true }
opt.on('-c', '--count', 'Disp count num.') {|v| my_option[:count] = true }
opt.on('--cache', 'Search only db.') {|v| option.groongaOnly = true } opt.on('--cache', 'Search only db.') {|v| option.groongaOnly = true }
opt.on('--color', 'Color highlight.') {|v| option.colorHighlight = true} opt.on('--color', 'Color highlight.') {|v| option.colorHighlight = true}
opt.on('--cs', '--case-sensitive', 'Case sensitivity.') {|v| my_option[:case_sensitive] = true } opt.on('--cs', '--case-sensitive', 'Case sensitivity.') {|v| my_option[:case_sensitive] = true }
opt.on('-d DIR', '--directory DIR', 'Start directory. (deafult:".")') {|v| current_dir = File.expand_path(v); my_option[:find_mode] = true} opt.on('-d DIR', '--directory DIR', 'Start directory. (deafult:".")') {|v| current_dir = File.expand_path(v); my_option[:find_mode] = true}
opt.on('-f FILE_PATH', '--file-path FILE_PATH', 'File path. (Enable multiple call)') {|v| option.filePatterns << v; my_option[:find_mode] = true } opt.on('-f FILE_PATH', '--file-path FILE_PATH', 'File path. (Enable multiple call)') {|v| option.filePatterns << v; my_option[:find_mode] = true }
opt.on('-i', '--ignore', 'Ignore case.') {|v| option.ignoreCase = true}
opt.on('-n NUM', 'Limits the number of match to show.') {|v| option.matchCountLimit = v.to_i } opt.on('-n NUM', 'Limits the number of match to show.') {|v| option.matchCountLimit = v.to_i }
opt.on('--no-snip', 'There being a long line, it does not snip.') {|v| option.noSnip = true } opt.on('--no-snip', 'There being a long line, it does not snip.') {|v| option.noSnip = true }
opt.on('-p PACKAGE', '--package PACKAGE', 'Specify search package.') {|v| setup_package(option, my_option, v) } opt.on('-p PACKAGE', '--package PACKAGE', 'Specify search package.') {|v| setup_package(option, my_option, v) }
Expand All @@ -60,11 +71,20 @@ def self.execute(stdout, arguments=[])
ap.after ap.after


arguments = ap.arguments arguments = ap.arguments

option.keywords = ap.keywords option.keywords = ap.keywords
my_option[:find_mode] = true unless ap.keywords.empty? my_option[:find_mode] = true unless ap.keywords.empty?


unless ap.gotowords.empty?
r = Util::parse_gotoline(ap.gotowords)
option.filePatterns += r[0]
option.gotoline = r[1]
my_option[:find_mode] = true
end

# p ap.arguments # p ap.arguments
# p ap.keywords # p ap.keywords
# p ap.gotowords


rescue NotFoundPackage => e rescue NotFoundPackage => e
stdout.puts "fatal: Not found package '#{e}'." stdout.puts "fatal: Not found package '#{e}'."
Expand Down Expand Up @@ -102,9 +122,18 @@ def self.execute(stdout, arguments=[])
stdout.puts stdout.puts
end end


# findgrep if (my_option[:count])
findGrep = FindGrep::FindGrep.new(arguments, option) # count mode
findGrep.searchAndPrint(stdout) option.isSilent = true
findGrep = FindGrep::FindGrep.new(arguments, option)
records = findGrep.pickupRecords
# stdout.puts "#{records.size} records (#{findGrep.time_s})"
stdout.puts "#{records.size} records"
else
# search mode
findGrep = FindGrep::FindGrep.new(arguments, option)
findGrep.searchAndPrint(stdout)
end
else else
stdout.print opt.help stdout.print opt.help
end end
Expand Down Expand Up @@ -140,17 +169,20 @@ def self.yaml_load
class ArgumentParser class ArgumentParser
attr_reader :arguments attr_reader :arguments
attr_reader :keywords attr_reader :keywords
attr_reader :gotowords


def initialize(arguments) def initialize(arguments)
@arguments = arguments @arguments = arguments
@keywords = []
@state = :line @state = :line
@keywords = []
@gotowords = []
end end


def prev def prev
@arguments.map! do |v| @arguments.map! do |v|
v.gsub("-l", ":l"). v.gsub("-l", ":l").
gsub("-k", ":k") gsub("-k", ":k").
gsub("-g", ":g")
end end
end end


Expand All @@ -165,13 +197,20 @@ def after
when ":k" when ":k"
@state = :keyword @state = :keyword
next next
when ":g"
@state = :gotoline
@gotowords += result
result = []
next
end end


case @state case @state
when :line when :line
result << v result << v
when :keyword when :keyword
@keywords << v @keywords << v
when :gotoline
@gotowords << v
end end
end end


Expand Down
7 changes: 7 additions & 0 deletions test/test_util.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -70,6 +70,13 @@ def test_downcase?
assert !Milkode::Util::downcase?("Dummy") assert !Milkode::Util::downcase?("Dummy")
assert !Milkode::Util::downcase?("dummyNode") assert !Milkode::Util::downcase?("dummyNode")
end end

def test_parse_gotoline
assert_equal [['a', 'b'], 123], Milkode::Util::parse_gotoline(['a', '123', 'b'])
assert_equal [['a', '123', 'b'], 55], Milkode::Util::parse_gotoline(['a', '123', 'b', '55'])
assert_equal [['a', 'b'], 1], Milkode::Util::parse_gotoline(['a', 'b'])
assert_equal [['a'], 55], Milkode::Util::parse_gotoline(['a:55'])
end


def teardown def teardown
teardown_custom(true) teardown_custom(true)
Expand Down

0 comments on commit 17d8216

Please sign in to comment.