lib/tidgrep/cli.rb in tidtools-0.0.4 vs lib/tidgrep/cli.rb in tidtools-0.0.5

- old
+ new

@@ -1,35 +1,39 @@ # -*- coding: utf-8 -*- require File.join(File.dirname(__FILE__), '../tidtools/tiddle') +require File.join(File.dirname(__FILE__), '../tidtools/platform') require 'optparse' +require 'kconv' module Tidgrep - # 圧縮表示時のパラメータ - MATCH_LINE_COMP_NUM = 5 + class Tidgrep + # 圧縮表示時のパラメータ + MATCH_LINE_COMP_NUM = 5 - MATCH_ONLY_TITLE_COMP_NUM = 5 + MATCH_ONLY_TITLE_COMP_NUM = 5 - MATCH_TIDDLE_LINE_NUM = 3 - MATCH_TIDDLE_COMP_NUM = 5 + MATCH_TIDDLE_LINE_NUM = 3 + MATCH_TIDDLE_COMP_NUM = 5 - MATCH_TWEET_LINE_NUM = 3 - MATCH_TWEET_COMP_NUM = 5 + MATCH_TWEET_LINE_NUM = 3 + MATCH_TWEET_COMP_NUM = 5 - class Tidgrep - def initialize(stdout, file_name, title, regexp_option, report, match_rule, is_comp, keywords) + def initialize(stdout, file_name, title, regexp_option, report, match_rule, is_comp, keywords, kcode) + @stdout = stdout @file_name = file_name @title = title @regexp_option = regexp_option @report = report @match_rule = match_rule @is_comp = is_comp + @kcode = kcode @title_regexp = @title && Regexp.new(@title, @regexp_option) @content_regexps = [] keywords.each do |keyword| - @content_regexps << Regexp.new(keyword, @regexp_option) + @content_regexps << Regexp.new(self.kconv(keyword), @regexp_option) end end def validOption? return false if !@file_name @@ -41,10 +45,26 @@ return false if content_regexp !~ target end return true end + def kconv(str) + if (@kcode != Kconv::UTF8) + str.kconv(@kconv, Kconv::UTF8) + else + str + end + end + + def print(msg) + @stdout.print self.kconv msg + end + + def puts(msg) + @stdout.puts self.kconv msg + end + def match_line tiddles = Tiddle.parse_sort_modified(@file_name) match_lines = 0 search_lines = 0 @@ -242,10 +262,11 @@ title = nil regexp_option = 0 report = false match_rule = "line" is_comp = false + kcode = Platform.get_shell_kcode 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 } @@ -259,10 +280,11 @@ title, regexp_option, report, match_rule, is_comp, - arguments) + arguments, + kcode) unless obj.validOption? puts opt.help exit end