add colors

This commit is contained in:
Superwhiskers 2018-02-28 17:44:42 -06:00
parent 7906cc03f1
commit 8c32ec5e06
3 changed files with 35 additions and 15 deletions

View file

@ -13,7 +13,7 @@ it and follow the instructions
#### prerequisites
- [golang](https://golang.org/)
- [reverseproxy](https://github.com/cssivision/reverseproxy)
- [go-colortext](https://github.com/daviddengcn/go-colortext)
- [ansicolor](https://github.com/shiena/ansicolor)
#### building
1. clone this repository

View file

@ -10,12 +10,15 @@ if you want a copy, go to http://www.gnu.org/licenses/
package main
import (
// internals
"fmt"
"runtime"
"flag"
"time"
"os"
"encoding/json"
// externals
"github.com/shiena/ansicolor"
)
func setup(fileMap map[string]string) {
@ -113,10 +116,20 @@ func setup(fileMap map[string]string) {
// handle the results
if (parsedRes.Server == serverResFor(test[x])) && (err == nil) && (res != "") {
fmt.Printf("%s\n", padStrToMatchStr(fmt.Sprintf("\r %s %s -> %s", utilIcons("success"), endpointsFor("ninty", test[x]), endpointsFor("local", test[x])), fmt.Sprintf(" %s %s -> %s ", utilIcons("uncertain"), endpointsFor("ninty", test[x]), endpointsFor("local", test[x])), " "))
if isWindows() {
Writer := ansicolor.NewAnsiColorWriter(os.Stdout)
fmt.Fprintf(Writer, "%s\n", padStrToMatchStr(fmt.Sprintf("\r %s%s%s%s %s -> %s", code("green"), code("bold"), utilIcons("success"), code("reset"), endpointsFor("ninty", test[x]), endpointsFor("local", test[x])), fmt.Sprintf(" %s %s -> %s", utilIcons("uncertain"), endpointsFor("ninty", test[x]), endpointsFor("local", test[x])), " "))
} else {
fmt.Printf("%s\n", padStrToMatchStr(fmt.Sprintf("\r %s%s%s%s %s -> %s", code("green"), code("bold"), utilIcons("success"), code("reset"), endpointsFor("ninty", test[x]), endpointsFor("local", test[x])), fmt.Sprintf(" %s %s -> %s", utilIcons("uncertain"), endpointsFor("ninty", test[x]), endpointsFor("local", test[x])), " "))
}
result[x] = true
} else {
fmt.Printf("%s\n", padStrToMatchStr(fmt.Sprintf("\r %s %s -> %s", utilIcons("failiure"), endpointsFor("ninty", test[x]), endpointsFor("local", test[x])), fmt.Sprintf(" %s %s -> %s ", utilIcons("uncertain"), endpointsFor("ninty", test[x]), endpointsFor("local", test[x])), " "))
if isWindows() {
Writer := ansicolor.NewAnsiColorWriter(os.Stdout)
fmt.Fprintf(Writer, "%s\n", padStrToMatchStr(fmt.Sprintf("\r %s%s%s%s %s -> %s", code("red"), code("bold"), utilIcons("failiure"), code("reset"), endpointsFor("ninty", test[x]), endpointsFor("local", test[x])), fmt.Sprintf(" %s %s -> %s", utilIcons("uncertain"), endpointsFor("ninty", test[x]), endpointsFor("local", test[x])), " "))
} else {
fmt.Printf("%s\n", padStrToMatchStr(fmt.Sprintf("\r %s%s%s%s %s -> %s", code("red"), code("bold"), utilIcons("failiure"), code("reset"), endpointsFor("ninty", test[x]), endpointsFor("local", test[x])), fmt.Sprintf(" %s %s -> %s", utilIcons("uncertain"), endpointsFor("ninty", test[x]), endpointsFor("local", test[x])), " "))
}
result[x] = false
}
@ -150,10 +163,20 @@ func setup(fileMap map[string]string) {
// handle the results
if (parsedRes2.Server == serverResFor(testOfficial[x])) && (err3 == nil) && (res2 != "") {
fmt.Printf("%s\n", padStrToMatchStr(fmt.Sprintf("\r %s %s -> %s", utilIcons("success"), endpointsFor("ninty", testOfficial[x]), endpointsFor("official", testOfficial[x])), fmt.Sprintf(" %s %s -> %s ", utilIcons("uncertain"), endpointsFor("ninty", testOfficial[x]), endpointsFor("official", testOfficial[x])), " "))
if isWindows() {
Writer := ansicolor.NewAnsiColorWriter(os.Stdout)
fmt.Fprintf(Writer, "%s\n", padStrToMatchStr(fmt.Sprintf("\r %s%s%s%s %s -> %s", code("green"), code("bold"), utilIcons("success"), code("reset"), endpointsFor("ninty", testOfficial[x]), endpointsFor("official", testOfficial[x])), fmt.Sprintf(" %s %s -> %s", utilIcons("uncertain"), endpointsFor("ninty", testOfficial[x]), endpointsFor("official", testOfficial[x])), " "))
} else {
fmt.Printf("%s\n", padStrToMatchStr(fmt.Sprintf("\r %s%s%s%s %s -> %s", code("green"), code("bold"), utilIcons("success"), code("reset"), endpointsFor("ninty", testOfficial[x]), endpointsFor("official", testOfficial[x])), fmt.Sprintf(" %s %s -> %s", utilIcons("uncertain"), endpointsFor("ninty", testOfficial[x]), endpointsFor("official", testOfficial[x])), " "))
}
resultOfficial[x] = true
} else {
fmt.Printf("%s\n", padStrToMatchStr(fmt.Sprintf("\r %s %s -> %s", utilIcons("failiure"), endpointsFor("ninty", testOfficial[x]), endpointsFor("official", testOfficial[x])), fmt.Sprintf(" %s %s -> %s ", utilIcons("uncertain"), endpointsFor("ninty", testOfficial[x]), endpointsFor("official", testOfficial[x])), " "))
if isWindows() {
Writer := ansicolor.NewAnsiColorWriter(os.Stdout)
fmt.Fprintf(Writer, "%s\n", padStrToMatchStr(fmt.Sprintf("\r %s%s%s%s %s -> %s", code("red"), code("bold"), utilIcons("failiure"), code("reset"), endpointsFor("ninty", testOfficial[x]), endpointsFor("official", testOfficial[x])), fmt.Sprintf(" %s %s -> %s", utilIcons("uncertain"), endpointsFor("ninty", testOfficial[x]), endpointsFor("official", testOfficial[x])), " "))
} else {
fmt.Printf("%s\n", padStrToMatchStr(fmt.Sprintf("\r %s%s%s%s %s -> %s", code("red"), code("bold"), utilIcons("failiure"), code("reset"), endpointsFor("ninty", testOfficial[x]), endpointsFor("official", testOfficial[x])), fmt.Sprintf(" %s %s -> %s", utilIcons("uncertain"), endpointsFor("ninty", testOfficial[x]), endpointsFor("official", testOfficial[x])), " "))
}
resultOfficial[x] = false
}

View file

@ -18,7 +18,6 @@ import (
"os/exec"
"runtime"
"strings"
"github.com/shiena/ansicolor"
)
@ -39,6 +38,9 @@ func length(x string) int { return len([]rune(x)); }
// pad string to match the length of another string
func padStrToMatchStr(pad string, match string, padWith string) string { if length(padWith) != 1 { fmt.Printf("[err] : '%s' is not 1 character long", padWith); os.Exit(1); }; for x := 0; x < length(match); x++ { pad += padWith; }; return pad; }
// is it windows
func isWindows() bool { return (runtime.GOOS == "windows"); }
/* give terminal style */
// set terminal title
@ -48,7 +50,7 @@ func ttitle(title string) { fmt.Print(strings.Join([]string {"\033]0;",title,"\0
func tcolor(cid int) string { return strings.Join([]string {"\033[",string(cid),"m"}, ""); }
// terminal color codes
func printColor(index string, text string) {
func code(index string) string {
// map to store terminal codes
var termCodes map[string]string
@ -59,7 +61,7 @@ func printColor(index string, text string) {
if runtime.GOOS == "darwin" { ansiTrick(); }
// fix prefix for windows
if runtime.GOOS == "windows" { prefix = "\x1b"; } else { prefix = "\033"; }
if isWindows() { prefix = "\x1b"; } else { prefix = "\033"; }
// codes go here
@ -81,12 +83,7 @@ func printColor(index string, text string) {
termCodes["cyan"] = "[96m"
termCodes["white"] = "[97m"
// join the prefix and the code
if runtime.GOOS == "windows" {
w := ansicolor.NewAnsiColorWriter(os.Stdout);
fmt.Fprintf(w, strings.Join([]string {prefix,termCodes[index],text,prefix,termCodes["reset"]}, ""))
} else {
fmt.Printf(strings.Join([]string {prefix,termCodes[index],text,prefix,termCodes["reset"]}, ""))
}
// output the correct terminal color code
return strings.Join([]string {prefix,termCodes[index]}, "")
}