gren 0.1.3 → 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,18 +1,22 @@
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
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/gren/util.rb
12
+ lib/platform.rb
13
+ lib/string_snip.rb
14
+ rake_rdoc_custom.rb
15
+ script/console
16
+ script/destroy
17
+ script/generate
18
+ test/test_gren.rb
19
+ test/test_gren_cli.rb
20
+ test/test_gren_util.rb
21
+ test/test_helper.rb
22
+ test/test_string_snip.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.3"
10
+ Version = "0.1.4"
11
11
  Gren::CLI.execute(STDOUT, ARGV)
@@ -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.3'
5
+ VERSION = '0.1.4'
6
6
  end
@@ -23,6 +23,7 @@ module Gren
23
23
  opt.on('--if REGEXP', '--ignore-file REGEXP', 'Ignore file pattern. (Enable multiple call)') {|v| option.ignoreFiles << v}
24
24
  opt.on('--id REGEXP', '--ignore-dir REGEXP', 'Ignore dir pattern. (Enable multiple call)') {|v| option.ignoreDirs << v}
25
25
  opt.on('-e ENCODE', '--encode ENCODE', 'Specify encode(none, auto, jis, sjis, euc, ascii, utf8, utf16). Default is "auto"') {|v| setupEncodeOption(option, v) }
26
+ opt.on('--no-snip', 'There being a long line, it does not snip.') {|v| option.noSnip = true }
26
27
  opt.parse!(arguments)
27
28
 
28
29
  # 検索オブジェクトの生成
@@ -5,12 +5,13 @@ require 'rubygems'
5
5
  require 'termcolor'
6
6
  require 'kconv'
7
7
  require File.join(File.dirname(__FILE__), '../platform')
8
+ require File.join(File.dirname(__FILE__), 'util')
8
9
 
9
10
  module Gren
10
11
  class FindGrep
11
12
  IGNORE_FILE = /(\A#.*#\Z)|(~\Z)|(\A\.#)/
12
13
  IGNORE_DIR = /(\A\.svn\Z)|(\A\.git\Z)|(\ACVS\Z)/
13
-
14
+
14
15
  Option = Struct.new(:keywordsSub,
15
16
  :keywordsOr,
16
17
  :directory,
@@ -22,7 +23,8 @@ module Gren
22
23
  :filePatterns,
23
24
  :ignoreFiles,
24
25
  :ignoreDirs,
25
- :kcode)
26
+ :kcode,
27
+ :noSnip)
26
28
 
27
29
  DEFAULT_OPTION = Option.new([],
28
30
  [],
@@ -35,7 +37,8 @@ module Gren
35
37
  [],
36
38
  [],
37
39
  [],
38
- Platform.get_shell_kcode)
40
+ Platform.get_shell_kcode,
41
+ false)
39
42
 
40
43
  def initialize(patterns, option)
41
44
  @option = option
@@ -83,7 +86,7 @@ module Gren
83
86
  unless (@option.colorHighlight)
84
87
  stdout.puts
85
88
  else
86
- stdout.puts TermColor.parse("<7>------------------------------------------------------------</7>")
89
+ stdout.puts HighLine::REVERSE + "------------------------------------------------------------" + HighLine::CLEAR
87
90
  end
88
91
 
89
92
  @result.print(stdout)
@@ -190,16 +193,13 @@ module Gren
190
193
  result, match_datas = match?(line)
191
194
 
192
195
  if ( result )
196
+ header = "#{fpath_disp}:#{index + 1}:"
197
+ line = Util::snip(line, match_datas) unless (@option.noSnip)
198
+
193
199
  unless (@option.colorHighlight)
194
- stdout.puts "#{fpath_disp}:#{index + 1}:#{line}"
200
+ stdout.puts header + line
195
201
  else
196
- header = "#{fpath_disp}:#{index + 1}"
197
-
198
- begin
199
- stdout.puts TermColor.parse("<34>#{header}</34>:") + coloring(line, match_datas)
200
- rescue REXML::ParseException
201
- stdout.puts header + line
202
- end
202
+ stdout.puts HighLine::BLUE + header + HighLine::CLEAR + Util::coloring(line, match_datas)
203
203
  end
204
204
 
205
205
  unless match_file
@@ -248,14 +248,5 @@ module Gren
248
248
  return result, result_match
249
249
  end
250
250
  private :match?
251
-
252
- def coloring(line, match_datas)
253
- match_datas.each do |m|
254
- line = line.split(m[0]).join(TermColor.parse("<42>#{m[0]}</42>"))
255
- end
256
- line
257
- end
258
- private :coloring
259
-
260
251
  end
261
252
  end
@@ -0,0 +1,39 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ require File.join(File.dirname(__FILE__), '../string_snip')
4
+
5
+ module Gren
6
+ class Util
7
+ MAX_LINE_SIZE = 256
8
+ HEADER_SIZE = 32
9
+ MARGIN_SIZE = 32
10
+ DELIMITER = '<<snip>>'
11
+
12
+ def self.snip(str, match_datas)
13
+ return str if (str.size <= MAX_LINE_SIZE)
14
+
15
+ ranges = []
16
+ ranges << (0..HEADER_SIZE-1)
17
+ ranges << (-HEADER_SIZE..-1)
18
+
19
+ match_datas.each do |m|
20
+ ranges << (m.begin(0)-MARGIN_SIZE..m.end(0)+MARGIN_SIZE)
21
+ end
22
+
23
+ snipper = StringSnip.new(MAX_LINE_SIZE, DELIMITER)
24
+ return snipper.snip(str, ranges)
25
+ end
26
+
27
+ def self.coloring(line, match_datas)
28
+ match_datas.each do |m|
29
+ line = line.split(m[0]).join(HighLine::ON_GREEN + m[0] + HighLine::CLEAR)
30
+ end
31
+
32
+ line = line.split(DELIMITER).join(HighLine::ON_CYAN + DELIMITER + HighLine::CLEAR)
33
+
34
+ line
35
+ end
36
+ end
37
+ end
38
+
39
+
@@ -0,0 +1,61 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ class StringSnip
4
+ def initialize(size = 256, delimiter = '<<snip>>', pri = nil)
5
+ @size = size
6
+ @delimiter = delimiter
7
+ @pri = pri
8
+ end
9
+
10
+ def snip(str, ranges)
11
+ @str = str
12
+ @ranges = ranges
13
+
14
+ # no snip
15
+ return @str if (@str.size <= @size)
16
+
17
+ # snip
18
+ @ranges = StringSnip::ranges_conv(@ranges, @str)
19
+ @ranges = StringSnip::ranges_sort(@ranges)
20
+ @ranges = StringSnip::ranges_compound(@ranges)
21
+
22
+ # result
23
+ results = []
24
+ @ranges.each {|r| results << @str[r] }
25
+ return results.join(@delimiter)
26
+ end
27
+
28
+ def self.ranges_conv(ranges, str)
29
+ ranges.map {|i| index_conv(str, i.begin)..index_conv(str, i.end)}
30
+ end
31
+
32
+ def self.index_conv(str, value)
33
+ if (value < 0)
34
+ str.size + value
35
+ else
36
+ value
37
+ end
38
+ end
39
+
40
+ def self.ranges_sort(ranges)
41
+ ranges.sort_by{|i| i.begin}
42
+ end
43
+
44
+ def self.ranges_compound(ranges)
45
+ result = []
46
+
47
+ index = 0
48
+ while (ranges.size > 0)
49
+ if (ranges.size > 1 && ranges[0].end + 1 >= ranges[1].begin)
50
+ v1, v2 = ranges.shift(2)
51
+ ranges.unshift v1.begin..((v1.end > v2.end) ? v1.end : v2.end)
52
+ else
53
+ result << ranges.shift
54
+ end
55
+ end
56
+
57
+ result
58
+ end
59
+ end
60
+
61
+
File without changes
File without changes
File without changes
@@ -0,0 +1,32 @@
1
+ require File.dirname(__FILE__) + '/test_helper.rb'
2
+
3
+ class TestGrenUtil < Test::Unit::TestCase
4
+ def setup
5
+ end
6
+
7
+ def test_snip
8
+ str = "abcdefghijkmlnopqrstuvwxyz"
9
+ assert_equal(str, Gren::Util::snip(str, nil))
10
+
11
+ str = "123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789|123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789|12345678901234567890123456789012345678901234567890123456"
12
+ assert_equal(str, Gren::Util::snip(str, nil))
13
+
14
+ str = "123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789|123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789|123456789012345678901234567890123456789012345678901234561"
15
+ match_datas = []
16
+ match_datas << str.match(/123456789\|/)
17
+ assert_equal("12345678901234567890123456789012<<snip>>90123456789012345678901234567890123456789|123456789012345678901234567890123<<snip>>67890123456789012345678901234561", Gren::Util::snip(str, match_datas))
18
+
19
+ str = "123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789|123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789|123456789012345678901234567890123456789012345678901234561"
20
+ match_datas = []
21
+ match_datas << str.match(/123456789\|/)
22
+ match_datas << str.match(/34567/)
23
+ assert_equal("12345678901234567890123456789012<<snip>>90123456789012345678901234567890123456789|123456789012345678901234567890123<<snip>>67890123456789012345678901234561", Gren::Util::snip(str, match_datas))
24
+
25
+ str = "123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789|123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789|123456789012345678901234567890123456789012345678901234561"
26
+ match_datas = []
27
+ match_datas << str.match(/123456789\|/)
28
+ match_datas << str.match(/01234561/)
29
+ assert_equal("12345678901234567890123456789012<<snip>>90123456789012345678901234567890123456789|123456789012345678901234567890123<<snip>>8901234567890123456789012345678901234561", Gren::Util::snip(str, match_datas))
30
+
31
+ end
32
+ end
@@ -1,3 +1,2 @@
1
1
  require 'stringio'
2
2
  require 'test/unit'
3
- require File.dirname(__FILE__) + '/../lib/gren'
@@ -0,0 +1,29 @@
1
+ require File.dirname(__FILE__) + '/test_helper.rb'
2
+
3
+ class TestStringSnip < Test::Unit::TestCase
4
+ def setup
5
+ end
6
+
7
+ def test_ranges_compound
8
+ ranges = [0..7, 8..232, 121..150, 248..255]
9
+ assert_equal(StringSnip.ranges_compound(ranges), [0..232, 248..255])
10
+
11
+ ranges = [10..20, 22..30, 33..40]
12
+ assert_equal(StringSnip.ranges_compound(ranges), [10..20, 22..30, 33..40])
13
+
14
+ ranges = [10..30, 20..30, 30..40]
15
+ assert_equal(StringSnip.ranges_compound(ranges), [10..40])
16
+ end
17
+
18
+ def test_string_snip
19
+ str = "123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789|123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789|12345678901234567890123456789012345678901234567890123456"
20
+
21
+ snipper = StringSnip.new
22
+ snip_str = snipper.snip(str, [0..7, -8..-1])
23
+ assert_equal(snip_str, str)
24
+
25
+ snipper = StringSnip.new(64)
26
+ snip_str = snipper.snip(str, [-8..-1, 10..20, 0..7])
27
+ assert_equal(snip_str, "12345678<<snip>>12345678901<<snip>>90123456")
28
+ end
29
+ end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 1
8
- - 3
9
- version: 0.1.3
8
+ - 4
9
+ version: 0.1.4
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-13 00:00:00 +09:00
17
+ date: 2010-08-15 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
- - 0
59
- version: 2.6.0
58
+ - 1
59
+ version: 2.6.1
60
60
  type: :development
61
61
  version_requirements: *id003
62
62
  description: gren is a next grep tool.
@@ -82,14 +82,18 @@ files:
82
82
  - lib/gren/cli.rb
83
83
  - lib/gren/findgrep.rb
84
84
  - lib/gren/result.rb
85
+ - lib/gren/util.rb
85
86
  - lib/platform.rb
87
+ - lib/string_snip.rb
86
88
  - rake_rdoc_custom.rb
87
89
  - script/console
88
90
  - script/destroy
89
91
  - script/generate
90
92
  - test/test_gren.rb
91
93
  - test/test_gren_cli.rb
94
+ - test/test_gren_util.rb
92
95
  - test/test_helper.rb
96
+ - test/test_string_snip.rb
93
97
  has_rdoc: true
94
98
  homepage: http://ongaeshi.github.com/gren
95
99
  licenses: []
@@ -124,4 +128,6 @@ summary: gren is a next grep tool.
124
128
  test_files:
125
129
  - test/test_gren.rb
126
130
  - test/test_gren_cli.rb
131
+ - test/test_gren_util.rb
127
132
  - test/test_helper.rb
133
+ - test/test_string_snip.rb