2022-02-12 16:19:43 +01:00
|
|
|
import std/os
|
|
|
|
import strutils
|
|
|
|
import std/osproc
|
|
|
|
import std/math
|
2022-04-28 21:58:40 +02:00
|
|
|
import base
|
2022-02-12 16:19:43 +01:00
|
|
|
|
2022-03-07 10:37:31 +01:00
|
|
|
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
|
|
|
|
|
2022-04-28 16:52:45 +02:00
|
|
|
proc getDesign(volume: string): (string,string) =
|
2022-03-07 10:37:31 +01:00
|
|
|
let vol = check_volume(volume)
|
2022-02-12 16:19:43 +01:00
|
|
|
var icon = " "
|
|
|
|
if vol == "muted":
|
2022-04-28 16:52:45 +02:00
|
|
|
return (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
|
|
|
|
|
|
|
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
|
|
|
|
|
2022-04-28 16:52:45 +02:00
|
|
|
proc get_volume*(run_once: bool = false) =
|
2022-02-12 16:19:43 +01:00
|
|
|
var last_vol: string = ""
|
|
|
|
while true:
|
|
|
|
let vol = get_current_volume()
|
2022-03-07 10:37:31 +01:00
|
|
|
if vol != last_vol or true:
|
2022-04-28 16:52:45 +02:00
|
|
|
let (text, main_text) = getDesign(vol)
|
2022-04-28 21:58:40 +02:00
|
|
|
var data = Info()
|
|
|
|
data.title = "Volume : "
|
|
|
|
data.html_text = text
|
|
|
|
data.full_text = main_text
|
|
|
|
data.color = foreground
|
|
|
|
data.border = green
|
|
|
|
data.background = black
|
|
|
|
data.selected_background = green
|
|
|
|
data.selected_color = black
|
2022-04-28 16:52:45 +02:00
|
|
|
let args = @["up", "down", "mute", "ncpamixer", "pavucontrol"]
|
|
|
|
let option = outputJSON(data,args)
|
2022-05-01 19:05:31 +02:00
|
|
|
if option in 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")
|
|
|
|
else:
|
|
|
|
try:
|
|
|
|
let vol = parseInt(option)
|
2022-05-01 23:49:55 +02:00
|
|
|
let x = execCmd("pamixer --set-volume " & $vol)
|
|
|
|
echo x
|
2022-04-28 16:52:45 +02:00
|
|
|
get_volume()
|
2022-05-01 19:05:31 +02:00
|
|
|
except:
|
|
|
|
echo getCurrentExceptionMsg()
|
2022-05-01 23:49:55 +02:00
|
|
|
get_volume()
|
2022-02-12 16:19:43 +01:00
|
|
|
if run_once:
|
|
|
|
break
|
|
|
|
last_vol = vol
|
2022-04-28 16:52:45 +02:00
|
|
|
if stoploop:
|
|
|
|
break
|
2022-05-01 23:49:55 +02:00
|
|
|
sleep(1000)
|
2022-02-12 16:19:43 +01:00
|
|
|
|
|
|
|
proc main() =
|
2022-04-28 16:52:45 +02:00
|
|
|
get_volume()
|
2022-02-12 16:19:43 +01:00
|
|
|
|
2022-04-28 16:52:45 +02:00
|
|
|
if isMainModule:
|
|
|
|
main()
|