lib/tidgrep/cli.rb in tidtools-0.0.3 vs lib/tidgrep/cli.rb in tidtools-0.0.4
- old
+ new
@@ -13,40 +13,24 @@
MATCH_TWEET_LINE_NUM = 3
MATCH_TWEET_COMP_NUM = 5
class Tidgrep
- def initialize
- @file_name = ENV['TIDGREP_PATH']
- @title = nil
- @regexp_option = 0
- @report = false
- @match_rule = "line"
- @is_comp = false
- end
+ def initialize(stdout, file_name, title, regexp_option, report, match_rule, is_comp, keywords)
+ @file_name = file_name
+ @title = title
+ @regexp_option = regexp_option
+ @report = report
+ @match_rule = match_rule
+ @is_comp = is_comp
- def setupParam(stdout, arguments)
- opt = OptionParser.new('tidgrep [option] keyword')
- opt.on('-f FILE_NAME', '--filename FILE_NAME', 'TiddlyWiki file name') {|v| @file_name = v }
- opt.on('-t TITLE', '--title TITLE', 'match title') {|v| @title = v }
- opt.on('-i', '--ignore', 'ignore case') {|v| @regexp_option |= Regexp::IGNORECASE }
- opt.on('-r', '--report', 'disp report') {|v| @report = true }
- opt.on('-m MATCH_RULE', '--match MATCH_RULE', 'match rule [line, tiddle, tweet]') {|v| @match_rule = v }
- opt.on('-c', '--comp', 'compression disp') {|v| @is_comp = true; @report = true }
- opt.parse!(arguments)
-
@title_regexp = @title && Regexp.new(@title, @regexp_option)
@content_regexps = []
- arguments.each do |keyword|
+ keywords.each do |keyword|
@content_regexps << Regexp.new(keyword, @regexp_option)
end
-
- unless validOption?
- puts opt.help
- exit
- end
end
def validOption?
return false if !@file_name
return @title || @content_regexps.size > 0
@@ -154,11 +138,17 @@
puts "--- #{tiddle.title} --------------------"
unless @is_comp
puts tiddle.content
else
- print tiddle.content.split(/\n/)[0..(MATCH_TIDDLE_LINE_NUM - 1)].join("\n") + "\n.\n"
+ tiddle_a = tiddle.content.split(/\n/)
+
+ if (tiddle_a.size <= MATCH_TIDDLE_LINE_NUM)
+ print tiddle.content
+ else
+ print tiddle_a[0..(MATCH_TIDDLE_LINE_NUM - 1)].join("\n") + "\n.\n"
+ end
end
if (@is_comp && match_tiddles >= MATCH_TIDDLE_COMP_NUM)
is_limit = true
print ".\n.\n"
@@ -202,11 +192,17 @@
end
unless @is_comp
print tweet
else
- print tweet.split(/\n/)[0..(MATCH_TWEET_LINE_NUM - 1)].join("\n") + "\n.\n"
+ tweet_a = tweet.split(/\n/)
+
+ if (tweet_a.size <= MATCH_TWEET_LINE_NUM)
+ print tweet
+ else
+ print tweet_a[0..(MATCH_TWEET_LINE_NUM - 1)].join("\n") + "\n.\n"
+ end
end
if (@is_comp && match_tweets >= MATCH_TWEET_COMP_NUM)
is_limit = true
print ".\n.\n"
@@ -221,15 +217,11 @@
puts "search tweets : #{search_tweets}"
puts "match tweets : #{match_tweets}"
end
end
- def execute(stdout, arguments=[])
- # パラメータの設定
- setupParam(stdout, arguments)
-
- # マッチルールごとに処理を変える
+ def execute
if (@content_regexps.size > 0)
case @match_rule
when "line"
match_line
when "tiddle"
@@ -244,9 +236,39 @@
end
class CLI
def self.execute(stdout, arguments=[])
- Tidgrep.new.execute(stdout, arguments)
+ file_name = ENV['TIDGREP_PATH']
+ title = nil
+ regexp_option = 0
+ report = false
+ match_rule = "line"
+ is_comp = false
+
+ opt = OptionParser.new('tidgrep [option] keyword')
+ opt.on('-f FILE_NAME', '--filename FILE_NAME', 'TiddlyWiki file name') {|v| file_name = v }
+ opt.on('-t TITLE', '--title TITLE', 'match title') {|v| title = v }
+ opt.on('-i', '--ignore', 'ignore case') {|v| regexp_option |= Regexp::IGNORECASE }
+ opt.on('-r', '--report', 'disp report') {|v| report = true }
+ opt.on('-m MATCH_RULE', '--match MATCH_RULE', 'match rule [line, tiddle, tweet]') {|v| match_rule = v }
+ opt.on('-c', '--comp', 'compression disp') {|v| is_comp = true; report = true }
+ opt.parse!(arguments)
+
+ obj = Tidgrep.new(stdout,
+ file_name,
+ title,
+ regexp_option,
+ report,
+ match_rule,
+ is_comp,
+ arguments)
+
+ unless obj.validOption?
+ puts opt.help
+ exit
+ end
+
+ obj.execute
end
end
end