init
This commit is contained in:
commit
8fc099e249
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
|
@ -0,0 +1,2 @@
|
|||
/build/*
|
||||
!/build/.gitkeep
|
8
Makefile
Normal file
8
Makefile
Normal file
|
@ -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
|
93
blocks/acpi/main.go
Normal file
93
blocks/acpi/main.go
Normal file
|
@ -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"),
|
||||
}
|
||||
}
|
31
blocks/date/main.go
Normal file
31
blocks/date/main.go
Normal file
|
@ -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)
|
||||
}
|
84
blocks/ip/main.go
Normal file
84
blocks/ip/main.go
Normal file
|
@ -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)
|
||||
}
|
42
blocks/ip_wan/main.go
Normal file
42
blocks/ip_wan/main.go
Normal file
|
@ -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)
|
||||
}
|
61
blocks/wireguard/main.go
Normal file
61
blocks/wireguard/main.go
Normal file
|
@ -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)
|
||||
}
|
0
build/.gitkeep
Normal file
0
build/.gitkeep
Normal file
14
go.mod
Normal file
14
go.mod
Normal file
|
@ -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
|
||||
)
|
22
go.sum
Normal file
22
go.sum
Normal file
|
@ -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=
|
12
main.go
Normal file
12
main.go
Normal file
|
@ -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)
|
||||
}
|
36
rendering/block.go
Normal file
36
rendering/block.go
Normal file
|
@ -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
|
||||
}
|
25
rendering/color.go
Normal file
25
rendering/color.go
Normal file
|
@ -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"]
|
||||
}
|
10
rendering/emoji.go
Normal file
10
rendering/emoji.go
Normal file
|
@ -0,0 +1,10 @@
|
|||
package rendering
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/enescakir/emoji"
|
||||
)
|
||||
|
||||
func Emoji(e emoji.Emoji) string {
|
||||
return fmt.Sprintf("<span font='FontAwesome'>%s</span>", e)
|
||||
}
|
34
rendering/text.go
Normal file
34
rendering/text.go
Normal file
|
@ -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("<span background='%s' foreground='%s'>%s</span>", 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)
|
||||
}
|
Loading…
Reference in a new issue