Class: Fcom::Parser

Inherits:
Object
  • Object
show all
Includes:
OptionsHelpers
Defined in:
lib/fcom/parser.rb

Overview

This class parses (and then reprints some of) STDIN according to the options passed to ‘fcom`.

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Parser

Returns a new instance of Parser.



9
10
11
# File 'lib/fcom/parser.rb', line 9

def initialize(options)
  @options = options
end

Instance Method Details

#parseObject

rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity rubocop:disable Metrics/MethodLength



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
# File 'lib/fcom/parser.rb', line 15

def parse
  expression_to_match = search_string
  expression_to_match = Regexp.escape(expression_to_match).gsub('\\ ', ' ') unless regex_mode?
  regex =
    Regexp.new(
      "((\\+|-)\\s?.*#{expression_to_match}.*|Omitted long (matching )?line)",
      ignore_case? ? Regexp::IGNORECASE : nil,
    )

  previous_commit = nil
  a_commit_has_matched = false
  filename = nil
  $stdin.each do |line|
    line.chomp!
    if (match = line.match(/^commit (.*)/)&.[](1))
      previous_commit = match
    elsif line.match?(/^diff /)
      old_filename = line.match(%r{ a/(\S+)})&.[](1) || '[weird filename]'
      new_filename = line.match(%r{ b/(\S+)})&.[](1) || '[weird filename]'
      filename =
        case
        when old_filename == new_filename then old_filename
        else "#{old_filename} --> #{new_filename}"
        end
    elsif line.match?(regex) && (filename.blank? || path_match?(filename))
      if previous_commit
        title, sha, author, date = previous_commit.split('|')
        short_sha = sha[0, 8]
        sha_with_url = "#{short_sha} ( https://github.com/#{repo}/commit/#{short_sha} )"

        puts("\n\n") if a_commit_has_matched # print commit separator, if needed
        puts([title, sha_with_url, author, date])
        puts('==============================================')

        previous_commit = nil
        a_commit_has_matched = true
      end

      if filename
        puts(filename)
        filename = nil
      end

      if line.start_with?('+')
        puts(line.green)
      elsif line.start_with?('-')
        puts(line.red)
      else
        puts(line)
      end
    end
  end
end