du_pretty 0.2.2 → 0.3.0

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
  SHA256:
3
- metadata.gz: d143049a63ea770de4a93da816eee87e78621238045d3d96a2c311b77cd82be6
4
- data.tar.gz: 785368c3b8c2e67300f79037cbd80f096e28e52ca3d86d02bd6722c7d608b379
3
+ metadata.gz: 61dc0af33ffecb01858522e4fa90309c5e04c6942bef5d8dbcbca1ec24f0aad3
4
+ data.tar.gz: 966e1e39727763f52a18a8a1fe418cfc845f1e3e37556adceb960c31d50e239e
5
5
  SHA512:
6
- metadata.gz: 68ab666e2c371cb4128a91f063869974d1ca92d9c3ea75d4af9a2d1ac4d68550cdc7f81e7899b85d2aa67a078ac2fc48622ca8ddf0b90d5283dfdc269e2b55ff
7
- data.tar.gz: 3117c5a6d46eeb69a2ab728793ea40b38b4547463a41681becc7975b893a4c865d147e82d4ea4500cc28ed1ffa2b8015326f344fc95c8653287ad15330f57c97
6
+ metadata.gz: f399efd4c2e46e23b22766a9d0c1cb589d6e7851064fafa969f9a68b6a36d90ff63c35f547efd10e8aef3a6d10023623358776db7ea2c3189efb85bbe14c0494
7
+ data.tar.gz: 65d2be03a70889c77f149f097995768875f20bf70665c36f4b1eee012cdad24d4280da6255e97ebcab68c228716c4b3395da8e57cb911e15e403f45e15dc0a0c
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- du_pretty (0.2.2)
4
+ du_pretty (0.3.0)
5
5
  colorize
6
6
  thor
7
7
 
@@ -36,4 +36,4 @@ DEPENDENCIES
36
36
  rspec (~> 3.0)
37
37
 
38
38
  BUNDLED WITH
39
- 1.16.1
39
+ 1.16.2
data/lib/du_pretty/cli.rb CHANGED
@@ -9,14 +9,26 @@ module DuPretty
9
9
  option :min_mbyte, type: :numeric
10
10
  option :min_kbyte, type: :numeric
11
11
  option :depth, type: :numeric, aliases: :d
12
+ option :sort, aliases: :s, type: :boolean
13
+ option :all, aliases: :a, type: :boolean
14
+ option :tree, type: :boolean, default: true
15
+
12
16
  def path(path = '.')
13
17
  min_kbyte = [(options[:min_gbyte] || 0) * 1024 * 1024, (options[:min_mbyte] || 0) * 1024, (options[:min_kbyte] || 0)].max
14
- print DuWrapper.new(
18
+ du_wrapper = DuWrapper.new(
15
19
  path,
16
20
  min_kbyte: min_kbyte,
17
- depth: options[:depth]
18
- ).pretty
19
- print "\n"
21
+ depth: options[:depth],
22
+ with_files: options[:all],
23
+ )
24
+ result = if options[:sort]
25
+ du_wrapper.sorted
26
+ elsif options[:tree] == false
27
+ du_wrapper.original
28
+ else
29
+ du_wrapper.tree
30
+ end
31
+ print result + "\n"
20
32
  end
21
33
  end
22
34
  end
@@ -1,3 +1,3 @@
1
1
  module DuPretty
2
- VERSION = "0.2.2"
2
+ VERSION = "0.3.0"
3
3
  end
data/lib/du_pretty.rb CHANGED
@@ -6,39 +6,62 @@ require 'du_pretty/cli'
6
6
 
7
7
  module DuPretty
8
8
  class DuWrapper
9
- def initialize(path, min_kbyte: 0, depth: nil)
9
+ def initialize(path, min_kbyte: 0, depth: nil, with_files: false)
10
10
  @path = File.expand_path(path, Pathname.pwd)
11
11
  @min_kbyte = min_kbyte
12
12
  @depth = depth
13
+ @with_files = with_files
13
14
  end
14
15
 
15
- def pretty
16
- du.split("\n")
17
- .map { |line| DiskUsage.new(line, @path) }
18
- .select { |x| x.kbyte >= @min_kbyte }
19
- .reverse
20
- .map(&:pretty)
21
- .join("\n")
16
+ def original
17
+ filtered_disk_usages.map(&:pretty).join("\n")
18
+ end
19
+
20
+ def sorted
21
+ filtered_disk_usages.sort_by(&:kbyte).map(&:pretty).join("\n")
22
+ end
23
+
24
+ def tree
25
+ filtered_disk_usages.reverse.map(&:tree_format).join("\n")
22
26
  end
23
27
 
24
28
  private
25
29
 
26
30
  def du
27
- options = @depth.nil? ? '' : "-d #{@depth}"
28
- `du -k #{options} #{@path}`
31
+ options = [
32
+ @depth.nil? ? nil : "-d #{@depth}",
33
+ @with_files ? '-a' : nil,
34
+ '-k'
35
+ ]
36
+ `du #{options.join(' ')} #{@path}`
37
+ end
38
+
39
+ def disk_usages
40
+ xs = du.split("\n").map { |line| DiskUsage.new(line, @path) }
41
+ total = xs.map(&:kbyte).max
42
+ xs.map { |x| DiskUsage.new(x.raw, @path, total: total) }
43
+ end
44
+
45
+ def filtered_disk_usages
46
+ disk_usages.select { |x| x.kbyte >= @min_kbyte }
29
47
  end
30
48
 
31
49
  class DiskUsage
32
50
  attr_accessor :raw, :kbyte, :path
33
51
 
34
- def initialize(raw, root)
52
+ def initialize(raw, root, total: nil)
35
53
  @raw = raw
36
54
  @root = root
37
55
  @kbyte = raw.split("\t").compact[0].to_i
38
56
  @path = raw.split("\t").compact[1]
57
+ @total = total
39
58
  end
40
59
 
41
60
  def pretty
61
+ pretty_byte + "\t." + relative_path
62
+ end
63
+
64
+ def tree_format
42
65
  pretty_path + ' ' + pretty_byte
43
66
  end
44
67
 
@@ -57,21 +80,21 @@ module DuPretty
57
80
  gb = mb / 1024
58
81
  if gb.positive?
59
82
  if gb > 10
60
- "#{gb}GB".red
83
+ "#{gb}GB (#{percentage})".red
61
84
  else
62
- "#{gb}GB".light_red
85
+ "#{gb}GB (#{percentage})".light_red
63
86
  end
64
87
  elsif mb.positive?
65
88
  if mb > 500
66
- "#{mb}MB".yellow
89
+ "#{mb}MB (#{percentage})".yellow
67
90
  else
68
- "#{mb}MB".light_yellow
91
+ "#{mb}MB (#{percentage})".light_yellow
69
92
  end
70
93
  else
71
94
  if @kbyte > 500
72
- "#{@kbyte}KB".green
95
+ "#{@kbyte}KB (#{percentage})".green
73
96
  else
74
- "#{@kbyte}KB".light_green
97
+ "#{@kbyte}KB (#{percentage})".light_green
75
98
  end
76
99
  end
77
100
  end
@@ -83,6 +106,11 @@ module DuPretty
83
106
  spaces + tree_symbol + basename
84
107
  end
85
108
 
109
+ def percentage
110
+ percentage = @kbyte * 1.0 / @total * 100
111
+ "#{percentage.ceil}%"
112
+ end
113
+
86
114
  def relative_path
87
115
  @path.sub(/^#{@root}/, '')
88
116
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: du_pretty
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Katsuma Narisawa
@@ -123,7 +123,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
123
123
  version: '0'
124
124
  requirements: []
125
125
  rubyforge_project:
126
- rubygems_version: 2.7.4
126
+ rubygems_version: 2.7.6
127
127
  signing_key:
128
128
  specification_version: 4
129
129
  summary: Simple wrapper for "du" to pretty print.