From 287c2895fd1962fedaea8c0168dd14880eb2fe06 Mon Sep 17 00:00:00 2001 From: nekohepott Date: Wed, 17 Jun 2026 04:16:27 +0300 Subject: [PATCH] age stat, config reset flag --- PKGBUILD | 4 ++ src/main.go | 27 +++++++++-- src/providers/config.go | 94 ++++++++++++++++++++++++++++++------ src/providers/systemstats.go | 16 ++++++ 4 files changed, 122 insertions(+), 19 deletions(-) diff --git a/PKGBUILD b/PKGBUILD index 7b78f24..3f1ebc0 100644 --- a/PKGBUILD +++ b/PKGBUILD @@ -42,4 +42,8 @@ package() { chmod 755 "$pkgdir/usr/share/gogofetch/logos" chmod 644 "$pkgdir/usr/share/gogofetch/logos/"*.png +} + +post_upgrade() { + echo "! It is recommended to run 'gogofetch --reset-config' after upgrading gogofetch-git to get the latest config." } \ No newline at end of file diff --git a/src/main.go b/src/main.go index cd175ed..6d1f8dc 100644 --- a/src/main.go +++ b/src/main.go @@ -10,6 +10,21 @@ import ( ) 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") + 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 { @@ -28,10 +43,12 @@ func main() { defaultLogo = providers.GetLogo(distID) } - width := flag.Int("w", 190, "Logo width in lines") - logoPath := flag.String("l", providers.GetAbsLogoPath(defaultLogo), "Path to the logo") - logoPos := flag.String("p", providers.NormalizeLogoPosition(conf.LogoPosition), "Logo position: left or right") - flag.Parse() + if *logoPath == "" { + *logoPath = providers.GetAbsLogoPath(defaultLogo) + } + if *logoPos == "" { + *logoPos = providers.NormalizeLogoPosition(conf.LogoPosition) + } position := providers.NormalizeLogoPosition(*logoPos) type Info struct { @@ -64,6 +81,8 @@ func main() { 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()}) } } diff --git a/src/providers/config.go b/src/providers/config.go index 047b9d2..3ae50b4 100644 --- a/src/providers/config.go +++ b/src/providers/config.go @@ -20,25 +20,86 @@ const ( White = "\033[1;37m" ) +const ConfigVersion = 1 + type Config struct { - Ascii bool `toml:"ascii"` - Layout []string `toml:"layout"` - CustomLogo string `toml:"custom_logo"` - LogoPosition string `toml:"logo_position"` - Key string `toml:"key"` - StatColor string `toml:"stat_color"` + ConfigVersion int `toml:"config_version"` + Ascii bool `toml:"ascii"` + Layout []string `toml:"layout"` + CustomLogo string `toml:"custom_logo"` + LogoPosition string `toml:"logo_position"` + Key string `toml:"key"` + StatColor string `toml:"stat_color"` } -func appendMissingConfigKeys(pathConf string, md toml.MetaData) error { +func ResetConfig() error { + pathConf, err := getConfigPath() + if err != nil { + return err + } + + if err := os.RemoveAll(pathConf); err != nil { + return err + } + + if err := os.MkdirAll(filepath.Dir(pathConf), 0755); err != nil { + return err + } + + defaultConfig := []byte(`config_version = 1 + +# Set this to "true" if you like ascii more than images (i'm too lazy to add support for all distros though, so there's only arch, nixos and debian) +ascii = false + +# Stats layout: reorder or remove items to customize your fetch +# Available: "dist", "host", "cpu", "kernel", "ram", "gpu", "de/wm", "pkgs", "shell", "terminal", "uptime", "age" +layout = [ + "host", + "dist", + "cpu", + "kernel", + "ram", + "gpu", + "de/wm", + "pkgs", + "shell", + "terminal", + "uptime", + "age", +] + +# Absolute path to a custom image or .txt file. Leave empty to use auto-detection. +custom_logo = "" + +# Position of the logo block relative to the stats block: "left" or "right". +logo_position = "left" + +# key symbol before stat (e.g. ->) +# default: "->" +key = "" + +# color of the stat label, default is blue +# available: blue, green, red, yellow, purple, cyan, white +stat_color = "" +`) + + return os.WriteFile(pathConf, defaultConfig, 0644) +} + +func appendMissingConfigKeys(pathConf string, md toml.MetaData, currentVersion int) error { var missing []string + if !md.IsDefined("config_version") || md.IsDefined("config_version") && currentVersion != ConfigVersion { + missing = append(missing, fmt.Sprintf("config_version = %d", ConfigVersion)) + } + if !md.IsDefined("ascii") { missing = append(missing, "# Set this to \"true\" if you like ascii more than images (i'm too lazy to add support for all distros though, so there's only arch, nixos and debian)\nascii = false") } if !md.IsDefined("layout") { missing = append(missing, `# Stats layout: reorder or remove items to customize your fetch -# Available: "dist", "host", "cpu", "kernel", "ram", "gpu", "de/wm", "pkgs", "shell", "terminal", "uptime" +# Available: "dist", "host", "cpu", "kernel", "ram", "gpu", "de/wm", "pkgs", "shell", "terminal", "uptime", "age" layout = [ "host", "dist", @@ -51,6 +112,7 @@ layout = [ "shell", "terminal", "uptime", + "age", ]`) } @@ -138,7 +200,7 @@ func InitConfig() (Config, string) { ascii = false # Stats layout: reorder or remove items to customize your fetch -# Available: "dist", "host", "cpu", "kernel", "ram", "gpu", "de/wm", "pkgs", "shell", "terminal", "uptime" +# Available: "dist", "host", "cpu", "kernel", "ram", "gpu", "de/wm", "pkgs", "shell", "terminal", "uptime", "age" layout = [ "host", "dist", @@ -151,6 +213,7 @@ layout = [ "shell", "terminal", "uptime", + "age", ] # Absolute path to a custom image or .txt file. Leave empty to use auto-detection. @@ -177,16 +240,17 @@ stat_color = "" if err != nil { fmt.Printf("Error loading config at %s: %v\n", pathConf, err) conf = Config{ - Layout: []string{"host", "dist", "cpu", "kernel", "ram", "gpu", "de/wm", "pkgs", "shell", "terminal", "uptime"}, - CustomLogo: "", - LogoPosition: "left", - Key: "->", - StatColor: "blue", + ConfigVersion: ConfigVersion, + Layout: []string{"host", "dist", "cpu", "kernel", "ram", "gpu", "de/wm", "pkgs", "shell", "terminal", "uptime", "age"}, + CustomLogo: "", + LogoPosition: "left", + Key: "->", + StatColor: "blue", } return conf, pathConf } - if err := appendMissingConfigKeys(pathConf, md); err != nil { + if err := appendMissingConfigKeys(pathConf, md, conf.ConfigVersion); err != nil { fmt.Printf("Warning: Could not update config at %s: %v\n", pathConf, err) } else { if _, err := toml.DecodeFile(pathConf, &conf); err != nil { diff --git a/src/providers/systemstats.go b/src/providers/systemstats.go index 9466b17..63940f7 100644 --- a/src/providers/systemstats.go +++ b/src/providers/systemstats.go @@ -233,3 +233,19 @@ func GetShell() string { } return strings.TrimSpace(string(out)) } + +func GetAge() string { + out, err := exec.Command("stat", "-c", "%W", "/").Output() + if err != nil { + fmt.Printf("Error executing command: %v\n", err) + return "unknown" + } + birthTime := strings.TrimSpace(string(out)) + if birthTime == "0" || birthTime == "-" { + return "unknown" + } + age, _ := strconv.Atoi(birthTime) + age = int(time.Now().Unix() - int64(age)) + birth := age / 86400 + return fmt.Sprintf("%d days", birth) +}