Skip to content

Commit

Permalink
use mamantoha/qrcode_terminal
Browse files Browse the repository at this point in the history
  • Loading branch information
mamantoha committed Sep 28, 2024
1 parent 3cae154 commit f6f7c1c
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 50 deletions.
4 changes: 4 additions & 0 deletions shard.lock
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ shards:
git: https://github.com/spider-gazelle/qr-code.git
version: 1.0.3

qrcode_terminal:
git: https://github.com/mamantoha/qrcode_terminal.git
version: 0.1.0

zip64:
git: https://github.com/crystal-garage/crystal-zip64.git
version: 0.2.1
Expand Down
4 changes: 2 additions & 2 deletions shard.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ dependencies:
zip64:
github: crystal-garage/crystal-zip64
version: ">= 0.2.0"
qr-code:
github: spider-gazelle/qr-code
qrcode_terminal:
github: mamantoha/qrcode_terminal
ip_address_list:
github: mamantoha/ip_address_list

Expand Down
51 changes: 3 additions & 48 deletions src/zipstream.cr
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ require "mime"
require "option_parser"
require "crystar"
require "zip64"
require "qr-code"
require "qrcode_terminal"
require "ip_address_list"

require "./zipstream/**"
Expand Down Expand Up @@ -62,7 +62,7 @@ module Zipstream

if config.qr?
puts "Or scan the QR code to access to download the file on your phone"
puts generate_qr_code(config.remote_url)
puts QRCodeTerminal.generate(config.remote_url)
end

Process.on_terminate { shutdown(server) }
Expand Down Expand Up @@ -94,7 +94,7 @@ module Zipstream

if config.qr?
puts "Or scan the QR code to access `#{config.remote_url}` on your phone"
puts generate_qr_code(config.remote_url)
puts QRCodeTerminal.generate(config.remote_url)
end

server.run unless config.env == "test"
Expand Down Expand Up @@ -222,49 +222,4 @@ module Zipstream

lines.join("\n")
end

private def generate_qr_code(text : String) : String
# based on https://github.com/gtanner/qrcode-terminal/blob/master/lib/main.js
qr = QRCode.new(text, level: :h)

platte = {
white_all: '█',
white_black: '▀',
black_white: '▄',
black_all: ' ',
}

module_count = qr.modules.size
module_data = qr.modules

border_top = platte[:black_white].to_s * (module_count + 2)
border_bottom = platte[:white_black].to_s * (module_count + 2)

String.build do |str|
str << border_top
str << '\n'

module_count.times.step(2).each do |row|
str << platte[:white_all] # left border

module_count.times.each do |col|
if !module_data.dig?(row, col) && !module_data.dig?(row + 1, col)
str << platte[:white_all]
elsif !module_data.dig?(row, col) && module_data.dig?(row + 1, col)
str << platte[:white_black]
elsif module_data.dig?(row, col) && !module_data.dig?(row + 1, col)
str << platte[:black_white]
else
str << platte[:black_all]
end
end

str << platte[:white_all] # right border

str << '\n'
end

str << border_bottom unless module_count.odd?
end
end
end

0 comments on commit f6f7c1c

Please sign in to comment.