octocounter 0.1.3 → 0.1.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b11e8449a533df79b3aed3ebca176d6cc9b27f4a
4
- data.tar.gz: 33a52e733f7f3a8884438d90c7110ff8d6680563
3
+ metadata.gz: 097ec5048b1f4fd93c33da4ddd6ea1e13d741fd3
4
+ data.tar.gz: 8168e52706bc3ee7b5f053492249511cfa2955cd
5
5
  SHA512:
6
- metadata.gz: 17da731f05194708b0e283c66b1dc0ce42ba95edd8d303936ed88c7b319f928f098c274745247814b4ccec25bf981f3d1d6333dbcd96be2acd581f3504cb4d10
7
- data.tar.gz: cd71a58760533d70d7d0dfadcd1c1a4a52800b7c8fef3cf34b10ca2c3361bda985082002557bb528551b348408f043cc59ebd2d850084301b21bcf13eeda48dd
6
+ metadata.gz: 66ab3ea6dbcd3fa0e48f834387cdb291b795da04b11943bed983a458b73d74bc6af3fcf839cc42934739e3136748ad85fbf80fd9eb5687f011a59e6a02187903
7
+ data.tar.gz: f5504f40295efd7adc6f3dc39b66c9bb16496f3a9cec1fa65f7c7d0a2865097ffd8009960b062920eb6701fe0ed1197af3ed84bbeede9fdcbe1606339c2ab864
data/lib/octocounter.rb CHANGED
@@ -1,41 +1,4 @@
1
1
  require "commander"
2
2
  require "fileutils"
3
3
 
4
- require "octocounter/version"
5
- require "octocounter/counter"
6
-
7
- PATH_ARG = "
8
- Usage:
9
- octocounter path/to/directory
10
- "
11
-
12
- DESCRIPTION = "List files in directories to get count with same content"
13
-
14
- module Octocounter
15
- class CommandBuilder
16
- include Commander::Methods
17
-
18
- def run
19
- program :name, "Octocounter"
20
- program :version, Octocounter::VERSION
21
- program :description, DESCRIPTION
22
-
23
- default_command :run
24
-
25
- command :run do |c|
26
- c.syntax = "octocounter path/to/directory"
27
- c.description = DESCRIPTION
28
- c.option '--all', String, "Show all list"
29
- c.action do |args, options|
30
- path = args.shift || abort(PATH_ARG)
31
-
32
- counter = Octocounter::Counter.new(path, options.all)
33
-
34
- counter.print_to_screen
35
- end
36
- end
37
-
38
- run!
39
- end
40
- end
41
- end
4
+ require "octocounter/command_builder"
@@ -0,0 +1,37 @@
1
+ require "octocounter/version"
2
+ require "octocounter/counter"
3
+
4
+ module Octocounter
5
+ class CommandBuilder
6
+ include Commander::Methods
7
+
8
+ PATH_ARG = "
9
+ Usage:
10
+ octocounter path/to/directory
11
+ "
12
+ DESCRIPTION = "List files in directories to get count with same content"
13
+
14
+ def run
15
+ program :name, "Octocounter"
16
+ program :version, Octocounter::VERSION
17
+ program :description, DESCRIPTION
18
+
19
+ default_command :run
20
+
21
+ command :run do |c|
22
+ c.syntax = "octocounter path/to/directory"
23
+ c.description = DESCRIPTION
24
+ c.option "--all", String, "Show all list"
25
+ c.action do |args, options|
26
+ path = args.shift || abort(PATH_ARG)
27
+
28
+ counter = Octocounter::Counter.new(path, options.all)
29
+
30
+ counter.print_to_screen
31
+ end
32
+ end
33
+
34
+ run!
35
+ end
36
+ end
37
+ end
@@ -6,7 +6,7 @@ module Octocounter
6
6
 
7
7
  REGEX_PATH = %r{/\/$/}
8
8
 
9
- attr_accessor :path, :list, :all
9
+ attr_reader :path, :list, :all
10
10
 
11
11
  def initialize(path, all)
12
12
  path = "#{path}/" unless path =~ REGEX_PATH
@@ -17,9 +17,8 @@ module Octocounter
17
17
 
18
18
  def calculate
19
19
  Dir.glob(path + "**/*").select { |f| File.file?(f) }.each do |file|
20
- open_file = File.open(file)
21
- file_path = open_file.path.sub(path, '')
22
- open_file.close
20
+ file_path = File.path(file).sub(path, "")
21
+
23
22
  if matched = matched?(file)
24
23
  matched[:count] += 1
25
24
  matched[:files] = "#{matched[:files]}\n#{file_path}"
@@ -30,14 +29,25 @@ module Octocounter
30
29
  list
31
30
  end
32
31
 
32
+ def print_to_screen
33
+ rows =
34
+ if all
35
+ calculate.map do |item|
36
+ [item[:files], content(item), item[:count]]
37
+ end
38
+ else
39
+ item = calculate.inject { |memo, i| memo && (memo[:count] > i[:count]) ? memo : i }
40
+ [[item[:files], content(item), item[:count]]]
41
+ end
42
+
43
+ puts Terminal::Table.new headings: %w[Files Content Count], rows: rows, style: { all_separators: true }
44
+ end
45
+
46
+ private
47
+
33
48
  def matched?(file)
34
49
  list.find do |item|
35
- file1, file2 = [File.open(file), File.open(item[:file])]
36
- size1, size2 = [file1.size, file2.size]
37
- file1.close
38
- file2.close
39
-
40
- size1 == size2 &&
50
+ File.size(file) == File.size(item[:file]) &&
41
51
  FileUtils.compare_file(item[:file], file)
42
52
  end
43
53
  end
@@ -50,21 +60,8 @@ module Octocounter
50
60
  index += 1
51
61
  break if index == 10
52
62
  end
53
- content.encode('UTF-8', 'binary', invalid: :replace, undef: :replace, replace: '')[0..50]
54
- end
55
-
56
- def print_to_screen
57
- rows =
58
- if all
59
- calculate.map do |item|
60
- [item[:files], content(item), item[:count]]
61
- end
62
- else
63
- item = calculate.inject { |memo, i| memo && (memo[:count] > i[:count]) ? memo : i }
64
- [[item[:files], content(item), item[:count]]]
65
- end
66
-
67
- puts Terminal::Table.new headings: ["Files", "Content", "Count"], rows: rows, style: { all_separators: true }
63
+ # force encode into UTF-8
64
+ content.encode("UTF-8", "binary", invalid: :replace, undef: :replace, replace: "")[0..50]
68
65
  end
69
66
  end
70
67
  end
@@ -1,3 +1,3 @@
1
1
  module Octocounter
2
- VERSION = "0.1.3"
2
+ VERSION = "0.1.4"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: octocounter
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dimas J. Taniawan
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-09-20 00:00:00.000000000 Z
11
+ date: 2017-09-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: terminal-table
@@ -113,6 +113,7 @@ files:
113
113
  - bin/octocounter
114
114
  - bin/setup
115
115
  - lib/octocounter.rb
116
+ - lib/octocounter/command_builder.rb
116
117
  - lib/octocounter/counter.rb
117
118
  - lib/octocounter/version.rb
118
119
  - octocounter.gemspec