putqr 0.1.0 → 0.2.0

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
  SHA256:
3
- metadata.gz: 894df85665fbab9c06ad27bff808f806ff9e552915ad0ad0776d078bee5581d4
4
- data.tar.gz: '006614608e3afd5b73943854e8811fd05fe08a49c60b8cdbb4dce5ac30536c0e'
3
+ metadata.gz: 1d3b1f8aa611f02bcf99143d51829ad21604efafaec1ff0c56a5e4e1e2501ebe
4
+ data.tar.gz: 7761362ac607108d1c937b2bdc033f373e190a4acd1f99754f14007d2b818974
5
5
  SHA512:
6
- metadata.gz: 24045838704daf36f8ad32221e4477a85a1818890619a45f475e5392184e7c502af475b436218e1911e6526980f1a03c60b6450c068812ab04422fdcd2db0095
7
- data.tar.gz: e4bae898597198756aad237d9f831f092eacf977ed6207008e5e45faee5ece117ce5361a1430674815f31267185d49a6250a71bafb74f0d4c8fc907ccd8c0ebc
6
+ metadata.gz: 9aff03258b067beefd1eba58e7112561aa3bf8f56ba50eeb80118db0b5deb5993fa1442a4121dd906a38ec1648d101dbdc22dce649c27ca06a9bb861563ec812
7
+ data.tar.gz: e1df7bb54e98649158d11511b1feb344494df646b667f322e9f0a8e6052311ef16aaaf2459db69d36818e62d0405a39f2cf8b603c06b31b14b0a519714681f6c
data/README.md CHANGED
@@ -1,10 +1,14 @@
1
1
  # putqr
2
2
 
3
+ [![Gem Version](https://badge.fury.io/rb/putqr.svg)](https://rubygems.org/gems/putqr)
4
+
3
5
  Display a QR code in your terminal.
4
6
 
5
7
  If you're using [iTerm2](https://iterm2.com) on macOS,
6
8
  the QR code will be displayed as an image.
7
9
 
10
+ ![Example: putqr https://example.com](images/example.png)
11
+
8
12
  ## Install
9
13
 
10
14
  Install `putqr` from [RubyGems](https://rubygems.org/):
data/bin/putqr CHANGED
@@ -1,15 +1,56 @@
1
1
  #!/usr/bin/env ruby
2
2
  # frozen_string_literal: true
3
3
 
4
+ require 'optparse'
4
5
  require 'putqr'
5
6
 
6
- if ARGV.any?
7
- content = ARGV.first
8
- STDERR.puts 'warning: only encoding first argument' if ARGV.size > 1
9
- else
10
- content = STDIN.read
7
+ def print_version
8
+ puts "putqr #{PutQR::VERSION} (#{PutQR::DATE})"
11
9
  end
12
10
 
13
- output = PutQR::QRCode.new(content).render
11
+ # rubocop:disable Metrics/MethodLength
12
+ def parse_options
13
+ {}.tap do |options|
14
+ OptionParser.new do |opts|
15
+ opts.banner = 'Usage: putqr [options] [message]'
16
+
17
+ opts.on('--[no-]image',
18
+ 'display as an image (on by default if supported)') do |image|
19
+ options[:image] = image
20
+ end
21
+
22
+ opts.on('--version',
23
+ 'print the version') do
24
+ print_version
25
+ exit
26
+ end
27
+ end.parse!
28
+ end
29
+ end
30
+ # rubocop:enable Metrics/MethodLength
31
+
32
+ def parse_arguments
33
+ if ARGV.any?
34
+ STDERR.puts 'warning: only encoding first message argument' if ARGV.size > 1
35
+ ARGV.first
36
+ else
37
+ STDIN.read.chomp
38
+ end
39
+ end
40
+
41
+ def render(message, options)
42
+ qrcode = PutQR::QRCode.new(message)
43
+
44
+ case options[:image]
45
+ when true then qrcode.render_image
46
+ when false then qrcode.render_ansi
47
+ else qrcode.render
48
+ end
49
+ end
50
+
51
+ options = parse_options
52
+ message = parse_arguments
53
+ output = render(message, options)
54
+
14
55
  exit(1) if output.nil?
15
56
  puts output
@@ -20,7 +20,7 @@ module PutQR
20
20
  !qrcode.nil?
21
21
  end
22
22
 
23
- # Render the QR code for display in the terminal.
23
+ # Render the QR code using the best method available for the terminal.
24
24
  # Returns a string.
25
25
  def render
26
26
  if ENV['TERM_PROGRAM'].start_with? 'iTerm'
@@ -36,15 +36,30 @@ module PutQR
36
36
  qrcode.as_ansi if valid?
37
37
  end
38
38
 
39
+ # Render the QR code as an inline image.
40
+ # Returns a string.
41
+ def render_image
42
+ render_image_iterm2
43
+ end
44
+
39
45
  # Render the QR code as an inline image for iTerm2.
40
46
  # Returns a string.
41
47
  def render_image_iterm2
42
48
  return nil unless valid?
43
49
 
50
+ # References:
51
+ # https://iterm2.com/documentation-images.html
52
+ # https://iterm2.com/utilities/imgcat
53
+
54
+ # tmux requires some extra work for unrecognised escape code sequences
55
+ screen = ENV['TERM'].start_with? 'screen'
56
+ prefix = screen ? "\ePtmux;\e\e]" : "\e]"
57
+ suffix = screen ? "\a\e\\" : "\a"
58
+
44
59
  png = qrcode.as_png(size: 600)
45
60
  png_base64 = Base64.encode64(png.to_s).chomp
46
61
  options = 'inline=1'
47
- "\033]1337;File=#{options}:#{png_base64}\007"
62
+ "#{prefix}1337;File=#{options}:#{png_base64}#{suffix}"
48
63
  end
49
64
 
50
65
  def self.generate_qrcode(content)
@@ -1,3 +1,4 @@
1
1
  module PutQR
2
- VERSION = '0.1.0'.freeze
2
+ VERSION = '0.2.0'.freeze
3
+ DATE = '2018-11-04'.freeze
3
4
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: putqr
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
  - Liam Cooke
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-11-03 00:00:00.000000000 Z
11
+ date: 2018-11-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rqrcode
@@ -36,10 +36,15 @@ files:
36
36
  - bin/putqr
37
37
  - lib/putqr.rb
38
38
  - lib/putqr/version.rb
39
- homepage: https://github.com/ljcooke/putqr
39
+ homepage: https://liamcooke.com/code/putqr/
40
40
  licenses:
41
41
  - MIT
42
- metadata: {}
42
+ metadata:
43
+ bug_tracker_uri: https://github.com/ljcooke/putqr/issues
44
+ changelog_uri: https://github.com/ljcooke/putqr/blob/master/CHANGELOG.md
45
+ documentation_uri: https://www.rubydoc.info/gems/putqr
46
+ homepage_uri: https://liamcooke.com/code/putqr/
47
+ source_code_uri: https://github.com/ljcooke/putqr
43
48
  post_install_message:
44
49
  rdoc_options: []
45
50
  require_paths: