print ascii if chafa not available

This commit is contained in:
nekohepott 2026-06-17 04:16:28 +03:00
parent 3f892b6605
commit 5ee58d92ec
No known key found for this signature in database
2 changed files with 62 additions and 35 deletions

View File

@ -37,8 +37,6 @@ func main() {
if conf.CustomLogo != "" {
defaultLogo = conf.CustomLogo
} else if conf.Ascii {
defaultLogo = providers.GetAscii(distID)
} else {
defaultLogo = providers.GetLogo(distID)
}
@ -57,32 +55,59 @@ func main() {
}
var stats []Info
for _, item := range conf.Layout {
switch strings.ToLower(item) {
case "host":
stats = append(stats, Info{"host", providers.GetHostname()})
case "dist":
stats = append(stats, Info{"os", prettyName})
case "cpu":
stats = append(stats, Info{"cpu", providers.GetCpu()})
case "kernel", "krnl":
stats = append(stats, Info{"krnl", providers.GetKernel()})
case "ram":
stats = append(stats, Info{"ram", providers.GetRam()})
case "gpu":
stats = append(stats, Info{"gpu", providers.GetGpu()})
case "de/wm", "de", "wm":
stats = append(stats, Info{"de/wm", providers.GetDE()})
case "pkgs":
stats = append(stats, Info{"pkgs", providers.GetPkgs()})
case "shell":
stats = append(stats, Info{"shell", providers.GetShell()})
case "terminal", "term":
stats = append(stats, Info{"term", providers.GetTerminal()})
case "uptime":
stats = append(stats, Info{"uptime", providers.GetUptime()})
case "age":
stats = append(stats, Info{"age", providers.GetAge()})
aliasKeys := map[string]string{
"kernel": "krnl",
"krnl": "krnl",
"terminal": "term",
"term": "term",
"de/wm": "de/wm",
"de": "de/wm",
"wm": "de/wm",
}
type statProvider struct {
label string
fn func() string
}
statProviders := map[string]statProvider{
"host": {label: "host", fn: providers.GetHostname},
"dist": {label: "os", fn: func() string { return prettyName }},
"cpu": {label: "cpu", fn: providers.GetCpu},
"krnl": {label: "kernel", fn: providers.GetKernel},
"ram": {label: "ram", fn: providers.GetRam},
"gpu": {label: "gpu", fn: providers.GetGpu},
"de/wm": {label: "de/wm", fn: providers.GetDE},
"pkgs": {label: "packages", fn: providers.GetPkgs},
"shell": {label: "shell", fn: providers.GetShell},
"term": {label: "terminal", fn: providers.GetTerminal},
"uptime": {label: "uptime", fn: providers.GetUptime},
"age": {label: "age", fn: providers.GetAge},
}
results := make([]*Info, len(conf.Layout))
var wg sync.WaitGroup
for i, item := range conf.Layout {
key := strings.ToLower(item)
if alias, ok := aliasKeys[key]; ok {
key = alias
}
provider, ok := statProviders[key]
if !ok {
continue
}
wg.Add(1)
go func(idx int, p statProvider) {
defer wg.Done()
results[idx] = &Info{Label: p.label, Value: p.fn()}
}(i, provider)
}
wg.Wait()
for _, info := range results {
if info != nil {
stats = append(stats, *info)
}
}
@ -104,10 +129,11 @@ func main() {
var logoLines []string
if _, err := os.Stat(*logoPath); err == nil {
logoLines, err = providers.RenderLogoChafa(*logoPath, uint(*width))
if conf.Ascii {
logoLines = providers.SplitLines(providers.PrintAsciiLogo(*logoPath))
} else {
logoLines = providers.RenderLogoChafa(*logoPath, uint(*width))
} else if err != nil {
logoLines = providers.SplitLines(providers.PrintAsciiLogo(*logoPath))
}
} else {
logoLines = []string{fmt.Sprintf("Logo not found at: %s", *logoPath)}

View File

@ -25,12 +25,13 @@ func GetLogo(id string) string {
func GetAscii(id string) string {
knownIDs := []string{"arch", "nixos", "debian"}
if slices.Contains(knownIDs, id) {
return path.Join("ascii", id+".txt")
return path.Join("logos", id+".txt")
}
return "ascii/linux.txt"
return "logos/linux.txt"
}
func PrintAsciiLogo(path string) string {
path = GetAscii(id)
content, err := os.ReadFile(path)
if err != nil {
return fmt.Sprintf("Error: Could not read ASCII file at %s\n", path)
@ -38,7 +39,7 @@ func PrintAsciiLogo(path string) string {
return string(content)
}
func RenderLogoChafa(path string, width uint) []string {
func RenderLogoChafa(path string, width uint) ([]string, error) {
cols := width / 8
if cols == 0 {
cols = 1
@ -47,9 +48,9 @@ func RenderLogoChafa(path string, width uint) []string {
cmd := exec.Command("chafa", path, "--format", "symbols", sizeArg, "--symbols=half")
out, err := cmd.Output()
if err != nil {
return []string{fmt.Sprintf("Chafa error: %v", err)}
return nil, err
}
return SplitLines(string(out))
return SplitLines(string(out)), err
}
func NormalizeLogoPosition(pos string) string {