catpix 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 1f6ffca3e7a8a1ecda737a7035cfc6098464c0f6
4
+ data.tar.gz: e8477a9b2b92e321275044fc0a4bcb3a8a5f5525
5
+ SHA512:
6
+ metadata.gz: c75fcf501af41168264127be579d69a62029649a4e134e846aadf6143b37e6279b62392e9ed76da1f5640d5809bbb3023a99ceff4e604ba564d3ea2b8a517fe5
7
+ data.tar.gz: c8add99b4d071378f05a1f3e954214ea3346de0bf6a5dce16065630da857f62b3b52d2a25e17ef499cf3180e141c7d19836e2ff1c86df70635da467581e81a41
@@ -0,0 +1,22 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in catpix.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Radek Pazdera
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,29 @@
1
+ # Catpix
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Usage
6
+
7
+ TODO: Write usage instructions here
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ gem 'catpix'
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install catpix
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it ( https://github.com/[my-github-username]/catpix/fork )
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create a new Pull Request
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,55 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'docopt'
4
+ require 'catpix'
5
+
6
+ doc = <<DOCOPT
7
+ Print images in the terminal.
8
+
9
+ Usage:
10
+ #{__FILE__} --help | --version
11
+ #{__FILE__} [options] <path>
12
+
13
+ Options:
14
+ -w=<width>, --limit-width Limit width of the image (factor of the size
15
+ of the terminal window) [default: 1].
16
+ -h=<height>, --limit-height Limit height of the image (factor of the size
17
+ of the terminal window) [default: 0],
18
+ -c=<pos>, --center Set x, y or xy centering in the window.
19
+ -b=<colour>, --bg Set background colour.
20
+ -f, --bg-fill Draw background around the image as well.
21
+
22
+ --help Show this message.
23
+ --version Print the version.
24
+ DOCOPT
25
+
26
+ begin
27
+ args = Docopt::docopt doc, :version => Catpix::VERSION
28
+ rescue Docopt::Exit => e
29
+ $stderr.puts e.message
30
+ exit 1
31
+ end
32
+
33
+ require 'pp'
34
+
35
+ pp args
36
+ options = {
37
+ :limit_x => args['--limit-width'].to_f,
38
+ :limit_y => args['--limit-height'].to_f,
39
+ :bg_fill => args['--bg-fill']
40
+ }
41
+
42
+ if args['--center']
43
+ options[:center_x] = true if args['--center'].include? 'x'
44
+ options[:center_y] = true if args['--center'].include? 'y'
45
+
46
+ if not args['--center'].include? 'x' or not args['--center'].include? 'x'
47
+ $stderr.puts "Error: Invalid --center argument. Use x, y or xy."
48
+ $stderr.puts doc
49
+ end
50
+ end
51
+
52
+ options[:bg] = args['--bg'] if args['--bg']
53
+
54
+ Catpix::print_image args['<path>'], options
55
+ exit 0
@@ -0,0 +1,31 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'catpix/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "catpix"
8
+ spec.version = Catpix::VERSION
9
+ spec.authors = ["Radek Pazdera"]
10
+ spec.email = ["me@radek.io"]
11
+ spec.summary = %q{Cat images into the terminal.}
12
+ spec.description = %q{A library and a script that lets you easily print
13
+ images (png, jpg, gif and others) inside the command
14
+ line using imagemagick and the extended 256 colour
15
+ palette.}
16
+ spec.homepage = "https://github.com/pazdera/catpix"
17
+ spec.license = "MIT"
18
+
19
+ spec.files = `git ls-files -z`.split("\x0")
20
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
21
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
22
+ spec.require_paths = ["lib"]
23
+
24
+ spec.add_dependency "tco", "~> 0.1", ">= 0.1.6"
25
+ spec.add_dependency "rmagick", "~> 2.15", ">= 2.15.2"
26
+ spec.add_dependency "docopt", "~> 0.5", ">= 0.5.0"
27
+ spec.add_dependency "ruby-terminfo", "~> 0.1", ">= 0.1.1"
28
+
29
+ spec.add_development_dependency "bundler", "~> 1.6"
30
+ spec.add_development_dependency "rake", "~> 10.4"
31
+ end
@@ -0,0 +1,75 @@
1
+ require "catpix/version"
2
+ require "catpix/private"
3
+
4
+ # Provides a function to print images in the terminal. A range of different
5
+ # formats is supported (check out what ImageMagick supports). Under the hood,
6
+ # this module uses two components:
7
+ #
8
+ # * [rmagick](https://rmagick.github.io/) to read and scale the images and
9
+ # * [tco](https://github.com/pazdera/tco) to map their pixels on the extended
10
+ # colour palette in the terminal.
11
+ #
12
+ # Some other minor features like centering and handling background colours
13
+ # are supplied directly by this module.
14
+ module Catpix
15
+ # Print an image to the terminal.
16
+ #
17
+ # All formats supported by ImageMagick are supported. The image's colours
18
+ # will be mapped onto the extended 256 colour palette. Also by default, it
19
+ # will be scaled down to fit the width of the terminal while keeping its
20
+ # proportions. This can be changed using the `options` parameter.
21
+ #
22
+ # @param [Hash] options Adjust some parameters of the image when printed.
23
+ # @option options [Float] :limit_x A factor of the terminal window's width.
24
+ # If present, the image will be scaled down
25
+ # to fit (proportions are kept). Using 0
26
+ # will disable the scaling. [default: 1.0]
27
+ # @option options [Float] :limit_y A factor of the terminal window's height.
28
+ # If present, the image will be scaled down
29
+ # to fit (proportions are kept). Using 0
30
+ # will disable the scaling. [default: 0]
31
+ # @option options [Boolean] :center_x Center the image horizontally in the
32
+ # terminal window. [default: false]
33
+ # @option options [Boolean] :center_y Center the image vertically in the
34
+ # terminal window. [default: false]
35
+ # @option options [String] :bg Background colour to use in case there are
36
+ # any fully transparent pixels in the image.
37
+ # This can be a RGB value '#c0ffee' or a tco
38
+ # alias 'red' or 'blue'. [default: nil]
39
+ # @option options [Boolean] :bg_fill Fill the margins around the image with
40
+ # background colour. [default: false]
41
+ def self.print_image(path, options={})
42
+ options = default_options.merge! options
43
+
44
+ img = load_image path
45
+ resize! img, options[:limit_x], options[:limit_y]
46
+
47
+ margins = get_margins img, options[:center_x], options[:center_y]
48
+ margin_colour = options[:bg_fill] ? options[:bg] : nil
49
+
50
+ print_vert_margin margins[:top], margin_colour
51
+
52
+ # print left margin for the first row
53
+ print_horiz_margin margins[:left], margin_colour
54
+
55
+ # print the image
56
+ img.each_pixel do |pixel, col, row|
57
+ if pixel.opacity == 65535
58
+ print_pixel options[:bg]
59
+ else
60
+ print_pixel get_normal_rgb pixel
61
+ end
62
+
63
+ if col >= img.columns - 1
64
+ print_horiz_margin margins[:right], margin_colour
65
+ puts
66
+
67
+ unless row == img.rows - 1
68
+ print_horiz_margin margins[:left], margin_colour
69
+ end
70
+ end
71
+ end
72
+
73
+ print_vert_margin margins[:bottom], margin_colour
74
+ end
75
+ end
@@ -0,0 +1,107 @@
1
+
2
+ require "rmagick"
3
+ require "tco"
4
+ require "terminfo"
5
+
6
+ module Catpix
7
+ private
8
+ def self.default_options
9
+ {
10
+ :limit_x => 1.0,
11
+ :limit_y => 0,
12
+ :center_x => false,
13
+ :center_y => false,
14
+ :bg => nil,
15
+ :bg_fill => false
16
+ }
17
+ end
18
+
19
+ def self.print_pixel(colour=nil)
20
+ if colour
21
+ print " ".bg colour
22
+ else
23
+ print " "
24
+ end
25
+ end
26
+
27
+ # Returns normalised size of the terminal window
28
+ #
29
+ # Catpix uses two blank characters to approximate pixels in the terminal,
30
+ # so we need to divide the width of the terminal by 2.
31
+ def self.get_screen_size
32
+ th, tw = TermInfo.screen_size
33
+ [tw / 2, th]
34
+ end
35
+
36
+ def self.load_image(path)
37
+ Magick::Image::read(path).first
38
+ end
39
+
40
+ def self.resize!(img, limit_x=0, limit_y=0)
41
+ tw, th = get_screen_size
42
+ iw = img.columns
43
+ ih = img.rows
44
+
45
+ width = if limit_x > 0
46
+ (tw * limit_x).to_i
47
+ else
48
+ iw
49
+ end
50
+
51
+ height = if limit_y > 0
52
+ (th * limit_y).to_i
53
+ else
54
+ ih
55
+ end
56
+
57
+ # Resize the image if it's bigger than the limited viewport
58
+ if iw > width or ih > height
59
+ img.change_geometry "#{width}x#{height}" do |cols, rows, img|
60
+ img.resize! (cols).to_i, (rows).to_i
61
+ end
62
+ end
63
+ end
64
+
65
+ # Returns the normalised RGB of a ImageMagick's pixel
66
+ def self.get_normal_rgb(pixel)
67
+ [pixel.red, pixel.green, pixel.blue].map { |v| 255*(v/65535.0) }
68
+ end
69
+
70
+ # Determine the margins based on the centering options
71
+ def self.get_margins(img, center_x, center_y)
72
+ margins = {}
73
+ tw, th = get_screen_size
74
+
75
+ x_space = tw - img.columns
76
+ if center_x
77
+ margins[:left] = x_space / 2
78
+ margins[:right] = x_space / 2 + x_space % 2
79
+ else
80
+ margins[:left] = 0
81
+ margins[:right] = x_space
82
+ end
83
+
84
+ y_space = th - img.rows
85
+ if center_y
86
+ margins[:top] = y_space / 2
87
+ margins[:bottom] = y_space / 2 + y_space % 2
88
+ else
89
+ margins[:top] = 0
90
+ margins[:bottom] = 0
91
+ end
92
+
93
+ margins
94
+ end
95
+
96
+ def self.print_vert_margin(size, colour)
97
+ tw, th = get_screen_size
98
+ size.times do
99
+ tw.times { print_pixel colour }
100
+ puts
101
+ end
102
+ end
103
+
104
+ def self.print_horiz_margin(size, colour)
105
+ size.times { print_pixel colour }
106
+ end
107
+ end
@@ -0,0 +1,4 @@
1
+ module Catpix
2
+ # Version of the gem
3
+ VERSION = "0.1.0"
4
+ end
metadata ADDED
@@ -0,0 +1,168 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: catpix
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Radek Pazdera
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-06-28 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: tco
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.1'
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 0.1.6
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - "~>"
28
+ - !ruby/object:Gem::Version
29
+ version: '0.1'
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: 0.1.6
33
+ - !ruby/object:Gem::Dependency
34
+ name: rmagick
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '2.15'
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: 2.15.2
43
+ type: :runtime
44
+ prerelease: false
45
+ version_requirements: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - "~>"
48
+ - !ruby/object:Gem::Version
49
+ version: '2.15'
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: 2.15.2
53
+ - !ruby/object:Gem::Dependency
54
+ name: docopt
55
+ requirement: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - "~>"
58
+ - !ruby/object:Gem::Version
59
+ version: '0.5'
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: 0.5.0
63
+ type: :runtime
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - "~>"
68
+ - !ruby/object:Gem::Version
69
+ version: '0.5'
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: 0.5.0
73
+ - !ruby/object:Gem::Dependency
74
+ name: ruby-terminfo
75
+ requirement: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - "~>"
78
+ - !ruby/object:Gem::Version
79
+ version: '0.1'
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: 0.1.1
83
+ type: :runtime
84
+ prerelease: false
85
+ version_requirements: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '0.1'
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ version: 0.1.1
93
+ - !ruby/object:Gem::Dependency
94
+ name: bundler
95
+ requirement: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - "~>"
98
+ - !ruby/object:Gem::Version
99
+ version: '1.6'
100
+ type: :development
101
+ prerelease: false
102
+ version_requirements: !ruby/object:Gem::Requirement
103
+ requirements:
104
+ - - "~>"
105
+ - !ruby/object:Gem::Version
106
+ version: '1.6'
107
+ - !ruby/object:Gem::Dependency
108
+ name: rake
109
+ requirement: !ruby/object:Gem::Requirement
110
+ requirements:
111
+ - - "~>"
112
+ - !ruby/object:Gem::Version
113
+ version: '10.4'
114
+ type: :development
115
+ prerelease: false
116
+ version_requirements: !ruby/object:Gem::Requirement
117
+ requirements:
118
+ - - "~>"
119
+ - !ruby/object:Gem::Version
120
+ version: '10.4'
121
+ description: |-
122
+ A library and a script that lets you easily print
123
+ images (png, jpg, gif and others) inside the command
124
+ line using imagemagick and the extended 256 colour
125
+ palette.
126
+ email:
127
+ - me@radek.io
128
+ executables:
129
+ - catpix
130
+ extensions: []
131
+ extra_rdoc_files: []
132
+ files:
133
+ - ".gitignore"
134
+ - Gemfile
135
+ - LICENSE.txt
136
+ - README.md
137
+ - Rakefile
138
+ - bin/catpix
139
+ - catpix.gemspec
140
+ - lib/catpix.rb
141
+ - lib/catpix/private.rb
142
+ - lib/catpix/version.rb
143
+ homepage: https://github.com/pazdera/catpix
144
+ licenses:
145
+ - MIT
146
+ metadata: {}
147
+ post_install_message:
148
+ rdoc_options: []
149
+ require_paths:
150
+ - lib
151
+ required_ruby_version: !ruby/object:Gem::Requirement
152
+ requirements:
153
+ - - ">="
154
+ - !ruby/object:Gem::Version
155
+ version: '0'
156
+ required_rubygems_version: !ruby/object:Gem::Requirement
157
+ requirements:
158
+ - - ">="
159
+ - !ruby/object:Gem::Version
160
+ version: '0'
161
+ requirements: []
162
+ rubyforge_project:
163
+ rubygems_version: 2.2.2
164
+ signing_key:
165
+ specification_version: 4
166
+ summary: Cat images into the terminal.
167
+ test_files: []
168
+ has_rdoc: