codestock 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (57) hide show
  1. data/.document +5 -0
  2. data/Gemfile +12 -0
  3. data/Gemfile.lock +18 -0
  4. data/LICENSE.txt +20 -0
  5. data/README.rdoc +19 -0
  6. data/Rakefile +63 -0
  7. data/VERSION +1 -0
  8. data/bin/cdstk +12 -0
  9. data/bin/cdv +11 -0
  10. data/bin/cdview +11 -0
  11. data/bin/cdweb +11 -0
  12. data/codestock.gemspec +131 -0
  13. data/lib/codestock.rb +0 -0
  14. data/lib/common/dbdir.rb +37 -0
  15. data/lib/common/display_util.rb +62 -0
  16. data/lib/common/grenfiletest.rb +19 -0
  17. data/lib/common/grensnip.rb +37 -0
  18. data/lib/common/platform.rb +17 -0
  19. data/lib/common/string_snip.rb +61 -0
  20. data/lib/common/util.rb +98 -0
  21. data/lib/findgrep/findgrep.rb +408 -0
  22. data/lib/findgrep/result.rb +43 -0
  23. data/lib/gren/cli.rb +65 -0
  24. data/lib/gren.rb +6 -0
  25. data/lib/grendb/cli.rb +43 -0
  26. data/lib/grenweb/cli.rb +54 -0
  27. data/lib/grenweb/database.rb +182 -0
  28. data/lib/grenweb/grenweb.ru +35 -0
  29. data/lib/grenweb/grep.rb +52 -0
  30. data/lib/grenweb/help.rb +40 -0
  31. data/lib/grenweb/home.rb +40 -0
  32. data/lib/grenweb/html_renderer.rb +244 -0
  33. data/lib/grenweb/public/css/gren.css +63 -0
  34. data/lib/grenweb/public/images/gren-icon-mini.png +0 -0
  35. data/lib/grenweb/public/images/gren-icon.png +0 -0
  36. data/lib/grenweb/query.rb +82 -0
  37. data/lib/grenweb/searcher.rb +130 -0
  38. data/lib/grenweb/viewer.rb +52 -0
  39. data/lib/mkgrendb/cli.rb +89 -0
  40. data/lib/mkgrendb/cli_old.rb +49 -0
  41. data/lib/mkgrendb/grendbyaml.rb +76 -0
  42. data/lib/mkgrendb/mkgrendb.rb +296 -0
  43. data/test/file_test_utils.rb +59 -0
  44. data/test/runner.rb +11 -0
  45. data/test/test_dbdir.rb +59 -0
  46. data/test/test_gren.rb +10 -0
  47. data/test/test_gren_util.rb +34 -0
  48. data/test/test_grendbyaml.rb +109 -0
  49. data/test/test_grenweb_cli.rb +10 -0
  50. data/test/test_grenweb_database.rb +37 -0
  51. data/test/test_grenweb_html_renderer.rb +41 -0
  52. data/test/test_grenweb_query.rb +54 -0
  53. data/test/test_grenweb_searcher.rb +35 -0
  54. data/test/test_helper.rb +2 -0
  55. data/test/test_mkgrendb.rb +163 -0
  56. data/test/test_string_snip.rb +31 -0
  57. metadata +229 -0
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/Gemfile ADDED
@@ -0,0 +1,12 @@
1
+ source "http://rubygems.org"
2
+ # Add dependencies required to use your gem here.
3
+ # Example:
4
+ # gem "activesupport", ">= 2.3.5"
5
+
6
+ # Add dependencies to develop your gem here.
7
+ # Include everything needed to run rake, tests, features, etc.
8
+ group :development do
9
+ gem "bundler", "~> 1.0.0"
10
+ gem "jeweler", "~> 1.5.2"
11
+ gem "rcov", ">= 0"
12
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,18 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ git (1.2.5)
5
+ jeweler (1.5.2)
6
+ bundler (~> 1.0.0)
7
+ git (>= 1.2.5)
8
+ rake
9
+ rake (0.8.7)
10
+ rcov (0.9.9)
11
+
12
+ PLATFORMS
13
+ ruby
14
+
15
+ DEPENDENCIES
16
+ bundler (~> 1.0.0)
17
+ jeweler (~> 1.5.2)
18
+ rcov
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 ongaeshi
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,19 @@
1
+ = codestock
2
+
3
+ Description goes here.
4
+
5
+ == Contributing to codestock
6
+
7
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
8
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
9
+ * Fork the project
10
+ * Start a feature/bugfix branch
11
+ * Commit and push until you are happy with your contribution
12
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
13
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
14
+
15
+ == Copyright
16
+
17
+ Copyright (c) 2011 ongaeshi. See LICENSE.txt for
18
+ further details.
19
+
data/Rakefile ADDED
@@ -0,0 +1,63 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'rake'
11
+
12
+ require 'jeweler'
13
+ Jeweler::Tasks.new do |gem|
14
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
15
+ gem.name = "codestock"
16
+ gem.homepage = "http://github.com/ongaeshi/codestock"
17
+ gem.license = "MIT"
18
+ gem.summary = %Q{one-line summary of your gem}
19
+ gem.description = %Q{longer description of your gem}
20
+ gem.email = "ongaeshi0621@gmail.com"
21
+ gem.authors = ["ongaeshi"]
22
+
23
+ # Include your dependencies below. Runtime dependencies are required when using your gem,
24
+ gem.add_runtime_dependency 'termcolor','>= 1.2.0'
25
+ gem.add_runtime_dependency 'rroonga','>= 1.0.0'
26
+ gem.add_runtime_dependency 'rack','>=1.2.1'
27
+ gem.add_runtime_dependency 'launchy', '>=0.3.7'
28
+
29
+ # and development dependencies are only needed for development (ie running rake tasks, tests, etc)
30
+ # gem.add_development_dependency 'rspec', '> 1.2.3'
31
+ end
32
+ Jeweler::RubygemsDotOrgTasks.new
33
+
34
+ # require 'rake/testtask'
35
+ # Rake::TestTask.new(:test) do |test|
36
+ # test.libs << 'lib' << 'test'
37
+ # test.pattern = 'test/**/test_*.rb'
38
+ # test.verbose = true
39
+ # end
40
+
41
+ task :test do
42
+ puts "Sorry, use (cd test;ruby runner.rb)"
43
+ # load "test/runner.rb"
44
+ end
45
+
46
+ require 'rcov/rcovtask'
47
+ Rcov::RcovTask.new do |test|
48
+ test.libs << 'test'
49
+ test.pattern = 'test/**/test_*.rb'
50
+ test.verbose = true
51
+ end
52
+
53
+ task :default => :test
54
+
55
+ require 'rake/rdoctask'
56
+ Rake::RDocTask.new do |rdoc|
57
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
58
+
59
+ rdoc.rdoc_dir = 'rdoc'
60
+ rdoc.title = "codestock #{version}"
61
+ rdoc.rdoc_files.include('README*')
62
+ rdoc.rdoc_files.include('lib/**/*.rb')
63
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
data/bin/cdstk ADDED
@@ -0,0 +1,12 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # Created on 2010-9-12.
4
+ # Copyright (c) 2010. All rights reserved.
5
+
6
+ require 'rubygems'
7
+ require File.expand_path(File.dirname(__FILE__) + "/../lib/gren")
8
+ require "mkgrendb/cli"
9
+ #require "mkgrendb/cli_old"
10
+
11
+ Version = "0.2.4"
12
+ Mkgrendb::CLI.execute(STDOUT, ARGV)
data/bin/cdv ADDED
@@ -0,0 +1,11 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # Created on 2010-8-25.
4
+ # Copyright (c) 2010. All rights reserved.
5
+
6
+ require 'rubygems'
7
+ require File.expand_path(File.dirname(__FILE__) + "/../lib/gren")
8
+ require "grendb/cli"
9
+
10
+ Version = "0.2.4"
11
+ Grendb::CLI.execute(STDOUT, ARGV)
data/bin/cdview ADDED
@@ -0,0 +1,11 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # Created on 2010-8-25.
4
+ # Copyright (c) 2010. All rights reserved.
5
+
6
+ require 'rubygems'
7
+ require File.expand_path(File.dirname(__FILE__) + "/../lib/gren")
8
+ require "grendb/cli"
9
+
10
+ Version = "0.2.4"
11
+ Grendb::CLI.execute(STDOUT, ARGV)
data/bin/cdweb ADDED
@@ -0,0 +1,11 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # Created on 2010-10-10.
4
+ # Copyright (c) 2010. All rights reserved.
5
+
6
+ require 'rubygems'
7
+ require File.expand_path(File.dirname(__FILE__) + "/../lib/gren")
8
+ require "grenweb/cli"
9
+
10
+ Version = "0.3.1"
11
+ Grenweb::CLI.execute(STDOUT, ARGV)
data/codestock.gemspec ADDED
@@ -0,0 +1,131 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{codestock}
8
+ s.version = "0.1.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["ongaeshi"]
12
+ s.date = %q{2011-03-11}
13
+ s.description = %q{longer description of your gem}
14
+ s.email = %q{ongaeshi0621@gmail.com}
15
+ s.executables = ["cdweb", "cdv", "cdstk", "cdview"]
16
+ s.extra_rdoc_files = [
17
+ "LICENSE.txt",
18
+ "README.rdoc"
19
+ ]
20
+ s.files = [
21
+ ".document",
22
+ "Gemfile",
23
+ "Gemfile.lock",
24
+ "LICENSE.txt",
25
+ "README.rdoc",
26
+ "Rakefile",
27
+ "VERSION",
28
+ "bin/cdstk",
29
+ "bin/cdv",
30
+ "bin/cdview",
31
+ "bin/cdweb",
32
+ "codestock.gemspec",
33
+ "lib/codestock.rb",
34
+ "lib/common/dbdir.rb",
35
+ "lib/common/display_util.rb",
36
+ "lib/common/grenfiletest.rb",
37
+ "lib/common/grensnip.rb",
38
+ "lib/common/platform.rb",
39
+ "lib/common/string_snip.rb",
40
+ "lib/common/util.rb",
41
+ "lib/findgrep/findgrep.rb",
42
+ "lib/findgrep/result.rb",
43
+ "lib/gren.rb",
44
+ "lib/gren/cli.rb",
45
+ "lib/grendb/cli.rb",
46
+ "lib/grenweb/cli.rb",
47
+ "lib/grenweb/database.rb",
48
+ "lib/grenweb/grenweb.ru",
49
+ "lib/grenweb/grep.rb",
50
+ "lib/grenweb/help.rb",
51
+ "lib/grenweb/home.rb",
52
+ "lib/grenweb/html_renderer.rb",
53
+ "lib/grenweb/public/css/gren.css",
54
+ "lib/grenweb/public/images/gren-icon-mini.png",
55
+ "lib/grenweb/public/images/gren-icon.png",
56
+ "lib/grenweb/query.rb",
57
+ "lib/grenweb/searcher.rb",
58
+ "lib/grenweb/viewer.rb",
59
+ "lib/mkgrendb/cli.rb",
60
+ "lib/mkgrendb/cli_old.rb",
61
+ "lib/mkgrendb/grendbyaml.rb",
62
+ "lib/mkgrendb/mkgrendb.rb",
63
+ "test/file_test_utils.rb",
64
+ "test/runner.rb",
65
+ "test/test_dbdir.rb",
66
+ "test/test_gren.rb",
67
+ "test/test_gren_util.rb",
68
+ "test/test_grendbyaml.rb",
69
+ "test/test_grenweb_cli.rb",
70
+ "test/test_grenweb_database.rb",
71
+ "test/test_grenweb_html_renderer.rb",
72
+ "test/test_grenweb_query.rb",
73
+ "test/test_grenweb_searcher.rb",
74
+ "test/test_helper.rb",
75
+ "test/test_mkgrendb.rb",
76
+ "test/test_string_snip.rb"
77
+ ]
78
+ s.homepage = %q{http://github.com/ongaeshi/codestock}
79
+ s.licenses = ["MIT"]
80
+ s.require_paths = ["lib"]
81
+ s.rubygems_version = %q{1.3.6}
82
+ s.summary = %q{one-line summary of your gem}
83
+ s.test_files = [
84
+ "test/file_test_utils.rb",
85
+ "test/runner.rb",
86
+ "test/test_dbdir.rb",
87
+ "test/test_gren.rb",
88
+ "test/test_gren_util.rb",
89
+ "test/test_grendbyaml.rb",
90
+ "test/test_grenweb_cli.rb",
91
+ "test/test_grenweb_database.rb",
92
+ "test/test_grenweb_html_renderer.rb",
93
+ "test/test_grenweb_query.rb",
94
+ "test/test_grenweb_searcher.rb",
95
+ "test/test_helper.rb",
96
+ "test/test_mkgrendb.rb",
97
+ "test/test_string_snip.rb"
98
+ ]
99
+
100
+ if s.respond_to? :specification_version then
101
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
102
+ s.specification_version = 3
103
+
104
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
105
+ s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
106
+ s.add_development_dependency(%q<jeweler>, ["~> 1.5.2"])
107
+ s.add_development_dependency(%q<rcov>, [">= 0"])
108
+ s.add_runtime_dependency(%q<termcolor>, [">= 1.2.0"])
109
+ s.add_runtime_dependency(%q<rroonga>, [">= 1.0.0"])
110
+ s.add_runtime_dependency(%q<rack>, [">= 1.2.1"])
111
+ s.add_runtime_dependency(%q<launchy>, [">= 0.3.7"])
112
+ else
113
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
114
+ s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
115
+ s.add_dependency(%q<rcov>, [">= 0"])
116
+ s.add_dependency(%q<termcolor>, [">= 1.2.0"])
117
+ s.add_dependency(%q<rroonga>, [">= 1.0.0"])
118
+ s.add_dependency(%q<rack>, [">= 1.2.1"])
119
+ s.add_dependency(%q<launchy>, [">= 0.3.7"])
120
+ end
121
+ else
122
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
123
+ s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
124
+ s.add_dependency(%q<rcov>, [">= 0"])
125
+ s.add_dependency(%q<termcolor>, [">= 1.2.0"])
126
+ s.add_dependency(%q<rroonga>, [">= 1.0.0"])
127
+ s.add_dependency(%q<rack>, [">= 1.2.1"])
128
+ s.add_dependency(%q<launchy>, [">= 0.3.7"])
129
+ end
130
+ end
131
+
data/lib/codestock.rb ADDED
File without changes
@@ -0,0 +1,37 @@
1
+ # -*- coding: utf-8 -*-
2
+ #
3
+ # @file
4
+ # @brief
5
+ # @author ongaeshi
6
+ # @date 2011/03/08
7
+
8
+ require 'fileutils'
9
+
10
+ module CodeStock
11
+ DEFAULT_PATH = '~/.codestock'
12
+
13
+ def db_default_dir
14
+ if (ENV['CODESTOCK_DEFAULT_DIR'])
15
+ File.expand_path ENV['CODESTOCK_DEFAULT_DIR']
16
+ else
17
+ File.expand_path DEFAULT_PATH
18
+ end
19
+ end
20
+
21
+ def dbdir?(path = '.')
22
+ FileTest.exist? db_yaml_path(path)
23
+ end
24
+
25
+ def db_groonga_path(path = '.')
26
+ (Pathname.new(path) + 'db/grendb.db').to_s
27
+ end
28
+
29
+ def db_expand_groonga_path(path = '.')
30
+ File.expand_path db_groonga_path(path)
31
+ end
32
+
33
+ def db_yaml_path(path = '.')
34
+ (Pathname.new(path) + 'grendb.yaml').to_s
35
+ end
36
+ end
37
+
@@ -0,0 +1,62 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ module Gren
4
+ module DisplayUtil
5
+ def time_s(time)
6
+ t = time.truncate
7
+ h = t / 3600
8
+ t = t % 3600
9
+ m = t / 60
10
+ t = t % 60
11
+ t += round(time - time.prec_i, 2)
12
+
13
+ if (h > 0 && m > 0)
14
+ "#{h}h #{m}m #{t}s"
15
+ elsif (m > 0)
16
+ "#{m}m #{t}s"
17
+ else
18
+ "#{t}sec"
19
+ end
20
+ end
21
+ module_function :time_s
22
+
23
+ def round(n, d)
24
+ (n * 10 ** d).round / 10.0 ** d
25
+ end
26
+ module_function :round
27
+
28
+ def size_s(size)
29
+ tb = 1024 ** 4
30
+ gb = 1024 ** 3
31
+ mb = 1024 ** 2
32
+ kb = 1024
33
+
34
+ if (size >= tb)
35
+ round(size / tb.prec_f, 2).to_s + "TB"
36
+ elsif (size >= gb)
37
+ round(size / gb.prec_f, 2).to_s + "GB"
38
+ elsif (size >= mb)
39
+ round(size / mb.prec_f, 2).to_s + "MB"
40
+ elsif (size >= kb)
41
+ round(size / kb.prec_f, 2).to_s + "KB"
42
+ else
43
+ size.to_s + "Byte"
44
+ end
45
+ end
46
+ module_function :size_s
47
+
48
+ def dump_methods(c)
49
+ unless c.is_a?(Class)
50
+ c = c.class
51
+ end
52
+
53
+ while (true)
54
+ p c
55
+ break if (c == Object)
56
+ puts "↓ " + c.public_instance_methods(false).inspect
57
+ c = c.superclass
58
+ end
59
+ end
60
+ module_function :dump_methods
61
+ end
62
+ end
@@ -0,0 +1,19 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ module GrenFileTest
4
+ IGNORE_FILE = /(\A#.*#\Z)|(~\Z)|(\A\.#)|(\.d\Z)|(\.map\Z)|(\.MAP\Z)/
5
+ IGNORE_DIR = /(\A\.svn\Z)|(\A\.git\Z)|(\ACVS\Z)/
6
+
7
+ def self.ignoreDir?(fpath)
8
+ IGNORE_DIR.match(File.basename(fpath))
9
+ end
10
+
11
+ def self.ignoreFile?(fpath)
12
+ IGNORE_FILE.match(File.basename(fpath))
13
+ end
14
+
15
+ def self.binary?(fpath)
16
+ s = File.read(fpath, 1024) or return false
17
+ return s.index("\x00")
18
+ end
19
+ end
@@ -0,0 +1,37 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ require File.join(File.dirname(__FILE__), 'string_snip')
4
+
5
+ class GrenSnip
6
+ MAX_LINE_SIZE = 256
7
+ HEADER_SIZE = 32
8
+ MARGIN_SIZE = 32
9
+ DELIMITER = '<<snip>>'
10
+
11
+ def self.snip(str, match_datas)
12
+ return str if (str.size <= MAX_LINE_SIZE)
13
+
14
+ ranges = []
15
+ ranges << (0..HEADER_SIZE-1)
16
+ ranges << (-HEADER_SIZE..-1)
17
+
18
+ match_datas.each do |m|
19
+ ranges << (m.begin(0)-MARGIN_SIZE..m.end(0)+MARGIN_SIZE)
20
+ end
21
+
22
+ snipper = StringSnip.new(MAX_LINE_SIZE, DELIMITER)
23
+ return snipper.snip(str, ranges)
24
+ end
25
+
26
+ def self.coloring(line, match_datas)
27
+ match_datas.each do |m|
28
+ line = line.split(m[0]).join(HighLine::ON_GREEN + m[0] + HighLine::CLEAR)
29
+ end
30
+
31
+ line = line.split(DELIMITER).join(HighLine::ON_CYAN + DELIMITER + HighLine::CLEAR)
32
+
33
+ line
34
+ end
35
+ end
36
+
37
+
@@ -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,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
+
@@ -0,0 +1,98 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ module Gren
4
+ module Util
5
+ def time_s(time)
6
+ t = time.truncate
7
+ h = t / 3600
8
+ t = t % 3600
9
+ m = t / 60
10
+ t = t % 60
11
+ t += round(time - time.prec_i, 2)
12
+
13
+ if (h > 0 && m > 0)
14
+ "#{h}h #{m}m #{t}s"
15
+ elsif (m > 0)
16
+ "#{m}m #{t}s"
17
+ else
18
+ "#{t}sec"
19
+ end
20
+ end
21
+ module_function :time_s
22
+
23
+ def round(n, d)
24
+ (n * 10 ** d).round / 10.0 ** d
25
+ end
26
+ module_function :round
27
+
28
+ def size_s(size)
29
+ tb = 1024 ** 4
30
+ gb = 1024 ** 3
31
+ mb = 1024 ** 2
32
+ kb = 1024
33
+
34
+ if (size >= tb)
35
+ round(size / tb.prec_f, 2).to_s + "TB"
36
+ elsif (size >= gb)
37
+ round(size / gb.prec_f, 2).to_s + "GB"
38
+ elsif (size >= mb)
39
+ round(size / mb.prec_f, 2).to_s + "MB"
40
+ elsif (size >= kb)
41
+ round(size / kb.prec_f, 2).to_s + "KB"
42
+ else
43
+ size.to_s + "Byte"
44
+ end
45
+ end
46
+ module_function :size_s
47
+
48
+ # アルファベットと演算子で表示する数を変える
49
+ ALPHABET_DISP_NUM = 5
50
+ OPERATOR_DISP_NUM = 10
51
+
52
+ def p_classtree(c)
53
+ unless c.is_a?(Class)
54
+ c = c.class
55
+ end
56
+
57
+ while (true)
58
+ puts c.name
59
+ break if (c == Object)
60
+ p_classtree_sub(c)
61
+ c = c.superclass
62
+ end
63
+ end
64
+ module_function :p_classtree
65
+
66
+ def p_classtree_sub(c)
67
+ # メソッドの一覧を得る
68
+ group = c.public_instance_methods(false).sort.partition { |m| m =~ /\w/ }
69
+ array = group.flatten
70
+ operator_start_index = group[0].size
71
+ limit = ALPHABET_DISP_NUM
72
+
73
+ print((array.size > limit) ? "| " : "↓ ")
74
+
75
+ counter = 0
76
+ array.each_with_index do |v, index|
77
+ if (index == operator_start_index)
78
+ limit = OPERATOR_DISP_NUM
79
+ counter = 0
80
+ puts
81
+ print((array.size - index > limit) ? "| " : "↓ ")
82
+ end
83
+
84
+ if (counter >= limit)
85
+ counter = 0
86
+ puts
87
+ print((array.size - index > limit) ? "| " : "↓ ")
88
+ end
89
+
90
+ print v + ", "
91
+ counter += 1
92
+ end
93
+ puts
94
+ end
95
+ module_function :p_classtree_sub
96
+
97
+ end
98
+ end