Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -20,4 +21,4 @@ $ go install

$ mkdir ~/.config/barkfetch
$ cp barkfetch.config ~/.config/barkfetch/config
```
```
53 changes: 17 additions & 36 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"errors"
"flag"
"fmt"
"io"
"os"
"strings"

Expand Down Expand Up @@ -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
}
})

Expand All @@ -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") {
Expand Down Expand Up @@ -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
}

Expand Down
27 changes: 7 additions & 20 deletions info/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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()
Expand Down Expand Up @@ -107,36 +102,28 @@ 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,
int(uptime/3600),
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,
Expand All @@ -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++

Expand Down
6 changes: 2 additions & 4 deletions info/raw.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand Down
34 changes: 8 additions & 26 deletions info/raw_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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
}
Expand All @@ -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]}
Expand All @@ -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{}
}

Expand Down
8 changes: 4 additions & 4 deletions info/raw_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand Down