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 != "" { if conf.CustomLogo != "" {
defaultLogo = conf.CustomLogo defaultLogo = conf.CustomLogo
} else if conf.Ascii {
defaultLogo = providers.GetAscii(distID)
} else { } else {
defaultLogo = providers.GetLogo(distID) defaultLogo = providers.GetLogo(distID)
} }
@ -57,32 +55,59 @@ func main() {
} }
var stats []Info var stats []Info
for _, item := range conf.Layout { aliasKeys := map[string]string{
switch strings.ToLower(item) { "kernel": "krnl",
case "host": "krnl": "krnl",
stats = append(stats, Info{"host", providers.GetHostname()}) "terminal": "term",
case "dist": "term": "term",
stats = append(stats, Info{"os", prettyName}) "de/wm": "de/wm",
case "cpu": "de": "de/wm",
stats = append(stats, Info{"cpu", providers.GetCpu()}) "wm": "de/wm",
case "kernel", "krnl": }
stats = append(stats, Info{"krnl", providers.GetKernel()})
case "ram": type statProvider struct {
stats = append(stats, Info{"ram", providers.GetRam()}) label string
case "gpu": fn func() string
stats = append(stats, Info{"gpu", providers.GetGpu()}) }
case "de/wm", "de", "wm":
stats = append(stats, Info{"de/wm", providers.GetDE()}) statProviders := map[string]statProvider{
case "pkgs": "host": {label: "host", fn: providers.GetHostname},
stats = append(stats, Info{"pkgs", providers.GetPkgs()}) "dist": {label: "os", fn: func() string { return prettyName }},
case "shell": "cpu": {label: "cpu", fn: providers.GetCpu},
stats = append(stats, Info{"shell", providers.GetShell()}) "krnl": {label: "kernel", fn: providers.GetKernel},
case "terminal", "term": "ram": {label: "ram", fn: providers.GetRam},
stats = append(stats, Info{"term", providers.GetTerminal()}) "gpu": {label: "gpu", fn: providers.GetGpu},
case "uptime": "de/wm": {label: "de/wm", fn: providers.GetDE},
stats = append(stats, Info{"uptime", providers.GetUptime()}) "pkgs": {label: "packages", fn: providers.GetPkgs},
case "age": "shell": {label: "shell", fn: providers.GetShell},
stats = append(stats, Info{"age", providers.GetAge()}) "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 var logoLines []string
if _, err := os.Stat(*logoPath); err == nil { if _, err := os.Stat(*logoPath); err == nil {
logoLines, err = providers.RenderLogoChafa(*logoPath, uint(*width))
if conf.Ascii { if conf.Ascii {
logoLines = providers.SplitLines(providers.PrintAsciiLogo(*logoPath)) logoLines = providers.SplitLines(providers.PrintAsciiLogo(*logoPath))
} else { } else if err != nil {
logoLines = providers.RenderLogoChafa(*logoPath, uint(*width)) logoLines = providers.SplitLines(providers.PrintAsciiLogo(*logoPath))
} }
} else { } else {
logoLines = []string{fmt.Sprintf("Logo not found at: %s", *logoPath)} 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 { func GetAscii(id string) string {
knownIDs := []string{"arch", "nixos", "debian"} knownIDs := []string{"arch", "nixos", "debian"}
if slices.Contains(knownIDs, id) { 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 { func PrintAsciiLogo(path string) string {
path = GetAscii(id)
content, err := os.ReadFile(path) content, err := os.ReadFile(path)
if err != nil { if err != nil {
return fmt.Sprintf("Error: Could not read ASCII file at %s\n", path) 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) return string(content)
} }
func RenderLogoChafa(path string, width uint) []string { func RenderLogoChafa(path string, width uint) ([]string, error) {
cols := width / 8 cols := width / 8
if cols == 0 { if cols == 0 {
cols = 1 cols = 1
@ -47,9 +48,9 @@ func RenderLogoChafa(path string, width uint) []string {
cmd := exec.Command("chafa", path, "--format", "symbols", sizeArg, "--symbols=half") cmd := exec.Command("chafa", path, "--format", "symbols", sizeArg, "--symbols=half")
out, err := cmd.Output() out, err := cmd.Output()
if err != nil { 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 { func NormalizeLogoPosition(pos string) string {