import ../../globurrl import std/[os,strutils,sequtils,osproc] const audio_tools = @["ncpamixer", "pavucontrol"] const vol_cmd = "pamixer" const vol_up = vol_cmd & " -i %v" # where %v is amount by const vol_down = vol_cmd & " -d %v" # where %v is amount by const vol_set = vol_cmd & " --set-volume %v" # where %v is amount by const vol_mute = vol_cmd & " -t" const vol_default_by = "5" const vol_get = vol_cmd & " --get-volume" const vol_get_mute = vol_cmd & " --get-mute" const default_bg = green const default_fg = black proc getCurrentVolume(): string {.gcsafe.} proc checkVolume(volume: string): string = var vol = volume if strip(vol) == "Connection error": sleep(1000) vol = getCurrentVolume() vol = checkVolume(vol) return vol proc getDesign(volume: string): (string,string) = let vol = checkVolume(volume) var icon = " " if vol == "muted": return (icon & "muted", 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 getCurrentVolume(): string = let mute = execCmdEx(vol_get_mute) if strip(mute.output) == "true": return "muted" let vol = execCmdEx(vol_get) return vol.output proc volumeUp() = let cmd = replace(vol_up, "%v", vol_default_by) discard execCmd(cmd) proc volumeDown() = let cmd = replace(vol_down, "%v", vol_default_by) discard execCmd(cmd) proc volumeMute() = discard execCmd(vol_mute) proc getVolume*(run_once: bool = false) = let vol = getCurrentVolume() let (text, main_text) = getDesign(vol) var data = newInfo("Volume") data.full_text = main_text data.selected_bg = default_bg data.selected_fg = default_fg # i3bar stuff data.html_text = text data.color = foreground data.border = green data.background = black let args = concat(@["up", "down", "mute", "---", "exit", "---"],audio_tools) let option = outputData(data,args) if option == "": return elif option in args: if option in audio_tools: discard(execCmd(option)) return case option: of "up": volumeUp() getVolume() of "down": volumeDown() getVolume() of "mute": volumeMute() getVolume() of "---": getVolume() of "exit": return else: try: let vol = parseInt(option) let cmd = replace(vol_set, "%v", $vol) let x = execCmd(cmd) getVolume() except: echo getCurrentExceptionMsg() getVolume() proc main() = getVolume() if isMainModule: block start: for arg in args: case arg: of "up": volumeUp() break start of "down": volumeDown() break start of "mute": volumeMute() break start main()