diff --git a/barkfetch.config b/barkfetch.config index 328b4e3..49a5b99 100644 --- a/barkfetch.config +++ b/barkfetch.config @@ -10,4 +10,6 @@ resolution=true cpu=true gpu=true memory=true +localip=false +remoteip=false colors=true diff --git a/cmd/cmd.go b/cmd/cmd.go index d4c133a..ae6ddfe 100644 --- a/cmd/cmd.go +++ b/cmd/cmd.go @@ -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") ) @@ -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) } diff --git a/info/info.go b/info/info.go index 9eabb9e..81f7d5e 100644 --- a/info/info.go +++ b/info/info.go @@ -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$`) @@ -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 diff --git a/info/raw.go b/info/raw.go index 41a8711..1ff1460 100644 --- a/info/raw.go +++ b/info/raw.go @@ -4,6 +4,9 @@ import ( "embed" _ "embed" "fmt" + "io" + "net" + "net/http" "os" "regexp" "runtime" @@ -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 diff --git a/info/raw_linux.go b/info/raw_linux.go index bbfb2f9..ea2bec9 100644 --- a/info/raw_linux.go +++ b/info/raw_linux.go @@ -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`) @@ -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" }