2022-04-28 21:58:40 +02:00
|
|
|
import base
|
2022-05-04 17:17:02 +02:00
|
|
|
import std/[os,strutils,sequtils,osproc,math]
|
2022-02-12 16:19:43 +01:00
|
|
|
|
2022-05-04 17:17:02 +02:00
|
|
|
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
|
2022-03-07 10:37:31 +01:00
|
|
|
|
2022-05-04 17:17:02 +02:00
|
|
|
|
|
|
|
proc getCurrentVolume(): string {.gcsafe.}
|
|
|
|
|
|
|
|
proc checkVolume(volume: string): string =
|
2022-03-07 10:37:31 +01:00
|
|
|
var vol = volume
|
|
|
|
if strip(vol) == "Connection error":
|
2022-05-04 17:17:02 +02:00
|
|
|
sleep(1000)
|
|
|
|
vol = getCurrentVolume()
|
|
|
|
vol = checkVolume(vol)
|
2022-03-07 10:37:31 +01:00
|
|
|
return vol
|
|
|
|
|
2022-04-28 16:52:45 +02:00
|
|
|
proc getDesign(volume: string): (string,string) =
|
2022-05-04 17:17:02 +02:00
|
|
|
let vol = checkVolume(volume)
|
2022-02-12 16:19:43 +01:00
|
|
|
var icon = " "
|
|
|
|
if vol == "muted":
|
2022-05-04 17:17:02 +02:00
|
|
|
return (icon & "muted", icon & "muted")
|
2022-02-12 16:19:43 +01:00
|
|
|
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 = " "
|
2022-04-28 16:52:45 +02:00
|
|
|
let main_text = icon & $pcnt & "%"
|
2022-03-07 10:37:31 +01:00
|
|
|
let text = "<span size=\"x-large\">" & icon & "</span>" & $pcnt & "%"
|
2022-04-28 16:52:45 +02:00
|
|
|
return (text, main_text)
|
2022-02-12 16:19:43 +01:00
|
|
|
|
2022-05-04 17:17:02 +02:00
|
|
|
proc getCurrentVolume(): string =
|
|
|
|
let mute = execCmdEx(vol_get_mute)
|
2022-02-12 16:19:43 +01:00
|
|
|
if strip(mute.output) == "true":
|
|
|
|
return "muted"
|
2022-05-04 17:17:02 +02:00
|
|
|
let vol = execCmdEx(vol_get)
|
2022-02-12 16:19:43 +01:00
|
|
|
return vol.output
|
|
|
|
|
2022-05-04 17:17:02 +02:00
|
|
|
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":
|
|
|
|
let cmd = replace(vol_up, "%v", vol_default_by)
|
|
|
|
discard execCmd(cmd)
|
|
|
|
getVolume()
|
|
|
|
of "down":
|
|
|
|
let cmd = replace(vol_down, "%v", vol_default_by)
|
|
|
|
discard execCmd(cmd)
|
|
|
|
getVolume()
|
|
|
|
of "mute":
|
|
|
|
discard execCmd(vol_mute)
|
|
|
|
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()
|
2022-02-12 16:19:43 +01:00
|
|
|
|
|
|
|
proc main() =
|
2022-05-04 17:17:02 +02:00
|
|
|
getVolume()
|
2022-02-12 16:19:43 +01:00
|
|
|
|
2022-04-28 16:52:45 +02:00
|
|
|
if isMainModule:
|
|
|
|
main()
|