age stat, config reset flag

This commit is contained in:
nekohepott 2026-06-17 04:16:27 +03:00
parent 0aae0065be
commit 287c2895fd
No known key found for this signature in database
4 changed files with 122 additions and 19 deletions

View File

@ -42,4 +42,8 @@ package() {
chmod 755 "$pkgdir/usr/share/gogofetch/logos" chmod 755 "$pkgdir/usr/share/gogofetch/logos"
chmod 644 "$pkgdir/usr/share/gogofetch/logos/"*.png 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."
} }

View File

@ -10,6 +10,21 @@ import (
) )
func main() { 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() conf, _ := providers.InitConfig()
if len(conf.Layout) == 0 { if len(conf.Layout) == 0 {
@ -28,10 +43,12 @@ func main() {
defaultLogo = providers.GetLogo(distID) defaultLogo = providers.GetLogo(distID)
} }
width := flag.Int("w", 190, "Logo width in lines") if *logoPath == "" {
logoPath := flag.String("l", providers.GetAbsLogoPath(defaultLogo), "Path to the logo") *logoPath = providers.GetAbsLogoPath(defaultLogo)
logoPos := flag.String("p", providers.NormalizeLogoPosition(conf.LogoPosition), "Logo position: left or right") }
flag.Parse() if *logoPos == "" {
*logoPos = providers.NormalizeLogoPosition(conf.LogoPosition)
}
position := providers.NormalizeLogoPosition(*logoPos) position := providers.NormalizeLogoPosition(*logoPos)
type Info struct { type Info struct {
@ -64,6 +81,8 @@ func main() {
stats = append(stats, Info{"term", providers.GetTerminal()}) stats = append(stats, Info{"term", providers.GetTerminal()})
case "uptime": case "uptime":
stats = append(stats, Info{"uptime", providers.GetUptime()}) stats = append(stats, Info{"uptime", providers.GetUptime()})
case "age":
stats = append(stats, Info{"age", providers.GetAge()})
} }
} }

View File

@ -20,25 +20,86 @@ const (
White = "\033[1;37m" White = "\033[1;37m"
) )
const ConfigVersion = 1
type Config struct { type Config struct {
Ascii bool `toml:"ascii"` ConfigVersion int `toml:"config_version"`
Layout []string `toml:"layout"` Ascii bool `toml:"ascii"`
CustomLogo string `toml:"custom_logo"` Layout []string `toml:"layout"`
LogoPosition string `toml:"logo_position"` CustomLogo string `toml:"custom_logo"`
Key string `toml:"key"` LogoPosition string `toml:"logo_position"`
StatColor string `toml:"stat_color"` 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 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") { 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") 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") { if !md.IsDefined("layout") {
missing = append(missing, `# Stats layout: reorder or remove items to customize your fetch 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 = [ layout = [
"host", "host",
"dist", "dist",
@ -51,6 +112,7 @@ layout = [
"shell", "shell",
"terminal", "terminal",
"uptime", "uptime",
"age",
]`) ]`)
} }
@ -138,7 +200,7 @@ func InitConfig() (Config, string) {
ascii = false ascii = false
# Stats layout: reorder or remove items to customize your fetch # 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 = [ layout = [
"host", "host",
"dist", "dist",
@ -151,6 +213,7 @@ layout = [
"shell", "shell",
"terminal", "terminal",
"uptime", "uptime",
"age",
] ]
# Absolute path to a custom image or .txt file. Leave empty to use auto-detection. # Absolute path to a custom image or .txt file. Leave empty to use auto-detection.
@ -177,16 +240,17 @@ stat_color = ""
if err != nil { if err != nil {
fmt.Printf("Error loading config at %s: %v\n", pathConf, err) fmt.Printf("Error loading config at %s: %v\n", pathConf, err)
conf = Config{ conf = Config{
Layout: []string{"host", "dist", "cpu", "kernel", "ram", "gpu", "de/wm", "pkgs", "shell", "terminal", "uptime"}, ConfigVersion: ConfigVersion,
CustomLogo: "", Layout: []string{"host", "dist", "cpu", "kernel", "ram", "gpu", "de/wm", "pkgs", "shell", "terminal", "uptime", "age"},
LogoPosition: "left", CustomLogo: "",
Key: "->", LogoPosition: "left",
StatColor: "blue", Key: "->",
StatColor: "blue",
} }
return conf, pathConf 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) fmt.Printf("Warning: Could not update config at %s: %v\n", pathConf, err)
} else { } else {
if _, err := toml.DecodeFile(pathConf, &conf); err != nil { if _, err := toml.DecodeFile(pathConf, &conf); err != nil {

View File

@ -233,3 +233,19 @@ func GetShell() string {
} }
return strings.TrimSpace(string(out)) 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)
}