daily pastebin goal
37%
SHARE
TWEET

Untitled

a guest Apr 16th, 2018 57 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/env ruby
  2.  
  3. #
  4. # a perl/ack in ruby
  5. # (with less features and less good)
  6. #
  7. # (license is the MIT one)
  8. #
  9.  
  10.  
  11. # TODO
  12. # [ ] -i (ignore case)
  13.  
  14. require 'set'
  15.  
  16.  
  17. #
  18. # USAGE
  19.  
  20. def print_usage
  21.   puts %{
  22.  
  23.   akr [options] {regex} [start_path]
  24. }
  25. end
  26.  
  27. #
  28. # COLORS
  29.  
  30. def color (colcode, s)
  31.   STDOUT.tty? ? "[#{colcode}m#{s}" : s
  32. end
  33. def green (s)
  34.   color(32, s)
  35. end
  36. def red (s)
  37.   color(31, s)
  38. end
  39.  
  40. def puts_line (l, reg)
  41.  
  42.   # TODO : what if regex starts with ^ ?
  43.  
  44.   i = 0
  45.   loop do
  46.     ll = l[i..-1]
  47.     break unless ll
  48.     if m = reg.match(ll)
  49.       print(red(m[0]))
  50.       i = i + m[0].length
  51.     else
  52.       print(l[i, 1])
  53.       i = i + 1
  54.     end
  55.   end
  56.   #puts
  57. end
  58.  
  59. def scan_file (regex, before, after, path)
  60.  
  61.   ls = Set.new
  62.  
  63.   lines = File.readlines(path)
  64.  
  65.   lines.each_with_index do |line, i|
  66.  
  67.     if regex.match(line)
  68.       ls << i
  69.       (1..before).to_a.each { |d| ls << (i - d) }
  70.       (1..after).to_a.each { |d| ls << (i + d) }
  71.     end
  72.   end
  73.  
  74.   return false if ls.size < 1
  75.  
  76.   puts
  77.   puts(green(path))
  78.  
  79.   prev = false
  80.   reg = Regexp.compile("^#{regex.to_s}")
  81.  
  82.   ls.sort.each do |i|
  83.  
  84.     next if i < 0
  85.  
  86.     puts '--' if prev && (i - prev > 1)
  87.     prev = (before > 0 || after > 0) ? i : false
  88.  
  89.     ln = i + 1
  90.     line = lines[i]
  91.  
  92.     next unless line
  93.  
  94.     print "#{ln}"
  95.     print regex.match(line) ? ':' : '-'
  96.     print ' '
  97.     print ' ' * (4 - ln.to_s.length)
  98.  
  99.     puts_line(line, reg)
  100.   end
  101.  
  102.   true
  103. end
  104.  
  105. #
  106. # handling params
  107.  
  108. offset = 0
  109. options = {}
  110.  
  111. loop do
  112.   a = ARGV[offset]
  113.   break unless a.match(/^-/)
  114.   options[a] = ARGV[offset + 1]
  115.   offset += 2
  116. end
  117.  
  118. before = options['-B'] || 0
  119. after = options['-A'] || 0
  120.  
  121. if c = options['-C']
  122.   before = after = c
  123. end
  124.  
  125. before = before.to_i
  126. after = after.to_i
  127.  
  128. regex = ARGV[offset]
  129. regex = Regexp.compile(regex)
  130. #regex = Regexp.compile("^(.*)#{regex}(.*)$")
  131.  
  132. spath = ARGV[offset + 1]
  133.  
  134. if regex == nil
  135.   print_usage
  136.   exit(1)
  137. end
  138.  
  139. #
  140. # do it ...
  141.  
  142. glob = spath ?
  143.   File.join(spath, '**', '*') :
  144.   File.join('**', '*')
  145.  
  146. exit_code = 1
  147.  
  148. Dir[glob].each do |path|
  149.  
  150.   next if File.directory?(path)
  151.  
  152.   scan_file(regex, before, after, path) && exit_code = 0
  153. end
  154.  
  155. exit(exit_code)
  156.   # 0 if there was a match, 1 else
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top