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 +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +4 -2
- data/lib/terminal_image/version.rb +1 -1
- data/lib/terminal_image.rb +34 -5
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b16055f18d2792f24876eba1985bc1c1cb270c50cb3fe824bd5d30b714194798
|
4
|
+
data.tar.gz: c2579200f84591705dcc3b48fc0bd7b0c440805fd3f09e80cdea88e4bb5efc7a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 64fec55d5edf74ff3c7f23aad8f6002f8225b2e653a9f6f56d82e3336e30f96407db93f2d392e5542189d11c4afdc1067ffa4fb1088fe752c7addc9a3be3788b
|
7
|
+
data.tar.gz: 07266ff60380f468933a3912cc049dddb16eb5695f116c3638fb5003213e006d25c4ea771cd0cbe46bead07c47a04d3be717d8055b8805310c95760d71923c0e
|
data/Gemfile.lock
CHANGED
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
|

|
6
7
|
|
@@ -15,8 +16,9 @@ gem 'terminal_image'
|
|
15
16
|
## Usage
|
16
17
|
|
17
18
|
```ruby
|
18
|
-
|
19
|
-
TerminalImage.
|
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
|
data/lib/terminal_image.rb
CHANGED
@@ -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
|
-
|
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
|
34
|
+
def encode_for_iterm2(file)
|
19
35
|
width, height = FastImage.size(file)
|
20
|
-
|
21
|
-
|
22
|
-
|
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
|