85 lines
1.3 KiB
Go
85 lines
1.3 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"github.com/atotto/clipboard"
|
|
r "gitnet.fr/deblan/i3-blocks-go/rendering"
|
|
"net"
|
|
"os"
|
|
"regexp"
|
|
"strings"
|
|
)
|
|
|
|
func main() {
|
|
argIface := os.Args[1]
|
|
argVersion := os.Args[2]
|
|
argName := os.Args[3]
|
|
|
|
if argIface == "" {
|
|
argIface = "eth0"
|
|
}
|
|
|
|
if argVersion == "" {
|
|
argVersion = "ip4"
|
|
}
|
|
|
|
if argName == "" {
|
|
argName = argIface
|
|
}
|
|
|
|
var (
|
|
iface *net.Interface
|
|
addrs []net.Addr
|
|
// ip net.IP
|
|
)
|
|
|
|
iface, err := net.InterfaceByName(argIface)
|
|
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
addrs, err = iface.Addrs()
|
|
|
|
var ip string
|
|
|
|
for _, addr := range addrs {
|
|
if ip == "" {
|
|
a := addr.String()
|
|
|
|
if argVersion == "ip4" && !strings.Contains(a, ":") {
|
|
ip = a
|
|
} else if argVersion == "ip6" && strings.Contains(a, ":") && !strings.Contains(a, "fe80") {
|
|
ip = a
|
|
}
|
|
}
|
|
}
|
|
|
|
if ip == "" {
|
|
return
|
|
}
|
|
|
|
reg := regexp.MustCompile(`/\d+$`)
|
|
ip = reg.ReplaceAllString(ip, "")
|
|
|
|
if os.Getenv("BLOCK_BUTTON") == "1" {
|
|
clipboard.WriteAll(ip)
|
|
}
|
|
|
|
label := r.TextWithPadding(
|
|
argName,
|
|
r.FB{
|
|
Foreground: r.Color("grey"),
|
|
Background: r.Color("black2"),
|
|
},
|
|
)
|
|
|
|
value := r.TextWithPadding(ip, r.FB{})
|
|
options := r.NewBlockOptions()
|
|
options.FullText = fmt.Sprintf("%s%s", label, value)
|
|
|
|
block := r.Block(fmt.Sprintf("ip_%s_%s", argIface, argVersion), options)
|
|
|
|
fmt.Println(block)
|
|
}
|