commit 8fc099e249e5f997d198309631696b6062b05866 Author: Simon Vieille Date: Sun Aug 28 14:59:15 2022 +0200 init diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d26d06b --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/build/* +!/build/.gitkeep diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..4e2a319 --- /dev/null +++ b/Makefile @@ -0,0 +1,8 @@ +.ONESHELL: +all: + for i in blocks/*; do + go build -ldflags '-s -w' -v -o "build/$$(basename "$$i")" "$$i/main.go" + done + +clean: + rm build/* 2>/dev/null || true diff --git a/blocks/acpi/main.go b/blocks/acpi/main.go new file mode 100644 index 0000000..f221b0c --- /dev/null +++ b/blocks/acpi/main.go @@ -0,0 +1,93 @@ +package main + +import ( + "fmt" + "github.com/enescakir/emoji" + "github.com/xellio/tools/acpi" + r "gitnet.fr/deblan/i3-blocks-go/rendering" + // "net" + // "os" + // "regexp" + // "strings" +) + +func main() { + datas, err := acpi.Battery() + + if err != nil { + return + } + + // value := r.TextWithPadding(ip, r.FB{}) + // + // block := r.Block(fmt.Sprintf("ip_%s_%s", argIface, argVersion), r.BlockOptions{ + // FullText: fmt.Sprintf("%s%s", label, value), + // }) + //fmt.Println(block) + + for _, data := range datas { + status := data.Status + level := data.Level + + if status == "Unknown" { + return + } + + if status == "Full" { + return + } + + if status == "Discharging" { + fmt.Println(CreateDischargingBlock(level)) + return + } + + if status == "Charging" { + fmt.Println(CreateChargingBlock(level)) + return + } + } +} + +func CreateChargingBlock(level int) string { + value := fmt.Sprintf("%d%%", level) + symbol := r.Emoji(emoji.UpArrow) + options := r.NewBlockOptions() + options.FullText = r.TextWithPadding(fmt.Sprintf("%s %s", value, symbol), CreateFB(level)) + + block := r.Block("battery", options) + + return block +} + +func CreateDischargingBlock(level int) string { + value := fmt.Sprintf("%d%%", level) + symbol := r.Emoji(emoji.DownArrow) + + block := r.Block("battery", r.BlockOptions{ + FullText: r.TextWithPadding(fmt.Sprintf("%s %s", value, symbol), CreateFB(level)), + }) + + return block +} + +func CreateFB(level int) r.FB { + if level < 20 { + return r.FB{ + Foreground: r.Color("black1"), + Background: r.Color("red"), + } + } + + if level < 50 { + return r.FB{ + Foreground: r.Color("white"), + Background: r.Color("orange"), + } + } + + return r.FB{ + Foreground: r.Color("white"), + Background: r.Color("green"), + } +} diff --git a/blocks/date/main.go b/blocks/date/main.go new file mode 100644 index 0000000..47570ed --- /dev/null +++ b/blocks/date/main.go @@ -0,0 +1,31 @@ +package main + +import ( + "fmt" + "github.com/enescakir/emoji" + "github.com/itchyny/timefmt-go" + r "gitnet.fr/deblan/i3-blocks-go/rendering" + "os" + "time" +) + +func main() { + argFormat := os.Args[1] + now := time.Now() + + if os.Getenv("BLOCK_BUTTON") == "1" { + } + + symbol := r.Emoji(emoji.Calendar) + date := timefmt.Format(now, argFormat) + + options := r.NewBlockOptions() + options.FullText = r.TextWithPadding(fmt.Sprintf("%s %s", symbol, date), r.FB{ + Foreground: r.Color("white"), + Background: r.Color("black4"), + }) + + block := r.Block("date", options) + + fmt.Println(block) +} diff --git a/blocks/ip/main.go b/blocks/ip/main.go new file mode 100644 index 0000000..938c5b1 --- /dev/null +++ b/blocks/ip/main.go @@ -0,0 +1,84 @@ +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) +} diff --git a/blocks/ip_wan/main.go b/blocks/ip_wan/main.go new file mode 100644 index 0000000..5c948b5 --- /dev/null +++ b/blocks/ip_wan/main.go @@ -0,0 +1,42 @@ +package main + +import ( + "fmt" + "github.com/atotto/clipboard" + r "gitnet.fr/deblan/i3-blocks-go/rendering" + "io" + "net/http" + "os" +) + +func main() { + resp, err := http.Get("https://api.ipify.org/") + + if err != nil { + return + } + + defer resp.Body.Close() + + ip, _ := io.ReadAll(resp.Body) + + label := r.TextWithPadding( + "wan", + r.FB{ + Foreground: r.Color("grey"), + Background: r.Color("black2"), + }, + ) + + if os.Getenv("BLOCK_BUTTON") == "1" { + clipboard.WriteAll(string(ip)) + } + + value := r.TextWithPadding(string(ip), r.FB{}) + options := r.NewBlockOptions() + options.FullText = fmt.Sprintf("%s%s", label, value) + + block := r.Block("ip_wan", options) + + fmt.Println(block) +} diff --git a/blocks/wireguard/main.go b/blocks/wireguard/main.go new file mode 100644 index 0000000..5238ae5 --- /dev/null +++ b/blocks/wireguard/main.go @@ -0,0 +1,61 @@ +package main + +import ( + "fmt" + r "gitnet.fr/deblan/i3-blocks-go/rendering" + "net" + "os" + "os/exec" + "strings" +) + +func main() { + argIface := os.Args[1] + argName := os.Args[2] + + var ( + iface *net.Interface + addrs []net.Addr + ) + + var ip string + + iface, err := net.InterfaceByName(argIface) + + if err == nil { + addrs, _ = iface.Addrs() + + for _, addr := range addrs { + if ip == "" { + a := addr.String() + + if !strings.Contains(a, ":") { + ip = a + } + } + } + } + + var fb r.FB + var command *exec.Cmd + + if ip == "" { + fb.Foreground = r.Color("grey1") + fb.Background = r.Color("black3") + command = exec.Command("sudo", "wg-quick", "up", argIface) + } else { + fb.Foreground = r.Color("black2") + fb.Background = r.Color("green") + command = exec.Command("sudo", "wg-quick", "down", argIface) + } + + if os.Getenv("BLOCK_BUTTON") == "1" { + command.Run() + } + + options := r.NewBlockOptions() + options.FullText = r.TextWithPadding(argName, fb) + block := r.Block(fmt.Sprintf("wireguard_%s", argIface), options) + + fmt.Println(block) +} diff --git a/build/.gitkeep b/build/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..413d5cb --- /dev/null +++ b/go.mod @@ -0,0 +1,14 @@ +module gitnet.fr/deblan/i3-blocks-go + +go 1.18 + +require ( + github.com/enescakir/emoji v1.0.0 + github.com/xellio/tools v0.0.0-20180807142225-794d9ef78e7d +) + +require ( + github.com/atotto/clipboard v0.1.4 // indirect + github.com/itchyny/timefmt-go v0.1.3 // indirect + github.com/stretchr/testify v1.8.0 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..c44c58c --- /dev/null +++ b/go.sum @@ -0,0 +1,22 @@ +github.com/atotto/clipboard v0.1.4 h1:EH0zSVneZPSuFR11BlR9YppQTVDbh5+16AmcJi4g1z4= +github.com/atotto/clipboard v0.1.4/go.mod h1:ZY9tmq7sm5xIbd9bOK4onWV4S6X0u6GY7Vn0Yu86PYI= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/enescakir/emoji v1.0.0 h1:W+HsNql8swfCQFtioDGDHCHri8nudlK1n5p2rHCJoog= +github.com/enescakir/emoji v1.0.0/go.mod h1:Bt1EKuLnKDTYpLALApstIkAjdDrS/8IAgTkKp+WKFD0= +github.com/itchyny/timefmt-go v0.1.3 h1:7M3LGVDsqcd0VZH2U+x393obrzZisp7C0uEe921iRkU= +github.com/itchyny/timefmt-go v0.1.3/go.mod h1:0osSSCQSASBJMsIZnhAaF1C2fCBTJZXrnj37mG8/c+A= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= +github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk= +github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= +github.com/xellio/tools v0.0.0-20180807142225-794d9ef78e7d h1:x1Cs1sxEVV51gmJnpMBQIs9Bh7BXnYQK3mf0soZSX/I= +github.com/xellio/tools v0.0.0-20180807142225-794d9ef78e7d/go.mod h1:AQ/vBUioO/A/ghK3n+MCBSdTaJ1yge9zvG2uvmLGn10= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/main.go b/main.go new file mode 100644 index 0000000..5bc2927 --- /dev/null +++ b/main.go @@ -0,0 +1,12 @@ +package main + +import ( + "fmt" + r "gitnet.fr/deblan/i3-blocks-go/rendering" +) + +func main() { + render := r.Block("Hello", r.BlockOptions{}) + + fmt.Println(render) +} diff --git a/rendering/block.go b/rendering/block.go new file mode 100644 index 0000000..25805f8 --- /dev/null +++ b/rendering/block.go @@ -0,0 +1,36 @@ +package rendering + +import ( + "encoding/json" + "strings" +) + +type BlockOptions struct { + FullText string `json:"full_text"` + Align string `json:"align"` + Name string `json:"name"` + Urgent bool `json:"urgent"` + Separator bool `json:"separator"` + SeparatorBlockWidth int `json:"separator_block_width"` +} + +func NewBlockOptions() BlockOptions { + return BlockOptions{ + Align: "left", + Urgent: false, + Separator: false, + SeparatorBlockWidth: 0, + } +} + +func Block(name string, options BlockOptions) string { + options.Name = name + + json, _ := json.Marshal(options) + block := string(json) + + block = strings.ReplaceAll(block, `\u003c`, "<") + block = strings.ReplaceAll(block, `\u003e`, ">") + + return block +} diff --git a/rendering/color.go b/rendering/color.go new file mode 100644 index 0000000..6ab74e1 --- /dev/null +++ b/rendering/color.go @@ -0,0 +1,25 @@ +package rendering + +func Color(name string) string { + colors := make(map[string]string) + + colors["grey1"] = "#CCCCCC" + colors["grey2"] = "#DDDDDD" + colors["white"] = "#E9F1FF" + colors["red"] = "#FF474A" + colors["orange"] = "#FF6836" + colors["green"] = "#B3FF6C" + colors["black1"] = "#000000" + colors["black2"] = "#333333" + colors["black3"] = "#222222" + colors["black4"] = "#444444" + + colors["default_background"] = colors["black1"] + colors["default_foreground"] = colors["grey2"] + + if color, exists := colors[name]; exists { + return color + } + + return colors["white"] +} diff --git a/rendering/emoji.go b/rendering/emoji.go new file mode 100644 index 0000000..9821b1c --- /dev/null +++ b/rendering/emoji.go @@ -0,0 +1,10 @@ +package rendering + +import ( + "fmt" + "github.com/enescakir/emoji" +) + +func Emoji(e emoji.Emoji) string { + return fmt.Sprintf("%s", e) +} diff --git a/rendering/text.go b/rendering/text.go new file mode 100644 index 0000000..91a8f0a --- /dev/null +++ b/rendering/text.go @@ -0,0 +1,34 @@ +package rendering + +import ( + "fmt" +) + +type FB struct { + Foreground string + Background string +} + +func Text(text string, fb FB) string { + if fb.Foreground == "" { + fb.Foreground = Color("default_foreground") + } + + if fb.Background == "" { + fb.Background = Color("default_background") + } + + return fmt.Sprintf("%s", fb.Background, fb.Foreground, text) +} + +func TextWithPadding(text string, fb FB) string { + return Text(fmt.Sprintf(" %s ", text), fb) +} + +func TextWithLeftPadding(text string, fb FB) string { + return Text(fmt.Sprintf(" %s", text), fb) +} + +func TextWithRightPadding(text string, fb FB) string { + return Text(fmt.Sprintf("%s ", text), fb) +}