import std/os import strutils import std/osproc import std/math import i3bar_base proc get_current_volume(): string {.gcsafe.} proc check_volume(volume: string): string = var vol = volume if strip(vol) == "Connection error": discard execCmdEx("pulseaudio -k; sleep 5; pulseaudio -D") vol = get_current_volume() vol = check_volume(vol) return vol proc getDesign(volume: string): (string,string) = let vol = check_volume(volume) var icon = " " if vol == "muted": return (icon & "muted", "") let pcnt = parseInt(strip(vol)) case pcnt: of 85..100: icon = " " of 55..84: icon = " " of 35..54: icon = " " of 10..34: icon = " " else: icon = " " let main_text = icon & $pcnt & "%" let text = "" & icon & "" & $pcnt & "%" return (text, main_text) proc get_current_volume(): string = let mute = execCmdEx("pamixer --get-mute") if strip(mute.output) == "true": return "muted" let vol = execCmdEx("pamixer --get-volume") return vol.output proc get_volume*(run_once: bool = false) = var last_vol: string = "" while true: let vol = get_current_volume() if vol != last_vol or true: let (text, main_text) = getDesign(vol) let data = i3barData( title: "Volume : ", html_text: text, full_text: main_text, color: foreground, border: green, background: black ) let args = @["up", "down", "mute", "ncpamixer", "pavucontrol"] let option = outputJSON(data,args) case option: of "up": discard execCmd("pamixer -i 5") get_volume() of "down": discard execCmd("pamixer -d 5") get_volume() of "mute": discard execCmd("pamixer -t") get_volume() of "ncpamixer": discard execCmd("ncpamixer") of "pavucontrol": discard execCmd("pavucontrol") if run_once: break last_vol = vol sleep(1000) if stoploop: break proc main() = get_volume() if isMainModule: main()