| 1 | #!/usr/bin/env python3 |
| 2 | """Turn text (e.g. a URL) into a scannable QR code printed to the terminal. |
| 3 | |
| 4 | Used by the WSLC-CustomContainer sample. The Windows host passes the text to |
| 5 | encode as command-line arguments; the QR is drawn with Unicode block characters |
| 6 | so it scans straight from the terminal. |
| 7 | """ |
| 8 | |
| 9 | import sys |
| 10 | |
| 11 | import qrcode |
| 12 | |
| 13 | |
| 14 | def main() -> int: |
| 15 | text = " ".join(sys.argv[1:]).strip() |
| 16 | if not text: |
| 17 | print("usage: qr.py <text-or-url>", file=sys.stderr) |
| 18 | return 2 |
| 19 | |
| 20 | qr = qrcode.QRCode(border=2) |
| 21 | qr.add_data(text) |
| 22 | qr.make(fit=True) |
| 23 | |
| 24 | print(f"QR code for: {text}\n") |
| 25 | # invert=True renders dark modules as spaces on a light background, which |
| 26 | # scans reliably in terminals with a dark color scheme. |
| 27 | qr.print_ascii(invert=True) |
| 28 | return 0 |
| 29 | |
| 30 | |
| 31 | if __name__ == "__main__": |
| 32 | sys.exit(main()) |