Sha256: 73f5ecff3347a067f52c47b6ba7425571db86431f02c890661749d3a02977c0e

Contents?: true

Size: 1.69 KB

Versions: 4

Compression:

Stored size: 1.69 KB

Contents

require 'sq_ruby_grep/version'
require 'sq_ruby_grep/ring_buffer'
require 'sq_ruby_grep/parser'

module SqRubyGrep


  def self.run(pattern:, file_path:, after_lines: 0, before_lines: 0, colorize: true)
    result = []

    File.open(file_path, 'r') do |file|
      current_position, last_match_position = 0, 0
      buffer = RingBuffer.new after_lines + before_lines
      f = false
      was_matches = false
      file.each_line do |line|
        current_position += 1

        if after_lines + before_lines > 0
          if line.match(pattern)
            if was_matches
              if (current_position - last_match_position) > after_lines + before_lines + 1
                result << (colorize ? "\e[0;36m--\033[0m" : "--")
                result.concat buffer.last(before_lines)
              else
                result.concat buffer
              end
            else
              result.concat buffer.last(before_lines)
            end

            result << (colorize ? line.gsub(/(#{pattern})/, "\e[01;31m\\1\033[0m") : line)
            f = false
            was_matches = true
            buffer.clear
            last_match_position = current_position
          else
            buffer << line
          end

          if (current_position - last_match_position) == after_lines + before_lines && was_matches
            result.concat buffer.shift(after_lines)
            # result.concat buffer.last(before_lines)
            f = true
          end
        else
          if line.match(pattern)
            result << (colorize ? line.gsub(/(#{pattern})/, "\e[01;31m\\1\033[0m") : line)
          end
        end
      end

      result.concat buffer.first(after_lines) unless f
    end

    result
  end


end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
sq_ruby_grep-0.0.7 lib/sq_ruby_grep.rb
sq_ruby_grep-0.0.6 lib/sq_ruby_grep.rb
sq_ruby_grep-0.0.5 lib/sq_ruby_grep.rb
sq_ruby_grep-0.0.4 lib/sq_ruby_grep.rb