goGoFetch/src/main.go

169 lines
4.4 KiB
Go

package main
import (
"flag"
"fmt"
"goGoFetch/src/providers"
_ "image/png"
"os"
"strings"
)
func main() {
resetConfig := flag.Bool("reset-config", false, "Reset config to defaults")
width := flag.Int("w", 190, "Logo width in lines")
logoPath := flag.String("l", "", "Path to the logo")
logoPos := flag.String("p", "", "Logo position: left or right")
ascii := flag.Bool("a", false, "Use ASCII art for logo")
flag.Parse()
if *resetConfig {
if err := providers.ResetConfig(); err != nil {
fmt.Printf("Error resetting config: %v\n", err)
os.Exit(1)
}
fmt.Println("Config reset to defaults")
return
}
conf, _ := providers.InitConfig()
if len(conf.Layout) == 0 {
conf.Layout = []string{"host", "dist", "cpu", "ram", "shell", "uptime"}
fmt.Println("Warning: No layout specified in config, using default")
}
distID, prettyName := providers.GetDist()
var defaultLogo string
if conf.CustomLogo != "" {
defaultLogo = conf.CustomLogo
} else {
defaultLogo = providers.GetLogo(distID, conf, ascii)
}
if *logoPath == "" {
*logoPath = providers.GetAbsLogoPath(defaultLogo)
}
if *logoPos == "" {
*logoPos = providers.NormalizeLogoPosition(conf.LogoPosition)
}
position := providers.NormalizeLogoPosition(*logoPos)
type Info struct {
Label string
Value string
}
var stats []Info
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)
}
}
key := "->"
statColor := providers.Blue
if conf.Key != "" {
key = conf.Key
}
if conf.StatColor != "" {
statColor = providers.GetColorCode(conf.StatColor)
}
var statsLines []string
for _, info := range stats {
statsLines = append(statsLines, fmt.Sprintf("%s %s%s%s: %s", key, statColor, info.Label, providers.Reset, info.Value))
}
var logoLines []string
if _, err := os.Stat(*logoPath); err == nil {
if conf.Ascii || (ascii != nil && *ascii) {
logoLines = providers.SplitLines(providers.PrintAsciiLogo(*logoPath))
} else if providers.IsGif(*logoPath) {
if err := providers.RenderLogoChafaAnimated(*logoPath, uint(*width), statsLines, position); err == nil {
return
}
parts := strings.Split(*logoPath, ".")
if len(parts) > 1 {
parts[1] = "txt"
}
*logoPath = strings.Join(parts, ".")
logoLines = providers.SplitLines(providers.PrintAsciiLogo(*logoPath))
if strings.Contains(*logoPath, ".txt") != true {
fmt.Printf("chafa binary not found, to print images please install chafa\n")
}
} else {
logoLines, err = providers.RenderLogoChafa(*logoPath, uint(*width))
if err != nil {
parts := strings.Split(*logoPath, ".")
if len(parts) > 1 {
parts[1] = "txt"
}
*logoPath = strings.Join(parts, ".")
logoLines = providers.SplitLines(providers.PrintAsciiLogo(*logoPath))
if strings.Contains(*logoPath, ".txt") != true {
fmt.Printf("chafa binary not found, to print images please install chafa\n")
}
}
}
} else {
logoLines = []string{fmt.Sprintf("Logo not found at: %s", *logoPath)}
}
providers.RenderSideBySide(statsLines, logoLines, position)
}