48 lines
1.3 KiB
Nim
48 lines
1.3 KiB
Nim
import os, osproc, strutils, system
|
|
|
|
type
|
|
SinkState = enum
|
|
Suspended, Running
|
|
|
|
Sink = tuple[index: int, description: string, state: SinkState]
|
|
|
|
proc listSinks(): seq[Sink] =
|
|
let outp = execProcess("pactl", args = ["list", "sinks"], options = {poUsePath})
|
|
var sink: Sink
|
|
var done = false
|
|
|
|
for line in outp.splitLines():
|
|
if line.startsWith("Sink"):
|
|
done = false
|
|
sink.index = line.split("#")[1].parseInt()
|
|
elif not done:
|
|
let trim = line.strip(trailing = false)
|
|
if trim.startsWith("State"):
|
|
let stateStr = trim.split(": ")[1]
|
|
sink.state = if stateStr == "SUSPENDED": Suspended else: Running
|
|
elif trim.startsWith("Description"):
|
|
done = true
|
|
sink.description = trim.split(": ")[1]
|
|
result.add(sink)
|
|
|
|
proc setDefaultSink(index: int): int =
|
|
let p = startProcess("pactl", args = ["set-default-sink", $index], options = {poUsePath})
|
|
result = p.waitForExit()
|
|
|
|
const
|
|
reset = "\e[0m"
|
|
bold = "\e[1m"
|
|
green = "\e[32m"
|
|
|
|
proc printSinks() =
|
|
for sink in listSinks():
|
|
if sink.state == Running:
|
|
echo bold & $sink.index & reset & " " & green & sink.description & reset
|
|
else:
|
|
echo bold & $sink.index & reset & " " & sink.description & reset
|
|
|
|
if paramCount() < 1:
|
|
printSinks()
|
|
else:
|
|
paramStr(1).parseInt().setDefaultSink().quit()
|