From d6c86790e972776cb50c11d1d16d9ee31cd4c45f Mon Sep 17 00:00:00 2001 From: sk44rt Date: Mon, 15 Jan 2024 02:35:10 +0100 Subject: [PATCH] just less shitcode --- README.md | 5 +++-- cmd/cmd.go | 53 +++++++++++++++------------------------------- info/info.go | 27 ++++++----------------- info/raw.go | 6 ++---- info/raw_darwin.go | 34 +++++++---------------------- info/raw_linux.go | 8 +++---- 6 files changed, 41 insertions(+), 92 deletions(-) diff --git a/README.md b/README.md index c7f71b2..fbedbb4 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,10 @@ # barkfetch -Barkfetch is an alternative to "neofetch", written in Go. Currently in very alpha. +Barkfetch is an [neofetch](https://github.com/dylanaraps/neofetch)-like utility, written in Go. Currently in very alpha. ![Barkfetch demonstration](.github/screenshot.png) # How to build +0. Make sure you have go compiler installed in your system 1. Clone repo and cd into it ```bash $ git clone https://github.com/xbt573/barkfetch && cd barkfetch @@ -20,4 +21,4 @@ $ go install $ mkdir ~/.config/barkfetch $ cp barkfetch.config ~/.config/barkfetch/config -``` +``` \ No newline at end of file diff --git a/cmd/cmd.go b/cmd/cmd.go index a5e2889..d6d123e 100644 --- a/cmd/cmd.go +++ b/cmd/cmd.go @@ -4,7 +4,6 @@ import ( "errors" "flag" "fmt" - "io" "os" "strings" @@ -32,12 +31,15 @@ var ( _colors = flag.Bool("colors", true, "Display colors") ) +var configCommonPaths = [3]string{"./barkfetch.config", os.ExpandEnv("$HOME/.config/barkfetch/config"), "/etc/barkfetch.config"} + // Helper function, returns true if flag was given at command-line func isFlagPassed(name string) bool { found := false flag.Visit(func(f *flag.Flag) { - if f.Name == name { - found = true + found = f.Name == name + if found { + return } }) @@ -46,41 +48,24 @@ func isFlagPassed(name string) bool { // Converts true to "true" and false to "false" func boolToString(input bool) string { - if input { - return "true" - } - - return "false" + return fmt.Sprintf("%t", input) } // Load config and return map of options func loadConfig() (map[string]string, error) { - f, err := os.Open("./barkfetch.config") - if err == nil { - goto configChosed - } - - f, err = os.Open(os.ExpandEnv("$HOME/.config/barkfetch/config")) - if err == nil { - goto configChosed - } - - f, err = os.Open("/etc/barkfetch.config") - if err == nil { - goto configChosed + var content []byte + for _, configPath := range configCommonPaths { + f, err := os.ReadFile(configPath) + if err == nil { + content = f + goto configChoosed + } } return map[string]string{}, ErrConfigNotFound -configChosed: - defer f.Close() - - raw, err := io.ReadAll(f) - if err != nil { - return map[string]string{}, err - } - - contents := string(raw) +configChoosed: + contents := string(content) config := parseConfig(contents) if isFlagPassed("logo") { @@ -147,17 +132,13 @@ func parseConfig(config string) map[string]string { options := make(map[string]string) for _, line := range strings.Split(config, "\n") { - if len(line) == 0 { - continue - } - - if line[0] == '#' { + if len(line) == 0 || line[0] == '#' { continue } kv := strings.Split(line, "=") - if len(kv) < 2 || len(kv) > 2 { + if len(kv) != 2 { continue } diff --git a/info/info.go b/info/info.go index 81f7d5e..81b0d5a 100644 --- a/info/info.go +++ b/info/info.go @@ -36,11 +36,7 @@ func GetInfoString(options map[string]string) string { for _, possibleOption := range possibleOptions { value, exists := options[possibleOption] - if !exists { - continue - } - - if value == "false" { + if !exists || value == "false" { continue } @@ -50,9 +46,8 @@ func GetInfoString(options map[string]string) string { output += os.Expand(logo.Logo, ColorExpand) + strings.Repeat("\x1b[F", logo.Lines-1) - offset = logo.MaxLength + 2 + offset, logolines = logo.MaxLength+2, logo.Lines Colors["caccent"] = Colors[logo.AccentColor] - logolines = logo.Lines case "userline": username := getRawUser() @@ -107,26 +102,20 @@ func GetInfoString(options map[string]string) string { "\x1b[%vG${caccent}Uptime:${creset}: n/a\n", offset, ) - } - - if uptime > 0 && uptime <= 60 { + } else if uptime > 0 && uptime <= 60 { output += formatAndColor( "\x1b[%vG${caccent}Uptime${creset}: %v s\n", offset, int(uptime), ) - } - - if uptime > 60 && uptime <= 3600 { + } else if uptime > 60 && uptime <= 3600 { output += formatAndColor( "\x1b[%vG${caccent}Uptime${creset}: %v m, %v s\n", offset, int(uptime/60), int(uptime%60), ) - } - - if uptime > 3600 && uptime <= 86400 { + } else if uptime > 3600 && uptime <= 86400 { output += formatAndColor( "\x1b[%vG${caccent}Uptime${creset}: %v h, %v m, %v s\n", offset, @@ -134,9 +123,7 @@ func GetInfoString(options map[string]string) string { int((uptime%3600)/60), int((uptime%3600)%60), ) - } - - if uptime > 86400 { + } else if uptime > 86400 { output += formatAndColor( "\x1b[%vG${caccent}Uptime${creset}: %v d, %v h, %v m, %v s\n", offset, @@ -145,7 +132,7 @@ func GetInfoString(options map[string]string) string { int(((uptime%86400)%3600)/60), int(((uptime%86400)%3600)%60), ) - } + } // turned into one block cuz uptime not changing lol lines++ diff --git a/info/raw.go b/info/raw.go index 1ff1460..798cce9 100644 --- a/info/raw.go +++ b/info/raw.go @@ -109,11 +109,9 @@ func getLogo(distro string) Logo { var logo Logo - match := getLogoRegex.FindStringSubmatch(logoText) - logo.Logo = match[1] + logo.Logo = getLogoRegex.FindStringSubmatch(logoText)[1] - match = getAccentRegex.FindStringSubmatch(logoText) - logo.AccentColor = match[1] + logo.AccentColor = getAccentRegex.FindStringSubmatch(logoText)[1] directiveFreeLogoText := replaceDirectivesRegex.ReplaceAllString(logoText, "") logo.Lines = len(strings.Split(directiveFreeLogoText, "\n")) diff --git a/info/raw_darwin.go b/info/raw_darwin.go index a6871e3..9f9cddf 100644 --- a/info/raw_darwin.go +++ b/info/raw_darwin.go @@ -45,11 +45,8 @@ func getRawUptime() int64 { } match := extractBoottimeRegex.FindStringSubmatch(string(out)) - if len(match) == 0 { - return -1 - } seconds, err := strconv.ParseInt(match[1], 10, 64) - if err != nil { + if len(match) == 0 || err != nil { return -1 } @@ -81,34 +78,24 @@ func getRawMemory() (used, total int) { } match := extractWiredMemoryRegex.FindStringSubmatch(string(vmStat)) - if len(match) == 0 { - return - } wired, err := strconv.ParseInt(match[1], 10, 64) - if err != nil { + if len(match) == 0 || err != nil { return } match = extractActiveMemoryRegex.FindStringSubmatch(string(vmStat)) - if len(match) == 0 { - return - } active, err := strconv.ParseInt(match[1], 10, 64) - if err != nil { + if len(match) == 0 || err != nil { return } match = extractCompressedMemoryRegex.FindStringSubmatch(string(vmStat)) - if len(match) == 0 { - return - } compressed, err := strconv.ParseInt(match[1], 10, 64) - if err != nil { + if len(match) == 0 || err != nil { return } - total = int(totalMemory / 1000000) - used = int((wired + active + compressed) * 4 / 1024) + total, used = int(totalMemory/1000000), int((wired+active+compressed)*4/1024) return } @@ -126,13 +113,11 @@ func getRawCpu() string { // Returns GPU manufacturer and model func getRawGpus() []string { out, err := exec.Command("system_profiler", "SPDisplaysDataType").Output() - if err != nil { - return []string{} - } contents := string(out) match := extractChipsetModelRegex.FindStringSubmatch(contents) - if len(match) == 0 { + + if err != nil || len(match) == 0 { return []string{} } return []string{match[1]} @@ -141,13 +126,10 @@ func getRawGpus() []string { // Returns main screen resolution func getRawScreenResolutions() []string { out, err := exec.Command("system_profiler", "SPDisplaysDataType").Output() - if err != nil { - return []string{} - } contents := string(out) match := extractResolutionRegex.FindStringSubmatch(contents) - if len(match) == 0 { + if err != nil || len(match) == 0 { return []string{} } diff --git a/info/raw_linux.go b/info/raw_linux.go index ea2bec9..4466efc 100644 --- a/info/raw_linux.go +++ b/info/raw_linux.go @@ -194,7 +194,7 @@ func getRawGpus() []string { gpus := []string{} for _, line := range match { - var manufacturer, model string + var manufacturer string if strings.Contains(line[1], "Intel") { manufacturer = "Intel" @@ -209,9 +209,9 @@ func getRawGpus() []string { } modelMatch := getGpuModelRegex.FindStringSubmatch(line[2]) - model = modelMatch[1] - - gpus = append(gpus, fmt.Sprintf("%v %v", manufacturer, model)) + if len(modelMatch) > 0 { + gpus = append(gpus, fmt.Sprintf("%v %v", manufacturer, modelMatch[1])) + } } return gpus