From 2a701fe19b8e3c96e9bbbc27886a9fe677e1e2c6 Mon Sep 17 00:00:00 2001 From: Simon Vieille Date: Mon, 29 Aug 2022 13:44:34 +0200 Subject: [PATCH] add block volume --- README.md | 6 ++++++ blocks/volume/main.go | 48 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+) create mode 100644 blocks/volume/main.go diff --git a/README.md b/README.md index 6155cd6..ef2638c 100644 --- a/README.md +++ b/README.md @@ -68,6 +68,12 @@ Show indicator of RSS. rss ``` +Show volume. + +``` +volume +``` + Toggler for wireguard. ``` diff --git a/blocks/volume/main.go b/blocks/volume/main.go new file mode 100644 index 0000000..c81cfca --- /dev/null +++ b/blocks/volume/main.go @@ -0,0 +1,48 @@ +package main + +import ( + "fmt" + r "gitnet.fr/deblan/i3-blocks-go/rendering" + "os" + "os/exec" + "regexp" + "strconv" +) + +func Volume(channel string) int { + output, _ := exec.Command("amixer", "get", channel).Output() + + regex, _ := regexp.Compile(`\[(\d+)%\]`) + data := regex.FindStringSubmatch(string(output)) + + volume, _ := strconv.Atoi(data[1]) + + return volume +} + +func main() { + argChannel := os.Args[1] + + volume := Volume(argChannel) + + var symbole string + + if volume == 0 { + symbole = "\uf026" + } else if volume < 50 { + symbole = "\uf027" + } else { + symbole = "\uf028" + } + + value := r.TextWithPadding(fmt.Sprintf("%d%% %s", volume, r.FontAwesome(symbole)), r.FB{ + Foreground: r.Color("grey1"), + Background: r.Color("black1"), + }) + + options := r.NewBlockOptions() + options.FullText = value + block := r.Block("volume", options) + + fmt.Println(block) +}