diff --git a/main.go b/main.go index 42d85af..199bca6 100644 --- a/main.go +++ b/main.go @@ -36,13 +36,13 @@ func main() { } var wifQR bytes.Buffer - if err := qr.Encode(wif, &wifQR); err != nil { + if err := qr.Copy(&wifQR, strings.NewReader(wif)); err != nil { fmt.Printf("Could not generate QR code for WIF: %s", err.Error()) os.Exit(-1) } var pubAddressQR bytes.Buffer - if err := qr.Encode(pubAddress, &pubAddressQR); err != nil { + if err := qr.Copy(&pubAddressQR, strings.NewReader(pubAddress)); err != nil { fmt.Printf("Could not generate QR code for pubAddress: %s", err.Error()) os.Exit(-1) } diff --git a/qr/encode.go b/qr/encode.go index a0e9030..dfe722a 100644 --- a/qr/encode.go +++ b/qr/encode.go @@ -2,6 +2,7 @@ package qr import ( "io" + "io/ioutil" "rsc.io/qr" ) @@ -9,52 +10,74 @@ import ( const blackSquare = "\033[40m \033[0m" const whiteSquare = "\033[47m \033[0m" +var newLine = []byte("\n") + type encodingParams struct { Level qr.Level + Reader io.Reader Writer io.Writer BlackChar string WhiteChar string } //Creates a QR that can be displayed on a terminal -func encode(text string, params encodingParams) error { +func encode(params encodingParams) error { w := params.Writer white := params.WhiteChar black := params.BlackChar + var text string + + if b, err := ioutil.ReadAll(params.Reader); err == nil { + text = string(b) + } else { + return err + } code, err := qr.Encode(text, params.Level) if err != nil { return err } - w.Write([]byte(white)) + textVersion := qrToText(code, []byte(white), []byte(black)) + _, err = w.Write(textVersion) + return err +} + +func qrToText(code *qr.Code, white, black []byte) []byte { + + textVersion := white for i := 0; i <= code.Size; i++ { - w.Write([]byte(white)) + textVersion = append(textVersion, white...) } - w.Write([]byte("\n")) + textVersion = append(textVersion, newLine...) for i := 0; i <= code.Size; i++ { - w.Write([]byte(white)) + textVersion = append(textVersion, white...) for j := 0; j <= code.Size; j++ { if code.Black(i, j) { - w.Write([]byte(black)) + textVersion = append(textVersion, black...) } else { - w.Write([]byte(white)) + textVersion = append(textVersion, white...) } } - w.Write([]byte("\n")) + textVersion = append(textVersion, newLine...) } - return err + + textVersion = append(textVersion, newLine...) + return textVersion } -//Encode encodes the given text as a QR code, the QR is written -//on the given writer -func Encode(text string, w io.Writer) error { +//Copy encodes the given reader as a QR code in text mode and copies it +//to the given writer +func Copy(w io.Writer, r io.Reader) error { + + //func Encode(text string, w io.Writer) error { params := encodingParams{ Level: qr.L, + Reader: r, Writer: w, BlackChar: blackSquare, WhiteChar: whiteSquare, } - return encode(text, params) + return encode(params) } diff --git a/qr/encode_test.go b/qr/encode_test.go index 58c648d..10fa0f0 100644 --- a/qr/encode_test.go +++ b/qr/encode_test.go @@ -2,6 +2,7 @@ package qr import ( "bytes" + "strings" "testing" ) @@ -23,7 +24,7 @@ func TestQRWritingDoesNotFail(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { w := &bytes.Buffer{} - if err := Encode(tt.args.text, w); (err != nil) != tt.wantErr { + if err := Copy(w, strings.NewReader(tt.args.text)); (err != nil) != tt.wantErr { t.Errorf("Encode() error = %v, wantErr %v", err, tt.wantErr) }