Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge branch 'develop'

  • Loading branch information...
commit 17d821664ec0dde49e4b66a521fe59b1597db81e 2 parents a85fd18 + b1daf7a
@ongaeshi authored
View
7 HISTORY.ja.rdoc
@@ -1,3 +1,10 @@
+=== 0.4.0 2012/02/02
+
+* gmilk
+ * -iオプションの復活
+ * -gオプションの追加(Goto line mode)
+ * -cオプションを追加(マッチ数のみを表示)
+
=== 0.3.0 2012/01/20
* gmilk
View
7 HISTORY.rdoc
@@ -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
* gmilk
View
2  VERSION
@@ -1 +1 @@
-0.3.0
+0.4.0
View
2  bin/gmilk
@@ -6,5 +6,5 @@
require 'rubygems'
require 'milkode/grep/cli_grep'
-Version = "0.3.0"
+Version = "0.4.0"
Milkode::CLI_Grep.execute(STDOUT, ARGV)
View
2  bin/milk
@@ -6,5 +6,5 @@
require 'rubygems'
require 'milkode/cdstk/cli_cdstk'
-Version = "0.3.0"
+Version = "0.4.0"
Milkode::CLI_Cdstk.execute(STDOUT, ARGV)
View
2  lib/milkode/cdweb/app.rb
@@ -18,7 +18,7 @@
set :haml, :format => :html5
get '/' do
- @version = "0.3.0"
+ @version = "0.4.0"
@package_num = Database.instance.fileList('').size
@file_num = Database.instance.fileNum
haml :index
View
29 lib/milkode/common/util.rb
@@ -103,7 +103,34 @@ def normalize_filename(str)
def downcase?(str)
str == str.downcase
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
View
40 lib/milkode/findgrep/findgrep.rb
@@ -35,7 +35,8 @@ class FindGrep
:isMatchFile,
:dispHtml,
:matchCountLimit,
- :keywords)
+ :keywords,
+ :gotoline)
DEFAULT_OPTION = Option.new([],
[],
@@ -57,7 +58,8 @@ class FindGrep
false,
false,
-1,
- [])
+ [],
+ -1)
class MatchCountOverError < RuntimeError ; end
@@ -139,7 +141,13 @@ def searchAndPrint(stdout)
def pickupRecords
raise unless @option.dbFile
- searchDatabase
+ records = searchDatabase
+ @result.time_stop
+ records
+ end
+
+ def time_s
+ Gren::Util::time_s(@result.time)
end
def searchFromDB(stdout, dir)
@@ -151,7 +159,17 @@ def searchFromDB(stdout, dir)
# 検索にヒットしたファイルを実際に検索
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|
if (@option.groongaOnly)
searchGroongaOnly(stdout, record)
@@ -172,6 +190,20 @@ def searchFromDB(stdout, dir)
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
# 全てのパターンを検索
table = @documents.select do |record|
View
51 lib/milkode/grep/cli_grep.rb
@@ -34,16 +34,27 @@ def self.execute(stdout, arguments=[])
-l, Change state 'line'. (Match line words.)
-k, Change state 'keyword'. (Match file-content or file-path.)
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:
EOF
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('--color', 'Color highlight.') {|v| option.colorHighlight = 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('-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('--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) }
@@ -60,11 +71,20 @@ def self.execute(stdout, arguments=[])
ap.after
arguments = ap.arguments
+
option.keywords = ap.keywords
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.keywords
+ # p ap.gotowords
rescue NotFoundPackage => e
stdout.puts "fatal: Not found package '#{e}'."
@@ -102,9 +122,18 @@ def self.execute(stdout, arguments=[])
stdout.puts
end
- # findgrep
- findGrep = FindGrep::FindGrep.new(arguments, option)
- findGrep.searchAndPrint(stdout)
+ if (my_option[:count])
+ # count mode
+ 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
stdout.print opt.help
end
@@ -140,17 +169,20 @@ def self.yaml_load
class ArgumentParser
attr_reader :arguments
attr_reader :keywords
+ attr_reader :gotowords
def initialize(arguments)
@arguments = arguments
- @keywords = []
@state = :line
+ @keywords = []
+ @gotowords = []
end
def prev
@arguments.map! do |v|
v.gsub("-l", ":l").
- gsub("-k", ":k")
+ gsub("-k", ":k").
+ gsub("-g", ":g")
end
end
@@ -165,6 +197,11 @@ def after
when ":k"
@state = :keyword
next
+ when ":g"
+ @state = :gotoline
+ @gotowords += result
+ result = []
+ next
end
case @state
@@ -172,6 +209,8 @@ def after
result << v
when :keyword
@keywords << v
+ when :gotoline
+ @gotowords << v
end
end
View
7 test/test_util.rb
@@ -70,6 +70,13 @@ def test_downcase?
assert !Milkode::Util::downcase?("Dummy")
assert !Milkode::Util::downcase?("dummyNode")
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
teardown_custom(true)
Please sign in to comment.
Something went wrong with that request. Please try again.