class PutQR::QRCode
Display a QR code in your terminal.
Attributes
qrcode[R]
Public Class Methods
generate_qrcode(content)
click to toggle source
# 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)
click to toggle source
# 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
new(content)
click to toggle source
Initialize the QR code with a string.
# File lib/putqr.rb, line 14 def initialize(content) @qrcode = QRCode.generate_qrcode(content) end
Public Instance Methods
render()
click to toggle source
Render the QR code using the best method available for the terminal. Returns a string.
# 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()
click to toggle source
Render the QR code using ANSI escape codes. Returns a string.
# File lib/putqr.rb, line 35 def render_ansi qrcode.as_ansi if valid? end
render_image()
click to toggle source
Render the QR code as an inline image. Returns a string.
# File lib/putqr.rb, line 41 def render_image render_image_iterm2 end
render_image_iterm2()
click to toggle source
Render the QR code as an inline image for iTerm2. Returns a string.
# 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 options = 'inline=1' "#{prefix}1337;File=#{options}:#{png_base64}#{suffix}" end
valid?()
click to toggle source
Can the string be encoded as a QR code?
# File lib/putqr.rb, line 19 def valid? !qrcode.nil? end