remove invert argument and use colors in terminal instead

This commit is contained in:
Unrud 2018-08-23 17:12:34 +02:00
parent 549de83fe3
commit f2a7c1dc1d
5 changed files with 113 additions and 11 deletions

View file

@ -32,6 +32,7 @@ import (
mathrand "math/rand"
"net"
"net/http"
"os"
"strconv"
"strings"
"time"
@ -129,13 +130,13 @@ func secureRandBase64(length int) string {
func main() {
var bind, certFile, keyFile, secret string
var showVersion, invert bool
var showVersion bool
flag.BoolVar(&showVersion, "version", false, "show program's version number and exit")
flag.StringVar(&bind, "bind", defaultBind, "bind server to [HOSTNAME]:PORT")
flag.StringVar(&secret, "secret", "", "shared secret for client authentication")
flag.StringVar(&certFile, "cert", "", "file containing TLS certificate")
flag.StringVar(&keyFile, "key", "", "file containing TLS private key")
flag.BoolVar(&invert, "invert", false, "use inverse colors for QR code")
flag.Bool("invert", false, "obsolete, not used anymore")
flag.Int("interval", 0, "obsolete, not used anymore")
flag.Parse()
if showVersion {
@ -225,7 +226,7 @@ func main() {
}
url := fmt.Sprintf("%s://%s/#%s\n", scheme, domain, secret)
readyMsg := "ready: " + url
qrCode, _ := GenerateQRCode(url, invert)
qrCode, _ := GenerateQRCode(url, terminalSupportsColor(os.Stderr.Fd()))
readyMsg += qrCode
if !tls {
readyMsg += ("" +

22
qr.go
View file

@ -21,36 +21,42 @@ package main
import qrcode "github.com/skip2/go-qrcode"
func GenerateQRCode(message string, invert bool) (string, error) {
func GenerateQRCode(message string, colorize bool) (string, error) {
q, err := qrcode.New(message, qrcode.Medium)
if err != nil {
return "", err
}
return qrCodeToString(q.Bitmap(), invert), nil
return qrCodeToString(q.Bitmap(), colorize), nil
}
func qrCodeToString(bits [][]bool, invert bool) string {
func qrCodeToString(bits [][]bool, colorize bool) string {
s := ""
for y := -1; y < len(bits); y += 2 {
if colorize {
s += terminalForegroundWhite + terminalBackgroundBlack
}
for x := range bits[0] {
upper := !invert
upper := false
if 0 <= y && y < len(bits) {
upper = bits[y][x]
}
lower := !invert
lower := false
if 0 <= y+1 && y+1 < len(bits) {
lower = bits[y+1][x]
}
if upper != invert && lower != invert {
if upper && lower {
s += " "
} else if upper == invert && lower != invert {
} else if !upper && lower {
s += "▀"
} else if upper != invert && lower == invert {
} else if upper && !lower {
s += "▄"
} else {
s += "█"
}
}
if colorize {
s += terminalForegroundReset + terminalBackgroundReset
}
s += "\n"
}
return s

27
terminal_color.go Normal file
View file

@ -0,0 +1,27 @@
/*
* Copyright (c) 2018 Unrud<unrud@outlook.com>
*
* This file is part of Remote-Touchpad.
*
* Foobar is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Remote-Touchpad is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Foobar. If not, see <http://www.gnu.org/licenses/>.
*/
package main
const (
terminalForegroundWhite string = "\033[37m"
terminalForegroundReset string = "\033[39m"
terminalBackgroundBlack string = "\033[40m"
terminalBackgroundReset string = "\033[49m"
)

29
terminal_color_posix.go Normal file
View file

@ -0,0 +1,29 @@
// +build !windows
/*
* Copyright (c) 2018 Unrud<unrud@outlook.com>
*
* This file is part of Remote-Touchpad.
*
* Foobar is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Remote-Touchpad is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Foobar. If not, see <http://www.gnu.org/licenses/>.
*/
package main
// #include <unistd.h>
import "C"
func terminalSupportsColor(fd uintptr) bool {
return C.isatty(C.int(fd)) != 0
}

39
terminal_color_windows.go Normal file
View file

@ -0,0 +1,39 @@
/*
* Copyright (c) 2018 Unrud<unrud@outlook.com>
*
* This file is part of Remote-Touchpad.
*
* Foobar is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Remote-Touchpad is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Foobar. If not, see <http://www.gnu.org/licenses/>.
*/
package main
import "syscall"
const (
enableProcessedOuput uint32 = 0x1
enableWrapAtEolOutput uint32 = 0x2
enableVirtualTerminalProcessing uint32 = 0x4
)
var (
kernel32DLL = syscall.NewLazyDLL("kernel32.dll")
setConsoleModeProc = kernel32DLL.NewProc("SetConsoleMode")
)
func terminalSupportsColor(fd uintptr) bool {
r, _, _ := setConsoleModeProc.Call(fd, uintptr(enableProcessedOuput|
enableWrapAtEolOutput|enableVirtualTerminalProcessing))
return r != 0
}