du_pretty 0.3.0 → 0.3.1

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
  SHA256:
3
- metadata.gz: 61dc0af33ffecb01858522e4fa90309c5e04c6942bef5d8dbcbca1ec24f0aad3
4
- data.tar.gz: 966e1e39727763f52a18a8a1fe418cfc845f1e3e37556adceb960c31d50e239e
3
+ metadata.gz: 39482a8862ce548bf8074ecf2564fda7aff0f48321a7ec6e106d26bf7c6953ff
4
+ data.tar.gz: 6213856a3b177f24bb6273b37e9ce9e7b70ce2610c96b80236f6779ed06779e8
5
5
  SHA512:
6
- metadata.gz: f399efd4c2e46e23b22766a9d0c1cb589d6e7851064fafa969f9a68b6a36d90ff63c35f547efd10e8aef3a6d10023623358776db7ea2c3189efb85bbe14c0494
7
- data.tar.gz: 65d2be03a70889c77f149f097995768875f20bf70665c36f4b1eee012cdad24d4280da6255e97ebcab68c228716c4b3395da8e57cb911e15e403f45e15dc0a0c
6
+ metadata.gz: f53e2724839bf1a5ed9e5e5e9e42d91db1d263fd31d5793ca3e221f8941e08cffd65f13f5e494add51dc967cb3b920a9870891fef245f698b45d16cf206a0d17
7
+ data.tar.gz: '08dd8ceae26022158f9b0febe7b5b30891ccdf5f3ebeb089ffb987ca894870c3a2db6ce99782f96f6a482d8ecc06033088d842573ca494706557a384b4af9d35'
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- du_pretty (0.3.0)
4
+ du_pretty (0.3.1)
5
5
  colorize
6
6
  thor
7
7
 
data/README.md CHANGED
@@ -1,38 +1,44 @@
1
1
  # DuPretty
2
+ Show pretty print of `du` results.
2
3
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/du_pretty`. To experiment with that code, run `bin/console` for an interactive prompt.
4
-
5
- TODO: Delete this and the text above, and describe your gem
4
+ [![Image from Gyazo](https://i.gyazo.com/8a7911cf58bb4ac1aa586cc24c8a307e.png)](https://gyazo.com/8a7911cf58bb4ac1aa586cc24c8a307e)
6
5
 
7
6
  ## Installation
8
-
9
- Add this line to your application's Gemfile:
10
-
11
- ```ruby
12
- gem 'du_pretty'
7
+ ```sh
8
+ $ gem install 'du_pretty'
13
9
  ```
14
10
 
15
- And then execute:
11
+ ## Usage
12
+ ```sh
13
+ $ du_pretty <path>
16
14
 
17
- $ bundle
15
+ # specify max depth
16
+ $ du_pretty -d 2 <path>
18
17
 
19
- Or install it yourself as:
18
+ # show directories if its size is 100KB+
19
+ $ du_pretty --max-kbyte 100 <path>
20
20
 
21
- $ gem install du_pretty
21
+ # show directories sorted by its size
22
+ $ du_pretty --sort <path>
22
23
 
23
- ## Usage
24
+ # help
25
+ $ du_pretty --help
26
+ ```
24
27
 
25
- TODO: Write usage instructions here
26
28
 
27
29
  ## Development
30
+ ```sh
31
+ # run
32
+ $ bundle install
33
+ $ bundle exec exe/du_pretty
28
34
 
29
- After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
-
31
- To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
35
+ # test
36
+ $ bundle exec rspec spec
37
+ ```
32
38
 
33
39
  ## Contributing
34
40
 
35
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/du_pretty. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
41
+ Bug reports and pull requests are welcome on GitHub at https://github.com/nullnull/du_pretty. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
36
42
 
37
43
  ## License
38
44
 
data/lib/du_pretty/cli.rb CHANGED
@@ -1,33 +1,48 @@
1
1
  require 'du_pretty'
2
+ require 'du_pretty/utils'
2
3
  require 'thor'
3
4
 
4
5
  module DuPretty
5
6
  class CLI < Thor
6
7
  default_task :path
7
- desc 'path', ''
8
- option :min_gbyte, type: :numeric
9
- option :min_mbyte, type: :numeric
10
- option :min_kbyte, type: :numeric
11
- option :depth, type: :numeric, aliases: :d
12
- option :sort, aliases: :s, type: :boolean
8
+ desc 'path', <<~EOT
9
+ Details:
10
+ -a, --all
11
+ Display an entry for each file in a file hierarchy.
12
+
13
+ -d depth
14
+ Display an entry for all files and directories depth directories deep.
15
+
16
+ --size n[kMGT]
17
+ Filter results of `du` by its filesize. if n is followed by a scale indicator then the file's size is compared to n scaled as:
18
+ k(default) kilobytes (1024 bytes)
19
+ M megabytes (1024 kilobytes)
20
+ G gigabytes (1024 megabytes)
21
+ T terabytes (1024 gigabytes)
22
+
23
+ --sort
24
+ Show results sorted by the file's size.
25
+ EOT
13
26
  option :all, aliases: :a, type: :boolean
27
+ option :depth, type: :numeric, aliases: :d
28
+ option :size, aliases: :s
29
+ option :sort, type: :boolean
14
30
  option :tree, type: :boolean, default: true
15
31
 
16
32
  def path(path = '.')
17
- min_kbyte = [(options[:min_gbyte] || 0) * 1024 * 1024, (options[:min_mbyte] || 0) * 1024, (options[:min_kbyte] || 0)].max
18
- du_wrapper = DuWrapper.new(
33
+ prettier = DuPretty::Prettier.new(
19
34
  path,
20
- min_kbyte: min_kbyte,
35
+ min_kbyte: DuPretty::Utils.size_to_kbyte(options[:size]),
21
36
  depth: options[:depth],
22
- with_files: options[:all],
37
+ with_files: options[:all]
23
38
  )
24
39
  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
40
+ prettier.sorted
41
+ elsif options[:tree] == false
42
+ prettier.original
43
+ else
44
+ prettier.tree
45
+ end
31
46
  print result + "\n"
32
47
  end
33
48
  end
@@ -0,0 +1,116 @@
1
+ require 'colorize'
2
+ require 'pathname'
3
+
4
+ module DuPretty
5
+ class Prettier
6
+ def initialize(path, min_kbyte: 0, depth: nil, with_files: false)
7
+ @path = File.expand_path(path, Pathname.pwd)
8
+ @min_kbyte = min_kbyte
9
+ @depth = depth
10
+ @with_files = with_files
11
+ end
12
+
13
+ def original
14
+ filtered_disk_usages.map(&:pretty).join("\n")
15
+ end
16
+
17
+ def sorted
18
+ filtered_disk_usages.sort_by(&:kbyte).map(&:pretty).join("\n")
19
+ end
20
+
21
+ def tree
22
+ filtered_disk_usages.reverse.map(&:tree_format).join("\n")
23
+ end
24
+
25
+ private
26
+
27
+ def du
28
+ options = [
29
+ @depth.nil? ? nil : "-d #{@depth}",
30
+ @with_files ? '-a' : nil,
31
+ '-k'
32
+ ]
33
+ `du #{options.join(' ')} #{@path}`
34
+ end
35
+
36
+ def disk_usages
37
+ xs = du.split("\n").map { |line| DiskUsage.new(line, @path) }
38
+ total = xs.map(&:kbyte).max
39
+ xs.map { |x| DiskUsage.new(x.raw, @path, total: total) }
40
+ end
41
+
42
+ def filtered_disk_usages
43
+ disk_usages.select { |x| x.kbyte >= @min_kbyte }
44
+ end
45
+
46
+ class DiskUsage
47
+ attr_accessor :raw, :kbyte, :path
48
+
49
+ def initialize(raw, root, total: nil)
50
+ @raw = raw
51
+ @root = root
52
+ @kbyte = raw.split("\t").compact[0].to_i
53
+ @path = raw.split("\t").compact[1]
54
+ @total = total
55
+ end
56
+
57
+ def pretty
58
+ pretty_byte + "\t." + relative_path
59
+ end
60
+
61
+ def tree_format
62
+ pretty_path + ' ' + pretty_byte
63
+ end
64
+
65
+ private
66
+
67
+ def basename
68
+ Pathname.new(@path).basename.to_s
69
+ end
70
+
71
+ def depth
72
+ Pathname.new(relative_path).each_filename.to_a.size - 1
73
+ end
74
+
75
+ def pretty_byte
76
+ mb = @kbyte / 1024
77
+ gb = mb / 1024
78
+ if gb.positive?
79
+ if gb > 10
80
+ "#{gb}GB (#{percentage})".red
81
+ else
82
+ "#{gb}GB (#{percentage})".light_red
83
+ end
84
+ elsif mb.positive?
85
+ if mb > 500
86
+ "#{mb}MB (#{percentage})".yellow
87
+ else
88
+ "#{mb}MB (#{percentage})".light_yellow
89
+ end
90
+ else
91
+ if @kbyte > 500
92
+ "#{@kbyte}KB (#{percentage})".green
93
+ else
94
+ "#{@kbyte}KB (#{percentage})".light_green
95
+ end
96
+ end
97
+ end
98
+
99
+ def pretty_path
100
+ return '.' if @path == @root
101
+ spaces = depth.positive? ? ' ' * depth : ''
102
+ tree_symbol = '└── '
103
+ spaces + tree_symbol + basename
104
+ end
105
+
106
+ def percentage
107
+ percentage = @kbyte * 1.0 / @total * 100
108
+ "#{percentage.ceil}%"
109
+ end
110
+
111
+ def relative_path
112
+ @path.sub(/^#{@root}/, '')
113
+ end
114
+ end
115
+ end
116
+ end
@@ -0,0 +1,17 @@
1
+ module DuPretty
2
+ module Utils
3
+ def self.size_to_kbyte(size)
4
+ return 0 if size.nil?
5
+ size = size.upcase
6
+ if size.end_with?('T')
7
+ size.to_i * (1024**3)
8
+ elsif size.end_with?('G')
9
+ size.to_i * (1024**2)
10
+ elsif size.end_with?('M')
11
+ size.to_i * 1024
12
+ else
13
+ size.to_i
14
+ end
15
+ end
16
+ end
17
+ end
@@ -1,3 +1,3 @@
1
1
  module DuPretty
2
- VERSION = "0.3.0"
2
+ VERSION = '0.3.1'.freeze
3
3
  end
data/lib/du_pretty.rb CHANGED
@@ -1,119 +1,6 @@
1
- require 'colorize'
2
- require 'pathname'
3
-
4
1
  require 'du_pretty/version'
5
2
  require 'du_pretty/cli'
3
+ require 'du_pretty/prettier'
6
4
 
7
5
  module DuPretty
8
- class DuWrapper
9
- def initialize(path, min_kbyte: 0, depth: nil, with_files: false)
10
- @path = File.expand_path(path, Pathname.pwd)
11
- @min_kbyte = min_kbyte
12
- @depth = depth
13
- @with_files = with_files
14
- end
15
-
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")
26
- end
27
-
28
- private
29
-
30
- def du
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 }
47
- end
48
-
49
- class DiskUsage
50
- attr_accessor :raw, :kbyte, :path
51
-
52
- def initialize(raw, root, total: nil)
53
- @raw = raw
54
- @root = root
55
- @kbyte = raw.split("\t").compact[0].to_i
56
- @path = raw.split("\t").compact[1]
57
- @total = total
58
- end
59
-
60
- def pretty
61
- pretty_byte + "\t." + relative_path
62
- end
63
-
64
- def tree_format
65
- pretty_path + ' ' + pretty_byte
66
- end
67
-
68
- private
69
-
70
- def basename
71
- Pathname.new(@path).basename.to_s
72
- end
73
-
74
- def depth
75
- Pathname.new(relative_path).each_filename.to_a.size - 1
76
- end
77
-
78
- def pretty_byte
79
- mb = @kbyte / 1024
80
- gb = mb / 1024
81
- if gb.positive?
82
- if gb > 10
83
- "#{gb}GB (#{percentage})".red
84
- else
85
- "#{gb}GB (#{percentage})".light_red
86
- end
87
- elsif mb.positive?
88
- if mb > 500
89
- "#{mb}MB (#{percentage})".yellow
90
- else
91
- "#{mb}MB (#{percentage})".light_yellow
92
- end
93
- else
94
- if @kbyte > 500
95
- "#{@kbyte}KB (#{percentage})".green
96
- else
97
- "#{@kbyte}KB (#{percentage})".light_green
98
- end
99
- end
100
- end
101
-
102
- def pretty_path
103
- return '.' if @path == @root
104
- spaces = depth.positive? ? ' ' * depth : ''
105
- tree_symbol = '└── '
106
- spaces + tree_symbol + basename
107
- end
108
-
109
- def percentage
110
- percentage = @kbyte * 1.0 / @total * 100
111
- "#{percentage.ceil}%"
112
- end
113
-
114
- def relative_path
115
- @path.sub(/^#{@root}/, '')
116
- end
117
- end
118
- end
119
6
  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.3.0
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Katsuma Narisawa
@@ -102,6 +102,8 @@ files:
102
102
  - exe/du_pretty
103
103
  - lib/du_pretty.rb
104
104
  - lib/du_pretty/cli.rb
105
+ - lib/du_pretty/prettier.rb
106
+ - lib/du_pretty/utils.rb
105
107
  - lib/du_pretty/version.rb
106
108
  homepage: https://github.com/nullnull/du_pretty
107
109
  licenses: