octocounter 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/octocounter/counter.rb +34 -8
- data/lib/octocounter/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b8b355d77d9ffad5e641bff456c9747f5bb75afc
|
4
|
+
data.tar.gz: 14d4018beb029824024de7c6780211a2b13167de
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c6b541aee5e70606b124062f3210f0a0d806fa874c41e97b1fe9ac3bcf5e7b93ec65a3cf2cb8c97430c8683840f8e16cf9c002cb2c41f3d414ce8a70c0621f8b
|
7
|
+
data.tar.gz: c91b003310aa8d0368d987f49a55076b445fd83a31ec5d96140fe36fbb05462a957f15da83c42827a11541f3ded571893fb5e374e841d25833a1255ba0957f01
|
data/lib/octocounter/counter.rb
CHANGED
@@ -3,18 +3,23 @@ require "terminal-table"
|
|
3
3
|
module Octocounter
|
4
4
|
class Counter
|
5
5
|
include Commander::Methods
|
6
|
-
|
6
|
+
|
7
|
+
REGEX_PATH = %r{/\/$/}
|
8
|
+
|
9
|
+
attr_accessor :path, :list
|
7
10
|
|
8
11
|
def initialize(path)
|
9
|
-
path = "#{path}/" unless path
|
12
|
+
path = "#{path}/" unless path =~ REGEX_PATH
|
10
13
|
@path = path
|
14
|
+
@list = []
|
11
15
|
end
|
12
16
|
|
13
17
|
def calculate
|
14
|
-
list = []
|
15
18
|
Dir.glob(path + "**/*").select { |f| File.file?(f) }.each do |file|
|
16
|
-
|
17
|
-
|
19
|
+
open_file = File.open(file)
|
20
|
+
file_path = open_file.path.sub(path, '')
|
21
|
+
open_file.close
|
22
|
+
if matched = matched?(file)
|
18
23
|
matched[:count] += 1
|
19
24
|
matched[:files] = "#{matched[:files]}\n#{file_path}"
|
20
25
|
else
|
@@ -24,11 +29,32 @@ module Octocounter
|
|
24
29
|
list
|
25
30
|
end
|
26
31
|
|
32
|
+
def matched?(file)
|
33
|
+
list.find do |item|
|
34
|
+
file1, file2 = [File.open(file), File.open(item[:file])]
|
35
|
+
size1, size2 = [file1.size, file2.size]
|
36
|
+
file1.close
|
37
|
+
file2.close
|
38
|
+
|
39
|
+
size1 == size2 &&
|
40
|
+
FileUtils.compare_file(item[:file], file)
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
def content(item)
|
45
|
+
content = ""
|
46
|
+
index = 0
|
47
|
+
IO.foreach(item[:file]) do |line|
|
48
|
+
content += line
|
49
|
+
index += 1
|
50
|
+
break if index == 10
|
51
|
+
end
|
52
|
+
content.encode('UTF-8', 'binary', invalid: :replace, undef: :replace, replace: '')[0..50]
|
53
|
+
end
|
54
|
+
|
27
55
|
def print_to_screen
|
28
56
|
rows = calculate.map do |item|
|
29
|
-
|
30
|
-
content = content.scan(/.{40}/).join("\n")
|
31
|
-
[item[:files], content, item[:count]]
|
57
|
+
[item[:files], content(item), item[:count]]
|
32
58
|
end
|
33
59
|
puts Terminal::Table.new headings: ["Files", "Content", "Count"], rows: rows, style: { all_separators: true }
|
34
60
|
end
|
data/lib/octocounter/version.rb
CHANGED