first commit
This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
package widgets
|
||||
|
||||
import (
|
||||
"ArinDash/util"
|
||||
"context"
|
||||
"strings"
|
||||
|
||||
"github.com/mum4k/termdash/terminal/terminalapi"
|
||||
"github.com/mum4k/termdash/widgetapi"
|
||||
"github.com/mum4k/termdash/widgets/text"
|
||||
"github.com/skip2/go-qrcode"
|
||||
)
|
||||
|
||||
type QRCodeOptions struct {
|
||||
data string
|
||||
}
|
||||
|
||||
func QRCode(data string) QRCodeOptions {
|
||||
widgetOptions["QRCodeOptions"] = createQRCode
|
||||
return QRCodeOptions{
|
||||
data: data,
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func QrCodeASCII(data string) string {
|
||||
bitmap := util.PanicOnErrorWithResult(qrcode.New(data, qrcode.Low)).Bitmap()
|
||||
|
||||
var stringBuilder strings.Builder
|
||||
for y := range bitmap {
|
||||
for x := range bitmap[y] {
|
||||
if bitmap[y][x] {
|
||||
stringBuilder.WriteString("██")
|
||||
} else {
|
||||
stringBuilder.WriteString(" ")
|
||||
}
|
||||
}
|
||||
stringBuilder.WriteByte('\n')
|
||||
}
|
||||
return stringBuilder.String()
|
||||
}
|
||||
|
||||
func createQRCode(_ context.Context, _ terminalapi.Terminal, opt interface{}) widgetapi.Widget {
|
||||
options, ok := opt.(QRCodeOptions)
|
||||
if !ok {
|
||||
panic("invalid options type")
|
||||
}
|
||||
|
||||
widget := util.PanicOnErrorWithResult(text.New())
|
||||
|
||||
util.PanicOnError(widget.Write(QrCodeASCII(options.data)))
|
||||
|
||||
return widget
|
||||
}
|
||||
Reference in New Issue
Block a user