terminal_image 0.1.0 → 0.2.0

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: 0221335b48716baa73eb3244232c02edf4586d0b471bca4a944d88829949f329
4
- data.tar.gz: 7eaeb2539c60204c43dbffd6cf9687ab8f84d2d6e66ce9ddf16a73188789539d
3
+ metadata.gz: b16055f18d2792f24876eba1985bc1c1cb270c50cb3fe824bd5d30b714194798
4
+ data.tar.gz: c2579200f84591705dcc3b48fc0bd7b0c440805fd3f09e80cdea88e4bb5efc7a
5
5
  SHA512:
6
- metadata.gz: 00ac13d148881d250b29e24dd2ca0d4fb003f855bb54fdb6630f9185d7593a911c0d2142c9bcfa653c5566877fbc747c7cb65b46ea9880a2bed5f7713abd53c5
7
- data.tar.gz: fd193359a153c2afd1d0d5180cb8015da9181df86e6d1ad80e0a3c762cc8e1f70af3463a6c3239a0e93cd17fdbd22e26744c9bd985a80714d8f78735b27c9c31
6
+ metadata.gz: 64fec55d5edf74ff3c7f23aad8f6002f8225b2e653a9f6f56d82e3336e30f96407db93f2d392e5542189d11c4afdc1067ffa4fb1088fe752c7addc9a3be3788b
7
+ data.tar.gz: 07266ff60380f468933a3912cc049dddb16eb5695f116c3638fb5003213e006d25c4ea771cd0cbe46bead07c47a04d3be717d8055b8805310c95760d71923c0e
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- terminal_image (0.1.0)
4
+ terminal_image (0.2.0)
5
5
  fastimage
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -1,6 +1,7 @@
1
1
  # TerminalImage
2
2
 
3
3
  TerminalImage is a library to show images on terminals.
4
+ Supporting iTerm2 and terminals which has installed [libsixel](https://github.com/saitoha/libsixel).
4
5
 
5
6
  ![](./images/example.png)
6
7
 
@@ -15,8 +16,9 @@ gem 'terminal_image'
15
16
  ## Usage
16
17
 
17
18
  ```ruby
18
- file = File.open('your-image-path.png')
19
- TerminalImage.show(file) # will show image on your terminal
19
+ TerminalImage.show(File.open('your-image-path.png')) # will show image on your terminal
20
+ TerminalImage.show_url('https://raw.githubusercontent.com/unhappychoice/terminal_image/master/images/sample.png')
21
+ puts TerminalImage.encode(File.open('your-image-path.png')) # will encode image to string which can be displayed in terminals
20
22
  ```
21
23
 
22
24
  ## Contributing
@@ -1,3 +1,3 @@
1
1
  module TerminalImage
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -1,25 +1,54 @@
1
1
  require "terminal_image/version"
2
2
  require 'fastimage'
3
+ require 'open-uri'
4
+ require 'tempfile'
3
5
 
4
6
  module TerminalImage
5
7
  class UnsupportedTerminal < StandardError; end
6
8
 
7
9
  class << self
10
+ def show_url(url)
11
+ tempfile = Tempfile.create
12
+ tempfile.binmode
13
+ URI.open(url) { |o| tempfile.write o.read }
14
+ TerminalImage.show(tempfile)
15
+ end
16
+
8
17
  def show(file)
18
+ puts encode(file)
19
+ end
20
+
21
+ def encode(file)
9
22
  if ENV['TERM_PROGRAM'] == 'iTerm.app'
10
- show_on_iterm2(file)
23
+ encode_for_iterm2(file)
24
+ elsif which 'img2sixel'
25
+ encode_for_libsixel(file)
11
26
  else
27
+ puts 'Use iTerm2 or install libsixel according to https://github.com/saitoha/libsixel#install'
12
28
  raise UnsupportedTerminal, 'Unsupported terminal'
13
29
  end
14
30
  end
15
31
 
16
32
  private
17
33
 
18
- def show_on_iterm2(file)
34
+ def encode_for_iterm2(file)
19
35
  width, height = FastImage.size(file)
20
- print "\033]1337;File=inline=1;width=#{width}px;height=#{height}px:"
21
- print Base64.strict_encode64(file.read)
22
- print "\a\n"
36
+ "\033]1337;File=inline=1;width=#{width}px;height=#{height}px:#{Base64.strict_encode64(file.read)}\a\n"
37
+ end
38
+
39
+ def encode_for_libsixel(file)
40
+ `img2sixel < #{file.path} 2>/dev/null`
41
+ end
42
+
43
+ def which(cmd)
44
+ exts = ENV['PATHEXT'] ? ENV['PATHEXT'].split(';') : ['']
45
+ ENV['PATH'].split(File::PATH_SEPARATOR).each do |path|
46
+ exts.each do |ext|
47
+ exe = File.join(path, "#{cmd}#{ext}")
48
+ return exe if File.executable?(exe) && !File.directory?(exe)
49
+ end
50
+ end
51
+ nil
23
52
  end
24
53
  end
25
54
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: terminal_image
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yuji Ueki