i3-blocks-go/blocks/ip/main.go

75 lines
1.3 KiB
Go
Raw Normal View History

2022-08-28 14:59:15 +02:00
package main
import (
2022-08-30 15:42:28 +02:00
"flag"
2022-08-28 14:59:15 +02:00
"fmt"
"github.com/atotto/clipboard"
r "gitnet.fr/deblan/i3-blocks-go/rendering"
"net"
"os"
"regexp"
"strings"
)
func main() {
2022-08-30 15:42:28 +02:00
argIface := flag.String("iface", "eth0", "the iface")
argVersion := flag.String("version", "ip4", "ip4 or ip6")
argName := flag.String("name", "eth0", "the name")
flag.Parse()
2022-08-28 14:59:15 +02:00
var (
iface *net.Interface
addrs []net.Addr
// ip net.IP
)
2022-08-30 15:42:28 +02:00
iface, err := net.InterfaceByName(*argIface)
2022-08-28 14:59:15 +02:00
if err != nil {
return
}
addrs, err = iface.Addrs()
var ip string
for _, addr := range addrs {
if ip == "" {
a := addr.String()
2022-08-30 15:42:28 +02:00
if *argVersion == "ip4" && !strings.Contains(a, ":") {
2022-08-28 14:59:15 +02:00
ip = a
2022-08-30 15:42:28 +02:00
} else if *argVersion == "ip6" && strings.Contains(a, ":") && !strings.Contains(a, "fe80") {
2022-08-28 14:59:15 +02:00
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(
2022-08-30 15:42:28 +02:00
*argName,
2022-08-28 14:59:15 +02:00
r.FB{
2022-08-28 22:22:22 +02:00
Foreground: r.Color("grey1"),
2022-08-28 14:59:15 +02:00
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)
}