Initial commit
This commit is contained in:
commit
b494610aa4
13
Audio.nimble
Normal file
13
Audio.nimble
Normal file
@ -0,0 +1,13 @@
|
||||
# Package
|
||||
|
||||
version = "0.1.0"
|
||||
author = "Hubert Van De Walle"
|
||||
description = "A new awesome nimble package"
|
||||
license = "Apache-2.0"
|
||||
srcDir = "src"
|
||||
bin = @["audio"]
|
||||
|
||||
|
||||
# Dependencies
|
||||
|
||||
requires "nim >= 1.4.2"
|
||||
47
src/audio.nim
Normal file
47
src/audio.nim
Normal file
@ -0,0 +1,47 @@
|
||||
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()
|
||||
Loading…
x
Reference in New Issue
Block a user