gif 0.3.1 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/bin/gif +10 -3
  3. data/lib/gif.rb +26 -15
  4. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 165b9964e1297f48e888c41703bc60873fb1d086
4
- data.tar.gz: c7cf4a57be53de182cda1ae76c1acef346c67d77
3
+ metadata.gz: 381700aba7daa8d7cd18428d36e073f820b23583
4
+ data.tar.gz: 67d826f4163430fe37697f78060f98dd36695e17
5
5
  SHA512:
6
- metadata.gz: 7d4d4b184d02fe81e089f28a0d98c07d817c4198afc7569f6fd53e491149c167eedd8aa410d147062e185e475b0becf7f6aceb3f64c3748ade4bf9f4cda0b13c
7
- data.tar.gz: b3c58679dc01f363bbb7bb05895254ff94023cf97c9e91971e47c8ccb997571a371e0321c4aa72a3f37bb98ca4a5d8be25618d6a2e9b29a39c9e45970adbf528
6
+ metadata.gz: 484dcefa73eda1fbc2e576d5a06740c2d0cd61dcffa241ba459990dfbecb45360823c710c11a377c402721a6cca191e285a3beff232dc75449dfb206fc57c2c0
7
+ data.tar.gz: bffad4f975e90f20309d1e1db4171e466010492f3e502cf35fe620df3c44e6e040256590d8d712d2cc6748fa101a83c0ea7f15b9c818064df6cb6fac08a26fab
data/bin/gif CHANGED
@@ -7,16 +7,21 @@ loops = 1
7
7
 
8
8
  options = {}
9
9
  OptionParser.new do |parser|
10
- parser.on("--l", "--loops") do |times|
10
+ parser.on("--l", "--loops") do |_|
11
11
  loops = ARGV.find { |arg| /\A\d+\z/.match(arg).to_s.to_i > 0 }
12
12
  end
13
13
 
14
+ parser.on("--m", "--modern") do |_|
15
+ options[:modern] = true
16
+ end
17
+
14
18
  parser.on("--setup", "--s") do |_|
15
19
  options[:setup] = true
16
20
  puts "Installing dependencies (gifsicle, imgcat) now..."
17
21
  `brew install gifsicle`
18
22
  `brew tap eddieantonio/eddieantonio`
19
23
  `brew install imgcat`
24
+ `npm install -g imgcat-cli`
20
25
  puts "Finished installing dependencies!"
21
26
  end
22
27
 
@@ -25,7 +30,9 @@ OptionParser.new do |parser|
25
30
  puts [
26
31
  "`gif` will play a random gif in your terminal",
27
32
  "`gif [search_terms]` will filter by search terms. for example, `gif cute pikachu` will cause you to immediately feel 100% happier.",
28
- "`--loops` or `--l` will let you loop the gif. for example, `gif cute pikachu --loops 10` will cause you to immediately feel 1000% happier.",
33
+ "`--modern will output much higher quality gifs but only works on iTerm 2.9 and later`",
34
+ "if you want your gifs to be higher quality by default, set $MODERN_TERMINAL to be 'true' - add `export MODERN_TERMINAL='true'` to your bash_profile/fish_config",
35
+ "`--loops` or `--l` will let you loop the gif on older versions of iTerm. for example, `gif cute pikachu --loops 10` will cause you to immediately feel 1000% happier.",
29
36
  "`--help`, `--h`, or `man` will print this paragraph."
30
37
  ]
31
38
  end
@@ -33,6 +40,6 @@ end.parse!
33
40
 
34
41
  if options[:setup].nil? && options[:help].nil?
35
42
  tags = ARGV.join "+"
36
- gif = Gif::CLI.new tags, loops
43
+ gif = Gif::CLI.new tags, loops, options[:modern]
37
44
  gif.fetch
38
45
  end
data/lib/gif.rb CHANGED
@@ -4,20 +4,18 @@ require 'digest/sha1'
4
4
 
5
5
  module Gif
6
6
  class CLI
7
- def initialize tags, loops
7
+ def initialize tags, loops, modern
8
8
  @tags = tags
9
9
  @loops = loops.to_i
10
+ ENV["MODERN_TERMINAL"] = "true" if modern
11
+ @modern = ENV["MODERN_TERMINAL"] if ENV["MODERN_TERMINAL"] == "true"
10
12
  end
11
13
 
12
14
  def fetch
13
15
  Dir.mktmpdir do |tmp|
14
16
  Dir.chdir(tmp)
15
17
  generate_url
16
- create_images
17
- rename_files
18
- @loops.times { gif_to_terminal }
19
- `rm #{@gif_sha}.gif`
20
- `rm #{@resized_sha}`
18
+ generate_gifs
21
19
  end
22
20
  end
23
21
 
@@ -26,17 +24,20 @@ module Gif
26
24
  def generate_url
27
25
  fetch_url = "https://api.giphy.com/v1/gifs/random?api_key=dc6zaTOxFJmzC&tag=#{@tags}"
28
26
  gif_json = JSON.parse `curl -s '#{fetch_url}'`
29
- @gif_url = gif_json["data"]["fixed_height_small_url"]
27
+ original_url = gif_json["data"]["image_original_url"]
28
+ fixed_height_url = gif_json["data"]["fixed_height_small_url"]
29
+ @modern ? @gif_url = original_url : @gif_url = fixed_height_url
30
30
  @gif_sha = Digest::SHA1.hexdigest @gif_url
31
31
  @resized_sha = Digest::SHA1.hexdigest @gif_sha
32
32
  end
33
33
 
34
34
  def create_images
35
- system "curl -s -o #{@gif_sha}.gif #{@gif_url}"
36
35
  width = `tput cols`
37
36
  height = `tput lines`
38
37
  wh = [width, "x", height].join.gsub /\n/,""
39
- fix_colors
38
+ suppress_output do
39
+ `gifsicle --colors=256 #{@gif_sha}.gif -o #{@gif_sha}.gif")`
40
+ end
40
41
  system "gifsicle --resize-fit #{wh} --unoptimize #{@gif_sha}.gif -o #{@resized_sha}"
41
42
  system "gifsicle --explode #{@resized_sha} -o frames.jpg"
42
43
  end
@@ -44,24 +45,34 @@ module Gif
44
45
  def rename_files
45
46
  filenames = Dir.glob "*.jpg.*"
46
47
  filenames.each_with_index do |filename, i|
47
- File.rename filename, "frames-" + i.to_s
48
+ File.rename filename, "frames-" + i.to_s
48
49
  end
49
50
  end
50
51
 
51
52
  def gif_to_terminal
52
53
  frames = `ls -1 frames-* | wc -l`.to_i
53
54
  frames.times do |n|
54
- system "imgcat -R frames-#{n}"
55
+ system "/usr/local/Cellar/imgcat/1.1.0/bin/imgcat -R frames-#{n}"
55
56
  sleep 1.0/24
56
- `rm frames-#{n}` if @loops < 1
57
57
  system "⌘ k"
58
58
  end
59
59
  @loops -= 1
60
60
  end
61
61
 
62
- def fix_colors
63
- suppress_output do
64
- `gifsicle --colors=256 #{@gif_sha}.gif -o #{@gif_sha}.gif")`
62
+ def gif_to_modern_terminal
63
+ width = `tput cols`.to_i
64
+ height = `tput lines`.to_i
65
+ system "imgcat -w #{width} -h #{height} #{@gif_sha}.gif"
66
+ end
67
+
68
+ def generate_gifs
69
+ system "curl -s -o #{@gif_sha}.gif #{@gif_url}"
70
+ if @modern
71
+ gif_to_modern_terminal
72
+ else
73
+ create_images
74
+ rename_files
75
+ @loops.times { gif_to_terminal }
65
76
  end
66
77
  end
67
78
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gif
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Lev Kravinsky