Simon Vieille
79fe14e059
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
73 lines
1.6 KiB
Go
73 lines
1.6 KiB
Go
package main
|
|
|
|
import (
|
|
"flag"
|
|
"fmt"
|
|
r "gitnet.fr/deblan/i3-blocks-go/rendering"
|
|
"os"
|
|
"os/exec"
|
|
"strconv"
|
|
"strings"
|
|
"sync"
|
|
)
|
|
|
|
func DiskUsage(path string) int {
|
|
outb, _ := exec.Command("df", "-h", path).Output()
|
|
output := string(outb)
|
|
lines := strings.Split(output, "\n")
|
|
columns := strings.Fields(lines[1])
|
|
value, _ := strconv.Atoi(strings.ReplaceAll(columns[4], "%", ""))
|
|
|
|
return value
|
|
}
|
|
|
|
func runFileExplorer(wg *sync.WaitGroup, path string) {
|
|
defer wg.Done()
|
|
command := exec.Command("xdg-open", path)
|
|
command.Run()
|
|
}
|
|
|
|
func main() {
|
|
argBlockName := flag.String("block", "du", "block name")
|
|
argName := flag.String("name", "du", "block name")
|
|
argMountPoint := flag.String("mount-point", "du", "block name")
|
|
argLimitWarning := flag.Int("limit-warning", 70, "limit for warning")
|
|
argLimitDanger := flag.Int("limit-danger", 90, "limit for danger")
|
|
flag.Parse()
|
|
|
|
used := DiskUsage(*argMountPoint)
|
|
fbUsed := r.FB{Background: r.Color("black1")}
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
if os.Getenv("BLOCK_BUTTON") == "1" {
|
|
wg.Add(1)
|
|
go runFileExplorer(&wg, fmt.Sprintf("file://%s", *argMountPoint))
|
|
}
|
|
|
|
if used < *argLimitWarning {
|
|
fbUsed.Foreground = r.Color("green")
|
|
} else if used < *argLimitDanger {
|
|
fbUsed.Foreground = r.Color("orange")
|
|
} else {
|
|
fbUsed.Foreground = r.Color("red")
|
|
}
|
|
|
|
label := r.TextWithPadding(
|
|
*argName,
|
|
r.FB{
|
|
Foreground: r.Color("grey1"),
|
|
Background: r.Color("black2"),
|
|
},
|
|
)
|
|
|
|
value := r.TextWithPadding(fmt.Sprintf("%d%%", used), fbUsed)
|
|
|
|
options := r.NewBlockOptions()
|
|
options.FullText = fmt.Sprintf("%s%s", label, value)
|
|
block := r.Block(*argBlockName, options)
|
|
|
|
fmt.Println(block)
|
|
wg.Wait()
|
|
}
|