Skip to content

Commit

Permalink
add classes for outputting format. some other light refactorings
Browse files Browse the repository at this point in the history
  • Loading branch information
technicalpickles committed Sep 5, 2021
1 parent 8fabfd6 commit 7c32a6f
Showing 1 changed file with 87 additions and 59 deletions.
146 changes: 87 additions & 59 deletions Sources/systemaudio/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@ import Rainbow
import SimplyCoreAudio
import TextTable


let simplyCA = SimplyCoreAudio()

struct SystemAudioCommand: ParsableCommand {
@Flag(name: .shortAndLong, help: "Input devices")
var input = false
Expand All @@ -23,6 +20,30 @@ struct SystemAudioCommand: ParsableCommand {
@Flag(name: .shortAndLong, help: "Generate output to be parsed by Alfred Script Filter")
var alfred: Bool = false

mutating func run() throws {
let devices = devices(isInput: input, isOutput: output)
var targetDevice: AudioDevice?

if uid != nil {
targetDevice = devices.first(where: { $0.uid == uid })!
}

if name != nil {
targetDevice = devices.first(where: { $0.name == name })!
}

if targetDevice != nil {
setDefaultDevice(device: targetDevice!, isInput: input, isOutput: output)
return
}

if alfred {
try AlfredFormatter(devices: devices).output()
} else {
ConsoleFormatter(devices: devices).output()
}
}

func setDefaultDevice(device: AudioDevice, isInput: Bool, isOutput: Bool) {
if isInput {
device.isDefaultInputDevice = true
Expand All @@ -36,6 +57,8 @@ struct SystemAudioCommand: ParsableCommand {
}

func devices(isInput _: Bool, isOutput _: Bool) -> [AudioDevice] {
let simplyCA = SimplyCoreAudio()

if input {
return simplyCA.allInputDevices
} else if output {
Expand All @@ -44,74 +67,79 @@ struct SystemAudioCommand: ParsableCommand {
return []
}
}
}

mutating func run() throws {
let devices = devices(isInput: input, isOutput: output)
var device: AudioDevice?

if uid != nil {
device = devices.first(where: { $0.uid == uid })!
}
class AlfredFormatter {
var devices = [AudioDevice]()

if name != nil {
device = devices.first(where: { $0.name == name })!
}
init(devices: [AudioDevice]) {
self.devices = devices
}

if device != nil {
setDefaultDevice(device: device!, isInput: input, isOutput: output)
return
}
func output() throws {
try print(toString())
}

if alfred {
// see https://www.alfredapp.com/help/workflows/inputs/script-filter/json/ for spec of alfred output
var response: [String: [Any]] = [:]

response["items"] = devices.map { device -> Any in
let isOutput = device.channels(scope: .output) > 0
let isInput = device.channels(scope: .input) > 0

var item: [String: Any] = [
"title": device.name,
"uid": device.uid!,
"arg": device.uid!,
"autocomplete": device.name,
]

if isOutput {
item["icon"] = ["path": "output.png"]
if device.isDefaultOutputDevice {
item["subtitle"] = "Currently selected"
}
func toString() throws -> String {
// see https://www.alfredapp.com/help/workflows/inputs/script-filter/json/ for spec of alfred output
var response: [String: [Any]] = [:]

response["items"] = devices.map { device -> Any in
let isOutput = device.channels(scope: .output) > 0
let isInput = device.channels(scope: .input) > 0

var item: [String: Any] = [
"title": device.name,
"uid": device.uid!,
"arg": device.uid!,
"autocomplete": device.name,
]

if isOutput {
item["icon"] = ["path": "output.png"]
if device.isDefaultOutputDevice {
item["subtitle"] = "Currently selected"
}
}

if isInput {
item["icon"] = ["path": "input.png"]
if device.isDefaultInputDevice {
item["subtitle"] = "Currently selected"
}
if isInput {
item["icon"] = ["path": "input.png"]
if device.isDefaultInputDevice {
item["subtitle"] = "Currently selected"
}

return item
}

let jsonData = try JSONSerialization.data(withJSONObject: response, options: [])
let jsonString = String(data: jsonData, encoding: String.Encoding.ascii)!
print(jsonString)
} else {
return item
}

let table = TextTable<AudioDevice> {
let isOutput = $0.channels(scope: .output) > 0
let isInput = $0.channels(scope: .input) > 0
let isDefaultDevice = (isOutput && $0.isDefaultOutputDevice) || (isInput && $0.isDefaultInputDevice)
return [
Column("NAME" <- $0.name),
Column("UID" <- $0.uid!),
Column("DEFAULT" <- isDefaultDevice ? "Yes" : ""),
]
}
table.print(devices, style: Style.org)
let jsonData = try JSONSerialization.data(withJSONObject: response, options: [])
let jsonString = String(data: jsonData, encoding: String.Encoding.ascii)!
return jsonString
}
}

class ConsoleFormatter {
var devices = [AudioDevice]()

init(devices: [AudioDevice]) {
self.devices = devices
}

func output() {
let table = TextTable<AudioDevice> {
let isOutput = $0.channels(scope: .output) > 0
let isInput = $0.channels(scope: .input) > 0
let isDefaultDevice = (isOutput && $0.isDefaultOutputDevice) || (isInput && $0.isDefaultInputDevice)
return [
Column("NAME" <- $0.name),
Column("UID" <- $0.uid!),
Column("DEFAULT" <- isDefaultDevice ? "Current" : ""),
]
}
table.print(devices, style: Style.org)
}
}



SystemAudioCommand.main()

0 comments on commit 7c32a6f

Please sign in to comment.