Class: Prak

Inherits:
Object
  • Object
show all
Defined in:
lib/prak.rb,
lib/prak/file.rb,
lib/prak/help.rb,
lib/prak/search.rb,
lib/prak/command_line.rb,
lib/prak/compile_match_file.rb

Constant Summary collapse

VERSION =
"1.3.2"
USAGE_HELP =
<<END
Usage: rak [OPTION]... PATTERN [FILES]

Search for PATTERN in each source file in the tree from cwd on down.
If [FILES] is specified, then only those files/directories are checked.
rak will search STDIN only if no FILES are specified.

Example: rak -i select

Searching:
  -i, --ignore-case     Ignore case distinctions
  -v, --invert-match    Invert match: select non-matching lines
  -w, --word-regexp     Force PATTERN to match only whole words
  -x, --line-regexp     Force PATTERN to match only whole lines
  -Q, --literal         Quote all metacharacters; expr is literal
  -s, --line-start      Match only at the start of a line
  -e, --line-start      Match only at the end of a line

Search output:
  -l, --files-with-matches
                        Only print filenames containing matches
  -L, --files-without-match
                        Only print filenames with no match
  -o                    Show only the part of a line matching PATTERN
                        (turns off text highlighting)
  --passthru            Print all lines, whether matching or not
  --output=expr         Output the evaluation of expr for each line
                        (turns off text highlighting)
  -m, --max-count=NUM   Stop searching in a file after NUM matches
  -H, --with-filename   Print the filename for each match
  -h, --no-filename     Suppress the prefixing filename on output
  -c, --count           Show number of lines matching per file

  --group               Group matches by file name.
                        (default: on when used interactively)
  --nogroup             One result per line, including filename, like grep
                        (default: on when the output is redirected)

  --[no]colour          Highlight the matching text (default: on unless
                        output is redirected, or on Windows)

  -A NUM, --after-context=NUM
                        Print NUM lines of trailing context after matching
                        lines.
  -B NUM, --before-context=NUM
                        Print NUM lines of leading context before matching
                        lines.
  -C [NUM], --context[=NUM]
                        Print NUM lines (default 2) of output context.

File finding:
  -f                    Only print the files found, without searching.
                        The PATTERN must not be specified.
  --sort-files          Sort the found files lexically.

File inclusion/exclusion:
  -n                    No descending into subdirectories
  -g REGEX              Only search in files matching REGEX.
  -k REGEX              Only search in files not matching REGEX.
  -a, --all             All files, regardless of extension (but still skips
                        blib, pkg, CVS, _darcs, .git, .pc, RCS, SCCS and .svn dirs)
  --ruby                Include only Ruby files.
  --type=ruby           Include only Ruby files.
  --noruby              Exclude Ruby files.
  --type=noruby         Exclude Ruby files.
                        See "rak --help type" for supported filetypes.
  --[no]follow          Follow symlinks.  Default is off.

Miscellaneous:
  --help                This help
  --version             Display version & copyright

You can specify a .prakrc file in your current directory having options
on each line, that will be sourced before ARGV
END
TYPES_HELP =
<<END
Usage: rak [OPTION]... PATTERN [FILES]

The following is the list of filetypes supported by rak.  You can
specify a file type with the --type=TYPE format, or the --TYPE
format.  For example, both --type=ruby and --ruby work.

Note that some extensions may appear in multiple types.  For example,
.pod files are both Perl and Parrot.

END
FILE_COLOUR =
"\033[1;31m"
MATCH_COLOUR =
"\033[1;37m\033[41m"
CLEAR_COLOURS =
"\033[0m"
FILE_TYPES =
{
  :asm         => %w( .s .S ),
  :cc          => %w( .c .h .xs ),
  :coffee      => %w( .coffee.js .coffee ),
  :cpp         => %w( .cpp .cc .m .h .hpp .C .H ),
  :csharp      => %w( .cs ),
  :css         => %w( .css .sass .scss .less ),
  :elisp       => %w( .el ),
  :haskell     => %w( .hs .lhs ),
  :hh          => %w( .h ),
  :html        => %w( .htm .html .shtml ),
  :image       => %w( .bmp .gif .jpg .jpeg .png .psd ),
  :lisp        => %w( .lisp ),
  :java        => %w( .java properties ),
  :js          => %w( .js ),
  :jsp         => %w( .jsp .jspx .jhtm .jhtml ),
  :log         => %w( .log ),
  :make        => %w( Makefile ),
  :mason       => %w( .mas .mhtml .mpl .mtxt ),
  :ocaml       => %w( .ml .mli ),
  :parrot      => %w( .pir .pasm .pmc .ops .pod .pg .tg ),
  :perl        => %w( .pl .pm .pod .t ),
  :php         => %w( .php .phpt .php3 .php4 .php5 ),
  :prolog      => %w( .pl .ecl ),
  :python      => %w( .py ),
  :ruby        => %w( .rb .rhtml .rjs .rxml Rakefile Gemfile Rakefile
                      .rake .gemspec .erb .haml .slim),
  :scheme      => %w( .scm ),
  :shell       => %w( .sh .bash .csh .ksh .zsh ),
  :sql         => %w( .sql .ctl ),
  :tcl         => %w( .tcl ),
  :tex         => %w( .tex .cls .sty ),
  :text        => %w( .txt .text ),
  :tt          => %w( .tt .tt2 .ttml ),
  :vala        => %w( .vala .vapi ),
  :vb          => %w( .bas .cls .frm .ctl .vb .resx ),
  :vim         => %w( .vim ),
  :yaml        => %w( .yaml .yml ),
  :xml         => %w( .xml .dtd .xslt )
}
VC_DIRS =
%w(blib CVS _darcs .git .pc RCS SCCS .svn pkg)

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.optObject (readonly)

Returns the value of attribute opt.



51
52
53
# File 'lib/prak/command_line.rb', line 51

def opt
  @opt
end

Class Method Details

.compile_match_fileObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/prak/compile_match_file.rb', line 8

def self.compile_match_file
  c = []
  c << %{def match_file(re, fn, file_separator) }
  c << %{  displayed_filename = false }
  c << %{  count = 0 }
  c << %{  i = 0 }
  c << %{  matches = [] }
  c << %{  print_num = 0 }

              if opt[:before_context] > 0
  c << %{    before_context = [] }
  c << %{    before_context_size = opt[:before_context] }
              end
  c << %{  if fn.is_a? String }

  if ruby_1_9?
    c << %{    f = File.open(fn, "r", :encoding => opt[:standard_encoding]) }
  else
    c << %{    f = File.open(fn, "r")  }
  end

  c << %{  elsif fn.is_a? IO }
  c << %{    f = fn }
  c << %{  end }

  c << %{  f.each_line do |line| }

  if ruby_1_9?
    c << %{line = line.force_encoding("BINARY") unless line.valid_encoding?}
  end

  c << %{    i += 1 }
                if opt[:print_output]
  c << %{      line.scan(re){ matches << eval(opt[:print_output]) } }
                else
  c << %{      line.scan(re){ matches << $~ } }
                end
                if opt[:print_match]
  c << %{      matches.each{|md| puts md.to_s } }
                end
  c << %{    count += matches.size }
                if opt[:invert_match]
                  if opt[:print_filename]
  c << %{        unless displayed_filename }
  c << %{          print file_separator }
  c << %{          file_separator.replace("\\n") }
  c << %{          puts FILE_COLOUR + fn + CLEAR_COLOURS }
  c << %{          displayed_filename = true }
  c << %{        end }
                  end
  c << %{      if matches.empty? }
                    if opt[:print_highlighted]
                      if opt[:print_line_number]
  c << %{            print "\#{i.to_s.rjust(4) }|" }
                      end
  c << %{          puts "\#{line.expand_tabs }" }
                    end
  c << %{      end }
  c << %{      matches.clear }
                else
  c << %{      if matches.empty? }
                    if opt[:print_entire_line_if_no_match]
  c << %{          puts line }
                    end
                    if opt[:use_context]
                      if opt[:before_context] > 0
  c << %{            if print_num == 0 }
  c << %{              before_context << [i, line] }
  c << %{              if before_context.length > before_context_size }
  c << %{                before_context.shift }
  c << %{              end }
  c << %{            end }
                      end
  c << %{          if print_num > 0 }
  c << %{            print_num -= 1 }
                        if opt[:print_highlighted]
                          if opt[:print_line_number]
  c << %{                print "\#{i.to_s.rjust(4) }|" }
                          end
  c << %{              puts "\#{line.expand_tabs }" }
  c << %{            end }
                      end
                    end
  c << %{      else }
  c << %{        print_num = opt[:after_context] }
                    if opt[:print_filename]
  c << %{          unless displayed_filename }
  c << %{            print file_separator }
  c << %{            file_separator.replace("\\n") }
  c << %{            puts FILE_COLOUR + fn + CLEAR_COLOURS }
  c << %{            displayed_filename = true }
  c << %{          end }
                    end
                    if opt[:before_context] > 0
  c << %{          before_context.each do |before_i, before_line| }
                        if opt[:print_line_number]
  c << %{              print "\#{before_i.to_s.rjust(4) }|" }
                        end
  c << %{            puts before_line.expand_tabs }
  c << %{          end }
  c << %{          before_context = [] }
                    end
                    if opt[:print_output]
  c << %{          matches.each {|m| puts m } }
                    end
                    if opt[:print_highlighted]
                      if opt[:print_line_number]
  c << %{            prefix = "\#{i.to_s.rjust(4) }|" }
                      else
                        if opt[:print_file_each_line]
  c << %{              fn_str = (fn.is_a?(String) ? FILE_COLOUR + fn +
                       CLEAR_COLOURS + ":" : "") }
  c << %{              prefix = fn_str + "\#{i.to_s }:" }
                        else
  c << %{              prefix = "" }
                        end
                      end
  c << %{          print_highlighted(prefix, line, matches) }
                    end
  c << %{      end }
                  if opt[:max_count]
  c << %{        if count >= opt[:max_count] }
  c << %{          break }
  c << %{        end }
                  end
  c << %{      matches.clear }
                end
  c << %{  end }
  c << %{  f.close if f === File }
              if opt[:print_num_matches]
                if opt[:invert_match]
  c << %{      puts "\#{fn }:\#{i-count }" }
                else
  c << %{      if count > 0 }
  c << %{        puts "\#{fn }:\#{count }" }
  c << %{      end }
                end
              end
              if opt[:print_file_if_match]
  c << %{    if count > 0 }
  c << %{      puts fn }
  c << %{    end }
              end

              if opt[:print_file_if_no_match]
  c << %{    if count == 0 }
  c << %{      puts fn }
  c << %{    end }
              end
  c << %{end }

  Prak.instance_eval c.join("\n")

end

.compile_pattern(str) ⇒ Object



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/prak/file.rb', line 122

def self.compile_pattern(str)
  if opt[:literal]
    str = Regexp.quote(str)
  end
  if opt[:match_whole_words]
    str = "\\b(?:" + str + ")\\b"
  end
  if opt[:match_whole_lines]
    str = "^(?:" + str + ")$"
  end
  if opt[:match_line_starts]
    str = "^(?:" + str + ")"
  end
  if opt[:match_line_ends]
    str = "(?:" + str + ")$"
  end
  str = str.gsub("~bang", "!").gsub("~ques", "?")
  if $use_onig
    flags = opt[:ignore_case] ? Oniguruma::OPTION_IGNORECASE : nil
    Oniguruma::ORegexp.new(str, :options => flags)
  else
    flags = opt[:ignore_case] ? Regexp::IGNORECASE : nil
    Regexp.new(str, flags)
  end
end

.compile_regexp(str) ⇒ Object



82
83
84
85
86
87
88
# File 'lib/prak/file.rb', line 82

def self.compile_regexp(str)
  if $use_onig
    Oniguruma::ORegexp.new(str)
  else
    Regexp.new(str)
  end
end

.each_file(todo, &blk) ⇒ Object

It’s really asking for Pathname…



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/prak/file.rb', line 38

def self.each_file(todo, &blk)
  if todo.empty?
    if STDIN.isatty
      todo = ["."]
    else
      opt[:print_filename] = false
      yield(STDIN)
      return
    end
  elsif todo.size == 1 and File.file?(todo[0])
    opt[:print_filename] = false
  end

  if opt[:sort_files]
    sortme = []
    todo.each do |item|
      find_all_files(item) do |fn|
        sortme << fn
      end
    end
    sortme.sort_by{|fn|fn.downcase}.each(&blk)
  else
    todo.each do |item|
      find_all_files(item, &blk)
    end
  end
end

.extension_regexp(extensions) ⇒ Object



90
91
92
93
# File 'lib/prak/file.rb', line 90

def self.extension_regexp(extensions)
  return nil if extensions.empty?
  Regexp.compile('(?:' + extensions.map{|x| Regexp.escape(x)}.join("|") + ')\z')
end

.file_relevant?(fn) ⇒ Boolean

Returns:

  • (Boolean)


3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/prak/file.rb', line 3

def self.file_relevant?(fn)
  # These don't change at this point
  @types_rx    ||= extension_regexp(FILE_TYPES.values.flatten)
  @includes_rx ||= extension_regexp(opt[:includes])
  @excludes_rx ||= extension_regexp(opt[:excludes])

  ext = File.basename(fn)
  ext = shebang_matches(fn) unless ext =~ @types_rx

  return false if !opt[:all_files]         and !ext or fn =~ /[~#]\z/
  return false if @includes_rx             and (ext||"") !~ @includes_rx
  return false if @excludes_rx             and (ext||"") =~ @excludes_rx
  return false if opt[:filename_regex]     and fn  !~ opt[:filename_regex]
  return false if opt[:neg_filename_regex] and fn  =~ opt[:neg_filename_regex]
  return true
end

.find_all_files(path, &blk) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/prak/file.rb', line 20

def self.find_all_files(path, &blk)
  return if File.socket?(path)
  return unless File.readable?(path)

  if File.file?(path)
    return unless file_relevant?(path)
    yield(path.sub(/\A\.\/+/, "").gsub(/\/+/, "/"))
  elsif File.directory?(path)
    Dir["#{path}/*"].each do |fn|
      next if VC_DIRS.any?{|vc| vc == File.basename(fn)}
      next if File.directory?(fn) and not opt[:descend]
      next if File.symlink?(fn) and not opt[:follow_symlinks]
      find_all_files(fn, &blk)
    end
  end
end

.parse_command_line_options(options = {}) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
# File 'lib/prak/command_line.rb', line 60

def self.parse_command_line_options options = {}

  @opt = options

  # file types
  opt[:includes] = []
  opt[:excludes] = []

  FILE_TYPES.each do |type, exts|
    if ARGV.delete('--'+type.to_s)
      ARGV << '--type'
      ARGV << type.to_s
    end
    if ARGV.delete('--no'+type.to_s)
      ARGV << '--type'
      ARGV << 'no'+type.to_s
    end
  end

  opts = GetoptLong.new(
    [ '--help', GetoptLong::OPTIONAL_ARGUMENT ],
    [ '--max-count', '-m', GetoptLong::REQUIRED_ARGUMENT ],
    [ '--files', '-f', GetoptLong::NO_ARGUMENT ],
    [ '--output', GetoptLong::REQUIRED_ARGUMENT ],
    [ '--version', GetoptLong::NO_ARGUMENT ],
    [ '-c', '--count', GetoptLong::NO_ARGUMENT ],
    [ '-h', '--no-filename', GetoptLong::NO_ARGUMENT ],
    [ '-i', '--ignore-case', GetoptLong::NO_ARGUMENT ],
    [ '-v', '--invert-match', GetoptLong::NO_ARGUMENT ],
    [ '-n', GetoptLong::NO_ARGUMENT ],
    [ '-Q', '--literal', GetoptLong::NO_ARGUMENT ],
    [ '-o', GetoptLong::NO_ARGUMENT ],
    [ '-w', '--word-regexp', GetoptLong::NO_ARGUMENT ],
    [ '--group', GetoptLong::NO_ARGUMENT ],
    [ '--nogroup', GetoptLong::NO_ARGUMENT ],
    [ '-l', '--files-with-matches', GetoptLong::NO_ARGUMENT ],
    [ '-L', '--files-without-matches', GetoptLong::NO_ARGUMENT ],
    [ '--passthru', GetoptLong::NO_ARGUMENT ],
    [ '-H', '--with-filename', GetoptLong::NO_ARGUMENT ],
    [ '--colour', GetoptLong::NO_ARGUMENT ],
    [ '--nocolour', GetoptLong::NO_ARGUMENT ],
    [ '--color', GetoptLong::NO_ARGUMENT ],
    [ '--nocolor', GetoptLong::NO_ARGUMENT ],
    [ '-a', '--all', GetoptLong::NO_ARGUMENT ],
    [ '--type', GetoptLong::REQUIRED_ARGUMENT ],
    [ '--sort-files', GetoptLong::NO_ARGUMENT ],
    [ '--follow', GetoptLong::NO_ARGUMENT ],
    [ '--nofollow', GetoptLong::NO_ARGUMENT ],
    [ '--after-context', '-A', GetoptLong::REQUIRED_ARGUMENT ],
    [ '--before-context', '-B', GetoptLong::REQUIRED_ARGUMENT ],
    [ '--context', '-C', GetoptLong::OPTIONAL_ARGUMENT ],
    [ '-g', GetoptLong::REQUIRED_ARGUMENT ],
    [ '-k', GetoptLong::REQUIRED_ARGUMENT ],
    [ '-x', '--line-regexp', GetoptLong::NO_ARGUMENT ],
    [ '-s', '--line-start', GetoptLong::NO_ARGUMENT ],
    [ '-e', '--line-end', GetoptLong::NO_ARGUMENT ],
    [ '-E', '--encoding', GetoptLong::REQUIRED_ARGUMENT ]
  )

  self.standard_options

  # if redirected (RAK_TEST allows us to redirect in testing and still
  # get the non-redirected defaults).
  unless STDOUT.isatty or ENV['RAK_TEST'] == "true"
    opt[:colour] = false
    opt[:print_file_each_line] = true
    opt[:print_filename] = false
    opt[:print_line_number] = false
  end

  begin
    opts.each do |option, arg|
      case option
      when '--help'
        if arg == ""
          puts USAGE_HELP
        elsif arg == "types" or arg == "type"
          puts TYPES_HELP
          FILE_TYPES.sort_by{|type,exts| type.to_s}.each do |type, exts|
            puts "    --[no]%-13s %s\n" % [type, exts.join(" ")]
          end
        end
        exit
      when '--max-count'
        opt[:max_count] = arg.to_i
      when '--files'
        opt[:only_print_filelist] = true
      when '--output'
        opt[:print_filename] = false
        opt[:print_line_number] = false
        opt[:print_output] = arg
        opt[:print_highlighted] = false
      when '--version'
        puts VERSION_INFO
        exit
      when '-c'
        opt[:print_num_matches] = true
        opt[:print_filename] = false
        opt[:print_line_number] = false
        opt[:print_highlighted] = false
      when '-h'
        opt[:print_filename] = false
        opt[:print_line_number] = false
        opt[:print_file_each_line] = false
      when '-i'
        opt[:ignore_case] = true
      when '-v'
        opt[:invert_match] = true
      when '-n'
        opt[:descend] = false
      when '-Q'
        opt[:literal] = true
      when '-o'
        opt[:print_match] = true
        opt[:print_filename] = false
        opt[:print_line_number] = false
        opt[:print_highlighted] = false
      when '-w'
        opt[:match_whole_words] = true
      when '--group'
        opt[:print_filename] = true
        opt[:print_file_each_line] = false
      when '--nogroup'
        opt[:print_file_each_line] = true
        opt[:print_filename] = false
        opt[:print_line_number] = false
      when '-l'
        opt[:print_filename] = false
        opt[:print_line_number] = false
        opt[:print_highlighted] = false
        opt[:print_file_if_match] = true
      when '-L'
        opt[:print_filename] = false
        opt[:print_line_number] = false
        opt[:print_highlighted] = false
        opt[:print_file_if_no_match] = true
      when '--passthru'
        opt[:print_entire_line_if_no_match] = true
      when '-H'
        opt[:print_filename] = true
        opt[:print_line_number] = true
      when '--nocolour', '--nocolor'
        opt[:colour] = false
      when '--colour', '--color'
        opt[:colour] = true
      when '-a'
        opt[:all_files] = true
      when '--type'
        if arg[0..1] == "no"
          type = arg[2..-1]
          arr = opt[:excludes]
        else
          type = arg
          arr = opt[:includes]
        end
        exts = FILE_TYPES[type.intern]
        unknown_type(type) unless exts
        exts.each do |ext|
          arr << ext
        end
      when '--sort-files'
        opt[:sort_files] = true
      when '--follow'
        opt[:follow_symlinks] = true
      when '--nofollow'
        opt[:follow_symlinks] = false
      when '--after-context'
        opt[:use_context] = true
        opt[:after_context] = arg.to_i
      when '--before-context'
        opt[:use_context] = true
        opt[:before_context] = arg.to_i
      when '--context'
        opt[:use_context] = true
        if arg == ""
          val = 2
        else
          val = arg.to_i
        end
        opt[:before_context] = val
        opt[:after_context] = val
      when '-g'
        opt[:filename_regex] = compile_regexp(arg)
      when '-k'
        opt[:neg_filename_regex] = compile_regexp(arg)
      when '-x'
        opt[:match_whole_lines] = true
      when '-s'
        opt[:match_line_starts] = true
      when '-e'
        opt[:match_line_ends] = true
      end
    end
  rescue GetoptLong::InvalidOption => ex
    puts "rak: see rak --help for usage."
    exit
  rescue SystemExit
    exit
  end
end


66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/prak/file.rb', line 66

def self.print_highlighted(prefix, line, matches)
  print prefix
  matches = matches.map{|md| md.offset(0)}
  cuts = [0, matches, line.size].flatten
  column = 0
  0.upto(cuts.size-2) do |i|
    part = line[cuts[i]...cuts[i+1]]
    part = part.expand_tabs(column)
    column += part.size
    print MATCH_COLOUR if i%2 == 1
    print part
    print CLEAR_COLOURS if i%2 == 1
  end
  print "\n" unless line[-1,1] == "\n"
end

.ruby_1_9?Boolean

Returns:

  • (Boolean)


4
5
6
# File 'lib/prak/compile_match_file.rb', line 4

def self.ruby_1_9?
  RUBY_VERSION.to_f >= 1.9
end

.search(run_opts = {}) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/prak/search.rb', line 3

def self.search run_opts = {}

  # command_line_options and sets some standards
  if run_opts.empty?
    if File.exists? ".prakrc"
      File.open(".prakrc").each do |line|
        ARGV << line.gsub(/r?\n/, "")
      end
    end
    parse_command_line_options
  end


  unless opt[:colour]
    FILE_COLOUR.replace ""
    CLEAR_COLOURS.replace ""
    MATCH_COLOUR.replace ""
  end

  if opt[:only_print_filelist]
    each_file(ARGV) do |fn|
      puts fn
    end
  elsif ARGV.empty?
    puts USAGE_HELP
    exit
  else
    re = compile_pattern(ARGV.shift)
    compiled = false
    file_separator = ""
    each_file(ARGV) do |fn|
      # each_file might turn off printing file name,
      # but only before first yield
      unless compiled
        compile_match_file
        compiled = true
      end
      match_file(re, fn, file_separator)
    end
  end

end

.shebang_matches(fn) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/prak/file.rb', line 95

def self.shebang_matches(fn)
  begin
    if ruby_1_9?
      line = File.open(fn, :encoding => opt[:standard_encoding])
    else
      line = File.open(fn)
    end
    line = line.readline

    if line =~ /^#!/
      if line =~ /\b(ruby|perl|php|python|make)[0-9.]*\b/i
        FILE_TYPES[$1.downcase.intern].first
      elsif line =~ /\b(sh|bash|csh|ksh|zsh)\b/
        ".sh"
      else
        ".other"
      end
    elsif line =~ /^<\?xml\b/
      ".xml"
    else
      false
    end
  rescue
    nil
  end
end

.standard_optionsObject



261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
# File 'lib/prak/command_line.rb', line 261

def self.standard_options
  opt[:max_count] = nil
  opt[:only_print_filelist] = false
  opt[:print_filename] = true
  opt[:print_line_number] = true
  opt[:print_output] = nil
  opt[:print_highlighted] = true
  opt[:print_num_matches] = false
  opt[:ignore_case] = false
  opt[:invert_match] = false
  opt[:descend] = true
  opt[:literal] = false
  opt[:print_match] = false
  opt[:match_whole_words] = false
  opt[:match_whole_lines] = false
  opt[:match_line_starts] = false
  opt[:match_line_ends] = false
  opt[:print_file_each_line] = false
  opt[:print_file_if_match] = false
  opt[:print_file_if_no_match] = false
  opt[:print_entire_line_if_no_match] = false
  opt[:colour] = true
  opt[:all_files] = false
  opt[:sort_files] = false
  opt[:follow_symlinks] = false
  opt[:after_context] = 0
  opt[:before_context] = 0
  opt[:collect_context] = false
  opt[:filename_regex] = nil
  opt[:neg_filename_regex] = nil
  opt[:standard_encoding] = "UTF-8"
end

.unknown_type(type) ⇒ Object



54
55
56
57
58
# File 'lib/prak/command_line.rb', line 54

def self.unknown_type(type)
  puts "rak: Unknown --type \"#{type}\""
  puts "rak: See rak --help types"
  exit
end