gren 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
data/History.txt CHANGED
@@ -1,9 +1,21 @@
1
1
  === 0.2.0 2010/08/xx
2
2
 
3
+ Commin soon!!
4
+
3
5
  !!!公開前に...
4
6
  英語にする
5
7
  文章を綺麗にする
6
8
 
9
+ === 0.1.3 2010/08/13
10
+
11
+ * 含まれない検索、OR検索に対応
12
+ * --subで指定したキーワードは''含まれない''
13
+ * --orで指定したキーワードは''いずれかを含む''
14
+
15
+ * 自動エンコードに対応
16
+ * ファイルの文字コードがばらばらでも、自動的にシェルで使用している文字コードにあわせてくれるようにした
17
+ * -e, --encode ENCODE Specify encode(none, auto, jis, sjis, euc, ascii, utf8, utf16). Default is "auto"
18
+
7
19
  === 0.1.2 2010/08/05
8
20
 
9
21
  * Bug fix.
@@ -11,7 +23,7 @@
11
23
 
12
24
  === 0.1.1 2010/08/04
13
25
 
14
- * --depth, --this で探索する階層数を指定出来るように
26
+ * --depth, --this(== "--depth 0")、探索する階層数を指定出来るように
15
27
  * -sでサイレントモード、マッチした行のみを表示する
16
28
  * -f, --if, --idオプションを何個でも設定出来るように
17
29
  * -cで色付き表示
data/Manifest.txt CHANGED
@@ -1,16 +1,18 @@
1
- History.txt
2
- Manifest.txt
3
- PostInstall.txt
4
- README.rdoc
5
- Rakefile
6
- bin/gren
7
- lib/gren.rb
8
- lib/gren/cli.rb
9
- lib/gren/findgrep.rb
10
- lib/gren/result.rb
11
- script/console
12
- script/destroy
13
- script/generate
14
- test/test_gren.rb
15
- test/test_gren_cli.rb
16
- test/test_helper.rb
1
+ History.txt
2
+ Manifest.txt
3
+ PostInstall.txt
4
+ README.rdoc
5
+ Rakefile
6
+ bin/gren
7
+ lib/gren.rb
8
+ lib/gren/cli.rb
9
+ lib/gren/findgrep.rb
10
+ lib/gren/result.rb
11
+ lib/platform.rb
12
+ rake_rdoc_custom.rb
13
+ script/console
14
+ script/destroy
15
+ script/generate
16
+ test/test_gren.rb
17
+ test/test_gren_cli.rb
18
+ test/test_helper.rb
data/bin/gren CHANGED
@@ -7,5 +7,5 @@ require 'rubygems'
7
7
  require File.expand_path(File.dirname(__FILE__) + "/../lib/gren")
8
8
  require "gren/cli"
9
9
 
10
- Version = "0.1.2"
10
+ Version = "0.1.3"
11
11
  Gren::CLI.execute(STDOUT, ARGV)
data/lib/gren.rb CHANGED
@@ -2,5 +2,5 @@ $:.unshift(File.dirname(__FILE__)) unless
2
2
  $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
3
3
 
4
4
  module Gren
5
- VERSION = '0.1.2'
5
+ VERSION = '0.1.3'
6
6
  end
data/lib/gren/cli.rb CHANGED
@@ -6,24 +6,27 @@ module Gren
6
6
  class CLI
7
7
  def self.execute(stdout, arguments=[])
8
8
  # オプション
9
- option = FindGrep::Option.new(".", -1, false, false, false, false, [], [], [])
9
+ option = FindGrep::DEFAULT_OPTION
10
10
 
11
11
  # オプション解析
12
12
  opt = OptionParser.new("#{File.basename($0)} [option] pattern")
13
+ opt.on('--sub PATTERN', 'Keyword is not included.') {|v| option.keywordsSub << v}
14
+ opt.on('--or PATTERN', 'Either of keyword is contained.') {|v| option.keywordsOr << v}
13
15
  opt.on('-d DIR', '--directory DIR', 'Start directory. (deafult:".")') {|v| option.directory = v}
14
16
  opt.on('--depth DEPTH', 'Limit search depth. ') {|v| option.depth = v.to_i}
15
17
  opt.on('--this', '"--depth 0"') {|v| option.depth = 0}
16
18
  opt.on('-i', '--ignore', 'Ignore case.') {|v| option.ignoreCase = true}
17
19
  opt.on('-s', '--silent', 'Silent. Display match line only.') {|v| option.isSilent = true}
18
20
  opt.on('--debug', 'Debug display.') {|v| option.debugMode = true}
19
- opt.on('-c', 'Color highlight.') {|v| option.colorHighlight = true}
21
+ opt.on('-c', '--color', 'Color highlight.') {|v| option.colorHighlight = true}
20
22
  opt.on('-f REGEXP', '--file-regexp REGEXP', 'Search file regexp. (Enable multiple call)') {|v| option.filePatterns << v}
21
23
  opt.on('--if REGEXP', '--ignore-file REGEXP', 'Ignore file pattern. (Enable multiple call)') {|v| option.ignoreFiles << v}
22
24
  opt.on('--id REGEXP', '--ignore-dir REGEXP', 'Ignore dir pattern. (Enable multiple call)') {|v| option.ignoreDirs << v}
25
+ opt.on('-e ENCODE', '--encode ENCODE', 'Specify encode(none, auto, jis, sjis, euc, ascii, utf8, utf16). Default is "auto"') {|v| setupEncodeOption(option, v) }
23
26
  opt.parse!(arguments)
24
27
 
25
28
  # 検索オブジェクトの生成
26
- if (ARGV.size > 0)
29
+ if (arguments.size > 0 || option.keywordsOr.size > 0)
27
30
  findGrep = FindGrep.new(arguments, option)
28
31
  findGrep.searchAndPrint(stdout)
29
32
  else
@@ -31,5 +34,31 @@ module Gren
31
34
  end
32
35
 
33
36
  end
37
+
38
+ def self.setupEncodeOption(option, encode)
39
+ case encode.downcase
40
+ when 'none'
41
+ option.kcode = Kconv::NOCONV
42
+ when 'auto'
43
+ option.kcode = Platform.get_shell_kcode
44
+ when 'jis'
45
+ option.kcode = Kconv::JIS
46
+ when 'sjis'
47
+ option.kcode = Kconv::SJIS
48
+ when 'euc'
49
+ option.kcode = Kconv::EUC
50
+ when 'ascii'
51
+ option.kcode = Kconv::ASCII
52
+ when 'utf8'
53
+ option.kcode = Kconv::UTF8
54
+ when 'utf16'
55
+ option.kcode = Kconv::UTF16
56
+ else
57
+ puts "Invalid encode."
58
+ puts " none, auto, jis, sjis, euc, ascii, utf8, utf16"
59
+ exit(-1)
60
+ end
61
+ end
62
+
34
63
  end
35
64
  end
data/lib/gren/findgrep.rb CHANGED
@@ -3,13 +3,17 @@ require 'find'
3
3
  require File.join(File.dirname(__FILE__), 'result')
4
4
  require 'rubygems'
5
5
  require 'termcolor'
6
+ require 'kconv'
7
+ require File.join(File.dirname(__FILE__), '../platform')
6
8
 
7
9
  module Gren
8
10
  class FindGrep
9
11
  IGNORE_FILE = /(\A#.*#\Z)|(~\Z)|(\A\.#)/
10
12
  IGNORE_DIR = /(\A\.svn\Z)|(\A\.git\Z)|(\ACVS\Z)/
11
13
 
12
- Option = Struct.new(:directory,
14
+ Option = Struct.new(:keywordsSub,
15
+ :keywordsOr,
16
+ :directory,
13
17
  :depth,
14
18
  :ignoreCase,
15
19
  :colorHighlight,
@@ -17,11 +21,27 @@ module Gren
17
21
  :debugMode,
18
22
  :filePatterns,
19
23
  :ignoreFiles,
20
- :ignoreDirs)
24
+ :ignoreDirs,
25
+ :kcode)
26
+
27
+ DEFAULT_OPTION = Option.new([],
28
+ [],
29
+ ".",
30
+ -1,
31
+ false,
32
+ false,
33
+ false,
34
+ false,
35
+ [],
36
+ [],
37
+ [],
38
+ Platform.get_shell_kcode)
21
39
 
22
40
  def initialize(patterns, option)
23
41
  @option = option
24
42
  @patternRegexps = strs2regs(patterns, @option.ignoreCase)
43
+ @subRegexps = strs2regs(option.keywordsSub, @option.ignoreCase)
44
+ @orRegexps = strs2regs(option.keywordsOr, @option.ignoreCase)
25
45
  @filePatterns = strs2regs(option.filePatterns)
26
46
  @ignoreFiles = strs2regs(option.ignoreFiles)
27
47
  @ignoreDirs = strs2regs(option.ignoreDirs)
@@ -165,16 +185,15 @@ module Gren
165
185
 
166
186
  open(fpath, "r") { |file|
167
187
  match_file = false
168
- file.each() { |line|
169
- line.chomp!
170
188
 
189
+ file2data(file).each_with_index { |line, index|
171
190
  result, match_datas = match?(line)
172
191
 
173
192
  if ( result )
174
193
  unless (@option.colorHighlight)
175
- stdout.puts "#{fpath_disp}:#{file.lineno}:#{line}"
194
+ stdout.puts "#{fpath_disp}:#{index + 1}:#{line}"
176
195
  else
177
- header = "#{fpath_disp}:#{file.lineno}"
196
+ header = "#{fpath_disp}:#{index + 1}"
178
197
 
179
198
  begin
180
199
  stdout.puts TermColor.parse("<34>#{header}</34>:") + coloring(line, match_datas)
@@ -196,10 +215,37 @@ module Gren
196
215
  end
197
216
  private :searchFile
198
217
 
218
+ def file2data(file)
219
+ data = file.read
220
+
221
+ if (@option.kcode != Kconv::NOCONV)
222
+ file_kcode = Kconv::guess(data)
223
+
224
+ if (file_kcode != @option.kcode)
225
+ # puts "encode!! #{fpath} : #{@option.kcode} <- #{file_kcode}"
226
+ data = data.kconv(@option.kcode, file_kcode)
227
+ end
228
+ end
229
+
230
+ data = data.split("\n");
231
+ end
232
+ private :file2data
233
+
199
234
  def match?(line)
200
235
  match_datas = []
201
236
  @patternRegexps.each {|v| match_datas << v.match(line)}
202
- return match_datas.all?, match_datas
237
+
238
+ sub_matchs = []
239
+ @subRegexps.each {|v| sub_matchs << v.match(line)}
240
+
241
+ or_matchs = []
242
+ @orRegexps.each {|v| or_matchs << v.match(line)}
243
+
244
+ result = match_datas.all? && !sub_matchs.any? && (or_matchs.empty? || or_matchs.any?)
245
+ result_match = match_datas + or_matchs
246
+ result_match.delete(nil)
247
+
248
+ return result, result_match
203
249
  end
204
250
  private :match?
205
251
 
data/lib/platform.rb ADDED
@@ -0,0 +1,17 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'kconv'
3
+
4
+ # LinuxやBSD等にも対応予定(その場合のエンコードって何が適切なんだろ?EUC?UTF8?)
5
+ class Platform
6
+ def self.windows_os?
7
+ RUBY_PLATFORM =~ /mswin(?!ce)|mingw|cygwin|bccwin/
8
+ end
9
+
10
+ def self.get_shell_kcode
11
+ if windows_os?
12
+ Kconv::SJIS
13
+ else
14
+ Kconv::UTF8
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,13 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ require 'rake'
4
+ require 'rake/rdoctask'
5
+
6
+ # RDocの文字コードをutf8に
7
+ Rake::RDocTask.class_eval {
8
+ alias :_option_list :option_list
9
+ def option_list
10
+ _option_list + ['-c', 'utf8']
11
+ end
12
+ }
13
+
data/script/console CHANGED
File without changes
data/script/destroy CHANGED
File without changes
data/script/generate CHANGED
File without changes
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 1
8
- - 2
9
- version: 0.1.2
8
+ - 3
9
+ version: 0.1.3
10
10
  platform: ruby
11
11
  authors:
12
12
  - ongaeshi
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-08-06 00:00:00 +09:00
17
+ date: 2010-08-13 00:00:00 +09:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -55,8 +55,8 @@ dependencies:
55
55
  segments:
56
56
  - 2
57
57
  - 6
58
- - 1
59
- version: 2.6.1
58
+ - 0
59
+ version: 2.6.0
60
60
  type: :development
61
61
  version_requirements: *id003
62
62
  description: gren is a next grep tool.
@@ -82,6 +82,8 @@ files:
82
82
  - lib/gren/cli.rb
83
83
  - lib/gren/findgrep.rb
84
84
  - lib/gren/result.rb
85
+ - lib/platform.rb
86
+ - rake_rdoc_custom.rb
85
87
  - script/console
86
88
  - script/destroy
87
89
  - script/generate