banhpho 0.1.4 → 0.1.5

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: d896092aad733d4690ade46adde34f6198fcb8f8
4
- data.tar.gz: e406a48df3513d4f891166ab951bedd4962a5c37
3
+ metadata.gz: 751cc27910314973aaeb03ef4ce9616034170913
4
+ data.tar.gz: a9a63e818cbba4478faf9098d8484e59b8d9148c
5
5
  SHA512:
6
- metadata.gz: fd8fdd61ea151777c3b1aa5b62d022e8d4b6bd39fd30245ef450d12321480a969cb4ec521b4d889e8bd77b4a8534ee84077b0b13dbe8dc05bb223a0d7659167a
7
- data.tar.gz: 93502506797490b53748b10befd3d1e0f0ec7863d7b86b99d56ca2a6f9b297203cd4ea7011f8538dd7fb4ac59002a60a1e6e749b40694fcf0a9ea36834460b84
6
+ metadata.gz: 59f1453d94cb2bfb7b53a1fa4e32aeb838296b019cdc255b274f56a33c5f5d111c0bba40ee02e2fc399cb36f10f442ac2c4caa7f8402631287f60c7d783bb19f
7
+ data.tar.gz: 08f059a1ca67b8a6b82426c208836a5de81d940527894ae9e10860378225d33ffcb5f03569277b1d91b29728cb3a319e4601098e22524772ad963c8ed18edc96
data/Gemfile CHANGED
@@ -2,4 +2,7 @@ source 'https://rubygems.org'
2
2
 
3
3
  # Specify your gem's dependencies in banhpho.gemspec
4
4
  gemspec
5
- gem 'thor', '~> 0.19.1'
5
+ gem 'thor', '~> 0.19.1'
6
+ gem 'colorize'
7
+ gem 'progress_bar'
8
+ gem 'terminal-table'
data/README.md CHANGED
@@ -1,26 +1,20 @@
1
- # Banh-Pho
1
+ [![Gem Version](https://badge.fury.io/rb/banhpho.svg)](https://badge.fury.io/rb/banhpho)
2
2
 
3
- A wrapper of [pngquant](https://pngquant.org/) for compressing all PNG files in a directory.
4
-
5
- ## Installation
3
+ # Banh Pho
6
4
 
7
- Add this line to your application's Gemfile:
8
-
9
- ```ruby
10
- gem 'banhpho'
11
- ```
5
+ A wrapper of [pngquant](https://pngquant.org/) for compressing all PNG files in a directory.
12
6
 
13
- And then execute:
7
+ ## Dependency
14
8
 
15
- $ bundle
9
+ Make sure you already download [pngquant](https://pngquant.org/releases.html) and put its location in `PATH` so `banhpho` can locate the executable.
16
10
 
17
- Or install it yourself as:
11
+ ## Installation
18
12
 
19
13
  $ gem install banhpho
20
14
 
21
15
  ## Usage
22
16
 
23
- TODO: Write usage instructions here
17
+ $ banhpho compress [DIR]
24
18
 
25
19
  ## Development
26
20
 
data/lib/banhpho/cli.rb CHANGED
@@ -1,22 +1,41 @@
1
1
  require "thor"
2
+ require 'colorize'
3
+ require 'progress_bar'
4
+ require 'terminal-table'
2
5
 
3
6
  module Banhpho
4
7
  class CLI < Thor
5
8
 
6
9
  desc "compress DIR", "compress all png files in DIR"
7
10
  def compress(dir)
8
- @count = 0
9
- @originalSize = 0
10
- @newSize = 0
11
-
12
11
  abort("Error: command pngquant is not found, please download it from https://pngquant.org/ and put it in your global path") unless command?("pngquant")
13
12
  abort("Error: argument '#{dir}' is not a valid directory") unless File.directory?(dir)
14
13
 
15
- compressInDir(dir)
16
- puts("==========")
17
- puts("#{@count} files compressed!")
18
- if @originalSize > 0 then
19
- puts("original size: #{prettyFileSize(@originalSize)}, after compression: #{prettyFileSize(@newSize)}, compression ratio: #{(1 - @newSize.to_f / @originalSize).round(4) * 100}%")
14
+ originalSize = 0
15
+ newSize = 0
16
+
17
+ imgs = []
18
+ compressInDir(dir) do |f|
19
+ imgs << f
20
+ originalSize += File.size(f)
21
+ end
22
+ bar = ProgressBar.new(imgs.size)
23
+ imgs.each do |f|
24
+ %x(pngquant --force --ext .png --skip-if-larger -- #{f})
25
+ bar.increment!
26
+ newSize += File.size(f)
27
+ end
28
+
29
+ count = imgs.size
30
+
31
+ if count > 0
32
+ rows = []
33
+ rows << ['Number of files', count]
34
+ rows << ['Original size', prettyFileSize(originalSize)]
35
+ rows << ['New size', prettyFileSize(newSize)]
36
+ rows << ['Compression ratio', "#{(1 - newSize.to_f / originalSize).round(4) * 100}%"]
37
+ report = Terminal::Table.new :title => 'Task completed', :rows => rows
38
+ puts report
20
39
  end
21
40
  end
22
41
 
@@ -26,24 +45,24 @@ module Banhpho
26
45
  puts VERSION
27
46
  end
28
47
 
48
+ desc "author", "print the author info"
49
+ def author()
50
+ puts 'Created by heartace [love@heartace.me], with ' + '♥'.colorize(:magenta)
51
+ end
52
+
29
53
  no_commands do
30
54
  def compressInDir (dir)
31
55
  Dir.foreach(dir) do |item|
32
56
  next if item == "." or item == ".."
33
57
  path = File.join(dir, item)
34
58
  if File.directory?(path)
35
- puts("checking directory: #{path}")
36
59
  compressInDir(path)
37
60
  next
38
61
  end
39
62
  ext = File.extname(path).downcase
40
63
  if ext == ".png" then
41
- @originalSize += File.size(path)
42
- exePath = path.gsub(/ /, "\\ ")
43
- puts(" compressing file: #{path}")
44
- %x(pngquant --force --ext .png --skip-if-larger -- #{exePath})
45
- @count += 1
46
- @newSize += File.size(path)
64
+ fixedPath = path.gsub(/ /, "\\ ")
65
+ yield fixedPath
47
66
  end
48
67
  end
49
68
  end
@@ -1,3 +1,3 @@
1
1
  module Banhpho
2
- VERSION = "0.1.4"
2
+ VERSION = "0.1.5"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: banhpho
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - heartace
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-12-11 00:00:00.000000000 Z
11
+ date: 2015-12-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler