Class: Fcom::Parser
- Inherits:
-
Object
- Object
- Fcom::Parser
- 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.
Constant Summary collapse
- COMMIT_HEADER_SEPARATOR =
'=============================================='
Instance Method Summary collapse
-
#initialize(options) ⇒ Parser
constructor
A new instance of Parser.
- #parse ⇒ Object
Constructor Details
#initialize(options) ⇒ Parser
Returns a new instance of Parser.
11 12 13 14 |
# File 'lib/fcom/parser.rb', line 11 def initialize() @options = @matching_commits = 0 end |
Instance Method Details
#parse ⇒ Object
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 |
# File 'lib/fcom/parser.rb', line 16 def parse expression_to_match = if fixed_strings? Regexp.escape(search_string).gsub('\\ ', ' ') else search_string end regex = Regexp.new( "\\A(?:[+-]\\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)) if commit_limit_reached? break end 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?(%r{\A(?:---|\+\+\+) }) next elsif line.match?(regex) && (filename.blank? || path_match?(filename)) if previous_commit title, sha, , date = previous_commit.split('|') short_sha = sha[0, 8] sha_with_url = "#{short_sha} ( https://github.com/#{repo}/commit/#{short_sha} )" if a_commit_has_matched # print commit separator, if needed puts("\n\n") end puts([title, sha_with_url, , date]) puts(COMMIT_HEADER_SEPARATOR) previous_commit = nil a_commit_has_matched = true @matching_commits += 1 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 |