From 0c1858e6bd0faa96ac885bf679b1cd70138533b6 Mon Sep 17 00:00:00 2001 From: nea Date: Thu, 22 Sep 2022 14:05:20 +0200 Subject: [PATCH] Ruin echos dreams --- go.mod | 2 +- main.go | 146 +++++++------------------------------------- utils/asciiarts.go | 11 +--- utils/printing.go | 148 +++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 173 insertions(+), 134 deletions(-) create mode 100644 utils/printing.go diff --git a/go.mod b/go.mod index c6d47dd..4bfef1d 100644 --- a/go.mod +++ b/go.mod @@ -1,4 +1,4 @@ -module neowofetch +module github.com/exhq/neowofetch go 1.19 diff --git a/main.go b/main.go index 5eaa6b6..06bbb6f 100644 --- a/main.go +++ b/main.go @@ -3,39 +3,26 @@ package main import ( "fmt" "io/ioutil" - "neowofetch/utils" "os" "os/exec" "path/filepath" "strconv" "strings" - "github.com/fatih/color" + "github.com/exhq/neowofetch/utils" ) var isuwuified bool = true -var asciiart []string -var aa int func getHome() string { return os.Getenv("HOME") } -func incrementaa() { - aa += 1 -} func getConfigFile() string { return getHome() + "/.config/neowofetch/conf" } -func initascii() { - asciiart = utils.Asciioverwrite(utils.Getascii(getDistro())) - aa = 0 - print(asciiart[aa]) - aa = aa + 1 -} - func handleConfig() { _, folder := os.Stat(filepath.Dir(getConfigFile())) _, file := os.Stat(getConfigFile()) @@ -66,118 +53,40 @@ func handleConfig() { } -func handlePrint(action, colour string, rest string) { +func handlePrint(action, format string, rest string) { if action == "print" { - Cprint(colour, rest, true) + utils.CutePrint(rest, format) } else if action == "println" { - Cprint(colour, rest, true) - print("\n") - if aa < len(asciiart) { - print(asciiart[aa]) - } - if aa == len(asciiart) || aa == len(asciiart)-1 { - print(strings.Repeat(" ", len(asciiart[1]))) - } - - if aa > len(asciiart) { - print(strings.Repeat(" ", len(asciiart[1]))) - } - incrementaa() + utils.CutePrint(rest, format) + utils.CuteNewLine() } else if action == "info" || action == "infoln" { switch rest { case "distro": - Cprint(colour, getDistro(), true) + utils.CutePrint(getDistro(), format) case "username": - Cprint(colour, getUsername(), true) + utils.CutePrint(getUsername(), format) case "uptime": no, _ := strconv.Atoi(getUptime()) - Cprint(colour, formatTime(no), false) + utils.CutePrint(formatTime(no), format) case "hostname": - Cprint(colour, getHostname(), true) + utils.CutePrint(getHostname(), format) case "GPU": - Cprint(colour, getGPU(), true) + utils.CutePrint(getGPU(), format) case "shell": - Cprint(colour, getShell(), true) + utils.CutePrint(getShell(), format) case "terminal": - Cprint(colour, getTerminal(), true) + utils.CutePrint(getTerminal(), format) case "memoryAll": - Cprint(colour, getMemory(false), true) + utils.CutePrint(getMemory(false), format) case "memoryUsed": - Cprint(colour, getMemory(true), true) + utils.CutePrint(getMemory(true), format) default: print("{UNKNOWN KEYWORD}") } - } - if action == "infoln" { - - print("\n") - if aa < len(asciiart) { - print(asciiart[aa]) - } else { - print(strings.Repeat(" ", len(asciiart[1]))) - } - incrementaa() - } -} - -func Cprint(colour string, message string, uwu bool) { - - if uwu && utils.Woulduwuify() { - message = uwuify(message) - } - yellow := color.New(color.FgYellow).SprintFunc() - red := color.New(color.FgRed).SprintFunc() - green := color.New(color.FgGreen).SprintFunc() - magenta := color.New(color.FgMagenta).SprintFunc() - blue := color.New(color.FgBlue).SprintFunc() - black := color.New(color.FgBlack).SprintFunc() - switch colour { - case "yellow": - print(yellow(message)) - case "white": - print(message) - case "magenta": - print(magenta(message)) - case "red": - print(red(message)) - case "blue": - print(blue(message)) - case "black": - print(black(message)) - case "green": - print(green(message)) - } -} - -func uwuify(message string) string { - var answer string - var sentence []string - var hasspace bool - if strings.Contains(message, " ") { - sentence = strings.Split(message, " ") - hasspace = true - } else { - sentence = strings.Split(message, " ") - hasspace = false - } - - for _, word := range sentence { - if !strings.Contains(strings.ToLower(word), "uwu") { - word = strings.Replace(word, "u", "UwU", -1) - - if strings.Contains(strings.ToLower(word), "owo") { - word = strings.Replace(word, "o", "OwO", -1) - } - word = strings.Replace(word, "r", "w", -1) - - } - if hasspace { - answer += word + " " - } else { - answer += word + if action == "infoln" { + utils.CuteNewLine() } } - return answer } func getHostname() string { @@ -190,9 +99,7 @@ func getUsername() string { shell, _ := cmd.Output() return strings.Replace(string(shell), "\n", "", -1) } -func getLogo() { -} func getDistro() string { distro, err := os.Open("/etc/os-release") if err != nil { @@ -201,7 +108,7 @@ func getDistro() string { } distro_info := make([]byte, 1024) distro.Read(distro_info) - distro.Close() + defer distro.Close() distro_list := strings.Split(string(distro_info), "\n") distro_tuples := make(map[string]string) for _, v := range distro_list { @@ -243,11 +150,11 @@ func getTheme() { func getIcons() { } func getTerminal() string { - _, existprgm := os.LookupEnv("TERM_PROGRAM") + a, existprgm := os.LookupEnv("TERM_PROGRAM") if !existprgm { return os.Getenv("TERM") } else { - return os.Getenv("TERM_PROGRAM") + return a } } @@ -313,16 +220,6 @@ func getMemory(used bool) string { } } -func handleremainingascii() { - if aa < len(asciiart) { - for i := 0; i < len(asciiart)-aa; i++ { - print("\n", asciiart[aa]) - incrementaa() - - } - } -} - func formatTime(seconds int) string { minutes := seconds / 60 secondsre := strconv.Itoa(seconds % 60) @@ -336,8 +233,7 @@ func getColorPalette() { func main() { utils.Initargs() - initascii() + utils.CutePrintInit(utils.Getascii(getDistro())) handleConfig() - handleremainingascii() - print("\n") + utils.CutePrintEnd() } diff --git a/utils/asciiarts.go b/utils/asciiarts.go index 7343143..f3f1eda 100644 --- a/utils/asciiarts.go +++ b/utils/asciiarts.go @@ -1,10 +1,6 @@ package utils -import ( - "strings" -) - -func Getascii(name string) []string { +func Getascii(name string) string { none := `!!!!!!!!!!!!!!! !!!!!!!!!!!!!!! !!!!noascii!!!! @@ -20,11 +16,10 @@ func Getascii(name string) []string { switch name { case "Arch Linux": - return strings.Split(arch, "\n") + return arch default: - return strings.Split(none, "\n") - + return none } } diff --git a/utils/printing.go b/utils/printing.go new file mode 100644 index 0000000..cc47d0e --- /dev/null +++ b/utils/printing.go @@ -0,0 +1,148 @@ +package utils + +import ( + "fmt" + "math/rand" + "strings" +) + +func rgb(r, g, b int) string { + return fmt.Sprintf("\x1b[38:2::%d:%d:%dm", r, g, b) +} + +var colors = map[string]string{ + "green": rgb(0, 255, 0), + "blue": rgb(0, 0, 255), + "red": rgb(255, 0, 0), +} + +var uwuEmotes = [15]string{ + "owo", + "UwU", + ">w<", + "^w^", + "●w●", + "☆w☆", + "𝗨𝘄𝗨", + "(´꒳`)", + "♥(。U ω U。)", + "(˘ε˘)", + "( ˘ᴗ˘ )", + "(*ฅ́˘ฅ̀*)", + "*screams*", + "*twerks*", + "*sweats*", +} +var logoIndex = 0 +var isInProgressLine = false +var logoLines []string +var logoWidth int + +func CutePrintInit(logo string) { + if noascii { + logo = "" + } + logoLines = strings.Split(logo, "\n") + logoWidth = 0 + for _, v := range logoLines { + lineLength := len([]rune(v)) + if lineLength > logoWidth { + logoWidth = lineLength + } + } +} + +func printLogoIfAtBeginningOfNewLine() { + if !isInProgressLine { + isInProgressLine = true + if logoIndex < len(logoLines) { + logoLine := logoLines[logoIndex] + logoIndex += 1 + logoLineLength := len([]rune(logoLine)) + padding := strings.Repeat(" ", logoWidth-logoLineLength) + fmt.Printf("%s%s", logoLine, padding) + } else { + fmt.Printf("%s", strings.Repeat(" ", logoWidth)) + } + } +} + +func uwuify(message string) (ret string) { + sentence := strings.Split(message, " ") + ret = "" + for i, word := range sentence { + if !strings.Contains(strings.ToLower(word), "uwu") { + word = strings.ReplaceAll(word, "u", "UwU") + + if strings.Contains(strings.ToLower(word), "owo") { + word = strings.ReplaceAll(word, "o", "OwO") + } + word = strings.ReplaceAll(word, "r", "w") + + } + if i != 0 { + ret += " " + } + ret += word + } + return ret +} + +type Format struct { + noUwuOverride bool + colorFormat string +} + +func parseFormat(format string) (parsedFormat Format) { + for _, v := range strings.Split(format, "|") { + colorFormat, isColor := colors[v] + if isColor { + parsedFormat.colorFormat += colorFormat + } else { + switch v { + case "italic": + parsedFormat.colorFormat += "\x1b[3m" + break + case "bold": + parsedFormat.colorFormat += "\x1b1" + break + case "nouwu": + parsedFormat.noUwuOverride = true + break + case "*": + break + default: + //println("Unknown format code: ", v) + } + } + } + return parsedFormat +} + +func CutePrint( + message string, + format string, +) { + printLogoIfAtBeginningOfNewLine() + parsedFormat := parseFormat(format) + willUwuify := shoulduwuify && !parsedFormat.noUwuOverride + if willUwuify { + message = uwuify(message) + } + fmt.Printf("%s%s\x1b[0m", parsedFormat.colorFormat, message) +} + +func CuteNewLine() { + printLogoIfAtBeginningOfNewLine() + if rand.Intn(5) == 0 && shoulduwuify { + fmt.Printf(" %s", uwuEmotes[rand.Intn(len(uwuEmotes))]) + } + isInProgressLine = false + fmt.Println() +} + +func CutePrintEnd() { + for logoIndex < len(logoLines) { + CuteNewLine() + } +}