From f2a7c1dc1d77cae67751937b06af2d453e9e2983 Mon Sep 17 00:00:00 2001 From: Unrud Date: Thu, 23 Aug 2018 17:12:34 +0200 Subject: [PATCH] remove invert argument and use colors in terminal instead --- main.go | 7 ++++--- qr.go | 22 ++++++++++++++-------- terminal_color.go | 27 +++++++++++++++++++++++++++ terminal_color_posix.go | 29 +++++++++++++++++++++++++++++ terminal_color_windows.go | 39 +++++++++++++++++++++++++++++++++++++++ 5 files changed, 113 insertions(+), 11 deletions(-) create mode 100644 terminal_color.go create mode 100644 terminal_color_posix.go create mode 100644 terminal_color_windows.go diff --git a/main.go b/main.go index aa9e68f..40ba744 100644 --- a/main.go +++ b/main.go @@ -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 += ("" + diff --git a/qr.go b/qr.go index 6e9e0f3..80f380c 100644 --- a/qr.go +++ b/qr.go @@ -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 diff --git a/terminal_color.go b/terminal_color.go new file mode 100644 index 0000000..af92fbb --- /dev/null +++ b/terminal_color.go @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2018 Unrud + * + * 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 . + */ + +package main + +const ( + terminalForegroundWhite string = "\033[37m" + terminalForegroundReset string = "\033[39m" + terminalBackgroundBlack string = "\033[40m" + terminalBackgroundReset string = "\033[49m" +) diff --git a/terminal_color_posix.go b/terminal_color_posix.go new file mode 100644 index 0000000..ea0e5d4 --- /dev/null +++ b/terminal_color_posix.go @@ -0,0 +1,29 @@ +// +build !windows + +/* + * Copyright (c) 2018 Unrud + * + * 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 . + */ + +package main + +// #include +import "C" + +func terminalSupportsColor(fd uintptr) bool { + return C.isatty(C.int(fd)) != 0 +} diff --git a/terminal_color_windows.go b/terminal_color_windows.go new file mode 100644 index 0000000..4fdf76a --- /dev/null +++ b/terminal_color_windows.go @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2018 Unrud + * + * 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 . + */ + +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 +}