#!/usr/bin/env ruby # ファイル名やファイルの内容が指定した正規表現に一致するファイルを検索します。 # # Yasunori Miyamoto # http://tipszone.jp/20120515_regexp_search/ # mailto: nori@tipszone.jp Version = '3.7' Release = '2013-08-08' require 'benchmark' require 'nkf' require 'optparse' OPTS = {} MODE = {} regexp_file = nil confirm = false opts = OptionParser.new opts.program_name = File.basename(__FILE__) opts.version = Version opts.release = Release opts.banner = "Usage: #{opts.program_name} [-l|-g|-n] (regexp|-r file) [target-patterns... (default: ./**/*)]" opts.on('-l', '--line [ANTEROPOSTERIOR][:MAX-LINE-SIZE]', 'Match with each line. (Display AP lines.)', /^(?!$)(?:\d++(?:,\d*+)?|,\d++)?(?::\d++)?$/) do |v| MODE[:line] = :line /^(?\d*+)(?:,(?\d*+))?(?::(?\d++))?$/ =~ v OPTS[:anteroposterior_lines] = [anterior, posterior || anterior].map(&:to_i) OPTS[:max_line_size] = max_line_size ? max_line_size.to_i : nil end opts.on('-g', '--grep [ANTEROPOSTERIOR][:MAX-LINE-SIZE]', 'Match with each line. (Output grep format.)', /^(?!$)(?:\d++(?:,\d*+)?|,\d++)?(?::\d++)?$/) do |v| MODE[:grep] = :grep /^(?\d*+)(?:,(?\d*+))?(?::(?\d++))?$/ =~ v OPTS[:anteroposterior_lines] = [anterior, posterior || anterior].map(&:to_i) OPTS[:max_line_size] = max_line_size ? max_line_size.to_i : nil end opts.on('-n', '--name', 'Match with file names.') {MODE[:name] = :name} opts.separator '' opts.on('-r', '--regexp file', 'Input regexp from file.') {|v| regexp_file = v} opts.on('-c', '--confirm') {confirm = true} opts.on('--[no-]colors', "Use colors in output.") {|v| OPTS[:colors] = v} opts.on('--limit NUM', /^\w*+$/, Integer) {|v| OPTS[:limit] = v} opts.on('--max-file-size BYTES', /^\w*+$/, Integer) {|v| OPTS[:max_size] = v} opts.on('--min-file-size BYTES', /^\w*+$/, Integer) {|v| OPTS[:min_size] = v} opts.separator '' opts.on('-b', '--benchmark') {OPTS[:benchmark] = true} # 端末に色付き文字を表示する module ConsoleColorString @decoration = { off: 0, bold: 1, underscore: 4, blink: 5, reverse: 7, concealed: 8, } @color = { black: 30, red: 31, green: 32, yellow: 33, blue: 34, magenta: 35, cyan: 36, white: 37, } class << self attr_reader :color, :decoration end def decoration(d) return self if self.empty? self.gsub(/^|(?<=\e\[0m)(?!\z)/, "\e[" << ConsoleColorString.decoration[d].to_s << 'm') .gsub(/(?