-
-
Notifications
You must be signed in to change notification settings - Fork 32
/
qrcode.lua
executable file
·109 lines (95 loc) · 3.31 KB
/
qrcode.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#!/usr/bin/env lua
local qrencode = dofile("qrencode.lua")
-- padding: number of padding rows/columns around QR code
local function matrix_to_string( tab, padding,padding_char,white_pixel,black_pixel )
local padding_string
local str_tab = {} -- hold each row of the qr code in a cell
-- Add (padding) blank columns at the left of the matrix
-- (left of each row string), inserting an extra (padding)
-- rows at the top and bottom
padding_string = string.rep(padding_char,padding)
for i=1,#tab + 2*padding do
str_tab[i] = padding_string
end
for x=1,#tab do
for y=1,#tab do
if tab[x][y] > 0 then
-- using y + padding because we added (padding) blank columns at the left for each string in str_tab array
str_tab[y + padding] = str_tab[y + padding] .. black_pixel
elseif tab[x][y] < 0 then
str_tab[y + padding] = str_tab[y + padding] .. white_pixel
else
str_tab[y + padding] = str_tab[y + padding] .. " X"
end
end
end
padding_string = string.rep(padding_char,#tab)
for i=1,padding do
-- fills in padding rows at top of matrix
str_tab[i] = str_tab[i] .. padding_string
-- fills in padding rows at bottom of matrix
str_tab[#tab + padding + i] = str_tab[#tab + padding + i] .. padding_string
end
-- Add (padding) blank columns to right of matrix (to the end of each row string)
padding_string = string.rep(padding_char,padding)
for i=1,#tab + 2*padding do
str_tab[i] = str_tab[i] .. padding_string
end
return str_tab
end
local use_ansi = false
local padding = 1
local padding_char
local black_pixel = "X"
local white_pixel = " "
local codeword
while true do
if arg[1] == nil then
break
elseif arg[1] == "-h" or arg[1] == "--help" then
codeword = nil
break
elseif arg[1] == "-a" then
use_ansi = true
elseif arg[1] == "-p" then
padding = arg[2]
table.remove(arg,2)
elseif arg[1] == "-c" then
padding_char = arg[2]
table.remove(arg,2)
elseif arg[1] == "-b" then
black_pixel = arg[2]
table.remove(arg,2)
elseif arg[1] == "-w" then
white_pixel = arg[2]
table.remove(arg,2)
else
codeword = arg[1]
end
table.remove(arg,1)
end
if use_ansi then
black_pixel = "\27[40m \27[0m"
white_pixel = "\27[1;47m \27[0m"
end
padding_char = padding_char or white_pixel
if codeword then
local ok, tab_or_message = qrencode.qrcode(codeword)
if not ok then
print(tab_or_message)
else
local rows
rows = matrix_to_string(tab_or_message,padding,padding_char,white_pixel,black_pixel)
for i=1,#rows do -- prints each "row" of the QR code on a line, one at a time
print(rows[i])
end
end
else
print("Usage:")
print(arg[0] .. " [-a] [-p <num>] [-c <char>] [-b <char>] [-w <char>] <contents>")
print("-a : use ansi colors (don't do this on a dos box)")
print("-p <num> : use padding of width <num> (default: 1)")
print("-b <char>: use <char> for black pixel (default: 'X')")
print("-w <char>: use <char> for white pixel (default: ' ')")
print("-c <char>: use <char> for padding (default: the white pixel)")
end