branding 0.0.1 → 0.0.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: 3688ed4157906260e426018c08b5236dbcf7a75b
4
- data.tar.gz: 3914f9b09f648a42dc171727409fb66ac35867f5
3
+ metadata.gz: 98c7c50a74686570bba45b496c478e1e3e34dbcc
4
+ data.tar.gz: 631b8ac04bd4f9aea66443e4c5224535bc32025d
5
5
  SHA512:
6
- metadata.gz: ad2c13b8bba0648fd37e98a19e36e83ab17314ba96570738417d3d4257559eb018e3f499976676de023117afa4be2d23163dc220aad4611ba953fa4f804266a9
7
- data.tar.gz: 853164e000f97ce4101213e664eec010a05bce557c099f2fdc116f7ff7cd46dc618f9a59ddc803e3620b43ba4ac6386723ce80d5c4bd053e7d4fb57487c5a5ec
6
+ metadata.gz: 575a40f1e189d2ceee0614e12e4773e8c73c4c90c46ca39cad7a3fcc781190f0a65ae59cdfaa23f69bb4483958c883cca484de3c70cbddcc3953d5b009fc3fad
7
+ data.tar.gz: 8526484d7c9776793bed44e8de82efb7f56f30c02c40dc4c963f01beb58733dc899c3edca79d5ba193bbfd279a3f54a39390f9e015df8dc5b7948eae3d929bfa
data/.rubocop.yml ADDED
@@ -0,0 +1,2 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.2
data/README.md CHANGED
@@ -6,17 +6,19 @@ Proud of your code? Add some bling to your terminal.
6
6
 
7
7
  * No dependencies!
8
8
  * Pure Ruby PNG decoding in < 200 LoC
9
+ * Hi-Res mode for 2x pixel density!
10
+ * Hi-Color mode for 720 colors!!
9
11
  * Loads of fun!
10
12
 
11
13
  ## Quality
12
14
 
13
- Much quality:
15
+ Much quality.
14
16
 
15
17
  ## How it works
16
18
 
17
19
  Branding will detect your Rails application's favicon. It adds a small
18
20
  initializer in development and testing environments that draws the favicon with
19
- ANSI control characters.
21
+ ANSI control characters whenever you run tests, a rake task or boot the console.
20
22
 
21
23
  ## Installation
22
24
 
@@ -4,14 +4,34 @@ module Branding
4
4
  class Canvas
5
5
  attr_reader :width, :height, :rows, :cols
6
6
 
7
- def initialize(width:,height:)
7
+ def self.terminal_size
8
+ # TODO: make sure we can get this on linux
9
+ begin
10
+ `stty size`.split.map(&:to_i)
11
+ rescue
12
+ [40, 100]
13
+ end
14
+ end
15
+
16
+ def initialize(width:, height:)
8
17
  @width, @height = width, height
9
- @rows, @cols = `stty size`.split.map { |x| x.to_i }
18
+ @rows, @cols = self.class.terminal_size
10
19
  @pixel_buffer = []
11
20
  end
12
21
 
13
- def load(pixels)
14
- Pixel2x.load_strategy(pixels, width: width, height: height) do |pixel|
22
+ def load(pixels, algo: :normal)
23
+ case algo
24
+ when :normal
25
+ klass = Pixel
26
+ when :hires
27
+ klass = Pixel2x
28
+ when :hicolor
29
+ raise 'Hi-Color coming soon!'
30
+ else
31
+ raise "Unknown pixel algo `#{algo}`"
32
+ end
33
+
34
+ klass.load_strategy(pixels, width: width, height: height) do |pixel|
15
35
  @pixel_buffer << pixel
16
36
  end
17
37
  end
data/lib/branding/cli.rb CHANGED
@@ -6,13 +6,15 @@ module Branding
6
6
  def initialize(args)
7
7
  @options = OpenStruct.new
8
8
 
9
- @options.command = :print_logo
10
9
  @options.file = args.last
11
10
 
12
11
  @parser = OptionParser.new do |opts|
13
12
  opts.banner = 'Usage: branding FILE'
14
- opts.on('-a') do |algo|
15
- @options = algo
13
+ opts.on('-p PIXEL',
14
+ '--pixel=PIXEL',
15
+ [:normal, :hires, :hicolor],
16
+ 'The pixel rendering algorithm (`normal`, `hires`, or `hicolor`)') do |pixel_algo|
17
+ @options.algo = pixel_algo.to_sym
16
18
  end
17
19
 
18
20
  opts.on_tail('-h', '--help', 'Show this message') do
@@ -26,11 +28,8 @@ module Branding
26
28
  end
27
29
 
28
30
  def run
29
- send(@options.command)
30
- end
31
-
32
- def print_logo
33
31
  logo = Branding::Logo.new(@options.file)
32
+ logo.algo = @options.algo
34
33
  logo.print
35
34
  end
36
35
  end
data/lib/branding/logo.rb CHANGED
@@ -1,12 +1,15 @@
1
1
  module Branding
2
- class Logo
2
+ class Logo # :nodoc:
3
+ attr_accessor :algo
4
+
3
5
  def initialize(path)
6
+ @algo = :normal
4
7
  @img = PNG.from_file(path)
5
8
  @canvas = Canvas.new(width: @img.width, height: @img.height)
6
9
  end
7
10
 
8
11
  def print
9
- @canvas.load(@img.pixels)
12
+ @canvas.load(@img.pixels, algo: @algo)
10
13
  @canvas.print
11
14
  nil
12
15
  end
@@ -3,21 +3,40 @@ module Branding
3
3
 
4
4
  unless Rails.env.production?
5
5
  initializer('branding.print_logo') do
6
- logo = Branding::Logo.new(Branding::Railtie.favicons)
7
- logo.print
6
+ begin
7
+ rows, cols = Canvas.terminal_size
8
+ ideal_width = cols / 6
9
+
10
+ logo = Branding::Logo.new(Branding::Railtie.best_icon(ideal_width))
11
+ logo.algo = :hires
12
+ logo.print
13
+ print "\n"
14
+ rescue
15
+ # We don't want to do anything if this causes an exception. Your time
16
+ # is too valuable to be dealing with busted amusement gems.
17
+ end
8
18
  end
9
19
  end
10
20
 
11
21
  ##
12
22
  # find the best suited icon in a rails app
13
- def self.favicons
23
+ def self.best_icon(ideal_width)
24
+ paths = icon_paths.sort_by do |path|
25
+ png = PNG.from_file(path)
26
+ (ideal_width - png.width).abs
27
+ end
28
+
29
+ if paths.empty?
30
+ else
31
+ paths.first
32
+ end
33
+ end
34
+
35
+ def self.icon_paths
14
36
  paths = ["#{Rails.root}/public/", "#{Rails.root}/app/assets/images/"]
15
37
  file_patterns = ['favicon*.png', 'apple-touch-icon*.png']
16
38
  patterns = paths.product(file_patterns).map(&:join)
17
-
18
- matches = Dir.glob(patterns)
19
-
20
- matches.sort_by{|path| File.stat(path).size }.first
39
+ Dir.glob(patterns)
21
40
  end
22
41
  end
23
42
  end
@@ -1,3 +1,3 @@
1
1
  module Branding
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -2,6 +2,6 @@ require 'spec_helper'
2
2
 
3
3
  describe Branding do
4
4
  it 'should have a version number' do
5
- Branding::VERSION.should_not be_nil
5
+ expect(Branding::VERSION).to_not be_nil
6
6
  end
7
7
  end
Binary file
data/spec/png_spec.rb CHANGED
@@ -1,14 +1,12 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  RSpec.describe Branding::PNG do
4
- it 'returns a pixel matrix' do
4
+ it 'returns a pixel list' do
5
5
  png = Branding::PNG.from_file(fixture_file('test3x3.png'))
6
- expect(png.pixels).to eq(
7
- [
8
- [0x111111, 0xc6c6c6, 0xffffff],
9
- [0xc6c6c6, 0x111111, 0xc6c6c6],
10
- [0xffffff, 0xc6c6c6, 0x111111]
11
- ]
12
- )
6
+ expect(png.pixels).to eq([
7
+ 0x111111ff, 0xc6c6c6ff, 0xffffffff,
8
+ 0xc6c6c6ff, 0x111111ff, 0xc6c6c6ff,
9
+ 0xffffffff, 0xc6c6c6ff, 0x111111ff
10
+ ])
13
11
  end
14
12
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: branding
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Stefan Natchev
@@ -90,6 +90,7 @@ extra_rdoc_files: []
90
90
  files:
91
91
  - ".gitignore"
92
92
  - ".rspec"
93
+ - ".rubocop.yml"
93
94
  - ".travis.yml"
94
95
  - Gemfile
95
96
  - LICENSE.txt
@@ -107,6 +108,7 @@ files:
107
108
  - lib/branding/railtie.rb
108
109
  - lib/branding/version.rb
109
110
  - spec/branding_spec.rb
111
+ - spec/fixtures/test3x3.png
110
112
  - spec/pixel_spec.rb
111
113
  - spec/png_spec.rb
112
114
  - spec/spec_helper.rb
@@ -136,6 +138,7 @@ specification_version: 4
136
138
  summary: Print your logo to the terminal.
137
139
  test_files:
138
140
  - spec/branding_spec.rb
141
+ - spec/fixtures/test3x3.png
139
142
  - spec/pixel_spec.rb
140
143
  - spec/png_spec.rb
141
144
  - spec/spec_helper.rb