Skip to content

Commit 81b7f79

Browse files
committed
add qrcode_terminal.py
1 parent 287dc32 commit 81b7f79

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

src/wechaty/utils/qrcode_terminal.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import qrcode
2+
import platform
3+
4+
5+
def qr_terminal_str(data,version=None):
6+
"""
7+
8+
:param data: qrcode data
9+
:param version:1-40 or None
10+
:return:
11+
"""
12+
if platform.system() == "Windows":
13+
white_block = '▇'
14+
black_block = ' '
15+
new_line = '\n'
16+
else:
17+
white_block = '\033[0;37;47m '
18+
black_block = '\033[0;37;40m '
19+
new_line = '\033[0m\n'
20+
21+
qr = qrcode.QRCode(version)
22+
qr.add_data(data)
23+
if version:
24+
qr.make()
25+
else:
26+
qr.make(fit=True)
27+
output = white_block*(qr.modules_count+2) + new_line
28+
for mn in qr.modules:
29+
output += white_block
30+
for m in mn:
31+
if m:
32+
output += black_block
33+
else:
34+
output += white_block
35+
output += white_block + new_line
36+
output += white_block*(qr.modules_count+2) + new_line
37+
return output
38+
39+
40+
def draw(data, version=None):
41+
output = qr_terminal_str(data,version)
42+
print (output)

0 commit comments

Comments
 (0)