Skip to content

Commit

Permalink
resolution output, some code refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
xbt573 committed Dec 7, 2022
1 parent c740844 commit b582ec3
Show file tree
Hide file tree
Showing 7 changed files with 192 additions and 143 deletions.
1 change: 1 addition & 0 deletions barkfetch.config
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ os=true
kernel=true
uptime=true
shell=true
resolution=true
cpu=true
gpu=true
memory=true
Expand Down
12 changes: 6 additions & 6 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ var (
_kernel = flag.Bool("kernel", true, "Display system kernel type and version")
_uptime = flag.Bool("uptime", true, "Display system uptime")
_shell = flag.Bool("shell", true, "Display current shell")
_resolution = flag.Bool("resolution", true, "Display screen resolution")
_cpu = flag.Bool("cpu", true, "Display CPU model")
_gpu = flag.Bool("gpu", true, "Display GPU manufacturer and model")
_memory = flag.Bool("memory", true, "Display used and total memory in megabytes")
Expand Down Expand Up @@ -107,6 +108,10 @@ configChosed:
config["shell"] = boolToString(*_shell)
}

if isFlagPassed("resolution") {
config["resolution"] = boolToString(*_resolution)
}

if isFlagPassed("cpu") {
config["cpu"] = boolToString(*_cpu)
}
Expand Down Expand Up @@ -159,12 +164,7 @@ func Run() error {
return err
}

sysinfo, err := info.GetInfoString(config)

if err != nil {
return err
}

sysinfo := info.GetInfoString(config)
fmt.Println(sysinfo)

return nil
Expand Down
101 changes: 49 additions & 52 deletions info/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

// Possible options, to make output sorted independent of config/cmd
var possibleOptions = []string{"logo", "userline", "userunderline", "os",
"kernel", "uptime", "shell", "cpu", "gpu", "memory", "colors"}
"kernel", "uptime", "shell", "resolution", "cpu", "gpu", "memory", "colors"}

// Regexp matching empty lines, useful to make output more pretty
var emptyLinesRegex = regexp.MustCompile(`(?m)\n$`)
Expand All @@ -23,7 +23,7 @@ func formatAndColor(format string, args ...any) string {
}

// Returns processed info for pretty output
func GetInfoString(options map[string]string) (string, error) {
func GetInfoString(options map[string]string) string {
// out string
var output string

Expand All @@ -45,10 +45,7 @@ func GetInfoString(options map[string]string) (string, error) {

switch possibleOption {
case "logo":
logo, err := getLogo(value)
if err != nil {
return "", err
}
logo := getLogo(value)

output += os.Expand(logo.Logo, ColorExpand) +
strings.Repeat("\x1b[F", logo.Lines-1)
Expand All @@ -58,10 +55,7 @@ func GetInfoString(options map[string]string) (string, error) {

case "userline":
username := getRawUser()
hostname, err := getRawHostname()
if err != nil {
return "", nil
}
hostname := getRawHostname()

output += formatAndColor(
"\x1b[%vG${caccent}%v${creset}@${caccent}%v${creset}\n",
Expand All @@ -73,10 +67,7 @@ func GetInfoString(options map[string]string) (string, error) {

case "userunderline":
username := getRawUser()
hostname, err := getRawHostname()
if err != nil {
return "", nil
}
hostname := getRawHostname()

output += formatAndColor(
"\x1b[%vG%v\n",
Expand All @@ -86,11 +77,7 @@ func GetInfoString(options map[string]string) (string, error) {
lines++

case "os":
os, err := getRawPrettyName()
if err != nil {
return "", err
}

os := getRawPrettyName()
arch := getRawArchitecture()

output += formatAndColor(
Expand All @@ -102,10 +89,7 @@ func GetInfoString(options map[string]string) (string, error) {
lines++

case "kernel":
kernel, err := getRawKernel()
if err != nil {
return "", err
}
kernel := getRawKernel()

output += formatAndColor(
"\x1b[%vG${caccent}Kernel${creset}: %v\n",
Expand All @@ -115,12 +99,16 @@ func GetInfoString(options map[string]string) (string, error) {
lines++

case "uptime":
uptime, err := getRawUptime()
if err != nil {
return "", err
uptime := getRawUptime()

if uptime <= 0 {
output += formatAndColor(
"\x1b[%vG${caccent}Uptime:${creset}: n/a\n",
offset,
)
}

if uptime <= 60 {
if uptime > 0 && uptime <= 60 {
output += formatAndColor(
"\x1b[%vG${caccent}Uptime${creset}: %v s\n",
offset,
Expand Down Expand Up @@ -159,12 +147,6 @@ func GetInfoString(options map[string]string) (string, error) {
}

lines++
// output += formatAndColor(
// "\x1b[%vG${caccent}Uptime${creset}: %v minutes\n",
// offset,
// int(uptime/60),
// )
// lines++

case "shell":
shell := getRawShell()
Expand All @@ -176,11 +158,18 @@ func GetInfoString(options map[string]string) (string, error) {
)
lines++

case "resolution":
resolution := getRawScreenResolution()

output += formatAndColor(
"\x1b[%vG${caccent}Resolution${creset}: %v\n",
offset,
resolution,
)
lines++

case "cpu":
cpu, err := getRawCpu()
if err != nil {
return "", err
}
cpu := getRawCpu()

output += formatAndColor(
"\x1b[%vG${caccent}CPU${creset}: %v\n",
Expand All @@ -190,9 +179,13 @@ func GetInfoString(options map[string]string) (string, error) {
lines++

case "gpu":
gpus, err := getRawGpus()
if err != nil {
return "", err
gpus := getRawGpus()

if len(gpus) == 0 {
output += formatAndColor(
"\x1b[%vG${caccent}GPU${creset}: n/a\n",
offset,
)
}

for _, gpu := range gpus {
Expand All @@ -205,18 +198,23 @@ func GetInfoString(options map[string]string) (string, error) {
}

case "memory":
used, total, err := getRawMemory()
if err != nil {
return "", err
used, total := getRawMemory()

if used <= 0 || total <= 0 {
output += formatAndColor(
"\x1b[%vG${caccent}Memory${creset}: n/a\n",
offset,
)
} else {
output += formatAndColor(
"\x1b[%vG${caccent}Memory${creset}: %v / %v Mb (%v%%)\n",
offset,
used,
total,
int(float32(used)/float32(total)*100.0),
)
}

output += formatAndColor(
"\x1b[%vG${caccent}Memory${creset}: %v / %v Mb (%v%%)\n",
offset,
used,
total,
int(float32(used)/float32(total)*100.0),
)
lines++

case "colors":
Expand Down Expand Up @@ -245,6 +243,5 @@ func GetInfoString(options map[string]string) (string, error) {
output += strings.Repeat("\n", logolines-lines)
}

return output, nil
// return emptyLinesRegex.ReplaceAllString(output, ""), nil
return output
}
2 changes: 1 addition & 1 deletion info/logos/void.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
UU |===| UU
\\ ^~^ //
`0PpppP'
`````${creset}
`````${creset}
19 changes: 11 additions & 8 deletions info/raw.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,13 @@ func getRawUser() string {
}

// Gets system hostname
func getRawHostname() (string, error) {
return os.Hostname()
func getRawHostname() string {
hostname, err := os.Hostname()
if err != nil {
return "n/a"
}

return hostname
}

// Gets OS architecture
Expand Down Expand Up @@ -59,17 +64,15 @@ var (

// Returns distro logo by name, or guesses if arg is "auto"
// Currently return small count of logos, mostly default
func getLogo(distro string) (Logo, error) {
logoText := ""
func getLogo(distro string) Logo {
logoText := _default

if distro == "auto" {
return getLogo(guessDistro())
}

bytes, err := logos.ReadFile(fmt.Sprintf("logos/%v.txt", distro))
if err != nil {
logoText = _default
} else {
if err == nil {
logoText = string(bytes)
}

Expand All @@ -94,5 +97,5 @@ func getLogo(distro string) (Logo, error) {

logo.MaxLength = max

return logo, nil
return logo
}
Loading

0 comments on commit b582ec3

Please sign in to comment.