Last active
January 17, 2019 14:55
-
-
Save kevin-cantwell/43ffdfd976f6a8e0933a45d2166d110b to your computer and use it in GitHub Desktop.
Print QR codes in the terminal
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"net/url" | |
"os" | |
qrcode "github.com/skip2/go-qrcode" | |
) | |
func main() { | |
if len(os.Args) < 4 { | |
fmt.Println(` | |
Usage: | |
qr "$TITLE" "$USERNAME" "$SECRET" | |
`) | |
os.Exit(1) | |
} | |
issuer := uriEncode(os.Args[1]) | |
username := uriEncode(os.Args[2]) | |
secret := uriEncode(os.Args[3]) | |
qr := fmt.Sprintf("otpauth://totp/%s:%s?secret=%s&issuer=%s", issuer, username, secret, issuer) | |
q, err := qrcode.New(qr, qrcode.Medium) | |
if err != nil { | |
fmt.Println(err) | |
os.Exit(1) | |
} | |
bitmap := q.Bitmap() | |
for i := 0; i < len(bitmap); i += 2 { | |
for j := 0; j < len(bitmap[i]); j++ { | |
var top, bottom bool | |
top = bitmap[i][j] | |
if i+1 < len(bitmap) { | |
bottom = bitmap[i+1][j] | |
} | |
switch { | |
case !top && !bottom: | |
fmt.Print(" ") | |
case !top && bottom: | |
fmt.Print("▄") | |
case top && !bottom: | |
fmt.Print("▀") | |
case top && bottom: | |
fmt.Print("█") | |
} | |
} | |
fmt.Print("\n") | |
} | |
} | |
func uriEncode(s string) string { | |
return (&url.URL{Path: s}).String() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment