Class: PutQR::QRCode
- Inherits:
-
Object
- Object
- PutQR::QRCode
- Defined in:
- lib/putqr.rb
Overview
Display a QR code in your terminal.
Instance Attribute Summary collapse
-
#qrcode ⇒ Object
readonly
Returns the value of attribute qrcode.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(content) ⇒ QRCode
constructor
Initialize the QR code with a string.
-
#render ⇒ Object
Render the QR code using the best method available for the terminal.
-
#render_ansi ⇒ Object
Render the QR code using ANSI escape codes.
-
#render_image ⇒ Object
Render the QR code as an inline image.
-
#render_image_iterm2 ⇒ Object
Render the QR code as an inline image for iTerm2.
-
#valid? ⇒ Boolean
Can the string be encoded as a QR code?.
Constructor Details
#initialize(content) ⇒ QRCode
Initialize the QR code with a string.
14 15 16 |
# File 'lib/putqr.rb', line 14 def initialize(content) @qrcode = QRCode.generate_qrcode(content) end |
Instance Attribute Details
#qrcode ⇒ Object (readonly)
Returns the value of attribute qrcode.
11 12 13 |
# File 'lib/putqr.rb', line 11 def qrcode @qrcode end |
Class Method Details
.generate_qrcode(content) ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/putqr.rb', line 65 def self.generate_qrcode(content) # Try each size until one fits (min_qr_version(content.size)..40).each do |version| begin return RQRCode::QRCode.new(content, level: :h, size: version) rescue RQRCode::QRCodeRunTimeError next end end nil end |
.min_qr_version(input_size) ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/putqr.rb', line 77 def self.min_qr_version(input_size) # Skip the lower QR code versions where the input is known to be too # long. These figures are based on the maximum number of characters # that can be encoded for a numeric string with high error correction. if input_size >= 2_524 then 36 elsif input_size >= 1_897 then 31 elsif input_size >= 969 then 21 elsif input_size >= 331 then 11 else 1 end end |
Instance Method Details
#render ⇒ Object
Render the QR code using the best method available for the terminal. Returns a string.
25 26 27 28 29 30 31 |
# File 'lib/putqr.rb', line 25 def render if ENV['TERM_PROGRAM'].start_with? 'iTerm' render_image_iterm2 else render_ansi end end |
#render_ansi ⇒ Object
Render the QR code using ANSI escape codes. Returns a string.
35 36 37 |
# File 'lib/putqr.rb', line 35 def render_ansi qrcode.as_ansi if valid? end |
#render_image ⇒ Object
Render the QR code as an inline image. Returns a string.
41 42 43 |
# File 'lib/putqr.rb', line 41 def render_image render_image_iterm2 end |
#render_image_iterm2 ⇒ Object
Render the QR code as an inline image for iTerm2. Returns a string.
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/putqr.rb', line 47 def render_image_iterm2 return nil unless valid? # References: # https://iterm2.com/documentation-images.html # https://iterm2.com/utilities/imgcat # tmux requires some extra work for unrecognised escape code sequences screen = ENV['TERM'].start_with? 'screen' prefix = screen ? "\ePtmux;\e\e]" : "\e]" suffix = screen ? "\a\e\\" : "\a" png = qrcode.as_png(size: 600) png_base64 = Base64.encode64(png.to_s).chomp = 'inline=1' "#{prefix}1337;File=#{}:#{png_base64}#{suffix}" end |
#valid? ⇒ Boolean
Can the string be encoded as a QR code?
19 20 21 |
# File 'lib/putqr.rb', line 19 def valid? !qrcode.nil? end |