2022-09-22 14:05:20 +02:00
|
|
|
package utils
|
|
|
|
|
|
|
|
import (
|
2022-09-22 14:44:27 +02:00
|
|
|
"encoding/base64"
|
2022-09-22 14:05:20 +02:00
|
|
|
"fmt"
|
2022-09-25 10:24:09 +02:00
|
|
|
"io/ioutil"
|
2022-09-22 14:44:27 +02:00
|
|
|
"os"
|
2022-09-23 11:03:50 +02:00
|
|
|
"path/filepath"
|
|
|
|
"strconv"
|
2022-09-22 14:05:20 +02:00
|
|
|
"strings"
|
2022-09-22 14:44:27 +02:00
|
|
|
|
2022-09-25 20:50:20 +02:00
|
|
|
"github.com/exhq/neowofetch/asciiarts"
|
2022-11-25 09:50:33 +01:00
|
|
|
"github.com/exhq/neowofetch/constants"
|
2022-09-22 14:44:27 +02:00
|
|
|
"github.com/exhq/neowofetch/data"
|
|
|
|
"github.com/exhq/neowofetch/images"
|
2022-09-22 14:05:20 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func rgb(r, g, b int) string {
|
|
|
|
return fmt.Sprintf("\x1b[38:2::%d:%d:%dm", r, g, b)
|
|
|
|
}
|
|
|
|
|
2022-09-23 11:03:50 +02:00
|
|
|
var colors = make(map[string]string)
|
2022-10-13 09:32:02 +02:00
|
|
|
var oldcolors = make(map[string]int)
|
2022-11-25 09:36:38 +01:00
|
|
|
|
2022-09-22 14:05:20 +02:00
|
|
|
var logoIndex = 0
|
|
|
|
var isInProgressLine = false
|
|
|
|
var logoLines []string
|
|
|
|
var logoWidth int
|
|
|
|
|
2022-09-22 14:44:27 +02:00
|
|
|
var pngWidth = 12
|
|
|
|
var pngHeight = 12
|
|
|
|
var pngData []byte
|
|
|
|
|
2022-10-14 10:43:33 +02:00
|
|
|
var colorconf = os.Getenv("HOME") + "/.config/neowofetch/colors"
|
|
|
|
var folderconf = filepath.Dir(colorconf)
|
|
|
|
var _, existcolorconf = os.Stat(colorconf)
|
|
|
|
var _, existfolderconf = os.Stat(folderconf)
|
2022-09-23 11:03:50 +02:00
|
|
|
|
2022-10-14 10:43:33 +02:00
|
|
|
func Initcolor() {
|
2022-09-23 11:03:50 +02:00
|
|
|
|
|
|
|
if os.IsNotExist(existfolderconf) {
|
|
|
|
os.Mkdir(folderconf, os.ModePerm)
|
|
|
|
}
|
|
|
|
if os.IsNotExist(existcolorconf) {
|
|
|
|
f, _ := os.Create(colorconf)
|
2022-11-25 09:50:33 +01:00
|
|
|
_, _ = f.WriteString(constants.Defaultcolor)
|
2022-09-23 11:03:50 +02:00
|
|
|
}
|
|
|
|
|
2022-10-04 12:35:41 +02:00
|
|
|
c, _ := os.ReadFile(colorconf)
|
|
|
|
content := string(c)
|
|
|
|
if Defaultcolor {
|
2022-11-25 09:50:33 +01:00
|
|
|
content = constants.Defaultcolor
|
2022-10-04 12:35:41 +02:00
|
|
|
}
|
2022-09-23 11:03:50 +02:00
|
|
|
lines := strings.Split(string(content), "\n")
|
2022-10-13 09:32:02 +02:00
|
|
|
|
2022-09-23 11:03:50 +02:00
|
|
|
for _, line := range lines {
|
2022-10-14 11:00:43 +02:00
|
|
|
if line == "" {
|
|
|
|
continue
|
|
|
|
}
|
2022-09-23 11:03:50 +02:00
|
|
|
word := strings.Split(line, " ")
|
2022-10-13 09:32:02 +02:00
|
|
|
|
2022-09-23 11:03:50 +02:00
|
|
|
R, _ := strconv.Atoi(word[1])
|
|
|
|
G, _ := strconv.Atoi(word[2])
|
|
|
|
B, _ := strconv.Atoi(word[3])
|
|
|
|
colors[word[0]] = rgb(R, G, B)
|
2022-10-13 09:32:02 +02:00
|
|
|
|
2022-09-23 11:03:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-22 14:44:27 +02:00
|
|
|
func CutePrintInit() {
|
|
|
|
dist := data.GetDistroVariable("ID")
|
2022-09-25 20:50:20 +02:00
|
|
|
logo := asciiarts.GetAscii(dist)
|
2022-11-13 18:47:35 +01:00
|
|
|
if Asciiforced {
|
|
|
|
logo = asciiarts.GetAscii(Forceddistro)
|
2022-09-25 10:24:09 +02:00
|
|
|
}
|
2022-09-22 14:05:20 +02:00
|
|
|
if noascii {
|
|
|
|
logo = ""
|
|
|
|
}
|
2022-09-22 14:44:27 +02:00
|
|
|
if usepng {
|
|
|
|
pngData = images.DistroImages[dist]
|
2022-09-23 11:03:50 +02:00
|
|
|
logo = strings.Repeat(" ", pngWidth) + " " + strings.Repeat("\n", pngHeight)
|
2022-09-22 14:44:27 +02:00
|
|
|
}
|
2022-09-25 12:27:18 +02:00
|
|
|
if Customascii {
|
|
|
|
body, _ := ioutil.ReadFile(asciidir)
|
|
|
|
logo = (string(body))
|
|
|
|
}
|
2022-09-22 14:05:20 +02:00
|
|
|
logoLines = strings.Split(logo, "\n")
|
|
|
|
logoWidth = 0
|
|
|
|
for _, v := range logoLines {
|
2022-09-25 20:50:20 +02:00
|
|
|
lineLength := len([]rune(v)) + 2
|
2022-09-22 14:05:20 +02:00
|
|
|
if lineLength > logoWidth {
|
2022-09-25 20:50:20 +02:00
|
|
|
logoWidth = lineLength
|
2022-09-22 14:05:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func printLogoIfAtBeginningOfNewLine() {
|
|
|
|
if !isInProgressLine {
|
|
|
|
isInProgressLine = true
|
|
|
|
if logoIndex < len(logoLines) {
|
|
|
|
logoLine := logoLines[logoIndex]
|
|
|
|
logoLineLength := len([]rune(logoLine))
|
|
|
|
padding := strings.Repeat(" ", logoWidth-logoLineLength)
|
|
|
|
fmt.Printf("%s%s", logoLine, padding)
|
|
|
|
} else {
|
|
|
|
fmt.Printf("%s", strings.Repeat(" ", logoWidth))
|
|
|
|
}
|
2022-09-22 14:44:27 +02:00
|
|
|
logoIndex += 1
|
2022-09-22 14:05:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-04 12:35:41 +02:00
|
|
|
func uwuify(message string) string {
|
2022-09-22 14:05:20 +02:00
|
|
|
sentence := strings.Split(message, " ")
|
2022-10-04 12:35:41 +02:00
|
|
|
ret := ""
|
2022-09-22 14:05:20 +02:00
|
|
|
for i, word := range sentence {
|
2022-11-25 09:36:38 +01:00
|
|
|
word = strings.ToLower(word)
|
2022-11-24 09:19:31 +01:00
|
|
|
|
2022-11-25 09:50:33 +01:00
|
|
|
if constants.Namechanges[word] != "" {
|
|
|
|
word = constants.Namechanges[word]
|
2022-11-24 09:19:31 +01:00
|
|
|
if i != 0 {
|
|
|
|
ret += " "
|
2022-09-22 14:05:20 +02:00
|
|
|
}
|
2022-11-25 09:36:38 +01:00
|
|
|
ret += word
|
2022-11-24 09:19:31 +01:00
|
|
|
continue
|
|
|
|
}
|
2022-09-22 14:05:20 +02:00
|
|
|
|
2022-11-25 09:36:38 +01:00
|
|
|
if len(word) > 5 {
|
|
|
|
if !strings.Contains(word, "owo") {
|
|
|
|
word = strings.ReplaceAll(word, "o", "OwO")
|
|
|
|
} else if !strings.Contains(word, "uwu") {
|
|
|
|
word = strings.ReplaceAll(word, "u", "UwU")
|
|
|
|
}
|
|
|
|
word = strings.ReplaceAll(strings.ReplaceAll(word, "r", "w"), "l", "w")
|
2022-09-22 14:05:20 +02:00
|
|
|
}
|
2022-11-24 09:19:31 +01:00
|
|
|
|
2022-09-22 14:05:20 +02:00
|
|
|
if i != 0 {
|
|
|
|
ret += " "
|
|
|
|
}
|
|
|
|
ret += word
|
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
|
|
|
type Format struct {
|
2022-11-16 07:41:35 +01:00
|
|
|
spaces int
|
2022-10-13 09:32:02 +02:00
|
|
|
noUwuOverride bool
|
|
|
|
colorFormat string
|
|
|
|
oldcolorFormat int
|
2022-09-22 14:05:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func parseFormat(format string) (parsedFormat Format) {
|
|
|
|
for _, v := range strings.Split(format, "|") {
|
|
|
|
colorFormat, isColor := colors[v]
|
2022-10-13 09:32:02 +02:00
|
|
|
parsedFormat.colorFormat += colorFormat
|
2022-09-23 11:03:50 +02:00
|
|
|
if isColor && hascolor {
|
2022-09-22 14:05:20 +02:00
|
|
|
parsedFormat.colorFormat += colorFormat
|
|
|
|
} else {
|
2022-11-16 07:41:35 +01:00
|
|
|
if strings.HasPrefix(v, "space") {
|
|
|
|
parsedFormat.spaces, _ = strconv.Atoi(v[6:])
|
|
|
|
return
|
|
|
|
}
|
2022-09-22 14:05:20 +02:00
|
|
|
switch v {
|
|
|
|
case "italic":
|
|
|
|
parsedFormat.colorFormat += "\x1b[3m"
|
|
|
|
case "bold":
|
|
|
|
parsedFormat.colorFormat += "\x1b1"
|
|
|
|
case "nouwu":
|
|
|
|
parsedFormat.noUwuOverride = true
|
|
|
|
case "*":
|
|
|
|
default:
|
2022-10-13 09:32:02 +02:00
|
|
|
if hascolor && !colorold {
|
2022-09-23 11:03:50 +02:00
|
|
|
println("Unknown format code: ", v)
|
|
|
|
}
|
2022-09-22 14:05:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return parsedFormat
|
|
|
|
}
|
|
|
|
|
2022-09-25 12:27:18 +02:00
|
|
|
func getcustomizeddistro() string {
|
2022-11-13 18:47:35 +01:00
|
|
|
if !Asciiforced {
|
2022-09-25 12:27:18 +02:00
|
|
|
return data.GetDistroVariable("ID")
|
|
|
|
} else {
|
2022-11-13 18:47:35 +01:00
|
|
|
return Forceddistro
|
2022-09-25 12:27:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-22 14:05:20 +02:00
|
|
|
func CutePrint(
|
|
|
|
message string,
|
|
|
|
format string,
|
|
|
|
) {
|
|
|
|
printLogoIfAtBeginningOfNewLine()
|
|
|
|
parsedFormat := parseFormat(format)
|
|
|
|
willUwuify := shoulduwuify && !parsedFormat.noUwuOverride
|
|
|
|
if willUwuify {
|
|
|
|
message = uwuify(message)
|
|
|
|
}
|
2022-11-16 07:41:35 +01:00
|
|
|
neededspaces := parsedFormat.spaces - len(message)
|
|
|
|
if neededspaces < 0 {
|
|
|
|
neededspaces = 0
|
|
|
|
}
|
2022-10-13 09:32:02 +02:00
|
|
|
if !colorold {
|
2022-11-16 07:41:35 +01:00
|
|
|
fmt.Printf("%s%s\x1b[0m%s", parsedFormat.colorFormat, message, strings.Repeat(" ", neededspaces))
|
2022-10-13 09:32:02 +02:00
|
|
|
} else {
|
2022-10-14 10:43:33 +02:00
|
|
|
if os.IsNotExist(existcolorconf) {
|
|
|
|
f, _ := os.Create(colorconf)
|
|
|
|
_, _ = f.WriteString("red 255 0 0 \ngreen 0 255 0\nblue 0 0 255\nwhite 255 255 255")
|
2022-10-16 14:02:02 +02:00
|
|
|
}
|
|
|
|
if colorold && hascolor {
|
2022-11-25 09:50:33 +01:00
|
|
|
for k, v := range constants.Color_map {
|
2022-10-16 14:02:02 +02:00
|
|
|
if strings.Contains(format, k) {
|
|
|
|
fmt.Printf("\033[1;%sm%s\033[m", v, message)
|
|
|
|
break
|
|
|
|
}
|
2022-10-13 09:32:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-22 14:05:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func CuteNewLine() {
|
|
|
|
printLogoIfAtBeginningOfNewLine()
|
|
|
|
isInProgressLine = false
|
|
|
|
fmt.Println()
|
|
|
|
}
|
|
|
|
|
|
|
|
func CutePrintEnd() {
|
|
|
|
for logoIndex < len(logoLines) {
|
|
|
|
CuteNewLine()
|
|
|
|
}
|
2022-09-22 14:44:27 +02:00
|
|
|
if usepng {
|
|
|
|
fmt.Printf("\x1b[%dA", logoIndex)
|
|
|
|
fmt.Printf("\x1b]1337;File=inline=1;width=%d;height=%d:", pngWidth, pngHeight)
|
|
|
|
enc := base64.NewEncoder(base64.StdEncoding, os.Stdout)
|
2022-09-25 12:27:18 +02:00
|
|
|
enc.Write(images.DistroImages[getcustomizeddistro()])
|
2022-09-22 14:44:27 +02:00
|
|
|
enc.Close()
|
|
|
|
fmt.Println("\a")
|
|
|
|
}
|
2022-09-22 14:05:20 +02:00
|
|
|
}
|