diff --git a/blocks/du/main.go b/blocks/du/main.go new file mode 100644 index 0000000..14e4f2f --- /dev/null +++ b/blocks/du/main.go @@ -0,0 +1,70 @@ +package main + +import ( + "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 := os.Args[1] + argName := os.Args[2] + argPoint := os.Args[3] + argLimitWarning, _ := strconv.Atoi(os.Args[4]) + argLimitDanger, _ := strconv.Atoi(os.Args[5]) + + used := DiskUsage(argPoint) + 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", argPoint)) + } + + 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() +}