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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b24b5139412adc0e56b9bf45205c7655953b6632
4
- data.tar.gz: 0188cf89c6d316d084d0ef2dc707665edcc1c831
3
+ metadata.gz: b8b355d77d9ffad5e641bff456c9747f5bb75afc
4
+ data.tar.gz: 14d4018beb029824024de7c6780211a2b13167de
5
5
  SHA512:
6
- metadata.gz: 7b4618bdf98cd02735b43764e75a2bdf3126d816d4336ee86e66ff640fba42f7dc8c1ded88a1761f95b21031f63fdf6a315515958da6c936f24a4d4b41af2f97
7
- data.tar.gz: 551109fc6ab98a4993a5e9bb76aaf24a985af6cf6f9c1a511052ded172f09756a6943b33213cbb77caf79df487cf7fc3dd7a6528ed179eda8c0976adea88a69b
6
+ metadata.gz: c6b541aee5e70606b124062f3210f0a0d806fa874c41e97b1fe9ac3bcf5e7b93ec65a3cf2cb8c97430c8683840f8e16cf9c002cb2c41f3d414ce8a70c0621f8b
7
+ data.tar.gz: c91b003310aa8d0368d987f49a55076b445fd83a31ec5d96140fe36fbb05462a957f15da83c42827a11541f3ded571893fb5e374e841d25833a1255ba0957f01
@@ -3,18 +3,23 @@ require "terminal-table"
3
3
  module Octocounter
4
4
  class Counter
5
5
  include Commander::Methods
6
- attr_accessor :path
6
+
7
+ REGEX_PATH = %r{/\/$/}
8
+
9
+ attr_accessor :path, :list
7
10
 
8
11
  def initialize(path)
9
- path = "#{path}/" unless path.match(/\/$/)
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
- file_path = File.open(file).path
17
- if matched = list.find { |item| FileUtils.compare_file(item[:file], file) }
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
- content = File.open(item[:file]).read.encode('UTF-8', 'binary', invalid: :replace, undef: :replace, replace: '')[0..100]
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
@@ -1,3 +1,3 @@
1
1
  module Octocounter
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.2"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: octocounter
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dimas J. Taniawan