Skip to content

Commit

Permalink
add local and public ip output (public is very slow for now)
Browse files Browse the repository at this point in the history
  • Loading branch information
xbt573 committed Dec 7, 2022
1 parent cd9b065 commit 2d485ec
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 5 deletions.
2 changes: 2 additions & 0 deletions barkfetch.config
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@ resolution=true
cpu=true
gpu=true
memory=true
localip=false
remoteip=false
colors=true
10 changes: 10 additions & 0 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ var (
_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 @@ -124,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
23 changes: 22 additions & 1 deletion 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", "resolution", "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 Down Expand Up @@ -227,6 +228,26 @@ func GetInfoString(options map[string]string) string {

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}Remote IP${creset}: %v\n",
offset,
remoteip,
)
lines++

case "colors":
colors := getRawColors()
i := 0
Expand Down
31 changes: 31 additions & 0 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 Down Expand Up @@ -32,6 +35,34 @@ func getRawHostname() string {
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
func getRawArchitecture() string {
return runtime.GOARCH
Expand Down
7 changes: 3 additions & 4 deletions info/raw_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (
var (
getTotalMemRegex = regexp.MustCompile(`MemTotal:\s+(\d+) kB`)
getFreeMemRegex = regexp.MustCompile(`MemFree:\s+(\d+) kB`)
getAvailableMemRegex = regexp.MustCompile(`MemAvailable:\s+(\d+) kB`)
getShMemRegex = regexp.MustCompile(`Shmem:\s+(\d+) kB`)
getBuffersRegex = regexp.MustCompile(`Buffers:\s+(\d+) kB`)
getCachedRegex = regexp.MustCompile(`Cached:\s+(\d+) kB`)
Expand Down Expand Up @@ -197,15 +196,15 @@ func getRawGpus() []string {
for _, line := range match {
var manufacturer, model string

if strings.Index(line[1], "Intel") != -1 {
if strings.Contains(line[1], "Intel") {
manufacturer = "Intel"
}

if strings.Index(line[1], "NVIDIA") != -1 {
if strings.Contains(line[1], "NVIDIA") {
manufacturer = "NVIDIA"
}

if strings.Index(line[1], "AMD") != -1 {
if strings.Contains(line[1], "AMD") {
manufacturer = "AMD"
}

Expand Down

0 comments on commit 2d485ec

Please sign in to comment.