Skip to content

Commit

Permalink
Merge pull request #12 from xbt573/develop
Browse files Browse the repository at this point in the history
Resolution, local and public ip outputs. Some code refactor
  • Loading branch information
xbt573 authored Dec 7, 2022
2 parents 97b2540 + 2d485ec commit 04fde6d
Show file tree
Hide file tree
Showing 7 changed files with 281 additions and 144 deletions.
3 changes: 3 additions & 0 deletions barkfetch.config
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ os=true
kernel=true
uptime=true
shell=true
resolution=true
cpu=true
gpu=true
memory=true
localip=false
remoteip=false
colors=true
22 changes: 16 additions & 6 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,12 @@ 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")
_localip = flag.Bool("localip", true, "Display local IP")
_remoteip = flag.Bool("remoteip", true, "Display remote IP")
_colors = flag.Bool("colors", true, "Display colors")
)

Expand Down Expand Up @@ -107,6 +110,10 @@ configChosed:
config["shell"] = boolToString(*_shell)
}

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

if isFlagPassed("cpu") {
config["cpu"] = boolToString(*_cpu)
}
Expand All @@ -119,6 +126,14 @@ configChosed:
config["memory"] = boolToString(*_memory)
}

if isFlagPassed("localip") {
config["localip"] = boolToString(*_localip)
}

if isFlagPassed("remoteip") {
config["remoteip"] = boolToString(*_remoteip)
}

if isFlagPassed("colors") {
config["colors"] = boolToString(*_colors)
}
Expand Down Expand Up @@ -159,12 +174,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
126 changes: 77 additions & 49 deletions info/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ 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", "localip",
"remoteip", "colors"}

// Regexp matching empty lines, useful to make output more pretty
var emptyLinesRegex = regexp.MustCompile(`(?m)\n$`)
Expand All @@ -23,7 +24,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 +46,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 +56,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 +68,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 +78,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 +90,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 +100,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 +148,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,12 +159,29 @@ func GetInfoString(options map[string]string) (string, error) {
)
lines++

case "cpu":
cpu, err := getRawCpu()
if err != nil {
return "", err
case "resolution":
resolutions := getRawScreenResolutions()

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

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

case "cpu":
cpu := getRawCpu()

output += formatAndColor(
"\x1b[%vG${caccent}CPU${creset}: %v\n",
offset,
Expand All @@ -190,9 +190,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,17 +209,42 @@ 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),
)
}

lines++

case "localip":
localip := getRawLocalIp()

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

case "remoteip":
remoteip := getRawOutboundIp()

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

Expand Down Expand Up @@ -245,6 +274,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}
50 changes: 42 additions & 8 deletions info/raw.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import (
"embed"
_ "embed"
"fmt"
"io"
"net"
"net/http"
"os"
"regexp"
"runtime"
Expand All @@ -23,8 +26,41 @@ 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
}

// Get local ip
func getRawLocalIp() string {
conn, err := net.Dial("udp", "8.8.8.8:80")
if err != nil {
return "n/a"
}
defer conn.Close()

localAddr := conn.LocalAddr().(*net.UDPAddr)
return localAddr.IP.String()
}

// Get outbound ip
func getRawOutboundIp() string {
resp, err := http.Get("https://api.ipify.org?format=text")
if err != nil {
return "n/a"
}
defer resp.Body.Close()

ip, err := io.ReadAll(resp.Body)
if err != nil {
return "n/a"
}

return string(ip)
}

// Gets OS architecture
Expand Down Expand Up @@ -59,17 +95,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 +128,5 @@ func getLogo(distro string) (Logo, error) {

logo.MaxLength = max

return logo, nil
return logo
}
Loading

0 comments on commit 04fde6d

Please sign in to comment.