Module: Diff

Defined in:
lib/githat.rb

Instance Method Summary collapse

Instance Method Details

#file_diff(file) ⇒ Object



64
65
66
# File 'lib/githat.rb', line 64

def file_diff(file)
  `git diff #{file}`
end

#file_extension(file_name) ⇒ Object



39
40
41
42
43
44
45
46
# File 'lib/githat.rb', line 39

def file_extension(file_name)
  extension = file_name.gsub /(.+\.)/, ''
  if extension.empty? || file_name == extension
    extension = files_with_no_extension[file_name]
    extension ||= 'text'
  end
  extension.to_sym
end

#files_infos(argv) ⇒ Object



14
15
16
17
18
19
20
21
# File 'lib/githat.rb', line 14

def files_infos(argv)
  files = []
  names = argv == [] ? files_names : argv
  names.map do |name|
    files << { name: name, diff: file_diff(name) }
  end
  files
end

#files_namesObject



68
69
70
# File 'lib/githat.rb', line 68

def files_names
  git_status.scan(/modified: .*/).map { |n| n.gsub(/modified: */, '') }
end

#files_with_no_extensionObject



84
85
86
87
88
89
90
91
92
# File 'lib/githat.rb', line 84

def files_with_no_extension
  {
    'Gemfile' => :rb,
    'Gemfile.lock' => :rb,
    'Rakefile' => :rb,
    'Makefile' => :makefile
  }
  # TODO: support it: .*rake == rb
end

#git_statusObject



72
73
74
# File 'lib/githat.rb', line 72

def git_status
  `git status`
end

#main(argv = nil) ⇒ Object



5
6
7
8
9
10
11
12
# File 'lib/githat.rb', line 5

def main(argv=nil)
  output = ''
  infos = files_infos(argv)
  infos.map do |file|
    output << parse_diff(file)
  end
  output
end

#parse_diff(file_info) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/githat.rb', line 23

def parse_diff(file_info)
  extension = file_extension(file_info[:name])
  splited = split_diff(file_info[:diff])
  heads, codes = splited[:heads], splited[:codes]
  complete_file_diff = ''

  (0...heads.size).map do |i|
    parsed_head = parse_with_diff(heads[i])
    parsed_code = parse_with_lang(codes[i], extension)
    parsed_code = parse_with_diff(parsed_code)
    complete_file_diff << (parsed_head << parsed_code << "\n")
  end

  complete_file_diff
end

#parse_with_diff(code) ⇒ Object



80
81
82
# File 'lib/githat.rb', line 80

def parse_with_diff(code)
  process(code, :diff)
end

#parse_with_lang(code, lang) ⇒ Object



76
77
78
# File 'lib/githat.rb', line 76

def parse_with_lang(code, lang)
  process(code, lang)
end

#process(code, lexer) ⇒ Object



94
95
96
# File 'lib/githat.rb', line 94

def process(code, lexer)
  Pygments.highlight code, formatter: 'terminal', lexer: lexer
end

#split_codes(diff) ⇒ Object



53
54
55
56
# File 'lib/githat.rb', line 53

def split_codes(diff)
  codes = diff.split(/^@@ .* @@/)
  codes[1..codes.size]
end

#split_diff(diff) ⇒ Object



48
49
50
51
# File 'lib/githat.rb', line 48

def split_diff(diff)
  { heads: split_heads(diff),
    codes: split_codes(diff) }
end

#split_heads(diff) ⇒ Object



58
59
60
61
62
# File 'lib/githat.rb', line 58

def split_heads(diff)
  heads = diff.scan(/^diff --git(?:.*\n){4}@@ .* @@/)
  heads << diff.scan(/^@@ .* @@/).tap(&:shift)
  heads.flatten
end