wmtools/i3bar_volume.nim

91 lines
2 KiB
Nim
Raw Normal View History

2022-02-12 16:19:43 +01:00
import std/os
import strutils
import std/osproc
import std/math
import i3bar_base
import std/threadpool
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
proc getDesign(volume: string): string =
let vol = check_volume(volume)
2022-02-12 16:19:43 +01:00
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 = ""
2022-03-07 10:37:31 +01:00
let text = "<span size=\"x-large\">" & icon & "</span>" & $pcnt & "%"
2022-02-12 16:19:43 +01:00
return 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()
2022-03-07 10:37:31 +01:00
if vol != last_vol or true:
2022-02-12 16:19:43 +01:00
let text = getDesign(vol)
let data = i3barData(
full_text: text,
color: foreground,
2022-02-26 22:44:00 +01:00
border: green,
background: black
2022-02-12 16:19:43 +01:00
)
outputJSON(data)
if run_once:
break
last_vol = vol
sleep(1000)
proc await_click_info() =
while true:
let input = parseInput()
case input.button:
of 4:
discard execCmd("pamixer -i 5")
get_volume(true)
clearInput(2)
of 5:
discard execCmd("pamixer -d 5")
get_volume(true)
clearInput(2)
of 1:
discard execCmd("pamixer -t")
get_volume(true)
of 3:
discard execCmd("alacritty -e ncpamixer")
get_volume(true)
else:
let no = false
proc main() =
spawn get_volume()
spawn await_click_info()
sync()
main()