volume.go (816B)
1 package volume 2 3 import ( 4 "context" 5 "fmt" 6 "os/exec" 7 "strings" 8 ) 9 10 type Block struct{} 11 12 func New() *Block { 13 return &Block{} 14 } 15 16 func (b *Block) Name() string { 17 return "volume" 18 } 19 20 func (b *Block) Render(ctx context.Context) (string, error) { 21 out, err := exec.CommandContext( 22 ctx, "pactl", "get-sink-mute", "@DEFAULT_SINK@", 23 ).Output() 24 if err != nil { 25 return "", fmt.Errorf("exec pactl failed: %v", err) 26 } 27 28 if strings.Contains(string(out), "yes") { 29 return fmt.Sprintf(""), nil 30 } 31 32 out, err = exec.Command( 33 "pactl", "get-sink-volume", "@DEFAULT_SINK@", 34 ).Output() 35 if err != nil { 36 return "", fmt.Errorf("exec pactl failed: %v", err) 37 } 38 39 if fields := strings.Fields(string(out)); len(fields) < 5 { 40 return fmt.Sprintf(" "), nil 41 } else { 42 return fmt.Sprintf(" %s", fields[4]), nil 43 } 44 }