Simplify color map system

This commit is contained in:
2025-08-23 17:43:51 +03:00
parent 74eb84e706
commit 1439a22378
3 changed files with 37 additions and 44 deletions
-2
View File
@@ -1,7 +1,5 @@
distro_ascii: auto distro_ascii: auto
fetch_script: auto fetch_script: auto
ansii_colors: []
force_config_ansii: false
show_fs_type: true show_fs_type: true
hidden_partitions: [] hidden_partitions: []
# Hiding squashfs prevents snaps from showing up # Hiding squashfs prevents snaps from showing up
+23
View File
@@ -0,0 +1,23 @@
package main
import (
"fmt"
"strconv"
"strings"
)
func setupColorMap(asciiArt string) map[string]string {
colorMap := make(map[string]string)
// Read colors from ascii art
if strings.HasPrefix(asciiArt, "#/") {
firstLine := strings.Split(asciiArt, "\n")[0]
ansiColors := strings.Split(strings.TrimPrefix(firstLine, "#/"), ";")
for i, ansiColor := range ansiColors {
colorMap["C"+strconv.Itoa(i+1)] = fmt.Sprintf("\033[38;5;%sm", ansiColor)
}
}
return colorMap
}
+13 -41
View File
@@ -25,8 +25,6 @@ var TimeTaken = false
var config = StormfetchConfig{ var config = StormfetchConfig{
Ascii: "auto", Ascii: "auto",
FetchScript: "auto", FetchScript: "auto",
AnsiiColors: make([]int, 0),
ForceConfigAnsii: false,
ShowFSType: false, ShowFSType: false,
HiddenPartitions: make([]string, 0), HiddenPartitions: make([]string, 0),
HiddenGPUS: make([]int, 0), HiddenGPUS: make([]int, 0),
@@ -189,48 +187,22 @@ func SetupFetchEnv(showTimeTaken bool) []string {
} }
func runStormfetch() { func runStormfetch() {
// Fetch ascii art and apply colors // Fetch ascii art
colorMap := make(map[string]string) asciiArt := GetDistroAsciiArt()
colorMap["C0"] = "\033[0m"
setColorMap := func() { // Setup color map
for i := 0; i < 6; i++ { colorMap := setupColorMap(asciiArt)
if i > len(config.AnsiiColors)-1 { if len(colorMap) > 0 {
colorMap["C"+strconv.Itoa(i+1)] = "\033[0m" asciiArt = os.Expand(asciiArt, func(s string) string {
continue
}
colorMap["C"+strconv.Itoa(i+1)] = fmt.Sprintf("\033[1m\033[38;5;%dm", config.AnsiiColors[i])
}
}
setColorMap()
ascii := GetDistroAsciiArt()
if strings.HasPrefix(ascii, "#/") {
firstLine := strings.Split(ascii, "\n")[0]
if !config.ForceConfigAnsii {
ansiiColors := strings.Split(strings.TrimPrefix(firstLine, "#/"), ";")
for i, color := range ansiiColors {
atoi, err := strconv.Atoi(color)
if err != nil {
log.Fatal(err)
}
if i < len(config.AnsiiColors) {
config.AnsiiColors[i] = atoi
} else {
config.AnsiiColors = append(config.AnsiiColors, atoi)
}
}
setColorMap()
}
ascii = os.Expand(ascii, func(s string) string {
return colorMap[s]
})
ascii = strings.TrimPrefix(ascii, firstLine+"\n")
} else {
ascii = os.Expand(ascii, func(s string) string {
return colorMap[s] return colorMap[s]
}) })
asciiArt = strings.SplitN(asciiArt, "\n", 2)[1]
} }
asciiSplit := strings.Split(ascii, "\n")
asciiNoColor := StripAnsii(ascii) asciiSplit := strings.Split(asciiArt, "\n")
asciiNoColor := StripAnsii(asciiArt)
// Execute fetch script // Execute fetch script
cmd := exec.Command("/bin/bash", fetchScriptPath) cmd := exec.Command("/bin/bash", fetchScriptPath)
cmd.Dir = path.Dir(fetchScriptPath) cmd.Dir = path.Dir(fetchScriptPath)