Class: Milkode::FindGrep
- Inherits:
-
Object
show all
- Defined in:
- lib/milkode/grep/findgrep.rb
Defined Under Namespace
Classes: MatchCountOverError, Result
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(patterns, option) ⇒ FindGrep
Returns a new instance of FindGrep.
58
59
60
61
62
63
64
65
66
67
68
69
70
|
# File 'lib/milkode/grep/findgrep.rb', line 58
def initialize(patterns, option)
@patterns = patterns
@option = option
@patternRegexps = strs2regs(patterns)
@subRegexps = strs2regs(option.patternsNot)
@orRegexps = strs2regs(option.patternsOr)
@filePatterns = (!@option.dbFile) ? strs2regs_simple(option.filePatterns) : []
@ignoreFiles = strs2regs_simple(option.ignoreFiles)
@ignoreDirs = strs2regs_simple(option.ignoreDirs)
@result = Result.new(option.directory)
open_database if @option.dbFile
require 'termcolor' if @option.colorHighlight
end
|
Instance Attribute Details
#documents ⇒ Object
Returns the value of attribute documents.
56
57
58
|
# File 'lib/milkode/grep/findgrep.rb', line 56
def documents
@documents
end
|
Class Method Details
.file2lines(file, kcode) ⇒ Object
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
|
# File 'lib/milkode/grep/findgrep.rb', line 389
def self.file2lines(file, kcode)
data = file.read
unless Milkode::Util.ruby19?
if (kcode != Kconv::NOCONV)
file_kcode = Kconv::guess(data)
if (file_kcode != kcode)
data = data.kconv(kcode, file_kcode)
end
end
else
data = data.kconv(kcode, Kconv::guess(data))
end
data = data.split($/)
end
|
Instance Method Details
#convert_path(path) ⇒ Object
446
447
448
449
450
451
452
|
# File 'lib/milkode/grep/findgrep.rb', line 446
def convert_path(path)
unless @option.expand_path
Milkode::Util.relative_path(path, Dir.pwd).to_s
else
path
end
end
|
#first_condition(match_datas, sub_matchs, or_matchs) ⇒ Object
438
439
440
441
442
443
444
|
# File 'lib/milkode/grep/findgrep.rb', line 438
def first_condition(match_datas, sub_matchs, or_matchs)
unless match_datas.empty?
match_datas[0]
else
or_matchs[0]
end
end
|
#getTextLineno(path, no) ⇒ Object
197
198
199
200
201
202
203
204
205
206
207
208
209
|
# File 'lib/milkode/grep/findgrep.rb', line 197
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
|
#open_database ⇒ Object
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
|
# File 'lib/milkode/grep/findgrep.rb', line 72
def open_database()
dbfile = Pathname(File.expand_path(@option.dbFile))
if dbfile.exist?
if !@grndb || @grndb.closed?
@grndb = Milkode::GroongaDatabase.new
@grndb.open_file(dbfile.to_s)
@documents = @grndb.documents
puts "open : #{dbfile.to_s} open." unless @option.isSilent
end
else
raise "error : #{dbfile.to_s} not found!!"
end
end
|
#pickupRecords ⇒ Object
150
151
152
153
154
155
|
# File 'lib/milkode/grep/findgrep.rb', line 150
def pickupRecords
raise unless @option.dbFile
records = searchDatabase
@result.time_stop
records
end
|
#searchAndPrint(stdout) ⇒ Object
111
112
113
114
|
# File 'lib/milkode/grep/findgrep.rb', line 111
def searchAndPrint(stdout)
records = searchDatabase
searchAndPrint2(stdout, records)
end
|
#searchAndPrint2(stdout, records) ⇒ Object
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
|
# File 'lib/milkode/grep/findgrep.rb', line 116
def searchAndPrint2(stdout, records)
unless (@option.dbFile)
searchFromDir(stdout, @option.directory, 0)
else
searchFromDB(stdout, records, @option.directory)
end
@result.time_stop
if (!@option.isSilent && !@option.dispHtml)
if (@option.debugMode)
stdout.puts
stdout.puts "--- search --------"
print_fpaths stdout, @result.search_files
stdout.puts "--- match --------"
print_fpaths stdout, @result.match_files
stdout.puts "--- ignore-file --------"
print_fpaths stdout, @result.ignore_files
stdout.puts "--- ignore-dir --------"
print_fpaths stdout, @result.prune_dirs
stdout.puts "--- unreadable --------"
print_fpaths stdout, @result.unreadable_files
end
unless (@option.colorHighlight)
stdout.puts
else
stdout.puts HighLine::REVERSE + "------------------------------------------------------------" + HighLine::CLEAR
end
@result.print(stdout)
end
end
|
#searchDatabase ⇒ Object
211
212
213
214
215
216
217
218
219
220
221
222
223
|
# File 'lib/milkode/grep/findgrep.rb', line 211
def searchDatabase
@documents.search(
:patterns => @patterns,
:keywords => @option.keywords,
:paths => @option.filePatterns,
:packages => @option.packages,
:strict_packages => @option.strict_packages,
:suffixs => @option.suffixs
)
end
|
#searchFromDB(stdout, records, dir) ⇒ Object
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
|
# File 'lib/milkode/grep/findgrep.rb', line 161
def searchFromDB(stdout, records, dir)
stdout.puts "Found : #{records.size} records." if (!@option.dispHtml && !@option.isSilent)
begin
if (@option.gotoline > 0)
records.each do |record|
if FileTest.exist?(record.path)
relative_path = convert_path(record.path)
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)
else
searchFile(stdout, record.path, record.path) if FileTest.exist?(record.path)
end
end
else
records.each do |record|
path = record.path
stdout.puts convert_path(path)
@result.match_file_count += 1
raise MatchCountOverError if (0 < @option.matchCountLimit && @option.matchCountLimit <= @result.match_file_count)
end
end
rescue MatchCountOverError
end
end
|
#strs2regs(strs) ⇒ Object
89
90
91
92
93
94
95
96
97
98
99
|
# File 'lib/milkode/grep/findgrep.rb', line 89
def strs2regs(strs)
regs = []
strs.each do |v|
option = 0
option |= Regexp::IGNORECASE if (@option.ignoreCase || (!@option.caseSensitive && Milkode::Util.downcase?(v)))
regs << Regexp.new(Regexp.escape(v), option)
end
regs
end
|
#strs2regs_simple(strs) ⇒ Object
101
102
103
104
105
106
107
108
109
|
# File 'lib/milkode/grep/findgrep.rb', line 101
def strs2regs_simple(strs)
regs = []
strs.each do |v|
regs << Regexp.new(Regexp.escape(v), 0)
end
regs
end
|
#time_s ⇒ Object
157
158
159
|
# File 'lib/milkode/grep/findgrep.rb', line 157
def time_s
Gren::Util.time_s(@result.time)
end
|